Hello All,

This is the second of three patches that fix errors found by Jerry/Claude.

Class PDTs have not quite received the attention of their derived type
cousins. I started to fix that in a recent patch but made the mistake
of confusing class data pointers with class pointers.

Additionally, the passed object for class array dummies is tucked away
in the DECL_LANG_SPECIFIC field, while the sym->backend_decl is the
class data array. This is corrected in this patch

Regression tests OK on FC45/x86_64 - OK for mainline?

Cheers

Paul
From 2ef5c9984791d85bee8c30f2487d18cd49a0a890 Mon Sep 17 00:00:00 2001
From: Paul Thomas <[email protected]>
Date: Fri, 20 Nov 2026 08:42:46 +0000
Subject: [PATCH] Fortran: [PDT] Regression: Parameterized derived type errors
 [PR128876]

2026-11-20  Paul Thomas  <[email protected]>

gcc/fortran
	PR fortran/128876
	* trans-decl.cc (gfc_trans_deferred_vars): Replace CLASS_DATA (
	sym)->attr.pointer with CLASS_DATA (sym)->attr.class_pointer.
	Delete CLASS_DATA (sym)->attr.pointer for dummies. The hidden
	DECL_LANG_SPECIFIC declaration should be used for class objects
	if it is available.

gcc/testsuite/
	PR fortran/128876
	* gfortran.dg/pdt_bug28.f90: New test.
	* gfortran.dg/pdt_bug29.f90: New test.
	* gfortran.dg/pdt_bug30.f90: New test.
---
 gcc/fortran/trans-decl.cc               | 15 +++++----
 gcc/testsuite/gfortran.dg/pdt_bug28.f90 | 45 +++++++++++++++++++++++++
 gcc/testsuite/gfortran.dg/pdt_bug29.f90 | 28 +++++++++++++++
 gcc/testsuite/gfortran.dg/pdt_bug30.f90 | 41 ++++++++++++++++++++++
 4 files changed, 123 insertions(+), 6 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/pdt_bug28.f90
 create mode 100644 gcc/testsuite/gfortran.dg/pdt_bug29.f90
 create mode 100644 gcc/testsuite/gfortran.dg/pdt_bug30.f90

diff --git a/gcc/fortran/trans-decl.cc b/gcc/fortran/trans-decl.cc
index 5129637165a..77908653511 100644
--- a/gcc/fortran/trans-decl.cc
+++ b/gcc/fortran/trans-decl.cc
@@ -4798,7 +4798,8 @@ gfc_init_default_pdt (gfc_symbol *sym, bool dealloc)
      there are no LEN components.  */
   c = sym->ts.u.derived->components;
   for (; c; c = c->next)
-    if (c->attr.pdt_len || !c->initializer)
+    if (c->attr.pdt_len || !c->initializer
+	|| (IS_PDT (c) && c->ts.u.derived->attr.alloc_comp))
       return NULL_TREE;
 
   gfc_init_block (&block);
@@ -5274,14 +5275,12 @@ gfc_trans_deferred_vars (gfc_symbol * proc_sym, gfc_wrapped_block * block)
 		}
 	    }
 	}
