[gcc r13-10406] fortran: Diagnose invalid array initializer after parameter substitution [PR103367]

Jerry DeLisle via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:6d3ada375f370a829ac67261c09772613d389420

commit r13-10406-g6d3ada375f370a829ac67261c09772613d389420
Author: Christopher Albert <[email protected]>
Date:   Wed Apr 8 22:37:11 2026 +0200

    fortran: Diagnose invalid array initializer after parameter substitution [PR103367]
    
    Keep the trunk fallback from r16-8509, but turn it into a real
    constant-expression error instead of silently returning an empty
    constructor.  This avoids the ICE from PR103367 without regressing
    the more specific diagnostics discussed in the Bugzilla follow-up.
    
            PR fortran/103367
    
    gcc/fortran/ChangeLog:
    
            * trans-array.cc (gfc_conv_array_initializer): Emit an error for
            invalid residual initializer expressions before returning a safe
            empty constructor.
    
    gcc/testsuite/ChangeLog:
    
            * gfortran.dg/pr103367.f90: Expect a constant-expression error and
            prune the legacy-extension warning.
    
    Signed-off-by: Christopher Albert <[email protected]>
    (cherry picked from commit 326fe37d6981c189fb0f2a5a4ab7f7a5b95ecf89)

Diff:
---
 gcc/fortran/trans-array.cc             | 10 +++++++---
 gcc/testsuite/gfortran.dg/pr103367.f90 |  7 ++++---
 2 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc
index cf4f7cda7add..023f38388742 100644
--- a/gcc/fortran/trans-array.cc
+++ b/gcc/fortran/trans-array.cc
@@ -6383,13 +6383,17 @@ gfc_conv_array_initializer (tree type, gfc_expr * expr)
     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.  */
+     constructor, structure constructor, or NULL.  Anything else is invalid
+     and must not ICE later in lowering.  */
   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);
+    {
+      gfc_error ("Array initializer at %L does not reduce to a constant "
+		 "expression", &expr->where);
+      return build_constructor (type, NULL);
+    }
 
   switch (expr->expr_type)
     {
diff --git a/gcc/testsuite/gfortran.dg/pr103367.f90 b/gcc/testsuite/gfortran.dg/pr103367.f90
index c4c860c65ffd..e7c89b41a7cf 100644
--- a/gcc/testsuite/gfortran.dg/pr103367.f90
+++ b/gcc/testsuite/gfortran.dg/pr103367.f90
@@ -1,14 +1,15 @@
 ! { dg-do compile }
 !
 ! PR fortran/103367
-! An undefined variable in a parameter array initializer reached
-! gfc_conv_array_initializer and hit gcc_unreachable().
+! Invalid initialization expressions must not ICE if they survive
+! semantic checking and reach array initializer lowering.
 
 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" }
+  integer :: y(1,2) = (x(b)%a) ! { dg-error "does not reduce to a constant expression" }
   print *, y
 end
+! { dg-prune-output "Legacy Extension: REAL array index" }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.