[flang-commits] [PATCH] D122440: [flang] Fix bogus error from assignment to CLASS(*)

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Thu Mar 24 15:53:12 PDT 2022


klausler created this revision.
klausler added a reviewer: vdonaldson.
klausler added a project: Flang.
Herald added a subscriber: jdoerfert.
Herald added a project: All.
klausler requested review of this revision.

Assignment semantics was coughing up bad errors and crashes for
intrinsic assignments to unlimited polymorphic entities while
looking for any (impossible) user defined ASSIGNMENT(=) generic
or intrinsic type conversion.


https://reviews.llvm.org/D122440

Files:
  flang/lib/Semantics/expression.cpp
  flang/lib/Semantics/tools.cpp
  flang/test/Semantics/resolve63.f90
  flang/test/Semantics/selecttype03.f90


Index: flang/test/Semantics/selecttype03.f90
===================================================================
--- flang/test/Semantics/selecttype03.f90
+++ flang/test/Semantics/selecttype03.f90
@@ -99,11 +99,11 @@
     integer :: i
     class(t1),DIMENSION(:),allocatable :: foo
     integer, dimension(2) :: U
-    U = (/ 1,2 /)  
+    U = (/ 1,2 /)
     if (i>0) then
       foo = array1(2,U)
     else if (i<0) then
-      !ERROR: No intrinsic or user-defined ASSIGNMENT(=) matches operand types TYPE(t1) and TYPE(t2)
+      !ERROR: No intrinsic or user-defined ASSIGNMENT(=) matches operand types CLASS(t1) and CLASS(t2)
       foo = array2(2,U)
     end if
   end function
Index: flang/test/Semantics/resolve63.f90
===================================================================
--- flang/test/Semantics/resolve63.f90
+++ flang/test/Semantics/resolve63.f90
@@ -265,9 +265,9 @@
     i = x + y
     i = x + i
     i = y + i
-    !ERROR: No intrinsic or user-defined OPERATOR(+) matches operand types TYPE(t2) and TYPE(t1)
+    !ERROR: No intrinsic or user-defined OPERATOR(+) matches operand types CLASS(t2) and CLASS(t1)
     i = y + x
-    !ERROR: No intrinsic or user-defined OPERATOR(+) matches operand types INTEGER(4) and TYPE(t1)
+    !ERROR: No intrinsic or user-defined OPERATOR(+) matches operand types INTEGER(4) and CLASS(t1)
     i = i + x
   end
 end
@@ -344,3 +344,18 @@
     call generic(null(), null())
   end subroutine
 end
+
+! Ensure no bogus errors for assignments to CLASS(*) allocatable
+module m10
+  type :: t1
+    integer :: n
+  end type
+ contains
+  subroutine test
+    class(*), allocatable :: poly
+    poly = 1
+    poly = 3.14159
+    poly = 'Il faut imaginer Sisyphe heureux'
+    poly = t1(1)
+  end subroutine
+end module
Index: flang/lib/Semantics/tools.cpp
===================================================================
--- flang/lib/Semantics/tools.cpp
+++ flang/lib/Semantics/tools.cpp
@@ -103,6 +103,9 @@
   if (!lhsType || !rhsType) {
     return Tristate::No; // error or rhs is untyped
   }
+  if (lhsType->IsUnlimitedPolymorphic() || rhsType->IsUnlimitedPolymorphic()) {
+    return Tristate::No;
+  }
   TypeCategory lhsCat{lhsType->category()};
   TypeCategory rhsCat{rhsType->category()};
   if (rhsRank > 0 && lhsRank != rhsRank) {
Index: flang/lib/Semantics/expression.cpp
===================================================================
--- flang/lib/Semantics/expression.cpp
+++ flang/lib/Semantics/expression.cpp
@@ -3593,7 +3593,8 @@
 void ArgumentAnalyzer::AddAssignmentConversion(
     const DynamicType &lhsType, const DynamicType &rhsType) {
   if (lhsType.category() == rhsType.category() &&
-      lhsType.kind() == rhsType.kind()) {
+      (lhsType.category() == TypeCategory::Derived ||
+          lhsType.kind() == rhsType.kind())) {
     // no conversion necessary
   } else if (auto rhsExpr{evaluate::ConvertToType(lhsType, MoveExpr(1))}) {
     std::optional<parser::CharBlock> source;
@@ -3682,7 +3683,10 @@
   if (i >= actuals_.size() || !actuals_[i]) {
     return "missing argument";
   } else if (std::optional<DynamicType> type{GetType(i)}) {
-    return type->category() == TypeCategory::Derived
+    return type->IsAssumedType()         ? "TYPE(*)"s
+        : type->IsUnlimitedPolymorphic() ? "CLASS(*)"s
+        : type->IsPolymorphic()          ? "CLASS("s + type->AsFortran() + ')'
+        : type->category() == TypeCategory::Derived
         ? "TYPE("s + type->AsFortran() + ')'
         : type->category() == TypeCategory::Character
         ? "CHARACTER(KIND="s + std::to_string(type->kind()) + ')'


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D122440.418066.patch
Type: text/x-patch
Size: 3602 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20220324/aeed63cd/attachment.bin>


More information about the flang-commits mailing list