On 11/15/26 5:51 AM, Mikael Morin wrote:
Le 15/11/2026 à 04:10, Jerry DeLisle a écrit :
See attached patch. Without a type-spec, an array constructor took the
type of its first value, so [x] with x CLASS(t) was itself CLASS(t).
ALLOCATE with SOURCE=[x] then ICEd in gfc_get_element_type and, past
that, in gfc_trans_allocate. F2023 7.8 gives an array constructor the
declared type of its values as both its declared and dynamic type, so
the patch makes [x] TYPE(t), the way [t :: x] is already handled. The
same check now enforces C7120, that all values have the same declared
type: [x, y] with y of a type extending t was accepted and ICEd, while
[y, x] was rejected.
You can take PR127541, and reference it in the patch, as it is exactly about
this topic. And the patch that I had in my regression hunt branch was similar
to yours.
Done. v2 is filed under PR127541 and still lists PR53653; the tests
are renamed pr127541.f90 and pr127541_2.f90
Reading the initial comment of PR53653, there is mention of rejection of
abstract and unlimited polymorphic types in constructors, but it seems that it
has been done since then. So the PR mutated to a different topic?
Yes. Abstract and unlimited polymorphic values are rejected now (C7125
and C7124). The ICEs with ALLOCATE and SOURCE=[x], x a CLASS scalar,
are what I had tracked under PR53653.
diff --git a/gcc/fortran/array.cc b/gcc/fortran/array.cc
index 733ce5d27d0..96885330479 100644
--- a/gcc/fortran/array.cc
+++ b/gcc/fortran/array.cc
@@ -1516,23 +1516,32 @@ cons_state;
static int
check_element_type (gfc_expr *expr, bool convert)
{
+ gfc_typespec ts;
+
if (cons_state == CONS_BAD)
return 0; /* Suppress further errors */
+ /* Without a type-spec, the values have the same declared type, which is
+ the type of the array constructor (F2023:C7120, 7.8 p2, p4). */
+ ts = expr->ts;
+ if (!convert && ts.type == BT_CLASS && ts.u.derived
+ && ts.u.derived->components && !UNLIMITED_POLY (expr))
+ ts = ts.u.derived->components->ts;
+
In some cases we can fail to build the class container, so it's better if we
support that case. That is check that either the is_class or class_ok attribute
is set.
Done: the declared type is taken only when ts.u.derived->attr.is_class
is set. Your first example now gives only the errors trunk gives.
pr127541_2.f90 has a case with SIZE ([v, t(2)]) for a CLASS v that is
not a dummy, allocatable or pointer; without the check it gave the
spurious INTEGER(4) error.
Here is an example; I couldn't find any valid one:
program prog
implicit none
type :: t
integer :: c
end type
class(t) :: x
x = t(1)
print *, [x, t(2)]
end program
It is rejected because x is not dummy or ALLOCATABLE or POINTER, but there is
also this spurious error:
10 | print *, [x, t(2)]
| 1
Error: Element in INTEGER(4) array constructor at (1) is TYPE(t)
if (cons_state == CONS_START)
{
- if (expr->ts.type == BT_UNKNOWN)
+ if (ts.type == BT_UNKNOWN)
cons_state = CONS_BAD;
else
{
cons_state = CONS_GOOD;
- constructor_ts = expr->ts;
+ constructor_ts = ts;
}
return 0;
}
- if (gfc_compare_types (&constructor_ts, &expr->ts))
+ if (gfc_compare_types (&constructor_ts, &ts))
return 0;
if (convert)
Trying to find a case where the class container is not successfully built, I
encountered the testcase below. With the patch, the compiler aborts; without it,
it rejects the testcase and exits cleanly.
It's probably a latent independent problem uncovered by your patch, so it can be
fixed separately.
Yes. Abstract and unlimited polymorphic values are rejected now (C7125
and C7124), which also covers the testcase in comment 0. The
reductions without ABSTRACT in comments 2 and 3, ALLOCATE with
SOURCE=[x] for a CLASS scalar x, still ICEd; v2 has them in
pr127541.f90, together with the PRINT from PR127541.
v2 attached, regression tested on x86_64.
OK for v2?
Jerry
module m
implicit none
type :: t
integer :: c
end type
end module
program prog
use m
implicit class(t) (x)
allocatable :: x
x = t(1)
print *, [x]
end program
From c1aafa843f6a553fd760cd1a20944fbd32335f7a Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Sun, 15 Nov 2026 11:40:08 -0800
Subject: [PATCH] fortran: [PR127541] An array constructor is not polymorphic
Without a type-spec, an array constructor took the type of its first
value, so a constructor of CLASS(t) values was itself CLASS(t).
ALLOCATE with such a constructor as SOURCE= hit a series of ICEs, and a
later value whose declared type extends t was accepted and ICEd. An
array constructor has the declared type of its values as its declared
and dynamic type, and the values shall have the same declared type
(F2023 7.8, C7120). Give an untyped array constructor the declared type
of a CLASS value, and require that type of every value. der_io_5.f90
expected errors for output of such constructors, which are valid.
Assisted-by: Claude Opus 5.5
PR fortran/127541
PR fortran/53653
gcc/fortran/ChangeLog:
* array.cc (check_element_type): Without a type-spec, take the
declared type of a CLASS value, and require the same declared type
of all values.
gcc/testsuite/ChangeLog:
* gfortran.dg/der_io_5.f90: Expect no errors for output of an array
constructor of polymorphic values.
* gfortran.dg/pr127541.f90: New test.
* gfortran.dg/pr127541_2.f90: New test.
---
gcc/fortran/array.cc | 16 +++++++++++---
gcc/testsuite/gfortran.dg/der_io_5.f90 | 4 ++--
gcc/testsuite/gfortran.dg/pr127541.f90 | 14 ++++++++++++
gcc/testsuite/gfortran.dg/pr127541_2.f90 | 27 ++++++++++++++++++++++++
4 files changed, 56 insertions(+), 5 deletions(-)
create mode 100644 gcc/testsuite/gfortran.dg/pr127541.f90
create mode 100644 gcc/testsuite/gfortran.dg/pr127541_2.f90
diff --git a/gcc/fortran/array.cc b/gcc/fortran/array.cc
index 733ce5d27d0..ff7cebf003d 100644
--- a/gcc/fortran/array.cc
+++ b/gcc/fortran/array.cc
@@ -1516,23 +1516,33 @@ cons_state;
static int
check_element_type (gfc_expr *expr, bool convert)
{
+ gfc_typespec ts;
+
if (cons_state == CONS_BAD)
return 0; /* Suppress further errors */
+ /* Without a type-spec, the values have the same declared type, which is
+ the type of the array constructor (F2023:C7120, 7.8 p2, p4). */
+ ts = expr->ts;
+ if (!convert && ts.type == BT_CLASS && ts.u.derived
+ && ts.u.derived->attr.is_class && ts.u.derived->components
+ && !UNLIMITED_POLY (expr))
+ ts = ts.u.derived->components->ts;
+
if (cons_state == CONS_START)
{
- if (expr->ts.type == BT_UNKNOWN)
+ if (ts.type == BT_UNKNOWN)
cons_state = CONS_BAD;
else
{
cons_state = CONS_GOOD;
- constructor_ts = expr->ts;
+ constructor_ts = ts;
}
return 0;
}
- if (gfc_compare_types (&constructor_ts, &expr->ts))
+ if (gfc_compare_types (&constructor_ts, &ts))
return 0;
if (convert)
diff --git a/gcc/testsuite/gfortran.dg/der_io_5.f90 b/gcc/testsuite/gfortran.dg/der_io_5.f90
index 193916c4a65..d3fafd4ecab 100644
--- a/gcc/testsuite/gfortran.dg/der_io_5.f90
+++ b/gcc/testsuite/gfortran.dg/der_io_5.f90
@@ -10,8 +10,8 @@ program p
type(t) :: x, y(1)
integer :: i
allocate (a,b(1))
- print *, [a] ! { dg-error "Data transfer element at .1. cannot be polymorphic" }
- print *, [(b(i),i=1,1)] ! { dg-error "Data transfer element at .1. cannot be polymorphic" }
+ print *, [a]
+ print *, [(b(i),i=1,1)]
print *, [x]
print *, [(y(i),i=1,1)]
end
diff --git a/gcc/testsuite/gfortran.dg/pr127541.f90 b/gcc/testsuite/gfortran.dg/pr127541.f90
new file mode 100644
index 00000000000..c7c10e8310e
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr127541.f90
@@ -0,0 +1,14 @@
+! { dg-do compile }
+!
+! PR 127541
+! PR 53653
+
+subroutine s
+ type t
+ integer :: i
+ end type
+ class(t), allocatable :: x, z(:), b(:), v(:), w(:)
+ allocate (z(1), source=[x])
+ allocate (b, source=[x, x])
+ print *, [v, w]
+end subroutine
diff --git a/gcc/testsuite/gfortran.dg/pr127541_2.f90 b/gcc/testsuite/gfortran.dg/pr127541_2.f90
new file mode 100644
index 00000000000..b56dd316916
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr127541_2.f90
@@ -0,0 +1,27 @@
+! { dg-do compile }
+!
+! PR 127541
+! PR 53653
+
+subroutine s
+ type t
+ integer :: i
+ end type
+ type, extends(t) :: t2
+ end type
+ class(t), allocatable :: x, z(:)
+ class(t2), allocatable :: u
+ type(t2) :: y
+ z = [x, y] ! { dg-error "Element in TYPE\\(t\\) array constructor" }
+ z = [y, x] ! { dg-error "Element in TYPE\\(t2\\) array constructor" }
+ z = [x, u] ! { dg-error "Element in TYPE\\(t\\) array constructor" }
+end subroutine
+
+subroutine s2
+ type t
+ integer :: c
+ end type
+ class(t) :: v ! { dg-error "must be dummy, allocatable or pointer" }
+ integer :: n
+ n = size ([v, t(2)])
+end subroutine
--
2.56.0