https://gcc.gnu.org/g:4edb219aef7eec695689cf8d63ab929a2bf7d7ed
commit r17-6302-g4edb219aef7eec695689cf8d63ab929a2bf7d7ed Author: Jerry DeLisle <[email protected]> Date: Sun Nov 15 11:40:08 2026 -0800 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. Diff: --- 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(-) diff --git a/gcc/fortran/array.cc b/gcc/fortran/array.cc index 733ce5d27d05..ff7cebf003d2 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 193916c4a651..d3fafd4ecab8 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 000000000000..c7c10e8310ec --- /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 000000000000..b56dd316916d --- /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
