[flang-commits] [flang] [flang] Catch bad usage case of whole assumed-size array (PR #132052)

via flang-commits flang-commits at lists.llvm.org
Wed Mar 19 09:07:55 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-semantics

Author: Peter Klausler (klausler)

<details>
<summary>Changes</summary>

Whole assumed-size arrays are generally not allowed outside specific contexts, where expression analysis notes that they can appear.  But contexts can nest, and in the case of an actual argument that turns out to be an array constructor, the permission to use a whole assumed-size array must be rescinded.

Fixes https://github.com/llvm/llvm-project/issues/131909.

---
Full diff: https://github.com/llvm/llvm-project/pull/132052.diff


3 Files Affected:

- (modified) flang/include/flang/Semantics/expression.h (+6-6) 
- (modified) flang/lib/Semantics/expression.cpp (+3-1) 
- (modified) flang/test/Semantics/array-constr-values.f90 (+5-1) 


``````````diff
diff --git a/flang/include/flang/Semantics/expression.h b/flang/include/flang/Semantics/expression.h
index b051c486641fe..068395072266f 100644
--- a/flang/include/flang/Semantics/expression.h
+++ b/flang/include/flang/Semantics/expression.h
@@ -258,16 +258,16 @@ class ExpressionAnalyzer {
   // Builds a typed Designator from an untyped DataRef
   MaybeExpr Designate(DataRef &&);
 
-protected:
-  int IntegerTypeSpecKind(const parser::IntegerTypeSpec &);
-
-private:
   // Allows a whole assumed-size array to appear for the lifetime of
   // the returned value.
-  common::Restorer<bool> AllowWholeAssumedSizeArray() {
-    return common::ScopedSet(isWholeAssumedSizeArrayOk_, true);
+  common::Restorer<bool> AllowWholeAssumedSizeArray(bool yes = true) {
+    return common::ScopedSet(isWholeAssumedSizeArrayOk_, yes);
   }
 
+protected:
+  int IntegerTypeSpecKind(const parser::IntegerTypeSpec &);
+
+private:
   // Allows an Expr to be a null pointer.
   common::Restorer<bool> AllowNullPointer() {
     return common::ScopedSet(isNullPointerOk_, true);
diff --git a/flang/lib/Semantics/expression.cpp b/flang/lib/Semantics/expression.cpp
index 39a58a4e23363..74570cdb8eda0 100644
--- a/flang/lib/Semantics/expression.cpp
+++ b/flang/lib/Semantics/expression.cpp
@@ -1915,7 +1915,9 @@ void ArrayConstructorContext::Add(const parser::AcValue::Triplet &triplet) {
 }
 
 void ArrayConstructorContext::Add(const parser::Expr &expr) {
-  auto restorer{exprAnalyzer_.GetContextualMessages().SetLocation(expr.source)};
+  auto restorer1{
+      exprAnalyzer_.GetContextualMessages().SetLocation(expr.source)};
+  auto restorer2{exprAnalyzer_.AllowWholeAssumedSizeArray(false)};
   Push(exprAnalyzer_.Analyze(expr));
 }
 
diff --git a/flang/test/Semantics/array-constr-values.f90 b/flang/test/Semantics/array-constr-values.f90
index 6742cd35fe52d..b6558c08089c6 100644
--- a/flang/test/Semantics/array-constr-values.f90
+++ b/flang/test/Semantics/array-constr-values.f90
@@ -2,9 +2,10 @@
 ! Confirm enforcement of constraints and restrictions in 7.8
 ! C7110, C7111, C7112, C7113, C7114, C7115
 
-subroutine arrayconstructorvalues()
+subroutine arrayconstructorvalues(asize)
   integer :: intarray(4)
   integer(KIND=8) :: k8 = 20
+  integer, intent(in) :: asize(*)
 
   TYPE EMPLOYEE
     INTEGER AGE
@@ -55,6 +56,9 @@ subroutine arrayconstructorvalues()
 
   !ERROR: Item is not suitable for use in an array constructor
   intarray(1:1) = [ arrayconstructorvalues ]
+
+  !ERROR: Whole assumed-size array 'asize' may not appear here without subscripts
+  intarray = [ asize ]
 end subroutine arrayconstructorvalues
 subroutine checkC7115()
   real, dimension(10), parameter :: good1 = [(99.9, i = 1, 10)]

``````````

</details>


https://github.com/llvm/llvm-project/pull/132052


More information about the flang-commits mailing list