https://gcc.gnu.org/g:915fdb3bd9773b7c8afd180ffee6f0ecdc79fc04
commit r16-4383-g915fdb3bd9773b7c8afd180ffee6f0ecdc79fc04 Author: Paul Thomas <[email protected]> Date: Sun Oct 12 08:21:11 2025 +0100 Fortran: Fix some minor PDT parse errors [PR95543,PR103748] 2025-10-12 Paul Thomas <[email protected]> gcc/fortran PR fortran/95543 PR fortran/103748 * decl.cc (insert_parameter_exprs): Guard param->expr before using it. (gfc_get_pdt_instance): Substitute paramaters in kind default initializers. (gfc_match_decl_type_spec): Emit an error if a type paramter specification list appears in a variable declaraion with a non-parameterized type. * primary.cc (gfc_match_rvalue): Emit an error if a type spec list is empty. gcc/testsuite/ PR fortran/95543 * gfortran.dg/pdt_17.f03: Change error message. * gfortran.dg/pdt_57.f03: New test. PR fortran/103748 * gfortran.dg/pdt_58.f03: New test. Diff: --- gcc/fortran/decl.cc | 18 +++++++++++++- gcc/fortran/primary.cc | 8 +++++- gcc/testsuite/gfortran.dg/pdt_17.f03 | 2 +- gcc/testsuite/gfortran.dg/pdt_57.f03 | 47 ++++++++++++++++++++++++++++++++++++ gcc/testsuite/gfortran.dg/pdt_58.f03 | 14 +++++++++++ 5 files changed, 86 insertions(+), 3 deletions(-) diff --git a/gcc/fortran/decl.cc b/gcc/fortran/decl.cc index 3fba8b1af396..5da3c2672456 100644 --- a/gcc/fortran/decl.cc +++ b/gcc/fortran/decl.cc @@ -3855,7 +3855,7 @@ insert_parameter_exprs (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED, if (strcmp (e->symtree->n.sym->name, param->name) == 0) break; - if (param) + if (param && param->expr) { copy = gfc_copy_expr (param->expr); *e = *copy; @@ -4028,6 +4028,12 @@ gfc_get_pdt_instance (gfc_actual_arglist *param_list, gfc_symbol **sym, /* Try simplification even for LEN expressions. */ bool ok; gfc_resolve_expr (kind_expr); + + if (c1->attr.pdt_kind + && kind_expr->expr_type != EXPR_CONSTANT + && type_param_spec_list) + gfc_insert_parameter_exprs (kind_expr, type_param_spec_list); + ok = gfc_simplify_expr (kind_expr, 1); /* Variable expressions seem to default to BT_PROCEDURE. TODO find out why this is and fix it. */ @@ -4810,6 +4816,16 @@ gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag) return MATCH_ERROR; } + if (dt_sym && decl_type_param_list + && dt_sym->attr.flavor == FL_DERIVED + && !dt_sym->attr.pdt_type + && !dt_sym->attr.pdt_template) + { + gfc_error ("Type %qs is not parameterized and so the type parameter spec " + "list at %C may not appear", dt_sym->name); + return MATCH_ERROR; + } + if (sym && sym->attr.flavor == FL_DERIVED && sym->attr.pdt_template && gfc_current_state () != COMP_DERIVED) diff --git a/gcc/fortran/primary.cc b/gcc/fortran/primary.cc index fd03ceace51f..cba4208a89fa 100644 --- a/gcc/fortran/primary.cc +++ b/gcc/fortran/primary.cc @@ -4083,7 +4083,13 @@ gfc_match_rvalue (gfc_expr **result) NULL. */ if (gfc_peek_ascii_char() == '(') type_spec_list = true; - + if (!actual_arglist && !type_spec_list) + { + gfc_error_now ("F2023 R755: The empty type specification at %C " + "is not allowed"); + m = MATCH_ERROR; + break; + } /* Generate this instance using the type parameters from the first argument list and return the parameter list in ctr_arglist. */ diff --git a/gcc/testsuite/gfortran.dg/pdt_17.f03 b/gcc/testsuite/gfortran.dg/pdt_17.f03 index d03e2d139a02..eab9ee9b54ee 100644 --- a/gcc/testsuite/gfortran.dg/pdt_17.f03 +++ b/gcc/testsuite/gfortran.dg/pdt_17.f03 @@ -6,6 +6,6 @@ ! program p type t(a) ! { dg-error "does not have a component" } - integer(kind=t()) :: x ! { dg-error "Expected initialization expression" } + integer(kind=t()) :: x ! { dg-error "empty type specification" } end type end diff --git a/gcc/testsuite/gfortran.dg/pdt_57.f03 b/gcc/testsuite/gfortran.dg/pdt_57.f03 new file mode 100644 index 000000000000..457ec7944840 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/pdt_57.f03 @@ -0,0 +1,47 @@ +! { dg-do compile } +! +! Test the fix for pr95543. The variable declaration in each subroutine used to ICE +! because the substitution of a in the default initializers of b was not being done. +! +! Contributed by Gerhard Steinmetz <[email protected]> +! +program p + call foo1 + call foo2 + call foo3 + call foo4 +contains + subroutine foo1 + type t(a, b) + integer, kind :: a = 4 + integer, kind :: b = a + 4 + end type + type(t()) :: z ! { dg-error "empty type specification" } + print *, z%b + end + subroutine foo2 + type t(a, b) + integer, kind :: a = 1 + integer, kind :: b = a + end type + type(t) :: z + print *, z%b + end + subroutine foo3 + type t(a, b) + integer, kind :: a = 1 + integer, kind :: b = a + end type + type(t(2)) :: z + print *, z%b + end + subroutine foo4 + type t(a, b) + integer, kind :: a = 4 + integer, kind :: b = a + 4 + end type + type(t(b = 6)) :: z + print *, z%b + end +end + diff --git a/gcc/testsuite/gfortran.dg/pdt_58.f03 b/gcc/testsuite/gfortran.dg/pdt_58.f03 new file mode 100644 index 000000000000..cf26e8a03bf1 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/pdt_58.f03 @@ -0,0 +1,14 @@ +! { dg-do compile } +! +! Test fix for PR103748. +! +! Contributed by Bastiaan Braams <[email protected]> +! +program test + implicit none + type f_type + integer, allocatable :: x(:) + end type f_type + type (f_type(n=9)) :: f ! { dg-error "is not parameterized" } + stop +end program test
