Skip to content

Commit 755180c

Browse files
committed
[flang] Avoid bogus errors with LBOUND/UBOUND(assumed rank array, DIM=)
Don't emit bogus compile-time error messages about out-of-range values for the DIM= argument to LBOUND/BOUND when the array in question is an assumed-rank dummy array argument. Differential Revision: https://reviews.llvm.org/D155494
1 parent 8b29048 commit 755180c

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

flang/lib/Evaluate/fold-integer.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,14 @@ Expr<Type<TypeCategory::Integer, KIND>> LBOUND(FoldingContext &context,
115115
using T = Type<TypeCategory::Integer, KIND>;
116116
ActualArguments &args{funcRef.arguments()};
117117
if (const auto *array{UnwrapExpr<Expr<SomeType>>(args[0])}) {
118-
if (int rank{array->Rank()}; rank > 0) {
118+
if (int rank{array->Rank()}; rank > 0 && !IsAssumedRank(*array)) {
119119
std::optional<int> dim;
120120
if (funcRef.Rank() == 0) {
121121
// Optional DIM= argument is present: result is scalar.
122122
if (auto dim64{ToInt64(args[1])}) {
123123
if (*dim64 < 1 || *dim64 > rank) {
124-
context.messages().Say("DIM=%jd dimension is out of range for "
125-
"rank-%d array"_err_en_US,
124+
context.messages().Say(
125+
"DIM=%jd dimension is out of range for rank-%d array"_err_en_US,
126126
*dim64, rank);
127127
return MakeInvalidIntrinsic<T>(std::move(funcRef));
128128
} else {
@@ -169,14 +169,14 @@ Expr<Type<TypeCategory::Integer, KIND>> UBOUND(FoldingContext &context,
169169
using T = Type<TypeCategory::Integer, KIND>;
170170
ActualArguments &args{funcRef.arguments()};
171171
if (auto *array{UnwrapExpr<Expr<SomeType>>(args[0])}) {
172-
if (int rank{array->Rank()}; rank > 0) {
172+
if (int rank{array->Rank()}; rank > 0 && !IsAssumedRank(*array)) {
173173
std::optional<int> dim;
174174
if (funcRef.Rank() == 0) {
175175
// Optional DIM= argument is present: result is scalar.
176176
if (auto dim64{ToInt64(args[1])}) {
177177
if (*dim64 < 1 || *dim64 > rank) {
178-
context.messages().Say("DIM=%jd dimension is out of range for "
179-
"rank-%d array"_err_en_US,
178+
context.messages().Say(
179+
"DIM=%jd dimension is out of range for rank-%d array"_err_en_US,
180180
*dim64, rank);
181181
return MakeInvalidIntrinsic<T>(std::move(funcRef));
182182
} else {
@@ -194,8 +194,8 @@ Expr<Type<TypeCategory::Integer, KIND>> UBOUND(FoldingContext &context,
194194
takeBoundsFromShape = false;
195195
if (dim) {
196196
if (semantics::IsAssumedSizeArray(symbol) && *dim == rank - 1) {
197-
context.messages().Say("DIM=%jd dimension is out of range for "
198-
"rank-%d assumed-size array"_err_en_US,
197+
context.messages().Say(
198+
"DIM=%jd dimension is out of range for rank-%d assumed-size array"_err_en_US,
199199
rank, rank);
200200
return MakeInvalidIntrinsic<T>(std::move(funcRef));
201201
} else if (auto ub{GetUBOUND(context, *named, *dim)}) {

flang/test/Semantics/misc-intrinsics.f90

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
program test_size
44
real :: scalar
55
real, dimension(5, 5) :: array
6-
call test(array)
6+
call test(array, array)
77
contains
8-
subroutine test(arg)
8+
subroutine test(arg, assumedRank)
99
real, dimension(5, *) :: arg
10+
real, dimension(..) :: assumedRank
1011
!ERROR: A dim= argument is required for 'size' when the array is assumed-size
1112
print *, size(arg)
1213
!ERROR: missing mandatory 'dim=' argument
@@ -21,6 +22,13 @@ subroutine test(arg)
2122
print *, size(scalar)
2223
!ERROR: missing mandatory 'dim=' argument
2324
print *, ubound(scalar)
25+
select rank(assumedRank)
26+
rank(1)
27+
!ERROR: DIM=2 dimension is out of range for rank-1 array
28+
print *, lbound(assumedRank, dim=2)
29+
!ERROR: DIM=2 dimension is out of range for rank-1 array
30+
print *, ubound(assumedRank, dim=2)
31+
end select
2432
! But these cases are fine:
2533
print *, size(arg, dim=1)
2634
print *, ubound(arg, dim=1)
@@ -32,5 +40,15 @@ subroutine test(arg)
3240
print *, ubound(arg(:,1))
3341
print *, shape(scalar)
3442
print *, shape(arg(:,1))
43+
print *, lbound(assumedRank, dim=2) ! can't check until run time
44+
print *, ubound(assumedRank, dim=2)
45+
select rank(assumedRank)
46+
rank(3)
47+
print *, lbound(assumedRank, dim=2)
48+
print *, ubound(assumedRank, dim=2)
49+
rank default
50+
print *, lbound(assumedRank, dim=2)
51+
print *, ubound(assumedRank, dim=2)
52+
end select
3553
end subroutine
3654
end

0 commit comments

Comments
 (0)