https://gcc.gnu.org/g:5e7c0ee446c9081c34f6e03230d8babbaa6b2f34
commit r17-6548-g5e7c0ee446c9081c34f6e03230d8babbaa6b2f34 Author: Paul Thomas <[email protected]> Date: Thu Nov 19 12:02:15 2026 +0000 Fortran: [PDT] Regression: Parameterized derived type errors [PR128877] 2021-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. Diff: --- 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(-) diff --git a/gcc/fortran/decl.cc b/gcc/fortran/decl.cc index 88bdb54dd2fc..01b7434444f2 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 5a8099ad1326..6b05f6a6a855 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 000000000000..bd1b017b2a3a --- /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
