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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions flang/include/flang/Semantics/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion flang/lib/Semantics/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
6 changes: 5 additions & 1 deletion flang/test/Semantics/array-constr-values.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)]
Expand Down