Hello All, Until now, 11 of the tests of the 'neg' directory, attached to comment #10 of PR82173, produced no compilation error as they should have. The attached patch fixes and exercises them all.
The patch is straightforward. Note that the first chunk is a bit puzzling in that a malady of the pdt_template has to be diagnosed on the PDT instance. The reason for this is explained by the comment in the chunk. Note also the need for gfc_error_now for this one case; otherwise the error disappears. I chose to put all the tests in one case for the testsuite, largely because it does no harm, since false alarms will produce an identifiable error, but also because patch (16/16) will be a complete reform of the PDT side of the gfortran testsuite. With this patch in place, all the 'neg' and 'pos' tests in comment 10 pass regression testing. It will take me a week or so to do a full analysis of overlaps between the tests in comment #10 and those already in gfortran.dg. I hope to radically decrease the number of PDT programs in the testsuite, in the hope of helping to reduce the execution time. Beyond this, the next step will be to work on the memory leaks, as described in PR121972. Regression tested on FC45/x86_64 with the gfortran.dg tests and the new ones from comment 10. OK for mainline? Regards Paul
From c7004b634bfb86a8e49c770ae7584068bb3051af Mon Sep 17 00:00:00 2001 From: Paul Thomas <[email protected]> Date: Thu, 26 Nov 2026 10:49:06 +0000 Subject: [PATCH] Fortran: [meta-bug] (15/16) Parameterized derived type errors [PR82173] 2026-11-26 Paul Thomas <[email protected]> gcc/fortran PR fortran/82173 * decl.cc (gfc_get_pdt_instance): Ensure that keywords in type spec lists correspond to parameters of the type (C799). * expr.cc (gfc_check_pointer_assign): Check that constant type spec parameters have the same value on both sides. * resolve.cc (resolve_external_pdt_args): New function. (resolve_function, resolve_call): Call it to enforce F2023 15.4.2.2, which requires that procedures with PDT duumy args have an explicit interface. (resolve_fl_variable): A type spec list value is reauired for a parameter that has no default value (C797). (gfc_resolve_finalizers): LEN parameters of PDT dummy args of final subroutines must be assumed (C791). (check_kind_exprs, gfc_check_kind_exprs): New functions. (resolve_component): Call them to ensure that constant exprs do not contain LEN parameters and that there is no forward ref to a KIND parameter. (resolve_fl_derived0): Check that PDT templates are compliant with C1802, C745, C759 and R733. gcc/testsuite/ PR fortran/82173 * gfortran.dg/pdt_constraints.f90: New test. --- gcc/fortran/decl.cc | 23 ++ gcc/fortran/expr.cc | 5 + gcc/fortran/resolve.cc | 200 +++++++++++++++++- gcc/testsuite/gfortran.dg/pdt_constraints.f90 | 157 ++++++++++++++ 4 files changed, 384 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gfortran.dg/pdt_constraints.f90 diff --git a/gcc/fortran/decl.cc b/gcc/fortran/decl.cc index 01b7434444f..1fcf0cc1d1f 100644 --- a/gcc/fortran/decl.cc +++ b/gcc/fortran/decl.cc @@ -4693,6 +4693,29 @@ gfc_get_pdt_instance (gfc_actual_arglist *param_list, gfc_symbol **sym, } } + /* This is a peculiar error to catch because it requires a complete type. The + PDT templates do not stand scrutiny because they are unresolvable. In + particular, Type extension and PDT components need to be included in the + derived type for gfc_find_component to do its work, which can only be done + with a PDT instance. */ + actual_param = param_list; + for (; actual_param; actual_param = actual_param->next) + { + gfc_ref *r; + c2 = NULL; + if (actual_param->name == NULL) + continue; + c2 = gfc_find_component (instance, actual_param->name, false, true, &r); + if (!c2) + { + /* The error is lost with gfc_error. */ + gfc_error_now ("C799: The keyword %qs at %C does not correspond to a " + "name of a type parameter of the type %qs", + actual_param->name, pdt->name); + return MATCH_ERROR; + } + } + if (alloc_seen) instance->attr.alloc_comp = 1; if (ptr_seen) diff --git a/gcc/fortran/expr.cc b/gcc/fortran/expr.cc index d31c807b958..2f237c27a81 100644 --- a/gcc/fortran/expr.cc +++ b/gcc/fortran/expr.cc @@ -4675,6 +4675,11 @@ gfc_check_pointer_assign (gfc_expr *lvalue, gfc_expr *rvalue, return false; } + /* Check that the type spec. parameters are the same on both sides. */ + if (lvalue->ts.type == BT_DERIVED && lvalue->ts.u.derived->attr.pdt_type + && !gfc_check_type_spec_parms (lvalue, rvalue, "in pointer assignment")) + return false; + if (lvalue->ts.type != BT_CLASS && lvalue->ts.kind != rvalue->ts.kind) { gfc_error ("Different kind type parameters in pointer " diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc index 6b05f6a6a85..0d4515155c3 100644 --- a/gcc/fortran/resolve.cc +++ b/gcc/fortran/resolve.cc @@ -2358,6 +2358,42 @@ cleanup: } +/* Enforce F2023 15.4.2.2 (4)(e): A procedure with a PDT dummy argument + requires an explicit interface. */ + +static bool +resolve_external_pdt_args (gfc_expr *expr, gfc_code *c) +{ + gfc_actual_arglist *arg = NULL; + gfc_symbol *sym = NULL; + + if (expr && expr->value.function.esym && expr->value.function.actual != NULL) + { + sym = expr->value.function.esym; + arg = expr->value.function.actual; + } + else if (c && c->ext.actual != NULL) + { + sym = c->resolved_sym ? c->resolved_sym : c->symtree->n.sym; + arg = c->ext.actual; + } + + if (!sym || sym->attr.if_source != IFSRC_UNKNOWN + || strcmp (sym->name, "_gfortran_move_alloc") == 0) + return true; + + for (; arg; arg = arg->next) + if (IS_PDT (arg->expr) || IS_CLASS_PDT (arg->expr)) + { + gfc_error ("F2023 15.4.2.2: Procedure %qs with a PDT dummy argument " + "at %L requires an explicit interface", sym->name, + &arg->expr->where); + return false; + } + + return true; +} + /* Do the checks of the actual argument list that are specific to elemental procedures. If called with c == NULL, we have a function, otherwise if expr == NULL, we have a subroutine. */ @@ -3588,6 +3624,10 @@ resolve_function (gfc_expr *expr) if (!resolve_elemental_actual (expr, NULL)) return false; + /* Enforce F2023 15.4.2.2 (4)(e) */ + if (!resolve_external_pdt_args (expr, NULL)) + return false; + if (omp_workshare_flag && expr->value.function.esym && ! gfc_elemental (expr->value.function.esym)) @@ -4339,6 +4379,10 @@ resolve_call (gfc_code *c) if (!resolve_elemental_actual (NULL, c)) return false; + /* Enforce F2023 15.4.2.2 (4)(e) */ + if (!resolve_external_pdt_args (NULL, c)) + return false; + /* Deal with complicated dependencies that the scalarizer cannot handle. */ if (c->resolved_sym && c->resolved_sym->attr.elemental && !no_formal_args && c->ext.actual && c->ext.actual->next) @@ -16310,6 +16354,26 @@ resolve_fl_variable (gfc_symbol *sym, int mp_flag) return false; } + if (IS_PDT (sym) && !sym->attr.dummy + && !sym->attr.allocatable && !sym->attr.pointer + && !sym->assoc && !sym->attr.select_type_temporary) + { + gfc_component *c = sym->ts.u.derived->components; + gfc_actual_arglist *p = sym->param_list; + for (; c ; c = c->next) + if (!c->attr.pdt_len) + continue; + else if (!p) + { + gfc_error ("C797: A type-param-spec for %qs at %L is required " + "for the parameter %qs that has no default value", + sym->name, &sym->declared_at, c->name); + return false; + } + else + p = p->next; + } + no_init_error: if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS) { @@ -16930,6 +16994,26 @@ gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable) goto error; } + if (IS_PDT (arg)) + { + gfc_component *c = arg->ts.u.derived->components; + gfc_actual_arglist *p = arg->param_list; + for (; c; c = c->next) + { + if (c->attr.pdt_len && !(p && p->spec_type == SPEC_ASSUMED)) + { + gfc_error ("C791: The dummy argument coresponding to the " + "length type parameter %qs of the final subroutine %qs at " + "%L must be assumed.", arg->name, list->proc_sym->name, + &list->proc_sym->declared_at); + list->proc_sym->refs++; + goto error; + } + if (p) + p = p->next; + } + } + /* Warn if the procedure is non-scalar and not assumed shape. */ if (warn_surprising && arg->as && arg->as->rank != 0 && arg->as->type != AS_ASSUMED_SHAPE) @@ -17861,6 +17945,51 @@ check_defined_assignments (gfc_symbol *derived) } +/* gfc_check_kind_exprs has two seperate functions. If 'comp0' is present it + checks a 'kind_expr', which is required to be a constant expression for any + LEN parameters. Otherwise, if 'comp0' is non-null, it is used to check for + forward referencing of type spec parameters. 'comp0' is the first component + of the PDT and 'comp1' is the current component in resolve_component. Note + that it also prevents self referencing. */ +static gfc_component *comp0 = NULL, *comp1; + +static bool +check_kind_exprs (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED, + int* f ATTRIBUTE_UNUSED) +{ + if (!comp0 && e->expr_type == EXPR_VARIABLE + && e->symtree->n.sym->attr.pdt_len) + return true; + + else if (comp0 && e->expr_type == EXPR_VARIABLE + && e->symtree->n.sym->attr.pdt_kind) + { + gfc_component *c = comp0; + bool forward_ref = true; + for (; c; c = c->next) + { + if (c->attr.pdt_kind + && !strcmp (e->symtree->n.sym->name, c->name) + && strcmp (comp1->name, c->name)) + forward_ref = false;; + if (c == comp1) + break; + } + if (forward_ref) + return true; + } + + return false; +} + + +static bool +gfc_check_kind_exprs (gfc_expr *e) +{ + return gfc_traverse_expr (e, NULL, &check_kind_exprs, 0); +} + + /* Resolve a single component of a derived type or structure. */ static bool @@ -17944,6 +18073,39 @@ resolve_component (gfc_component *c, gfc_symbol *sym) return false; } + if (sym->attr.pdt_template && c->kind_expr + && gfc_check_kind_exprs (c->kind_expr)) + { + gfc_error ("F2023 10.1.12: Only a KIND parameter may be " + "as a primary a CONSTANT expression. The kind " + "selector at %L contains a LEN parameter.", + &c->kind_expr->where); + return false; + } + + if (sym->attr.pdt_template && c->initializer + && gfc_check_kind_exprs (c->initializer)) + { + gfc_error ("F2023 10.1.12: Only a KIND parameter may be " + "as a primary a CONSTANT expression. The " + "initializer at %L contains a LEN parameter.", + &c->initializer->where); + return false; + } + + comp0 = sym->components; + comp1 = c; + if (sym->attr.pdt_template && c->initializer + && (c->attr.pdt_kind || c->attr.pdt_len) + && gfc_check_kind_exprs (c->initializer)) + { + gfc_error ("F2023 10.1.12 (12): The kind type parameter used in an " + "initializer expression at %L shall be PREVIOUSLY declared", + &c->initializer->where); + return false; + } + comp0 = comp1 = NULL; + if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.pdt_template && !sym->attr.pdt_type && !sym->attr.pdt_template && !(gfc_get_derived_super_type (sym) @@ -18498,9 +18660,18 @@ resolve_fl_derived0 (gfc_symbol *sym) && !ensure_not_abstract (sym, super_type)) return false; - /* Check that there is a component for every PDT parameter. */ + /* Check that there is a component for every PDT parameter and that the PDT + template is compliant with C1802, C745, C759 and R733. */ if (sym->attr.pdt_template) { + if (sym->attr.is_bind_c) + gfc_error ("C1802: The derived type at %L with the BIND attribute " + "shall not have type parameters", &sym->declared_at); + + if (sym->attr.sequence) + gfc_error ("C745: The SEQUENCE type at %L shall not have any type " + "parameter", &sym->declared_at); + for (f = sym->formal; f; f = f->next) { if (!f->sym) @@ -18514,6 +18685,33 @@ resolve_fl_derived0 (gfc_symbol *sym) break; } } + + for (c = sym->components; c; c = c->next) + if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.pdt_template + && c->param_list) + { + gfc_actual_arglist *parms = c->param_list; + for (; parms; parms = parms->next) + if (parms->spec_type == SPEC_ASSUMED) + { + gfc_error ("C759: The type-param-value in the component-" + "def-stmt for %qs at %L shall be a colon or a " + "component specification expression.", c->name, + &c->loc); + break; + } + } + else if ((c->attr.pdt_kind || c->attr.pdt_len) && c->initializer + && ((c->initializer->expr_type == EXPR_VARIABLE + && !(gfc_expr_attr (c->initializer).pdt_kind + || gfc_expr_attr (c->initializer).pdt_len)) + || (!gfc_simplify_expr (c->initializer, 0) + && !gfc_is_constant_expr (c->initializer)))) + { + gfc_error ("R733: The type parameter default value at %L shall be " + "a scalar-int-constant-expr", &c->loc); + break; + } } /* Add derived type to the derived type list. */ diff --git a/gcc/testsuite/gfortran.dg/pdt_constraints.f90 b/gcc/testsuite/gfortran.dg/pdt_constraints.f90 new file mode 100644 index 00000000000..07e5b87730c --- /dev/null +++ b/gcc/testsuite/gfortran.dg/pdt_constraints.f90 @@ -0,0 +1,157 @@ +! { dg-do compile } +! +! This test gathers a list of constraints that are the last to be satisfied from +! the 'neg' directory in comment #10 of PR82173. +! +! Contributed by Jerry Delisle <[email protected]> +! +! R733: a type parameter default value is a scalar-int-constant-expr. +module m12 + integer :: var = 3 + type :: t(n) + integer, len :: n = var ! { dg-error "shall be a scalar-int-constant-expr" } + integer :: v(n) + end type +end module + +! C745: a SEQUENCE type shall not have any type parameter. +module m13 + type :: t(n) ! { dg-error "shall not have any type parameter" } + sequence + integer, len :: n = 1 + integer :: v(3) + end type +end module + +! C1802: a derived type with the BIND attribute shall not have type parameters. +module m14 + use iso_c_binding + type, bind(c) :: t(n) ! { dg-error "with the BIND attribute shall not have type parameters" } + integer, len :: n = 1 + integer(c_int) :: v + end type +end module + +! C759: a type-param-value in a component-def-stmt shall be a colon or a component +! specification expression. +subroutine n15 + type :: t(k, n) + integer, kind :: k = 4 + integer, len :: n = 1 + real(k) :: v(n) + end type + type :: holder(n) + integer, len :: n = 1 + type(t(4, *)) :: c ! { dg-error "a colon or a component specification expression." } + end type + + type (holder(2)) :: x + print *, size (x%c%v) +end + +! C797: a type-param-spec is required for a parameter that has no default value. +module m18 + type :: t(n) + integer, len :: n + integer :: v(n) + end type +end module +subroutine n18 + use m18 + type(t) :: x ! { dg-error "has no default value" } + print *, x%n +end subroutine + +! C799: each keyword shall be the name of a type parameter of the type. +module m20 + type :: t(k, n) + integer, kind :: k = 4 + integer, len :: n = 1 + real(k) :: v(n) + end type +end module +subroutine n20 + use m20 + type(t(4, nope=3)) :: x ! { dg-error "does not correspond to a name" } + x%v = 0.0 +end subroutine + +! C791: all length type parameters of a final subroutine's dummy argument shall be assumed. +module m31 + type :: t(n) + integer, len :: n = 1 + integer :: v(n) + contains + final :: fin + end type +contains + subroutine fin (a) ! { dg-error "must be assumed" } + type(t(3)), intent(inout) :: a + end subroutine +end module + +! F2023 10.2.2.3 p4: nondeferred type parameters of the pointer shall equal those of the target. +module m36 + type :: t(k, n) + integer, kind :: k = 4 + integer, len :: n = 1 + real(k) :: v(n) + end type +end module +subroutine n36 + use m36 + type(t(4, 3)), pointer :: ptr + type(t(4, 5)), target :: tgt + tgt%v = 0.0 + ptr => tgt ! { dg-error "Mismatched type parameters" } +end subroutine + +! F2023 15.4.2.2 (4)(e): a procedure with a PDT dummy argument requires an explicit interface. +module m37 + type :: t(k, n) + integer, kind :: k = 4 + integer, len :: n = 1 + real(k) :: v(n) + end type +end module +subroutine n37 + use m37 + type(t(4, 3)) :: a + integer :: i_sum, external_fcn + a%v = 0.0 + call external_sub (a) ! { dg-error "requires an explicit interface" } + i_sum = external_fcn (a) ! { dg-error "requires an explicit interface" } +end subroutine + +subroutine external_sub (a) + use m37 + type(t(4, *)), intent(in) :: a + print *, a%n +end subroutine + +function external_fcn (arg) result (res) + use m37 + integer :: res + type(t(4, 3)) :: arg + res = sum (arg%v) +end function + +! F2023 7.5.3.1 p5 / 10.1.12 (12): a type parameter may be used as a primary +! in a specification expression in the derived-type-def, but only a KIND type +! parameter may be used in a CONSTANT expression - and a kind selector +! requires a constant expression. +module m45 + type :: t(n) + integer, len :: n = 4 + real(n) :: v ! { dg-error "contains a LEN parameter" } + end type +end module + +! F2023 10.1.12 (12): a kind type parameter used in a constant expression shall be PREVIOUSLY declared. +module m46 + type :: t(a, b) + integer, kind :: a = b ! { dg-error "shall be PREVIOUSLY declared" } + integer, kind :: b = 4 + real(a) :: v + end type +end module -- 2.56.1
