[PATCH, fortran] Followup on PR103367

Jerry D <[email protected]>
Newsgroups gmane.comp.gcc.patches,gmane.comp.gcc.fortran
Message-ID <[email protected]>
I plan to commit the attached patch shortly to close this out.

Regression tests OK

Thanks for comments all.

Regards

Jerry

---

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]>
pr103367.diff (text/x-patch, 2.9 KB)
From 7e53117e487fac5a1d4f209b40070921ae8a0416 Mon Sep 17 00:00:00 2001
From: Christopher Albert <[email protected]>
Date: Wed, 8 Apr 2026 22:37:11 +0200
Subject: [PATCH] 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]>
---
 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 ed299c8b21b..b57c18aba00 100644
--- a/gcc/fortran/trans-array.cc
+++ b/gcc/fortran/trans-array.cc
@@ -7099,13 +7099,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 c4c860c65ff..e7c89b41a7c 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" }
-- 
2.53.0
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.