See attached patch. An IMPLICIT CLASS variable that appears only as an
assignment target, a bare actual argument or an ALLOCATE object gets
its type in resolve_symbol, in the middle of the gfc_traverse_ns walk.
The class container built then is not in the walk's snapshot, so it is
never resolved, its _vptr keeps a NULL type, and gfc_get_derived_type
crashes on it. Resolving the container right after the implicit typing
fixes it; resolve_symbol returns at once for a symbol it has already
resolved.
While testing I found that an implicitly typed allocate-object with a
type-spec is rejected as type incompatible, even "allocatable :: a(:)"
with "allocate (real :: a(3))", because gfc_match_allocate checks it
before it has a type. The patch gives it its implicit type there;
without one (IMPLICIT NONE) the checks are skipped and resolution
reports the missing type once.
The fixes are in resolve_symbol in resolve.cc and gfc_match_allocate in
match.cc; the rest is the two new tests.
Regression tested on x86_64.
I plan to commit this as obvious and simple.
Regards,
Jerry
---
fortran: [PR128764] ICE and bogus error for implicitly typed variables
An IMPLICIT CLASS variable typed only during resolution caused an ICE in
gfc_get_derived_type: its class container is added to the namespace
after gfc_traverse_ns took its snapshot, so the container was never
resolved and its _vptr had no type. resolve_symbol now resolves the
container after implicit typing. An implicitly typed allocate-object
with a type-spec was also rejected as type incompatible, because
gfc_match_allocate checked it before it had a type; it now gets its
implicit type there first.
Assisted-by: Claude Opus 5.5
PR fortran/128764
gcc/fortran/ChangeLog:
* match.cc (gfc_match_allocate): Give an untyped allocate-object
its implicit type before checking it against the type-spec.
* resolve.cc (resolve_symbol): Resolve the class container of a
variable given an implicit CLASS type.
gcc/testsuite/ChangeLog:
* gfortran.dg/allocate_with_typespec_8.f90: New test.
* gfortran.dg/pr128764.f90: New test.
>From 62bc16edf42f9bbffba7f18649e4880182fe37fb Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Sun, 22 Nov 2026 19:13:19 -0800
Subject: [PATCH] fortran: [PR128764] ICE and bogus error for implicitly typed
variables
An IMPLICIT CLASS variable typed only during resolution caused an ICE in
gfc_get_derived_type: its class container is added to the namespace
after gfc_traverse_ns took its snapshot, so the container was never
resolved and its _vptr had no type. resolve_symbol now resolves the
container after implicit typing. An implicitly typed allocate-object
with a type-spec was also rejected as type incompatible, because
gfc_match_allocate checked it before it had a type; it now gets its
implicit type there first.
Assisted-by: Claude Opus 5.5
PR fortran/128764
gcc/fortran/ChangeLog:
* match.cc (gfc_match_allocate): Give an untyped allocate-object
its implicit type before checking it against the type-spec.
* resolve.cc (resolve_symbol): Resolve the class container of a
variable given an implicit CLASS type.
gcc/testsuite/ChangeLog:
* gfortran.dg/allocate_with_typespec_8.f90: New test.
* gfortran.dg/pr128764.f90: New test.
---
gcc/fortran/match.cc | 10 ++++-
gcc/fortran/resolve.cc | 4 ++
.../gfortran.dg/allocate_with_typespec_8.f90 | 45 +++++++++++++++++++
gcc/testsuite/gfortran.dg/pr128764.f90 | 32 +++++++++++++
4 files changed, 90 insertions(+), 1 deletion(-)
create mode 100644 gcc/testsuite/gfortran.dg/allocate_with_typespec_8.f90
create mode 100644 gcc/testsuite/gfortran.dg/pr128764.f90
diff --git a/gcc/fortran/match.cc b/gcc/fortran/match.cc
index efa0b67d86c..40afa402fa7 100644
--- a/gcc/fortran/match.cc
+++ b/gcc/fortran/match.cc
@@ -5424,9 +5424,17 @@ gfc_match_allocate (void)
goto cleanup;
}
+ /* An implicitly typed allocate-object is otherwise typed only during
+ resolution, but the type-spec checks need its type. Without an
+ implicit type, resolution reports the error. */
+ if (ts.type != BT_UNKNOWN
+ && tail->expr->ts.type == BT_UNKNOWN && sym->ts.type == BT_UNKNOWN
+ && gfc_set_default_type (sym, 0, sym->ns))
+ tail->expr->ts = sym->ts;
+
/* The ALLOCATE statement had an optional typespec. Check the
constraints. */
- if (ts.type != BT_UNKNOWN)
+ if (ts.type != BT_UNKNOWN && tail->expr->ts.type != BT_UNKNOWN)
{
/* Enforce F03:C624. */
if (!gfc_type_compatible (&tail->expr->ts, &ts))
diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc
index 8924284f2ab..55b8e6a42c7 100644
--- a/gcc/fortran/resolve.cc
+++ b/gcc/fortran/resolve.cc
@@ -19081,6 +19081,10 @@ skip_interfaces:
if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
{
gfc_set_default_type (sym, 1, NULL);
+ /* A class container built here is added to the namespace after
+ gfc_traverse_ns took its snapshot, so resolve it now. */
+ if (sym->ts.type == BT_CLASS)
+ resolve_symbol (sym->ts.u.derived);
}
if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
diff --git a/gcc/testsuite/gfortran.dg/allocate_with_typespec_8.f90 b/gcc/testsuite/gfortran.dg/allocate_with_typespec_8.f90
new file mode 100644
index 00000000000..b26d0544318
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/allocate_with_typespec_8.f90
@@ -0,0 +1,45 @@
+! { dg-do compile }
+!
+! PR 128764
+
+module m
+ implicit none
+ type :: t
+ integer :: c
+ end type
+ type, extends(t) :: t2
+ integer :: d
+ end type
+end module
+
+subroutine s1
+ use m
+ implicit class(t) (x), type(t) (y), integer(8) (i)
+ allocatable :: r, a(:), x, xa(:), y, i(:)
+ allocate (real :: r, a(3))
+ allocate (t2 :: x, xa(2))
+ allocate (t :: y)
+ allocate (integer(8) :: i(2))
+end subroutine
+
+subroutine s2
+ allocatable :: a(:)
+ call s3
+contains
+ subroutine s3
+ allocate (real :: a(4))
+ end subroutine
+end subroutine
+
+subroutine s4
+ implicit real(8) (d)
+ allocatable :: a(:), d(:)
+ allocate (integer :: a(3)) ! { dg-error "is type incompatible" }
+ allocate (real(4) :: d(2)) ! { dg-error "differs from the kind type parameter" }
+end subroutine
+
+subroutine s5
+ implicit none
+ allocatable :: z(:) ! { dg-error "has no IMPLICIT type" }
+ allocate (real :: z(2))
+end subroutine
diff --git a/gcc/testsuite/gfortran.dg/pr128764.f90 b/gcc/testsuite/gfortran.dg/pr128764.f90
new file mode 100644
index 00000000000..c334fc71bf4
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr128764.f90
@@ -0,0 +1,32 @@
+! { dg-do compile }
+!
+! PR 128764
+
+module m
+ implicit none
+ type :: t
+ integer :: c
+ end type
+end module
+
+module n
+ use m
+ implicit class(t) (x)
+ allocatable :: xm
+end module
+
+program p
+ use m
+ implicit class(t) (x)
+ allocatable :: x, xa(:)
+ pointer :: xp
+ type(t), target :: y
+ x = t(1)
+ xa = [t(1), t(2)]
+ xp => y
+ call s
+contains
+ subroutine s
+ x = t(2)
+ end subroutine
+end program
--
2.56.1