Both bug6-alloc-self-component-ice.f90 and
bug17-final-rank-overload-ice.f90 fail with:
   12 | end program  ! Seg fault at trans-decl.cc:1697
      |                                              1
internal compiler error: Segmentation fault
0x1f5d97f internal_error(char const*, ...)
../../gcc/gcc/diagnostic-global-context.cc:787
0xc219bf crash_signal
../../gcc/gcc/toplev.cc:325
0x5dff99 gfc_get_symbol_decl(gfc_symbol*)
../../gcc/gcc/fortran/trans-decl.cc:1676

The fix is trivial - guard against calling  gfc_allocate_pdt_comp
without sym->param_list.

Passes regtesting on FC44/x86_64. OK for mainline.

Cheers

Paul
From 07409c4ff4ce21c613abbeb4c211b20e76dbb355 Mon Sep 17 00:00:00 2001
From: Paul Thomas <[email protected]>
Date: Thu, 8 Oct 2026 11:39:06 +0100
Subject: [PATCH] Fortran: [meta-bug] [PDT] Parameterized derived type errors
 [PR82173]

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

gcc/fortran
	PR fortran/82173
	* trans-decl.cc (gfc_trans_deferred_vars): gfc_init_default_pdt
	cannot be called unless the symbol has a param_list.

gcc/testsuite/
	PR fortran/82173
	* gfortran.dg/pdt_bug6.f90: New test.
	* gfortran.dg/pdt_bug17.f90: New test.
---
 gcc/fortran/trans-decl.cc               |  3 +-
 gcc/testsuite/gfortran.dg/pdt_bug17.f90 | 39 +++++++++++++++++++++++++
 gcc/testsuite/gfortran.dg/pdt_bug6.f90  | 12 ++++++++
 3 files changed, 53 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gfortran.dg/pdt_bug17.f90
 create mode 100644 gcc/testsuite/gfortran.dg/pdt_bug6.f90

diff --git a/gcc/fortran/trans-decl.cc b/gcc/fortran/trans-decl.cc
index a614e71eced..02c4c72259d 100644
--- a/gcc/fortran/trans-decl.cc
+++ b/gcc/fortran/trans-decl.cc
@@ -5161,7 +5161,8 @@ gfc_trans_deferred_vars (gfc_symbol * proc_sym, gfc_wrapped_block * block)
 	  if (!sym->attr.dummy && !sym->attr.pointer)
 	    {
 	      tmp = gfc_init_default_pdt (sym, true);
-	      if (!sym->attr.allocatable && tmp == NULL_TREE)
+	      if (!sym->attr.allocatable && tmp == NULL_TREE
+		  && sym->param_list)
 		{
 		  tmp = gfc_allocate_pdt_comp (sym->ts.u.derived,
 					       sym->backend_decl,
diff --git a/gcc/testsuite/gfortran.dg/pdt_bug17.f90 b/gcc/testsuite/gfortran.dg/pdt_bug17.f90
new file mode 100644
index 00000000000..2f76ae33289
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pdt_bug17.f90
@@ -0,0 +1,39 @@
+! { dg-do run }
+! See attachment to PR82173 comment 10 - reproducers/bug17-final-rank-overload-ice.f90
+module mm
+  type :: t(k, n)
+    integer, kind :: k = 4
+    integer, len :: n = 1
+    real(k) :: v(n)
+  contains
+    final :: fin4, fin8, fin4_r1
+  end type
+  integer :: tally = 0
+contains
+  subroutine fin4 (a)
+    type(t(4, *)), intent(inout) :: a
+    tally = tally + 1
+  end subroutine
+  subroutine fin8 (a)
+    type(t(8, *)), intent(inout) :: a
+    tally = tally + 10
+  end subroutine
+  subroutine fin4_r1 (a)
+    type(t(4, *)), intent(inout) :: a(:)
+    tally = tally + 100
+  end subroutine
+end module   ! Segmentation fault here
+
+  use mm
+  call foo
+!  Each final routine is called twice - assignment and leaving scope
+  if (tally /= 222) stop 1 
+contains
+  subroutine foo
+  type(t(4, 2)) :: a, b(2)
+  type(t(8, 2)) :: c
+  a = t(4, 2)([1.0,2.0])
+  b = [t(4, 2)([1.0,2.0]), t(4, 2)([3.0,4.0])]
+  c = t(8, 2)([1.0,2.0])
+  end
+end
diff --git a/gcc/testsuite/gfortran.dg/pdt_bug6.f90 b/gcc/testsuite/gfortran.dg/pdt_bug6.f90
new file mode 100644
index 00000000000..3fa573e9189
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pdt_bug6.f90
@@ -0,0 +1,12 @@
+! { dg-do run }
+! See attachment to PR82173 comment 10 - reproducers/bug6-alloc-self-component-ice.f90
+program g2
+  type :: node(n)
+    integer, len :: n = 1
+    real :: v(n)
+    type(node(:)), allocatable :: owned
+  end type
+  type(node(3)) :: head
+  head%v = 1.0
+  if (allocated (head%owned)) stop 1
+end program  ! Seg fault at trans-decl.cc:1697
-- 
2.55.0

Reply via email to