The attached patch fixes bug14-bogus-deferred-param-error.f90,
bug14-bogus-deferred-param-error.f90:14:13:
14 | allocate (a(1)%extra(2)) ! Error: Allocate-object at (1)
with a deferred type parameter
| 1~~~~~~~~~~~~
Error: Allocate-object at (1) with a deferred type parameter requires
either a type-spec or SOURCE tag or a MOLD tag.
This is an incorrect application of F2023 C937. The patch detects
components, which are 'deferrable' and handles them accordingly. I
believe that the patch is sufficiently easy to follow that further
explanation is unnecessary.
Passes regtesting on FC44/x86_64. OK for mainline.
Cheers
Paul
From 177593cab4a23bf1a5c4c5476b3535c4a85cbbcc Mon Sep 17 00:00:00 2001
From: Paul Thomas <[email protected]>
Date: Wed, 7 Oct 2026 11:16:02 +0100
Subject: [PATCH] Fortran: [meta-bug] [PDT] Parameterized derived type errors
[PR82173]
2026-10-07 Paul Thomas <[email protected]>
gcc/fortran
PR fortran/82173
* match.cc (match_derived_type_spec): Null type_param_spec_list
(gfc_match_allocate): Detect deferrable components; ie ordinary
allocatable arrays with deferred SHAPE and no type parameters
saw that 'saw_deferrable' and 'deferred_locus' can be set
appropriately.
gcc/testsuite/
PR fortran/82173
* gfortran.dg/pdt_bug14.f90: New test.
---
gcc/fortran/match.cc | 16 ++++++++++++----
gcc/testsuite/gfortran.dg/pdt_bug14.f90 | 17 +++++++++++++++++
2 files changed, 29 insertions(+), 4 deletions(-)
create mode 100644 gcc/testsuite/gfortran.dg/pdt_bug14.f90
diff --git a/gcc/fortran/match.cc b/gcc/fortran/match.cc
index d61757046d2..2dfeea6eaab 100644
--- a/gcc/fortran/match.cc
+++ b/gcc/fortran/match.cc
@@ -2432,6 +2432,7 @@ match_derived_type_spec (gfc_typespec *ts)
bool is_pdt_template = false;
old_locus = gfc_current_locus;
+ type_param_spec_list = NULL;
if (gfc_match ("%n", name) != MATCH_YES)
{
@@ -5341,17 +5342,24 @@ gfc_match_allocate (void)
goto cleanup;
}
- if (tail->expr->ts.deferred
- || (tail->expr->symtree->n.sym->param_list
- && spec_list_type (tail->expr->symtree->n.sym->param_list)
+ bool deferrable = !(tail->expr->ts.type != BT_UNKNOWN
+ && tail->expr->ts.type != BT_CHARACTER
+ && !IS_PDT (tail->expr) && !IS_CLASS_PDT (tail->expr)
+ && (gfc_expr_attr (tail->expr).allocatable
+ || gfc_expr_attr (tail->expr).pointer));
+
+ if ((tail->expr->ts.deferred
+ || (tail->expr->symtree->n.sym->param_list
+ && spec_list_type (tail->expr->symtree->n.sym->param_list)
== SPEC_DEFERRED))
+ && deferrable)
{
saw_deferred = true;
deferred_locus = tail->expr->where;
}
else if ((tail->expr->ts.type == BT_DERIVED
|| tail->expr->ts.type == BT_CLASS)
- && tail->expr->ref)
+ && tail->expr->ref && deferrable)
{
for (ref = tail->expr->ref; ref; ref = ref->next)
if (ref->type == REF_COMPONENT
diff --git a/gcc/testsuite/gfortran.dg/pdt_bug14.f90 b/gcc/testsuite/gfortran.dg/pdt_bug14.f90
new file mode 100644
index 00000000000..4aa0f3166a1
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pdt_bug14.f90
@@ -0,0 +1,17 @@
+! { dg-do run }
+! Rejects valid code: `a` has a deferred type parameter, but `a(i)%extra`
+! is an ordinary allocatable INTEGER array with deferred SHAPE and no type
+! parameters at all, so C937 does not apply to it.
+! See attachment to PR82173 comment 10 - ~/reducers/bug14-bogus-deferred-param-error.f90
+program p
+ type :: t(n)
+ integer, len :: n = 1
+ integer :: v(n)
+ integer, allocatable :: extra(:)
+ end type
+ type(t(:)), allocatable :: a(:)
+ allocate (t(4) :: a(3))
+ allocate (a(1)%extra(2)) ! Error: Allocate-object at (1) with a deferred type parameter
+ a(1)%extra = 5 ! requires either a type-spec or SOURCE tag or a MOLD tag
+ if (sum (a(1)%extra) /= 10) stop 1
+end program
--
2.55.0