https://gcc.gnu.org/g:0c3daf65d25db056f27981e309f574fae1ce6b9c
commit r17-6605-g0c3daf65d25db056f27981e309f574fae1ce6b9c Author: Jerry DeLisle <[email protected]> Date: Sat Nov 21 18:16:06 2026 -0800 fortran: [PR128936] LEN of a PDT character array component is scalar For a CHARACTER array component whose length is a LEN parameter, gfc_simplify_len replaced LEN (x%ch) by a reference to the parameter but kept the rank of the component, dropped the array reference of the base object and ignored KIND=. An assignment of the result was rejected, LEN (y(2)%ch) hit an ICE, and KIND=8 gave a default integer. Keep the array references of the base, resolve the new reference so that it becomes a scalar type parameter inquiry, and convert it to the requested kind. reset_array_ref_to_scalar freed the end and stride of a section without clearing them, so y(2:3)%n freed them twice; clear them. Assisted-by: Claude Opus 5.5 PR fortran/128936 gcc/fortran/ChangeLog: * resolve.cc (reset_array_ref_to_scalar): Clear the end and stride after freeing them. * simplify.cc (gfc_simplify_len): Keep the array references of the base object for a LEN-parameter string component, resolve the result and convert it to the requested kind. gcc/testsuite/ChangeLog: * gfortran.dg/pdt_99.f90: New test. Diff: --- gcc/fortran/resolve.cc | 8 +++--- gcc/fortran/simplify.cc | 29 +++++++++++++++++--- gcc/testsuite/gfortran.dg/pdt_99.f90 | 51 ++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 8 deletions(-) diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc index 283231a8e650..bfb2593ab10b 100644 --- a/gcc/fortran/resolve.cc +++ b/gcc/fortran/resolve.cc @@ -6109,10 +6109,10 @@ reset_array_ref_to_scalar (gfc_expr *expr, gfc_ref *array_ref) else array_ref->u.ar.start[dim] = gfc_copy_expr (unity); - if (array_ref->u.ar.end[dim]) - gfc_free_expr (array_ref->u.ar.end[dim]); - if (array_ref->u.ar.stride[dim]) - gfc_free_expr (array_ref->u.ar.stride[dim]); + gfc_free_expr (array_ref->u.ar.end[dim]); + array_ref->u.ar.end[dim] = NULL; + gfc_free_expr (array_ref->u.ar.stride[dim]); + array_ref->u.ar.stride[dim] = NULL; } gfc_free_expr (unity); } diff --git a/gcc/fortran/simplify.cc b/gcc/fortran/simplify.cc index 2cec0011608f..8781f8ab78bf 100644 --- a/gcc/fortran/simplify.cc +++ b/gcc/fortran/simplify.cc @@ -5086,12 +5086,33 @@ gfc_simplify_len (gfc_expr *e, gfc_expr *kind) } else { + /* Replace the string component and what follows it by the + LEN parameter, keeping the array reference of the base, + which resolution makes scalar. */ gfc_expr *len_expr = gfc_copy_expr (e); - gfc_free_ref_list (len_expr->ref); - len_expr->ref = NULL; + gfc_ref **ref = &len_expr->ref; + while (*ref && (*ref)->type == REF_ARRAY) + ref = &(*ref)->next; + gfc_free_ref_list (*ref); + *ref = NULL; gfc_find_component (len_expr->symtree->n.sym->ts.u.derived, - name, false, true, &len_expr->ref); - len_expr->ts = len_expr->ref->u.c.component->ts; + name, false, true, ref); + len_expr->ts = (*ref)->u.c.component->ts; + gfc_free_shape (&len_expr->shape, len_expr->rank); + if (!gfc_resolve_ref (len_expr)) + { + gfc_free_expr (len_expr); + return NULL; + } + gfc_expression_rank (len_expr); + if (len_expr->ts.kind != k) + { + gfc_typespec ts; + gfc_clear_ts (&ts); + ts.type = BT_INTEGER; + ts.kind = k; + gfc_convert_type_warn (len_expr, &ts, 2, 0); + } return len_expr; } } diff --git a/gcc/testsuite/gfortran.dg/pdt_99.f90 b/gcc/testsuite/gfortran.dg/pdt_99.f90 new file mode 100644 index 000000000000..50dc7f83ba2c --- /dev/null +++ b/gcc/testsuite/gfortran.dg/pdt_99.f90 @@ -0,0 +1,51 @@ +! { dg-do run } +! +! PR 128936 + +module pdt_99_m + type t(n, m) + integer, len :: n, m + character(len=n) :: ch(2) + character(len=n) :: s + character(len=n+m) :: u(3) + end type +contains + subroutine check_assumed (a, b) + type(t(*,*)) :: a, b(:) + character(len=len(a%ch)) :: buf + if (len (a%ch) /= 5) stop 20 + if (len (buf) /= 5) stop 21 + if (len (b%s) /= 5) stop 22 + if (len (b(1)%ch) /= 5) stop 23 + end subroutine +end module + +program p + use pdt_99_m + implicit none + type(t(4,1)) :: x, y(-1:1) + type(t(:,:)), allocatable :: z(:) + integer :: i, j, k + + x%ch = 'abcdef' + i = len (x%ch) + if (i /= 4) stop 1 + if (len (x%ch) /= 4) stop 2 + if (len (x%u) /= 5) stop 3 + if (kind (len (x%ch, kind=8)) /= 8) stop 4 + if (len (x%ch, kind=8) /= 4_8) stop 5 + if (any ([len (x%ch), len (x%u)] /= [4, 5])) stop 6 + i = 2; j = 3 + if (len (x%ch(1)(i:j)) /= 2) stop 7 + + if (len (y(0)%ch) /= 4) stop 8 + if (len (y%s) /= 4) stop 9 + if (len (y(0:1)%s) /= 4) stop 10 + if (y(0:1)%n /= 4) stop 11 + + k = 5 + allocate (t(k,2) :: z(3)) + if (len (z(2)%ch) /= 5) stop 12 + if (len (z(2:3)%s) /= 5) stop 13 + call check_assumed (z(1), z) +end program
