See attached patch. For a CHARACTER array component whose length is a
LEN parameter, gfc_simplify_len builds its result from a copy of the
argument with all references dropped and the type parameter added as
a component reference. The copy keeps the rank and shape of x%ch, and
an array reference on the base object, as in y(2)%ch, is lost. The
patch keeps the leading array references, runs gfc_resolve_ref on the
result, which makes it a scalar type parameter inquiry through
reset_array_ref_to_scalar, and converts it to the KIND= kind. That
function left freed end and stride pointers in a section, which
y(2:3)%n already hits on trunk as an ICE in free_expr0.
The fix is in gfc_simplify_len in simplify.cc, plus the clearing of
the freed pointers in reset_array_ref_to_scalar in resolve.cc; the
rest is the new test.
Regression tested on x86_64.
OK for mainline?
Regards,
Jerry
---
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.
>From 93bcc0c998e603aac8f40a063eb3f5d3f480d952 Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Sat, 21 Nov 2026 18:16:06 -0800
Subject: [PATCH] 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.
---
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(-)
create mode 100644 gcc/testsuite/gfortran.dg/pdt_99.f90
diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc
index fad430490c6..8924284f2ab 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 b58a6af5d06..c8978b41316 100644
--- a/gcc/fortran/simplify.cc
+++ b/gcc/fortran/simplify.cc
@@ -5076,12 +5076,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 00000000000..50dc7f83ba2
--- /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
--
2.56.1