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.

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?

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. 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.


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


Reply via email to