-      else if (IS_CLASS_PDT (sym) && !CLASS_DATA (sym)->attr.pointer)
+      else if (IS_CLASS_PDT (sym) && !CLASS_DATA (sym)->attr.class_pointer)
 	{
 	  gfc_component *data = CLASS_DATA (sym);
 	  is_pdt_type = true;
 	  gfc_init_block (&tmpblock);
-	  if (!(sym->attr.dummy
-		|| CLASS_DATA (sym)->attr.pointer
-		|| CLASS_DATA (sym)->attr.allocatable))
+	  if (!sym->attr.dummy && !CLASS_DATA (sym)->attr.allocatable)
 	    {
 	      tmp = gfc_class_data_get (sym->backend_decl);
 	      tmp = gfc_allocate_pdt_comp (data->ts.u.derived, tmp,
@@ -5322,7 +5321,11 @@ gfc_trans_deferred_vars (gfc_symbol * proc_sym, gfc_wrapped_block * block)
 		 treatment in gfc_trans_allocate.  */
 	      if (all_explicit)
 		{
-		  tmp = gfc_class_data_get (sym->backend_decl);
+		  tmp = sym->backend_decl;
+		  if (DECL_LANG_SPECIFIC (tmp))
+		    tmp = GFC_DECL_SAVED_DESCRIPTOR (tmp);
+
+		  tmp = gfc_class_data_get (tmp);
 		  tmp = gfc_check_pdt_dummy (declared, tmp,
 					     class_data->as ?
 						      class_data->as->rank : 0,
diff --git a/gcc/testsuite/gfortran.dg/pdt_bug28.f90 b/gcc/testsuite/gfortran.dg/pdt_bug28.f90
new file mode 100644
index 00000000000..5607ebb7dfc
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pdt_bug28.f90
@@ -0,0 +1,45 @@
+! { dg-do run }
+!
+! PR128876
+!
+! Type-bound call on a PDT component of a PDT variable: ICE in
+! gfc_spec_list_type (expr.cc), called from the hidden-PDT-argument code
+! in gfc_conv_procedure_call, which takes the base variable's param_list
+! ('m' of c_t) and looks it up in the component's type (h_t)
+!
+! Regression from r17 6335154826f, PDT (9/16) [PR82173]; works with 16.2.1.
+! Found building fiats (src/fiats/training_configuration_s.F90:36).
+!
+! Contributed by Jerry Delisle  <[email protected]>
+!
+module pdtmod
+  implicit none
+  type h_t(k)
+    integer, kind :: k = kind(1.0)
+    real(k) :: x = real(1, k)
+  contains
+    generic :: to_i => r4_to_i, r8_to_i
+    procedure, private :: r4_to_i, r8_to_i
+  end type
+  type c_t(m)
+    integer, kind :: m = kind(1.0)
+    type(h_t(m)) :: h
+  end type
+contains
+  pure integer function r4_to_i (self)
+    class(h_t(kind(1.0))), intent(in) :: self
+    r4_to_i = int (self%x)
+  end function
+  pure integer function r8_to_i (self)
+    class(h_t(kind(1.0d0))), intent(in) :: self
+    r8_to_i = 2 * int (self%x)
+  end function
+end module pdtmod
+program p
+  use pdtmod
+  implicit none
+  type(c_t) :: c
+  type(c_t(kind(1.0d0))) :: d
+  if (c%h%to_i () /= 1) stop 1
+  if (d%h%to_i () /= 2) stop 2
+end program
diff --git a/gcc/testsuite/gfortran.dg/pdt_bug29.f90 b/gcc/testsuite/gfortran.dg/pdt_bug29.f90
new file mode 100644
index 00000000000..e8c0d25f48f
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pdt_bug29.f90
@@ -0,0 +1,28 @@
+! { dg-do compile }
+! { dg-options "-fcheck=bounds" }
+!
+! PR128876
+!
+! CLASS dummy of a PDT type with -fcheck=bounds: ICE (gcc_unreachable) in
+! gfc_trans_deferred_vars.  -fcheck=bounds defers the dummy's init, and
+! the CLASS PDT branch there now requires !CLASS_DATA (sym)->attr.pointer,
+! which is never true: _data of a class container always has attr.pointer;
+! a CLASS POINTER entity is attr.class_pointer.
+!
+! Regression from r17 6335154826f, PDT (9/16) [PR82173]; works with 16.2.1.
+! Found building fiats (example/learn-addition.F90, fpm debug profile).
+!
+! Contributed by Jerry Delisle  <[email protected]>
+!
+module pdt_class_dummy_m
+  implicit none
+  type t(k)
+    integer, kind :: k = kind(1.0)
+    real(k) :: x
+  end type
+contains
+  subroutine s (a)
+    class(t), intent(in) :: a
+    print *, a%x
+  end subroutine
+end module
diff --git a/gcc/testsuite/gfortran.dg/pdt_bug30.f90 b/gcc/testsuite/gfortran.dg/pdt_bug30.f90
new file mode 100644
index 00000000000..a5460210a16
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pdt_bug30.f90
@@ -0,0 +1,41 @@
+! { dg-do run }
+!
+! PR128876
+!
+! Local variable of a PDT type with a component of a recursive PDT type:
+! the default initialization is an assignment from a constructor
+! temporary, which first deallocates the old value of l%node%next, i.e.
+! uninitialized stack.  free(): invalid pointer / SIGSEGV at run time.
+! Regression on trunk; works with 16.2.1.  Found running the fiats tests
+! (__deallocate_neuron_m_PDTneuron_t_4 in neural_network from_json).
+!
+! Contributed by Jerry Delisle  <[email protected]>
+!
+module pdt_rec_comp_m
+  implicit none
+  type node_t(k)
+    integer, kind :: k = kind(1.0)
+    real(k) :: b
+    type(node_t(k)), allocatable :: next
+  end type
+  type layer_t(k)
+    integer, kind :: k = kind(1.0)
+    type(node_t(k)) :: node
+  end type
+contains
+  subroutine s (r)
+    real, intent(out) :: r
+    type(layer_t) :: l
+    l%node%b = 1.0
+    allocate (l%node%next)
+    l%node%next%b = 2.0
+    r = l%node%next%b
+  end subroutine
+end module
+program p
+  use pdt_rec_comp_m
+  implicit none
+  real :: r
+  call s (r)
+  if (r /= 2.0) stop 1
+end program
-- 
2.56.1

Reply via email to