Dear all, this patch is a follow-up to the previous patch for this PR that ignored other situations where an array-valued function of type character was passed around e.g. to TRANSPOSE(). The patch assumes that the string length is knows after the scalarizer.
Caveat: this patch may be incomplete. The testcase exercises several situations which either use the intrinsic RESHAPE or a function mimicking it. There is one situation where an "unfortunate" ordering of procedures generates an ICE. See testcase. I've chosen to submit the patch anyway to get feedback. If someone finds a solution to the failure, I'd be glad. Otherwise we could still push this to mainline and leave the PR open. Regtested on x86_64-pc-linux-gnu. OK for mainline? Thoughts about a backport to 16-branch after decent waiting? Thanks, Harald
From 006ee541b74c6402720676aeab29c6bd6820bbba Mon Sep 17 00:00:00 2001 From: Harald Anlauf <[email protected]> Date: Mon, 23 Nov 2026 22:58:24 +0100 Subject: [PATCH] fortran: Character length of array-valued function of type character [PR128682] PR fortran/128682 gcc/fortran/ChangeLog: * trans-array.cc (gfc_conv_expr_descriptor): For an array-valued function of type character, the scalarizer already resolved the string length. There is no need to attempt to rederive it. gcc/testsuite/ChangeLog: * gfortran.dg/transpose_char_function.f90: New test. --- gcc/fortran/trans-array.cc | 4 +- .../gfortran.dg/transpose_char_function.f90 | 97 +++++++++++++++++++ 2 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gfortran.dg/transpose_char_function.f90 diff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc index aee183a5fef..7cbb2b01c19 100644 --- a/gcc/fortran/trans-array.cc +++ b/gcc/fortran/trans-array.cc @@ -8606,7 +8606,8 @@ gfc_conv_expr_descriptor (gfc_se *se, gfc_expr *expr) /* Set the string_length for a character array. */ if (expr->ts.type == BT_CHARACTER) { - if (deferred_array_component && !substr) + if ((deferred_array_component && !substr) + || expr->expr_type == EXPR_FUNCTION) se->string_length = ss_info->string_length; else se->string_length = gfc_get_expr_charlen (expr); @@ -8618,6 +8619,7 @@ gfc_conv_expr_descriptor (gfc_se *se, gfc_expr *expr) tmp = se->string_length; if (expr->ts.deferred && expr->ts.u.cl->backend_decl + && expr->expr_type != EXPR_FUNCTION && VAR_P (expr->ts.u.cl->backend_decl)) gfc_add_modify (&se->pre, expr->ts.u.cl->backend_decl, tmp); else diff --git a/gcc/testsuite/gfortran.dg/transpose_char_function.f90 b/gcc/testsuite/gfortran.dg/transpose_char_function.f90 new file mode 100644 index 00000000000..d654915431d --- /dev/null +++ b/gcc/testsuite/gfortran.dg/transpose_char_function.f90 @@ -0,0 +1,97 @@ +! { dg-do run } +! PR fortran/128682 +! +! Exercise transpose() of array-valued functions of type character +! (TODO: fix remaining ICE triggered by line 87) + +program p + implicit none(type, external) + character(len=6) :: blk = 'ab#.#.' + integer, parameter :: w = 2, h = 3 + integer :: i + character, allocatable :: ref(:,:) + ref = transpose (reshape ([(blk(i:i), i=1, len(blk))], [w, h])) + call show (transpose (reshape ([(blk(i:i), i=1, len(blk))], [w, h]))) + call show (transpose (char1_fixed ())) + call show (transpose (char1_defer ())) + call show (transpose (charn_fixed (1))) + call show (transpose (charn_defer (1))) + call show (transpose (chard_defer ())) + call show (transpose (my_reshape ([(blk(i:i), i=1, len(blk))], [w, h]))) + call show1 ([(blk(i:i), i=1, len(blk))]) + call show2 ([(blk(i:i), i=1, len(blk))]) + call show3 ([(blk(i:i), i=1, len(blk))]) + call show4 ([(blk(i:i), i=1, len(blk))]) ! test inactive! + deallocate (ref) +contains + ! constant length character, explicit and deferred shape + function char1_fixed () result (res) + character(len=1) :: res(w,h) + integer :: i + res = reshape ([(blk(i:i), i=1, len(blk))], [w, h]) + end function + + function char1_defer () result (res) + character(len=1), allocatable :: res(:,:) + integer :: i + res = reshape ([(blk(i:i), i=1, len(blk))], [w, h]) + end function + + ! explicit length character, explicit and deferred shape + function charn_fixed (n) result (res) + integer, intent(in) :: n + character(len=n) :: res(w,h) + integer :: i + res = reshape ([(blk(i:i), i=1, len(blk))], [w, h]) + end function + + function charn_defer (n) result (res) + integer, intent(in) :: n + character(len=n), allocatable :: res(:,:) + integer :: i + res = reshape ([(blk(i:i), i=1, len(blk))], [w, h]) + end function + + ! deferred length character + function chard_defer () result (res) + character(len=:), allocatable :: res(:,:) + integer :: i + res = reshape ([(blk(i:i), i=1, len(blk))], [w, h]) + end function + + subroutine show1 (arr) + character(*), intent(in) :: arr(:) + call show (transpose (reshape (arr, [w, h]))) + end subroutine + + subroutine show3 (arr) + character(*), intent(in) :: arr(:) + call show (transpose (my_reshape (arr, [w, h]))) ! ICE + end subroutine + + function my_reshape (arr, vec) result (res) + character(*), intent(in) :: arr(:) + integer, intent(in) :: vec(:) + character(len(arr)) :: res(vec(1),vec(2)) + res = reshape (arr, [vec(1),vec(2)]) + end function my_reshape + + subroutine show2 (arr) + character(*), intent(in) :: arr(:) + call show (transpose (reshape (arr, [w, h]))) + end subroutine + + subroutine show4 (arr) + character(*), intent(in) :: arr(:) +!-- the following line does not yet compile: +! call show (transpose (my_reshape (arr, [w, h]))) ! ICE + end subroutine + + subroutine show (g) + character, intent(in) :: g(:, :) + if (any (g /= ref)) then + stop 1 + print '(a)', g + end if + end subroutine show +end program p -- 2.51.0
