[flang] Catch obscure structure constructor error
authorPeter Klausler <pklausler@nvidia.com>
Tue, 7 Feb 2023 23:09:03 +0000 (15:09 -0800)
committerPeter Klausler <pklausler@nvidia.com>
Mon, 13 Feb 2023 23:24:35 +0000 (15:24 -0800)
A scalar value in a structure constructor may correspond to an
array component in the derived type only when that component has
a shape to which the scalar value may be expanded.

Differential Revision: https://reviews.llvm.org/D143822

flang/lib/Semantics/expression.cpp
flang/test/Semantics/data01.f90
flang/test/Semantics/structconst06.f90 [new file with mode: 0644]

index 14dcb71..8f20fc7 100644 (file)
@@ -2024,8 +2024,9 @@ MaybeExpr ExpressionAnalyzer::Analyze(
                         "component", "value")};
                 if (checked && *checked && GetRank(*componentShape) > 0 &&
                     GetRank(*valueShape) == 0 &&
-                    !IsExpandableScalar(*converted, GetFoldingContext(),
-                        *componentShape, true /*admit PURE call*/)) {
+                    (IsDeferredShape(*symbol) ||
+                        !IsExpandableScalar(*converted, GetFoldingContext(),
+                            *componentShape, true /*admit PURE call*/))) {
                   AttachDeclaration(
                       Say(expr.source,
                           "Scalar value cannot be expanded to shape of array component '%s'"_err_en_US,
index 4de7ff9..9046487 100644 (file)
@@ -12,7 +12,7 @@ module m1
   integer :: myAge = 2
   type(person) associated
   type hasAlloc
-    integer, allocatable :: a(:)
+    integer, allocatable :: a
   end type
 end
 
diff --git a/flang/test/Semantics/structconst06.f90 b/flang/test/Semantics/structconst06.f90
new file mode 100644 (file)
index 0000000..d5a4041
--- /dev/null
@@ -0,0 +1,9 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1
+! Don't expand scalars for allocatable components.
+module m
+  type t
+    real, allocatable :: a(:)
+  end type
+  !ERROR: Scalar value cannot be expanded to shape of array component 'a'
+  type(t) :: x = t(0.)
+end module