Skip to content

[flang] Catch bad usage case of whole assumed-size array #132052

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2025

Conversation

klausler
Copy link
Contributor

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 #131909.

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 llvm#131909.
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:semantics labels Mar 19, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 19, 2025

@llvm/pr-subscribers-flang-semantics

Author: Peter Klausler (klausler)

Changes

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 #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 --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)]

@klausler klausler merged commit 6b9716b into llvm:main Mar 19, 2025
12 of 13 checks passed
@klausler klausler deleted the bug131909 branch March 19, 2025 19:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:semantics flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[flang] Missing diagnostic that an assumed-size array is being passed as the ac-value of an array constructor
3 participants