bug7-parent-default-init.f90 fails in runtime with:
STOP 1
because the default initializer of 'plain' is not applied to the extended type.

bug22-import-pdt-ice.f90 fails with:
   26 |   use t2_m
      |      1
internal compiler error: in gfc_release_symbol, at fortran/symbol.cc:3412
0x1f5d97f internal_error(char const*, ...)
../../gcc/gcc/diagnostic-global-context.cc:787
0x451689 fancy_abort(char const*, int, char const*)
../../gcc/gcc/diagnostics/context.cc:1813

Although these are distinct errors, it was convenient to bundle these
fixes together.
The first two chunks fix bug7. The default initializer for the non-PDT
parent requires a check that all its component initializers are
present. Bug22 is fixed by ensuring that the reference count of
instance is bumped up again to allow for its double passage through
gfc_free_namespace: Once for the interface and again through the
module.

Passes regtesting on FC44/x86_64. OK for mainline.

Cheers

Paul
From b595003ad4ff7290aa09815198b9e104a19b001e Mon Sep 17 00:00:00 2001
From: Paul Thomas <[email protected]>
Date: Tue, 6 Oct 2026 17:04:41 +0100
Subject: [PATCH] Fortran: [meta-bug] [PDT] Parameterized derived type errors
 [PR82173]

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

gcc/fortran
	PR fortran/82173
	* decl.cc (gfc_get_pdt_instance): Declare componet 'c' and use
	it to determine if a parent PDT can have a valid initializer by
	asserting that all its components have initializers.
	An imported PDT instance must have its refs incremented.

gcc/testsuite/
	PR fortran/82173
	* gfortran.dg/pdt_bug7.f90: New test.
	* gfortran.dg/pdt_bug22.f90: New test.
---
 gcc/fortran/decl.cc                     | 14 +++++++++++-
 gcc/testsuite/gfortran.dg/pdt_bug22.f90 | 30 +++++++++++++++++++++++++
 gcc/testsuite/gfortran.dg/pdt_bug7.f90  | 18 +++++++++++++++
 3 files changed, 61 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gfortran.dg/pdt_bug22.f90
 create mode 100644 gcc/testsuite/gfortran.dg/pdt_bug7.f90

diff --git a/gcc/fortran/decl.cc b/gcc/fortran/decl.cc
index 01b6109940c..028ab14be23 100644
--- a/gcc/fortran/decl.cc
+++ b/gcc/fortran/decl.cc
@@ -4394,6 +4394,7 @@ gfc_get_pdt_instance (gfc_actual_arglist *param_list, gfc_symbol **sym,
 	  && c1->ts.u.derived
 	  && gfc_get_derived_super_type (*sym) == c2->ts.u.derived)
 	{
+	  gfc_component *c;
 	  if (c1->ts.u.derived->attr.pdt_template)
 	    {
 	      gfc_formal_arglist *f;
@@ -4442,6 +4443,15 @@ gfc_get_pdt_instance (gfc_actual_arglist *param_list, gfc_symbol **sym,
 	    }
 	  instance->attr.extension = c2->ts.u.derived->attr.extension + 1;
 
+	  /* To be a valid initializer of the parent type, all the components
+	     should have initializers and not be allocatable.  */
+	  c = c2->ts.u.derived->components;
+	  for (; c; c = c->next)
+	    if (!c->initializer)
+	      break;
+
+	  if (!IS_PDT (c2) && !c2->ts.u.derived->attr.alloc_comp && !c)
+	    c2->initializer = gfc_generate_initializer (&c2->ts, false);
 	  continue;
 	}
 
@@ -4683,8 +4693,10 @@ gfc_get_pdt_instance (gfc_actual_arglist *param_list, gfc_symbol **sym,
   if (ptr_seen)
     instance->attr.pointer_comp = 1;
 
-
   gfc_commit_symbol (instance);
+  if (pdt->attr.imported)
+    instance->refs++;;
+
   if (ext_param_list)
     *ext_param_list = type_param_spec_list;
   *sym = instance;
diff --git a/gcc/testsuite/gfortran.dg/pdt_bug22.f90 b/gcc/testsuite/gfortran.dg/pdt_bug22.f90
new file mode 100644
index 00000000000..effc9e43cbb
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pdt_bug22.f90
@@ -0,0 +1,30 @@
+! { dg-do run }
+! See attachment to PR82173 comment 10 - reproducers/bug22-import-pdt-ice.f90
+module t_m
+  type :: t(n)
+    integer, len :: n = 1
+    real :: v(n)
+  end type
+end module t_m
+
+module t2_m
+  use t_m
+  interface
+    subroutine handler (a)
+      import :: t
+      type(t(*)), intent(in) :: a
+    end subroutine
+  end interface
+end module t2_m
+
+subroutine handler (a)
+  use t_m
+  type(t(*)), intent(in) :: a
+  if (any (int (a%v) /= [1,2,3])) stop 1
+end subroutine
+
+  use t2_m
+  type(t(3)) :: x
+  x%v = [1.,2.,3.]
+  call handler (x)
+end
diff --git a/gcc/testsuite/gfortran.dg/pdt_bug7.f90 b/gcc/testsuite/gfortran.dg/pdt_bug7.f90
new file mode 100644
index 00000000000..7762b97a416
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pdt_bug7.f90
@@ -0,0 +1,18 @@
+! { dg-do run }
+! See attachment to PR82173 comment 10 - reproducers/bug7-parent-default-init.f90
+program h1
+  type :: plain
+    integer :: id = 11
+    real :: weight = 0.5
+  end type
+  type, extends(plain) :: para(n)
+    integer, len :: n = 1
+    real :: v(n)
+  end type
+  type(para(3)) :: p
+  type(plain) :: q
+  if (p%id /= 11) stop 1                       ! Inherited values 
+  if (int (200.0 * p%weight) /= 100) stop 2    ! were zero
+  if (q%id /= 11) stop 3 
+  if (int (200.0 * q%weight) /= 100) stop 4
+end program
-- 
2.55.0

Reply via email to