Skip to content

Commit 3bc8aa7

Browse files
authored
[flang] Catch whole assumed-size array as RHS (llvm#132819)
The right-hand side expression of an intrinsic assignment statement may not be the name of an assumed-size array dummy argument.
1 parent 6df27dd commit 3bc8aa7

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

flang/include/flang/Semantics/expression.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ class ExpressionAnalyzer {
257257

258258
// Builds a typed Designator from an untyped DataRef
259259
MaybeExpr Designate(DataRef &&);
260+
void CheckForWholeAssumedSizeArray(parser::CharBlock, const Symbol *);
260261

261262
// Allows a whole assumed-size array to appear for the lifetime of
262263
// the returned value.

flang/lib/Semantics/expression.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,20 +1077,24 @@ MaybeExpr ExpressionAnalyzer::Analyze(const parser::Name &n) {
10771077
n.symbol->attrs().reset(semantics::Attr::VOLATILE);
10781078
}
10791079
}
1080-
if (!isWholeAssumedSizeArrayOk_ &&
1081-
semantics::IsAssumedSizeArray(
1082-
ResolveAssociations(*n.symbol))) { // C1002, C1014, C1231
1083-
AttachDeclaration(
1084-
SayAt(n,
1085-
"Whole assumed-size array '%s' may not appear here without subscripts"_err_en_US,
1086-
n.source),
1087-
*n.symbol);
1088-
}
1080+
CheckForWholeAssumedSizeArray(n.source, n.symbol);
10891081
return Designate(DataRef{*n.symbol});
10901082
}
10911083
}
10921084
}
10931085

1086+
void ExpressionAnalyzer::CheckForWholeAssumedSizeArray(
1087+
parser::CharBlock at, const Symbol *symbol) {
1088+
if (!isWholeAssumedSizeArrayOk_ && symbol &&
1089+
semantics::IsAssumedSizeArray(ResolveAssociations(*symbol))) {
1090+
AttachDeclaration(
1091+
SayAt(at,
1092+
"Whole assumed-size array '%s' may not appear here without subscripts"_err_en_US,
1093+
symbol->name()),
1094+
*symbol);
1095+
}
1096+
}
1097+
10941098
MaybeExpr ExpressionAnalyzer::Analyze(const parser::NamedConstant &n) {
10951099
auto restorer{GetContextualMessages().SetLocation(n.v.source)};
10961100
if (MaybeExpr value{Analyze(n.v)}) {
@@ -3362,7 +3366,8 @@ const Assignment *ExpressionAnalyzer::Analyze(const parser::AssignmentStmt &x) {
33623366
ArgumentAnalyzer analyzer{*this};
33633367
const auto &variable{std::get<parser::Variable>(x.t)};
33643368
analyzer.Analyze(variable);
3365-
analyzer.Analyze(std::get<parser::Expr>(x.t));
3369+
const auto &rhsExpr{std::get<parser::Expr>(x.t)};
3370+
analyzer.Analyze(rhsExpr);
33663371
std::optional<Assignment> assignment;
33673372
if (!analyzer.fatalErrors()) {
33683373
auto restorer{GetContextualMessages().SetLocation(variable.GetSource())};
@@ -3392,6 +3397,8 @@ const Assignment *ExpressionAnalyzer::Analyze(const parser::AssignmentStmt &x) {
33923397
}
33933398
}
33943399
}
3400+
CheckForWholeAssumedSizeArray(
3401+
rhsExpr.source, UnwrapWholeSymbolDataRef(analyzer.GetExpr(1)));
33953402
}
33963403
assignment.emplace(analyzer.MoveExpr(0), analyzer.MoveExpr(1));
33973404
if (procRef) {

flang/test/Semantics/assign04.f90

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,15 @@ subroutine s5()
9999

100100
subroutine s6(x)
101101
integer :: x(*)
102+
integer, allocatable :: ja(:)
102103
x(1:3) = [1, 2, 3]
103104
x(:3) = [1, 2, 3]
104105
!ERROR: Assumed-size array 'x' must have explicit final subscript upper bound value
105106
x(:) = [1, 2, 3]
106107
!ERROR: Whole assumed-size array 'x' may not appear here without subscripts
107108
x = [1, 2, 3]
109+
!ERROR: Whole assumed-size array 'x' may not appear here without subscripts
110+
ja = x
108111
associate (y => x) ! ok
109112
!ERROR: Whole assumed-size array 'y' may not appear here without subscripts
110113
y = [1, 2, 3]

0 commit comments

Comments
 (0)