[flang-commits] [flang] dc6fadf - [flang] Detect UBOUND() error on assumed-size array

peter klausler via flang-commits flang-commits at lists.llvm.org
Tue Feb 2 11:37:55 PST 2021


Author: peter klausler
Date: 2021-02-02T11:37:44-08:00
New Revision: dc6fadf7082821e4144b8b7ba256889bcba6f455

URL: https://github.com/llvm/llvm-project/commit/dc6fadf7082821e4144b8b7ba256889bcba6f455
DIFF: https://github.com/llvm/llvm-project/commit/dc6fadf7082821e4144b8b7ba256889bcba6f455.diff

LOG: [flang] Detect UBOUND() error on assumed-size array

UBOUND() is not allowed on the last dimension of an
assumed-size array dummy argument.

Differential Revision: https://reviews.llvm.org/D95831

Added: 
    flang/test/Evaluate/folding19.f90

Modified: 
    flang/lib/Evaluate/fold-integer.cpp
    flang/test/Semantics/spec-expr.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Evaluate/fold-integer.cpp b/flang/lib/Evaluate/fold-integer.cpp
index c81df1fd4069..877051963fc8 100644
--- a/flang/lib/Evaluate/fold-integer.cpp
+++ b/flang/lib/Evaluate/fold-integer.cpp
@@ -23,7 +23,7 @@ Expr<Type<TypeCategory::Integer, KIND>> LBOUND(FoldingContext &context,
         if (auto dim64{GetInt64Arg(args[1])}) {
           if (*dim64 < 1 || *dim64 > rank) {
             context.messages().Say("DIM=%jd dimension is out of range for "
-                                   "rank-%d array"_en_US,
+                                   "rank-%d array"_err_en_US,
                 *dim64, rank);
             return MakeInvalidIntrinsic<T>(std::move(funcRef));
           } else {
@@ -78,7 +78,7 @@ Expr<Type<TypeCategory::Integer, KIND>> UBOUND(FoldingContext &context,
         if (auto dim64{GetInt64Arg(args[1])}) {
           if (*dim64 < 1 || *dim64 > rank) {
             context.messages().Say("DIM=%jd dimension is out of range for "
-                                   "rank-%d array"_en_US,
+                                   "rank-%d array"_err_en_US,
                 *dim64, rank);
             return MakeInvalidIntrinsic<T>(std::move(funcRef));
           } else {
@@ -95,8 +95,11 @@ Expr<Type<TypeCategory::Integer, KIND>> UBOUND(FoldingContext &context,
         if (symbol.Rank() == rank) {
           takeBoundsFromShape = false;
           if (dim) {
-            if (semantics::IsAssumedSizeArray(symbol) && *dim == rank) {
-              return Expr<T>{-1};
+            if (semantics::IsAssumedSizeArray(symbol) && *dim == rank - 1) {
+              context.messages().Say("DIM=%jd dimension is out of range for "
+                                     "rank-%d assumed-size array"_err_en_US,
+                  rank, rank);
+              return MakeInvalidIntrinsic<T>(std::move(funcRef));
             } else if (auto ub{GetUpperBound(context, *named, *dim)}) {
               return Fold(context, ConvertToType<T>(std::move(*ub)));
             }

diff  --git a/flang/test/Evaluate/folding19.f90 b/flang/test/Evaluate/folding19.f90
new file mode 100644
index 000000000000..a8052df8fd4b
--- /dev/null
+++ b/flang/test/Evaluate/folding19.f90
@@ -0,0 +1,22 @@
+! RUN: not %f18 %s 2>&1 | FileCheck %s
+! Check errors found in folding
+! TODO: test others emitted from flang/lib/Evaluate
+module m
+ contains
+  subroutine s1(a,b)
+    real :: a(*), b(:)
+    !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
+    integer :: lb1(lbound(a,1))
+    !CHECK: error: DIM=0 dimension is out of range for rank-1 array
+    integer :: ub2(ubound(a,0))
+    !CHECK: error: DIM=2 dimension is out of range for rank-1 array
+    integer :: ub3(ubound(a,2))
+    !CHECK: error: DIM=0 dimension is out of range for rank-1 array
+    integer :: lb2(lbound(b,0))
+    !CHECK: error: DIM=2 dimension is out of range for rank-1 array
+    integer :: lb3(lbound(b,2))
+  end subroutine
+end module
+

diff  --git a/flang/test/Semantics/spec-expr.f90 b/flang/test/Semantics/spec-expr.f90
index c02cabc04895..58ffe1254387 100644
--- a/flang/test/Semantics/spec-expr.f90
+++ b/flang/test/Semantics/spec-expr.f90
@@ -79,7 +79,7 @@ end subroutine s7a
 subroutine s7bi(assumedArg)
   integer, dimension(2, *) :: assumedArg
   real, dimension(ubound(assumedArg, 1)) :: realArray1
-  ! Should be an error since 2 is the last dimension of an assumed-size array
+  !ERROR: DIM=2 dimension is out of range for rank-2 assumed-size array
   real, dimension(ubound(assumedArg, 2)) :: realArray2
 end subroutine s7bi
 


        


More information about the flang-commits mailing list