[flang-commits] [PATCH] D119744: [flang] Accept structure constructor value for polymorphic component

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Mon Feb 14 10:57:13 PST 2022


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

Semantic analysis was emitting a bogus error message when a structure
constructor contains a monomorphic value for a (limited) polymorphic
component of a derived type.  The type compatibility test was too
strict; this patch relaxes it a little to allow values that could
be assigned or passed to a variable or dummy argument with that type.
Also add some quotes to an error message that was sometimes confusing
without them, and remove a repeated space character from another.


https://reviews.llvm.org/D119744

Files:
  flang/lib/Evaluate/tools.cpp
  flang/lib/Semantics/expression.cpp
  flang/test/Semantics/data02.f90
  flang/test/Semantics/structconst01.f90
  flang/test/Semantics/structconst02.f90


Index: flang/test/Semantics/structconst02.f90
===================================================================
--- flang/test/Semantics/structconst02.f90
+++ flang/test/Semantics/structconst02.f90
@@ -32,9 +32,9 @@
 !    call scalararg(scalar(4)(5.,6,(7._8,8._2),4_'b',.true._4))
     call scalararg(scalar(4)(ix=5.,rx=6,zx=(7._8,8._2),cx=4_'b',lx=.true.))
     call scalararg(scalar(4)(5.,6,(7._8,8._2),4_'b',.true.))
-    !ERROR: Value in structure constructor of type CHARACTER(1) is incompatible with component 'ix' of type INTEGER(4)
+    !ERROR: Value in structure constructor of type 'CHARACTER(1)' is incompatible with component 'ix' of type 'INTEGER(4)'
     call scalararg(scalar(4)(ix='a'))
-    !ERROR: Value in structure constructor of type LOGICAL(4) is incompatible with component 'ix' of type INTEGER(4)
+    !ERROR: Value in structure constructor of type 'LOGICAL(4)' is incompatible with component 'ix' of type 'INTEGER(4)'
     call scalararg(scalar(4)(ix=.false.))
     !ERROR: Rank-1 array value is not compatible with scalar component 'ix'
     call scalararg(scalar(4)(ix=[1]))
Index: flang/test/Semantics/structconst01.f90
===================================================================
--- flang/test/Semantics/structconst01.f90
+++ flang/test/Semantics/structconst01.f90
@@ -73,11 +73,18 @@
       class(*), allocatable :: p
     end type poly
     type(poly) :: x
+    type :: poly2
+      class(type1(1)), allocatable :: p1
+      type(type1(1)), allocatable :: p2
+    end type poly2
+    type(type1(1)) :: t1val
+    type(poly2) :: x2
     ! These cases are not errors
     x = poly(1)
     x = poly('hello')
     x = poly(type1(1)(123))
-    !ERROR: Value in structure constructor is incompatible with  component 'p' of type CLASS(*)
+    x2 = poly2(t1val, t1val)
+    !ERROR: Value in structure constructor is incompatible with component 'p' of type CLASS(*)
     x = poly(z'feedface')
   end subroutine
 end module module1
Index: flang/test/Semantics/data02.f90
===================================================================
--- flang/test/Semantics/data02.f90
+++ flang/test/Semantics/data02.f90
@@ -6,7 +6,7 @@
     character(1) :: c
   end type
   type(t) :: x
-  !ERROR: Value in structure constructor of type INTEGER(4) is incompatible with component 'c' of type CHARACTER(KIND=1,LEN=1_8)
+  !ERROR: Value in structure constructor of type 'INTEGER(4)' is incompatible with component 'c' of type 'CHARACTER(KIND=1,LEN=1_8)'
   data x /t(1)/
 end
 
Index: flang/lib/Semantics/expression.cpp
===================================================================
--- flang/lib/Semantics/expression.cpp
+++ flang/lib/Semantics/expression.cpp
@@ -1754,8 +1754,8 @@
           } else if (valueType) {
             AttachDeclaration(
                 Say(expr.source,
-                    "Value in structure constructor of type %s is "
-                    "incompatible with component '%s' of type %s"_err_en_US,
+                    "Value in structure constructor of type '%s' is "
+                    "incompatible with component '%s' of type '%s'"_err_en_US,
                     valueType->AsFortran(), symbol->name(),
                     symType->AsFortran()),
                 *symbol);
@@ -1763,7 +1763,7 @@
             AttachDeclaration(
                 Say(expr.source,
                     "Value in structure constructor is incompatible with "
-                    " component '%s' of type %s"_err_en_US,
+                    "component '%s' of type %s"_err_en_US,
                     symbol->name(), symType->AsFortran()),
                 *symbol);
           }
Index: flang/lib/Evaluate/tools.cpp
===================================================================
--- flang/lib/Evaluate/tools.cpp
+++ flang/lib/Evaluate/tools.cpp
@@ -653,7 +653,9 @@
     break;
   case TypeCategory::Derived:
     if (auto fromType{x.GetType()}) {
-      if (type == *fromType) {
+      if (type.IsTkCompatibleWith(*fromType)) {
+        // "x" could be assigned or passed to "type", or appear in a
+        // structure constructor as a value for a component with "type"
         return std::move(x);
       }
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D119744.408512.patch
Type: text/x-patch
Size: 4165 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20220214/7b0047e2/attachment.bin>


More information about the flang-commits mailing list