diff --git a/flang/lib/Evaluate/fold-integer.cpp b/flang/lib/Evaluate/fold-integer.cpp --- a/flang/lib/Evaluate/fold-integer.cpp +++ b/flang/lib/Evaluate/fold-integer.cpp @@ -156,17 +156,21 @@ using T = Type; ActualArguments &args{funcRef.arguments()}; if (const auto *array{UnwrapExpr>(args[0])}) { - if (int rank{array->Rank()}; rank > 0 || IsAssumedRank(*array)) { - std::optional dim; - if (funcRef.Rank() == 0) { - // Optional DIM= argument is present: result is scalar. - if (!CheckDimArg(args[1], *array, context.messages(), true, dim)) { - return MakeInvalidIntrinsic(std::move(funcRef)); - } else if (!dim) { - // DIM= is present but not constant, or error - return Expr{std::move(funcRef)}; - } + std::optional dim; + if (funcRef.Rank() == 0) { + // Optional DIM= argument is present: result is scalar. + if (!CheckDimArg(args[1], *array, context.messages(), true, dim)) { + return MakeInvalidIntrinsic(std::move(funcRef)); + } else if (!dim) { + // DIM= is present but not constant, or error + return Expr{std::move(funcRef)}; } + } + if (IsAssumedRank(*array)) { + // Would like to return 1 if DIM=.. is present, but that would be + // hiding a runtime error if the DIM= were too large (including + // the case of an assumed-rank argument that's scalar). + } else if (int rank{array->Rank()}; rank > 0) { bool lowerBoundsAreOne{true}; if (auto named{ExtractNamedEntity(*array)}) { const Symbol &symbol{named->GetLastSymbol()}; @@ -203,17 +207,18 @@ using T = Type; ActualArguments &args{funcRef.arguments()}; if (auto *array{UnwrapExpr>(args[0])}) { - if (int rank{array->Rank()}; rank > 0 || IsAssumedRank(*array)) { - std::optional dim; - if (funcRef.Rank() == 0) { - // Optional DIM= argument is present: result is scalar. - if (!CheckDimArg(args[1], *array, context.messages(), false, dim)) { - return MakeInvalidIntrinsic(std::move(funcRef)); - } else if (!dim) { - // DIM= is present but not constant - return Expr{std::move(funcRef)}; - } + std::optional dim; + if (funcRef.Rank() == 0) { + // Optional DIM= argument is present: result is scalar. + if (!CheckDimArg(args[1], *array, context.messages(), false, dim)) { + return MakeInvalidIntrinsic(std::move(funcRef)); + } else if (!dim) { + // DIM= is present but not constant, or error + return Expr{std::move(funcRef)}; } + } + if (IsAssumedRank(*array)) { + } else if (int rank{array->Rank()}; rank > 0) { bool takeBoundsFromShape{true}; if (auto named{ExtractNamedEntity(*array)}) { const Symbol &symbol{named->GetLastSymbol()}; diff --git a/flang/test/Evaluate/errors01.f90 b/flang/test/Evaluate/errors01.f90 --- a/flang/test/Evaluate/errors01.f90 +++ b/flang/test/Evaluate/errors01.f90 @@ -6,8 +6,8 @@ real x end type t contains - subroutine s1(a,b) - real :: a(*), b(:) + subroutine s1(a,b,c) + real :: a(*), b(:), c(..) !CHECK: error: DIM=1 dimension is out of range for rank-1 assumed-size array integer :: ub1(ubound(a,1)) !CHECK-NOT: error: DIM=1 dimension is out of range for rank-1 assumed-size array @@ -20,6 +20,10 @@ integer :: lb2(lbound(b,0)) !CHECK: error: DIM=2 dimension is out of range for rank-1 array integer :: lb3(lbound(b,2)) + !CHECK: error: DIM=0 dimension must be positive + integer :: lb4(lbound(c,0)) + !CHECK: error: DIM=666 dimension is too large for any array (maximum rank 15) + integer :: lb4(lbound(c,666)) end subroutine subroutine s2 integer, parameter :: array(2,3) = reshape([(j, j=1, 6)], shape(array))