https://gcc.gnu.org/g:ae303d0fe00834fac29a947c9ad8c1a88cf1c548
commit ae303d0fe00834fac29a947c9ad8c1a88cf1c548 Author: Mikael Morin <[email protected]> Date: Thu Nov 5 17:09:54 2026 +0100 fortran: Fix descriptor init in ALLOCATE from array subref SOURCE [PR128415] This fixes a regression (spotted by AI) introduced by r17-5758, aka: commit e9a6d9dee3098bc32d7b5da750a65755fea7a734 Author: Mikael Morin <[email protected]> Date: Fri Oct 30 14:33:34 2026 +0100 fortran: Fix copy from polymorphic pointer source in ALLOCATE [PR128033] which dropped a forced temporary for array subreferences and as a result enabled new ways of failure. TODO -- >8 -- Correct the type of the descriptor used as SOURCE in an ALLOCATE statement, and the initialization of its dtype, when the array-spec of the allocation is implicit, guessed from the SOURCE, and the SOURCE expression is an array subreference. The type and dtype were taken from the base array, ignoring subreferences, resulting in a wrong element length set for the allocated descriptor and an observable effect with the IS_CONTIGUOUS intrinsic. The problem is in gfc_conv_expr_descriptor, where the subref_array_target variable, controling the choice of the array descriptor element type, is defined as the is_subref_array predicate plus additional conditions. The additional conditions are false and mask the value of the predicate in the example added to the testsuite. The fix stores the value of the is_subref_array predicate in a dedicated variable without the additional conditions, and uses that variable in the condition deciding the array descriptor element type. PR fortran/128033 PR fortran/128415 gcc/fortran/ChangeLog: * trans-array.cc (gfc_conv_expr_descriptor): Use a dedicated variable storing the value of is_subref_array alone. Use it for the choice of element type. gcc/testsuite/ChangeLog: * gfortran.dg/allocate_with_source_35.f90: New test. Diff: --- gcc/fortran/trans-array.cc | 6 ++-- .../gfortran.dg/allocate_with_source_35.f90 | 33 ++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc index d1045ccfb19a..72df2f48c1ac 100644 --- a/gcc/fortran/trans-array.cc +++ b/gcc/fortran/trans-array.cc @@ -8192,6 +8192,7 @@ gfc_conv_expr_descriptor (gfc_se *se, gfc_expr *expr) stmtblock_t block; tree start; int full; + bool subref_array = false; bool subref_array_target = false; bool deferred_array_component = false; bool substr = false; @@ -8248,7 +8249,8 @@ gfc_conv_expr_descriptor (gfc_se *se, gfc_expr *expr) substr = info->ref && info->ref->next && info->ref->next->type == REF_SUBSTRING; - subref_array_target = (is_subref_array (expr) + subref_array = is_subref_array (expr); + subref_array_target = (subref_array && (se->direct_byref || se->force_no_tmp || expr->ts.type == BT_CHARACTER)); @@ -8611,7 +8613,7 @@ gfc_conv_expr_descriptor (gfc_se *se, gfc_expr *expr) /* Otherwise make a new one. The element type is that of the subobject for a subreference of the array. */ if (expr->ts.type == BT_CHARACTER - || (subref_array_target && !se->direct_byref)) + || (subref_array && !se->direct_byref)) { gfc_typespec *ts = &expr->ts; if (ts->type == BT_CLASS diff --git a/gcc/testsuite/gfortran.dg/allocate_with_source_35.f90 b/gcc/testsuite/gfortran.dg/allocate_with_source_35.f90 new file mode 100644 index 000000000000..fb2a1756e2b2 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/allocate_with_source_35.f90 @@ -0,0 +1,33 @@ +! { dg-do run } +! +! PR fortran/128415 +! Check correct initialization of the array descriptor element length in the +! context of an ALLOCATE statement whose array spec is implicit, guessed from +! the SOURCE. + +program prog + implicit none + integer, parameter :: n = 5 + type :: t1 + integer :: c1, c2 + end type + type(t1) :: a(n) + integer :: i + a = [ (t1(3*i+2, 2*i*i+1), i=1,n) ] + call sub1(a) +contains + subroutine sub1(x) + type(t1) :: x(n) + class(*), pointer :: p(:) + integer :: i + allocate(p, source=x%c2) + call check_logical(is_contiguous(p), .true., 1) + deallocate(p) + end subroutine + subroutine check_logical(x, y, e) + logical, intent(in) :: x, y + integer, intent(in) :: e + !print *, e, (x .eqv. y ? "PASS" : "FAIL"), x, y + if (x .neqv. y) error stop e + end subroutine +end program
