Skip to content

[flang] Catch attempts to subscribe empty arrays #108246

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
Sep 12, 2024
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
2 changes: 1 addition & 1 deletion flang/include/flang/Semantics/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ class ExpressionAnalyzer {
const semantics::Scope &, bool C919bAlreadyEnforced = false);
MaybeExpr CompleteSubscripts(ArrayRef &&);
MaybeExpr ApplySubscripts(DataRef &&, std::vector<Subscript> &&);
void CheckConstantSubscripts(ArrayRef &);
void CheckSubscripts(ArrayRef &);
bool CheckRanks(const DataRef &); // Return false if error exists.
bool CheckPolymorphic(const DataRef &); // ditto
bool CheckDataRef(const DataRef &); // ditto
Expand Down
11 changes: 9 additions & 2 deletions flang/lib/Semantics/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ MaybeExpr ExpressionAnalyzer::CompleteSubscripts(ArrayRef &&ref) {
// Subscripts of named constants are checked in folding.
// Subscripts of DATA statement objects are checked in data statement
// conversion to initializers.
CheckConstantSubscripts(ref);
CheckSubscripts(ref);
}
return Designate(DataRef{std::move(ref)});
}
Expand Down Expand Up @@ -326,7 +326,7 @@ MaybeExpr ExpressionAnalyzer::ApplySubscripts(
std::move(dataRef.u));
}

void ExpressionAnalyzer::CheckConstantSubscripts(ArrayRef &ref) {
void ExpressionAnalyzer::CheckSubscripts(ArrayRef &ref) {
// Fold subscript expressions and check for an empty triplet.
const Symbol &arraySymbol{ref.base().GetLastSymbol()};
Shape lb{GetLBOUNDs(foldingContext_, NamedEntity{arraySymbol})};
Expand Down Expand Up @@ -390,6 +390,13 @@ void ExpressionAnalyzer::CheckConstantSubscripts(ArrayRef &ref) {
for (Subscript &ss : ref.subscript()) {
auto dimLB{ToInt64(lb[dim])};
auto dimUB{ToInt64(ub[dim])};
if (dimUB && dimLB && *dimUB < *dimLB) {
AttachDeclaration(
Say("Empty array dimension %d cannot be subscripted as an element or non-empty array section"_err_en_US,
dim + 1),
arraySymbol);
break;
}
std::optional<ConstantSubscript> val[2];
int vals{0};
if (auto *triplet{std::get_if<Triplet>(&ss.u)}) {
Expand Down
8 changes: 7 additions & 1 deletion flang/test/Semantics/expr-errors06.f90
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
! RUN: %python %S/test_errors.py %s %flang_fc1 -Werror
! Check out-of-range subscripts
subroutine subr(da)
real a(10), da(2,1)
real a(10), da(2,1), empty(1:0,1)
integer, parameter :: n(2) = [1, 2]
integer unknown
!ERROR: DATA statement designator 'a(0_8)' is out of range
Expand Down Expand Up @@ -39,4 +39,10 @@ subroutine subr(da)
print *, da(1,0)
!WARNING: Subscript 2 is greater than upper bound 1 for dimension 2 of array
print *, da(1,2)
print *, empty([(j,j=1,0)],1) ! ok
print *, empty(1:0,1) ! ok
print *, empty(:,1) ! ok
print *, empty(i:j,k) ! ok
!ERROR: Empty array dimension 1 cannot be subscripted as an element or non-empty array section
print *, empty(i,1)
end
Loading