Hello All, This is the first of three patches for PDT bugs found by Jerry, assisted by Claude.
This patch is relatively trivial. The error arises because resolve_component attempts to pass real PDT template components, with integer initializers, through gfc_check_assign_symbol. Since the component has kind=0, the error in the title is generated. Since the template is never used in anger, the fix is to prevent the call to gfc_check_assign_symbol. The chunks in decl.cc streamline the handling of PDT component initializers. In particular, the typespec of the component is not donated to the initializer unless its type is unknown or of the same type as the component. Regression tests on FC45/x86_64. OK for mainline? Cheers Paul
From c5918b449edcd08efa9e2395f7d94b2d36d5dfcb Mon Sep 17 00:00:00 2001 From: Paul Thomas <[email protected]> Date: Thu, 19 Nov 2026 12:02:15 +0000 Subject: [PATCH] Fortran: [PDT] Regression: Parameterized derived type errors [PR128877] 2026-11-20 Paul Thomas <[email protected]> gcc/fortran PR fortran/128877 * decl.cc (gfc_trans_deferred_vars): Defer updating type and simplification of component initalizers to the end of the loop. Only update the typespec of the initializer to that of the component if it is of unknown type or the same type. * resolve.cc (resolve_component): Do not pass real PDT template components with kind=0 through gfc_check_assign_symbol. gcc/testsuite/ PR fortran/128877 * gfortran.dg/pdt_bug27.f90: New test. --- gcc/fortran/decl.cc | 15 +++++++------- gcc/fortran/resolve.cc | 1 + gcc/testsuite/gfortran.dg/pdt_bug27.f90 | 27 +++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 gcc/testsuite/gfortran.dg/pdt_bug27.f90 diff --git a/gcc/fortran/decl.cc b/gcc/fortran/decl.cc index 88bdb54dd2f..01b7434444f 100644 --- a/gcc/fortran/decl.cc +++ b/gcc/fortran/decl.cc @@ -4648,11 +4648,9 @@ gfc_get_pdt_instance (gfc_actual_arglist *param_list, gfc_symbol **sym, gfc_dt_lower_string (c2->ts.u.derived->name)); if (s) c2->initializer->symtree = s; - c2->initializer->ts = c2->ts; if (!s) gfc_insert_parameter_exprs (c2->initializer, type_param_spec_list); - gfc_simplify_expr (c2->initializer, 1); } } @@ -4665,8 +4663,6 @@ gfc_get_pdt_instance (gfc_actual_arglist *param_list, gfc_symbol **sym, || c2->attr.pdt_array) && c1->initializer) { c2->initializer = gfc_copy_expr (c1->initializer); - if (c2->initializer->ts.type == BT_UNKNOWN) - c2->initializer->ts = c2->ts; gfc_insert_parameter_exprs (c2->initializer, type_param_spec_list); /* The template initializers are parsed using gfc_match_expr rather than gfc_match_init_expr. Apply the missing reduction to the @@ -4676,19 +4672,24 @@ gfc_get_pdt_instance (gfc_actual_arglist *param_list, gfc_symbol **sym, gfc_free_expr (c2->initializer); goto error_return; } - gfc_simplify_expr (c2->initializer, 1); } - /* Pick up any remaining initializers that could be simplified. */ + /* Pick up any remaining initializers and, if necessary, substitite + parameters, simplify and correct the typespec. */ if (c1->initializer) { if (!c2->initializer) c2->initializer = gfc_copy_expr (c1->initializer); + if (gfc_derived_parameter_expr (c2->initializer)) gfc_insert_parameter_exprs (c2->initializer, type_param_spec_list); - c2->initializer->ts = c2->ts; + if (!gfc_is_constant_expr (c2->initializer)) gfc_simplify_expr (c2->initializer, 1); + + if (c2->initializer->ts.type == BT_UNKNOWN + || (c2->initializer->ts.type == c2->ts.type)) + c2->initializer->ts = c2->ts; } } diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc index 5a8099ad132..6b05f6a6a85 100644 --- a/gcc/fortran/resolve.cc +++ b/gcc/fortran/resolve.cc @@ -18288,6 +18288,7 @@ resolve_component (gfc_component *c, gfc_symbol *sym) if (c->initializer && !sym->attr.vtype && !c->attr.pdt_kind && !c->attr.pdt_len + && !(sym->attr.pdt_template && c->ts.type == BT_REAL && c->ts.kind == 0) && !gfc_check_assign_symbol (sym, c, c->initializer)) return false; diff --git a/gcc/testsuite/gfortran.dg/pdt_bug27.f90 b/gcc/testsuite/gfortran.dg/pdt_bug27.f90 new file mode 100644 index 00000000000..bd1b017b2a3 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/pdt_bug27.f90 @@ -0,0 +1,27 @@ +! { dg-do run } +! +! Contributed by Jerry Delisle <[email protected]> +! +module pdt_kind_init_m + implicit none +! real(k) :: x = 0 in a PDT with KIND parameter k was rejected: +! "Cannot convert INTEGER(4) to REAL(0)". Valid (F2018 7.5.4.4) +! 16.2.1 rejects it too. + type t(k) + integer, kind :: k = kind(1.0) + real(k) :: x = 0 + end type +end module + + +program p + call pst_kind_param_real_init +contains + subroutine pst_kind_param_real_init + use pdt_kind_init_m + implicit none + type(t) :: a + type(t(kind(1.0d0))) :: b + if (a%x /= 0 .or. b%x /= 0) stop 1 + end subroutine pst_kind_param_real_init +end program -- 2.56.1
