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.

The fix is in check_element_type in array.cc; the rest is tests.

Regression tested on x86_64.

OK for mainline?

Regards,

Jerry

---

fortran: [PR53653] 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/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/pr53653.f90: New test.
        * gfortran.dg/pr53653_2.f90: New test.
>From 78624c70c4ebd5716cc2ecaf0d6d61ad3edcea9a Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Sat, 14 Nov 2026 19:05:17 -0800
Subject: [PATCH] fortran: [PR53653] 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/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/pr53653.f90: New test.
	* gfortran.dg/pr53653_2.f90: New test.
---
 gcc/fortran/array.cc                    | 15 ++++++++++++---
 gcc/testsuite/gfortran.dg/der_io_5.f90  |  4 ++--
 gcc/testsuite/gfortran.dg/pr53653.f90   | 12 ++++++++++++
 gcc/testsuite/gfortran.dg/pr53653_2.f90 | 17 +++++++++++++++++
 4 files changed, 43 insertions(+), 5 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/pr53653.f90
 create mode 100644 gcc/testsuite/gfortran.dg/pr53653_2.f90

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;
+
   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/pr53653.f90 b/gcc/testsuite/gfortran.dg/pr53653.f90
new file mode 100644
index 00000000000..78aa5e5a3b9
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr53653.f90
@@ -0,0 +1,12 @@
+! { dg-do compile }
+!
+! PR 53653
+
+subroutine s
+  type t
+    integer :: i
+  end type
+  class(t), allocatable :: x, z(:), b(:)
+  allocate (z(1), source=[x])
+  allocate (b, source=[x, x])
+end subroutine
diff --git a/gcc/testsuite/gfortran.dg/pr53653_2.f90 b/gcc/testsuite/gfortran.dg/pr53653_2.f90
new file mode 100644
index 00000000000..0a425aab5b8
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr53653_2.f90
@@ -0,0 +1,17 @@
+! { dg-do compile }
+!
+! 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
-- 
2.56.0

Reply via email to