https://gcc.gnu.org/g:5b0e9c6077669d964f819382f245672cdeb62c94
commit r17-5110-g5b0e9c6077669d964f819382f245672cdeb62c94 Author: Paul Thomas <[email protected]> Date: Tue Oct 13 12:06:58 2026 +0100 Fortran: [PDT] (5/16) Parameterized derived type errors [PR82173] 2026-10-13 Paul Thomas <[email protected]> gcc/fortran PR fortran/82173 * decl.cc (gfc_get_pdt_instance): An new, imported PDT instance must have its refs incremented. Provide a non-PDT parent type with a default initializer. Set 'instance' referenced. gcc/testsuite/ PR fortran/82173 * gfortran.dg/pdt_bug7.f90: New test. * gfortran.dg/pdt_bug22.f90: New test. Diff: --- gcc/fortran/decl.cc | 7 ++++++- gcc/testsuite/gfortran.dg/pdt_bug22.f90 | 30 ++++++++++++++++++++++++++++++ gcc/testsuite/gfortran.dg/pdt_bug7.f90 | 18 ++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/gcc/fortran/decl.cc b/gcc/fortran/decl.cc index 01b6109940c5..46a1dece8919 100644 --- a/gcc/fortran/decl.cc +++ b/gcc/fortran/decl.cc @@ -4323,6 +4323,7 @@ gfc_get_pdt_instance (gfc_actual_arglist *param_list, gfc_symbol **sym, gfc_get_sym_tree (instance->name, gfc_current_ns, &s, false, &gfc_current_locus); s->n.sym = instance; + instance->refs++; } s->n.sym->attr.imported = 1; s->import_only = 1; @@ -4442,6 +4443,9 @@ gfc_get_pdt_instance (gfc_actual_arglist *param_list, gfc_symbol **sym, } instance->attr.extension = c2->ts.u.derived->attr.extension + 1; + /* Add the parent initializer. */ + if (!IS_PDT (c2) && !c2->ts.u.derived->attr.alloc_comp) + c2->initializer = gfc_generate_initializer (&c2->ts, false); continue; } @@ -4683,8 +4687,9 @@ gfc_get_pdt_instance (gfc_actual_arglist *param_list, gfc_symbol **sym, if (ptr_seen) instance->attr.pointer_comp = 1; - + gfc_set_sym_referenced (instance); gfc_commit_symbol (instance); + 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 000000000000..effc9e43cbb6 --- /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 000000000000..7762b97a416b --- /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
