Skip to content

[flang] Catch whole assumed-size array as RHS #132819

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 26, 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
1 change: 1 addition & 0 deletions flang/include/flang/Semantics/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ class ExpressionAnalyzer {

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

// Allows a whole assumed-size array to appear for the lifetime of
// the returned value.
Expand Down
27 changes: 17 additions & 10 deletions flang/lib/Semantics/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1077,20 +1077,24 @@ MaybeExpr ExpressionAnalyzer::Analyze(const parser::Name &n) {
n.symbol->attrs().reset(semantics::Attr::VOLATILE);
}
}
if (!isWholeAssumedSizeArrayOk_ &&
semantics::IsAssumedSizeArray(
ResolveAssociations(*n.symbol))) { // C1002, C1014, C1231
AttachDeclaration(
SayAt(n,
"Whole assumed-size array '%s' may not appear here without subscripts"_err_en_US,
n.source),
*n.symbol);
}
CheckForWholeAssumedSizeArray(n.source, n.symbol);
return Designate(DataRef{*n.symbol});
}
}
}

void ExpressionAnalyzer::CheckForWholeAssumedSizeArray(
parser::CharBlock at, const Symbol *symbol) {
if (!isWholeAssumedSizeArrayOk_ && symbol &&
semantics::IsAssumedSizeArray(ResolveAssociations(*symbol))) {
AttachDeclaration(
SayAt(at,
"Whole assumed-size array '%s' may not appear here without subscripts"_err_en_US,
symbol->name()),
*symbol);
}
}

MaybeExpr ExpressionAnalyzer::Analyze(const parser::NamedConstant &n) {
auto restorer{GetContextualMessages().SetLocation(n.v.source)};
if (MaybeExpr value{Analyze(n.v)}) {
Expand Down Expand Up @@ -3362,7 +3366,8 @@ const Assignment *ExpressionAnalyzer::Analyze(const parser::AssignmentStmt &x) {
ArgumentAnalyzer analyzer{*this};
const auto &variable{std::get<parser::Variable>(x.t)};
analyzer.Analyze(variable);
analyzer.Analyze(std::get<parser::Expr>(x.t));
const auto &rhsExpr{std::get<parser::Expr>(x.t)};
analyzer.Analyze(rhsExpr);
std::optional<Assignment> assignment;
if (!analyzer.fatalErrors()) {
auto restorer{GetContextualMessages().SetLocation(variable.GetSource())};
Expand Down Expand Up @@ -3392,6 +3397,8 @@ const Assignment *ExpressionAnalyzer::Analyze(const parser::AssignmentStmt &x) {
}
}
}
CheckForWholeAssumedSizeArray(
rhsExpr.source, UnwrapWholeSymbolDataRef(analyzer.GetExpr(1)));
}
assignment.emplace(analyzer.MoveExpr(0), analyzer.MoveExpr(1));
if (procRef) {
Expand Down
3 changes: 3 additions & 0 deletions flang/test/Semantics/assign04.f90
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,15 @@ subroutine s5()

subroutine s6(x)
integer :: x(*)
integer, allocatable :: ja(:)
x(1:3) = [1, 2, 3]
x(:3) = [1, 2, 3]
!ERROR: Assumed-size array 'x' must have explicit final subscript upper bound value
x(:) = [1, 2, 3]
!ERROR: Whole assumed-size array 'x' may not appear here without subscripts
x = [1, 2, 3]
!ERROR: Whole assumed-size array 'x' may not appear here without subscripts
ja = x
associate (y => x) ! ok
!ERROR: Whole assumed-size array 'y' may not appear here without subscripts
y = [1, 2, 3]
Expand Down
Loading