Thanks Mikael,

(note there is a regression related to IPA on trunk at the moment)

The master branch has been updated by Jerry DeLisle <[email protected]>:

https://gcc.gnu.org/g:4edb219aef7eec695689cf8d63ab929a2bf7d7ed

commit r17-6302-g4edb219aef7eec695689cf8d63ab929a2bf7d7ed

On 11/15/26 1:42 PM, Mikael Morin wrote:
Le 15/11/2026 à 21:04, Jerry D a écrit :
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?

OK, thanks.


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


Reply via email to