]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
fortran: Fix ICE in gfc_conv_array_initializer with invalid index [PR103367]
authorChristopher Albert <albert@tugraz.at>
Sun, 29 Mar 2026 20:18:43 +0000 (22:18 +0200)
committerJerry DeLisle <jvdelisle@gcc.gnu.org>
Tue, 7 Apr 2026 17:22:39 +0000 (10:22 -0700)
An undefined variable used as an array index in a parameter initializer
expression reaches gfc_conv_array_initializer after parameter substitution
with an unexpected expression type, hitting gcc_unreachable().

Guard against unexpected expression types by returning a zero-filled
constructor, since the frontend has already diagnosed the error.

PR fortran/103367

gcc/fortran/ChangeLog:

* trans-array.cc (gfc_conv_array_initializer): Return empty
constructor for unexpected expression types after parameter
substitution.

gcc/testsuite/ChangeLog:

* gfortran.dg/pr103367.f90: New test.

Signed-off-by: Christopher Albert <albert@tugraz.at>
gcc/fortran/trans-array.cc
gcc/testsuite/gfortran.dg/pr103367.f90 [new file with mode: 0644]

index d03fe1daccf463a128ca15862359a25b86661a96..ed299c8b21bb250457766be70a81dfd3d1d4e251 100644 (file)
@@ -7098,6 +7098,15 @@ gfc_conv_array_initializer (tree type, gfc_expr * expr)
       && expr->symtree->n.sym->value)
     expr = expr->symtree->n.sym->value;
 
+  /* After parameter substitution the expression should be a constant, array
+     constructor, structure constructor, or NULL.  Anything else means the
+     frontend already diagnosed an error; return a zero-filled array.  */
+  if (expr->expr_type != EXPR_CONSTANT
+      && expr->expr_type != EXPR_STRUCTURE
+      && expr->expr_type != EXPR_ARRAY
+      && expr->expr_type != EXPR_NULL)
+    return build_constructor (type, NULL);
+
   switch (expr->expr_type)
     {
     case EXPR_CONSTANT:
diff --git a/gcc/testsuite/gfortran.dg/pr103367.f90 b/gcc/testsuite/gfortran.dg/pr103367.f90
new file mode 100644 (file)
index 0000000..c4c860c
--- /dev/null
@@ -0,0 +1,14 @@
+! { dg-do compile }
+!
+! PR fortran/103367
+! An undefined variable in a parameter array initializer reached
+! gfc_conv_array_initializer and hit gcc_unreachable().
+
+program p
+  type t
+    integer :: a(1,2) = 3
+  end type
+  type(t), parameter :: x(1) = t(4)
+  integer :: y(1,2) = (x(b)%a)  ! { dg-warning "Legacy Extension" }
+  print *, y
+end