Skip to content

Commit 6b9716b

Browse files
authored
[flang] Catch bad usage case of whole assumed-size array (#132052)
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.
1 parent 3c657ce commit 6b9716b

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

flang/include/flang/Semantics/expression.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,16 +258,16 @@ class ExpressionAnalyzer {
258258
// Builds a typed Designator from an untyped DataRef
259259
MaybeExpr Designate(DataRef &&);
260260

261-
protected:
262-
int IntegerTypeSpecKind(const parser::IntegerTypeSpec &);
263-
264-
private:
265261
// Allows a whole assumed-size array to appear for the lifetime of
266262
// the returned value.
267-
common::Restorer<bool> AllowWholeAssumedSizeArray() {
268-
return common::ScopedSet(isWholeAssumedSizeArrayOk_, true);
263+
common::Restorer<bool> AllowWholeAssumedSizeArray(bool yes = true) {
264+
return common::ScopedSet(isWholeAssumedSizeArrayOk_, yes);
269265
}
270266

267+
protected:
268+
int IntegerTypeSpecKind(const parser::IntegerTypeSpec &);
269+
270+
private:
271271
// Allows an Expr to be a null pointer.
272272
common::Restorer<bool> AllowNullPointer() {
273273
return common::ScopedSet(isNullPointerOk_, true);

flang/lib/Semantics/expression.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1915,7 +1915,9 @@ void ArrayConstructorContext::Add(const parser::AcValue::Triplet &triplet) {
19151915
}
19161916

19171917
void ArrayConstructorContext::Add(const parser::Expr &expr) {
1918-
auto restorer{exprAnalyzer_.GetContextualMessages().SetLocation(expr.source)};
1918+
auto restorer1{
1919+
exprAnalyzer_.GetContextualMessages().SetLocation(expr.source)};
1920+
auto restorer2{exprAnalyzer_.AllowWholeAssumedSizeArray(false)};
19191921
Push(exprAnalyzer_.Analyze(expr));
19201922
}
19211923

flang/test/Semantics/array-constr-values.f90

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
! Confirm enforcement of constraints and restrictions in 7.8
33
! C7110, C7111, C7112, C7113, C7114, C7115
44

5-
subroutine arrayconstructorvalues()
5+
subroutine arrayconstructorvalues(asize)
66
integer :: intarray(4)
77
integer(KIND=8) :: k8 = 20
8+
integer, intent(in) :: asize(*)
89

910
TYPE EMPLOYEE
1011
INTEGER AGE
@@ -55,6 +56,9 @@ subroutine arrayconstructorvalues()
5556

5657
!ERROR: Item is not suitable for use in an array constructor
5758
intarray(1:1) = [ arrayconstructorvalues ]
59+
60+
!ERROR: Whole assumed-size array 'asize' may not appear here without subscripts
61+
intarray = [ asize ]
5862
end subroutine arrayconstructorvalues
5963
subroutine checkC7115()
6064
real, dimension(10), parameter :: good1 = [(99.9, i = 1, 10)]

0 commit comments

Comments
 (0)