Hi all, attached patch fixes a missing substring ref on a saved allocatable string. The issue seems to be, that the variable is declared to be a character pointer and not a character array. When using the latter (why not), it works as expected and does not produce any regressions.
Regtests ok on x86_64-pc-linux-gnu / F41. Ok for mainlines? Regards, Andre -- Andre Vehreschild * Email: vehre ad gmx dot de
From f05992908acd4aeded9d40d78db4c6720a500e5b Mon Sep 17 00:00:00 2001 From: Andre Vehreschild <ve...@gcc.gnu.org> Date: Mon, 2 Jun 2025 10:41:48 +0200 Subject: [PATCH] Fortran: Fix missing substring ref for allocatable saved vars [PR120483] PR fortran/120483 gcc/fortran/ChangeLog: * trans-types.cc (gfc_sym_type): Create array type also for deferred length, allocatable, saved strings. gcc/testsuite/ChangeLog: * gfortran.dg/save_8.f90: New test. --- gcc/fortran/trans-types.cc | 4 +--- gcc/testsuite/gfortran.dg/save_8.f90 | 13 +++++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gfortran.dg/save_8.f90 diff --git a/gcc/fortran/trans-types.cc b/gcc/fortran/trans-types.cc index e15b1bb89f0..2d37d3d1933 100644 --- a/gcc/fortran/trans-types.cc +++ b/gcc/fortran/trans-types.cc @@ -2502,9 +2502,7 @@ gfc_sym_type (gfc_symbol * sym, bool is_bind_c) && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c) || (sym->ts.deferred - && (!sym->ts.u.cl - || !sym->ts.u.cl->backend_decl - || sym->attr.save)) + && (!sym->ts.u.cl || !sym->ts.u.cl->backend_decl)) || (sym->attr.dummy && sym->attr.value && gfc_length_one_character_type_p (&sym->ts)))) diff --git a/gcc/testsuite/gfortran.dg/save_8.f90 b/gcc/testsuite/gfortran.dg/save_8.f90 new file mode 100644 index 00000000000..8e9198caeb1 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/save_8.f90 @@ -0,0 +1,13 @@ +!{ dg-do run } + +! Check PR120483 is fixed. +! Contributed by Thomas Koenig <tkoe...@gcc.gnu.org> +! and Peter Güntert <pe...@guentert.com> + +program save_8 + implicit none + character(len=:), allocatable, save :: s1 + s1 = 'ABC' + if (s1(3:3) /= 'C') stop 1 +end program save_8 + -- 2.49.0