Fortran: do not attempt simplification of [LU]BOUND for pointer/allocatable
authorHarald Anlauf <anlauf@gmx.de>
Tue, 23 Nov 2021 20:39:36 +0000 (21:39 +0100)
committerHarald Anlauf <anlauf@gmx.de>
Tue, 23 Nov 2021 21:02:40 +0000 (22:02 +0100)
gcc/fortran/ChangeLog:

PR fortran/103392
* simplify.c (simplify_bound): Do not try to simplify
LBOUND/UBOUND for arrays with POINTER or ALLOCATABLE attribute.

gcc/testsuite/ChangeLog:

PR fortran/103392
* gfortran.dg/bound_simplification_7.f90: New test.

gcc/fortran/simplify.c
gcc/testsuite/gfortran.dg/bound_simplification_7.f90 [new file with mode: 0644]

index 6a6b3fb..c9e13b5 100644 (file)
@@ -4266,6 +4266,12 @@ simplify_bound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind, int upper)
             || (as->type == AS_ASSUMED_SHAPE && upper)))
     return NULL;
 
+  /* 'array' shall not be an unallocated allocatable variable or a pointer that
+     is not associated.  */
+  if (array->expr_type == EXPR_VARIABLE
+      && (gfc_expr_attr (array).allocatable || gfc_expr_attr (array).pointer))
+    return NULL;
+
   gcc_assert (!as
              || (as->type != AS_DEFERRED
                  && array->expr_type == EXPR_VARIABLE
diff --git a/gcc/testsuite/gfortran.dg/bound_simplification_7.f90 b/gcc/testsuite/gfortran.dg/bound_simplification_7.f90
new file mode 100644 (file)
index 0000000..3efecdf
--- /dev/null
@@ -0,0 +1,18 @@
+! { dg-do compile }
+! PR fortran/103392 - ICE in simplify_bound
+
+program p
+  integer, allocatable :: a(1:1) ! { dg-error "deferred shape or assumed rank" }
+  integer :: b(1) = lbound(a)    ! { dg-error "does not reduce" }
+  integer :: c(1) = ubound(a)    ! { dg-error "does not reduce" }
+end
+
+subroutine s(x, y)
+  type t
+     integer :: i(3)
+  end type t
+  type(t), pointer     :: x(:)
+  type(t), allocatable :: y(:)
+  integer, parameter   :: m(1) = ubound (x(1)% i)
+  integer              :: n(1) = ubound (y(1)% i)
+end subroutine s