Hi All,

I intend to push the attached to mainline tomorrow morning if there
are no objections.

Cheers

Paul
From 962caa6cd0a025056ffaf8f28ff53f9dece6a84b Mon Sep 17 00:00:00 2001
From: Paul Thomas <[email protected]>
Date: Mon, 6 Apr 2026 15:02:15 +0100
Subject: [PATCH] Fortran: Fix ICE instantiating nested PDTs [PR124598]

2026-04-06  Paul Thomas  <[email protected]>

gcc/fortran
	PR fortran/124598
	* trans-expr.cc (gfc_conv_structure): Do not add parameterized
	components to a structure constructor.

gcc/testsuite/
	PR fortran/124598
	* gfortran.dg/pdt_91.f03: New test.
---
 gcc/fortran/trans-expr.cc            |  4 +++-
 gcc/testsuite/gfortran.dg/pdt_91.f03 | 30 ++++++++++++++++++++++++++++
 2 files changed, 33 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gfortran.dg/pdt_91.f03

diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc
index f50ddce8e82..6c0bd5ce910 100644
--- a/gcc/fortran/trans-expr.cc
+++ b/gcc/fortran/trans-expr.cc
@@ -10528,7 +10528,9 @@ gfc_conv_structure (gfc_se * se, gfc_expr * expr, int init)
 	 components.  Although the latter have a default initializer
 	 of EXPR_NULL,... by default, the static nullify is not needed
 	 since this is done every time we come into scope.  */
-      if (!c->expr || (cm->attr.allocatable && cm->attr.flavor != FL_PROCEDURE))
+      if (!c->expr
+	  || (cm->attr.allocatable && cm->attr.flavor != FL_PROCEDURE)
+	  || (IS_PDT (cm) && has_parameterized_comps (cm->ts.u.derived)))
 	continue;
 
       if (cm->initializer && cm->initializer->expr_type != EXPR_NULL
diff --git a/gcc/testsuite/gfortran.dg/pdt_91.f03 b/gcc/testsuite/gfortran.dg/pdt_91.f03
new file mode 100644
index 00000000000..ca0a79130ed
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pdt_91.f03
@@ -0,0 +1,30 @@
+! {dg-do compile }
+! { dg-options "-fdump-tree-original" }
+!
+! Test the fix for PR124598.
+!
+! Contributed by Antoine Lemoine  <[email protected]>
+!
+program test_pdt
+   implicit none
+
+   type t_foo
+      integer :: c
+   end type t_foo
+
+   type t_bar(n)
+      integer, len :: n = 1
+      type(t_foo) :: foo(n)
+   end type t_bar
+
+   type t_baz(n)
+      integer, len :: n = 1
+      type(t_bar(n)) :: bar(n)
+   end type t_baz
+
+   type(t_baz(n=10)) :: baz         ! Used to ICE at this line
+!   baz%bar(1)%foo = t_foo(1)
+   print *, baz%bar(1)%foo
+end program test_pdt
+! Check that the upper bound for baz%bar%foo is set correctly.
+! { dg-final { scan-tree-dump-times "foo.dim.0..ubound = 10" 1 "original" } }
-- 
2.53.0

Reply via email to