[fortran] PR124598] (PDT) - ICE instantiating nested PDTs

Message ID CAGkQGiJDuiOKSmoHX31hnoS1joUhqzbZCb3cC=RsDvtVGYKSag@mail.gmail.com
State New
Headers
Series [fortran] PR124598] (PDT) - ICE instantiating nested PDTs |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gcc_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_gcc_check--master-aarch64 success Test passed

Commit Message

Paul Richard Thomas April 6, 2026, 2:08 p.m. UTC
  Hi All,

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

Cheers

Paul
  

Comments

Harald Anlauf April 6, 2026, 7:25 p.m. UTC | #1
Am 06.04.26 um 4:08 PM schrieb Paul Richard Thomas:
> Hi All,
> 
> I intend to push the attached to mainline tomorrow morning if there
> are no objections.

Hi Paul,

no objections to the patch, but the testcase prints undefined
values unless you uncomment the line

!   baz%bar(1)%foo = t_foo(1)

(Try either valgrind or MALLOC_PERTURB_=42)

Cheers,
Harald

> Cheers
> 
> Paul
  

Patch

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

2026-04-06  Paul Thomas  <pault@gcc.gnu.org>

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  <antoine.lemoine@bordeaux-inp.fr>
+!
+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