Fortran: error recovery simplifying PACK with invalid arguments [PR106049]
authorHarald Anlauf <anlauf@gmx.de>
Tue, 5 Jul 2022 20:20:05 +0000 (22:20 +0200)
committerHarald Anlauf <anlauf@gmx.de>
Tue, 12 Jul 2022 17:18:19 +0000 (19:18 +0200)
gcc/fortran/ChangeLog:

PR fortran/106049
* simplify.cc (is_constant_array_expr): A non-zero-sized constant
array shall have a non-empty constructor.  When the constructor is
empty or missing, treat as non-constant.

gcc/testsuite/ChangeLog:

PR fortran/106049
* gfortran.dg/pack_simplify_1.f90: New test.

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

index ab59fbc..fb72599 100644 (file)
@@ -233,6 +233,18 @@ is_constant_array_expr (gfc_expr *e)
   if (e->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (e))
     return false;
 
+  /* A non-zero-sized constant array shall have a non-empty constructor.  */
+  if (e->rank > 0 && e->shape != NULL && e->value.constructor == NULL)
+    {
+      mpz_init_set_ui (size, 1);
+      for (int j = 0; j < e->rank; j++)
+       mpz_mul (size, size, e->shape[j]);
+      bool not_size0 = (mpz_cmp_si (size, 0) != 0);
+      mpz_clear (size);
+      if (not_size0)
+       return false;
+    }
+
   for (c = gfc_constructor_first (e->value.constructor);
        c; c = gfc_constructor_next (c))
     if (c->expr->expr_type != EXPR_CONSTANT
diff --git a/gcc/testsuite/gfortran.dg/pack_simplify_1.f90 b/gcc/testsuite/gfortran.dg/pack_simplify_1.f90
new file mode 100644 (file)
index 0000000..06bc55a
--- /dev/null
@@ -0,0 +1,15 @@
+! { dg-do compile }
+! PR fortran/106049 - ICE in gfc_simplify_pack
+! Contributed by G.Steinmetz
+
+program p
+  type t
+  end type
+  logical, parameter :: m(0) = [ logical :: ]
+  type(t), parameter :: a(0) = [ t :: ]
+  type(t), parameter :: b(1) = [ t()  ]
+  type(t), parameter :: c(1) = [ t :: ]        ! { dg-error "Different shape" }
+  type(t), parameter :: d(0) = pack(a, m)
+  type(t), parameter :: e(1) = pack(b, [.true.])
+  type(t), parameter :: f(1) = pack(c, [.true.])
+end