Dear All,

debugging this PR sent me deep into the woods.  To find where things
went wrong, I ran two parallel gdb sessions on a testcase once with
character(kind=1) and with character(kind=4), and found that the
code diverged in get_array_span, as the TYPE_STRING_FLAG was set
for the former but not set for the latter.

I did not find why it was set at all for the former, but decided
that it should be set for all character types.  That fixed it.
(If someone thinks that I am wrong, please speak up!)

This allowed to disable the temporary workarounds I put in
testcase deferred_character_39.f90, so this should guarantee
that the present fix works and does not break something obvious.

Regtested on x86_64-pc-linux-gnu.  OK for mainline?

And for joint backport with the fix for pr108581 to 15-branch?

Thanks,
Harald

From 9225d903338784b199b6530483c84f4983fd0753 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <[email protected]>
Date: Wed, 24 Sep 2025 20:36:32 +0200
Subject: [PATCH] Fortran: ICE in character(kind=4) deferred-length array
 reference [PR121939]

	 PR fortran/121939

gcc/fortran/ChangeLog:

	* trans-types.cc (gfc_init_types): Set string flag for all
	character types.

gcc/testsuite/ChangeLog:

	* gfortran.dg/deferred_character_39.f90: Disable temporary
	workaround for character(kind=4) deferred-length bug.
---
 gcc/fortran/trans-types.cc                          |  1 +
 gcc/testsuite/gfortran.dg/deferred_character_39.f90 | 12 +++++++-----
 2 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/gcc/fortran/trans-types.cc b/gcc/fortran/trans-types.cc
index a9e512ed135..26645b0f7f6 100644
--- a/gcc/fortran/trans-types.cc
+++ b/gcc/fortran/trans-types.cc
@@ -1135,6 +1135,7 @@ gfc_init_types (void)
     {
       type = gfc_build_uint_type (gfc_character_kinds[index].bit_size);
       type = build_qualified_type (type, TYPE_UNQUALIFIED);
+      TYPE_STRING_FLAG (type) = 1;
       snprintf (name_buf, sizeof(name_buf), "character(kind=%d)",
 		gfc_character_kinds[index].kind);
       PUSH_TYPE (name_buf, type);
diff --git a/gcc/testsuite/gfortran.dg/deferred_character_39.f90 b/gcc/testsuite/gfortran.dg/deferred_character_39.f90
index 564f94bb9bd..38ec431fc60 100644
--- a/gcc/testsuite/gfortran.dg/deferred_character_39.f90
+++ b/gcc/testsuite/gfortran.dg/deferred_character_39.f90
@@ -1,5 +1,6 @@
 ! { dg-do run }
 ! PR fortran/108581 - issues with rank-2 deferred-length character arrays
+! PR fortran/121939 - ICE in gfc_conv_string_parameter
 
 program p
   call pr108581
@@ -79,8 +80,8 @@ subroutine test2
   allocate (a, source = reshape (str1,[1,size(str1)]))
   allocate (b, source = reshape (str1,[1,size(str1)]))
   allocate (c, source = reshape (str4,[1,size(str4)]))
-! allocate (d, source=c)        ! ICE, tracked as pr121939
-  d => c
+  allocate (d, source = c)      ! fixed with pr121939
+! d => c
   ! Positive non-unit stride
   s0 = concat (str1(1::2))
   write(s1,'(4A)') a(1,1::2)
@@ -124,13 +125,14 @@ subroutine test2
   if (s2 /= s0) stop 26
   if (s3 /= s0) stop 27
   if (s4 /= s0) stop 28
-  deallocate (a,b,c)
+  deallocate (a,b,c,d)
 
   ! More complex cases with shape=[2,4]
   allocate (e, source = reshape (str2,[2,size(str2,2)]))
   allocate (f, source = reshape (str2,[2,size(str2,2)]))
   allocate (g, source = reshape (str5,[2,size(str5,2)]))
-  h => g
+  allocate (h, source = reshape (str5,[2,size(str5,2)])) ! fixed with pr121939
+! h => g
   s0 = concat (str2(1,3:1:-2))
   write(s1,'(4A)') e(1,3:1:-2)
   write(s2,'(4A)') f(1,3:1:-2)
@@ -220,7 +222,7 @@ subroutine test2
   if (s2 /= s0) stop 66
   if (s3 /= s0) stop 67
   if (s4 /= s0) stop 68
-  deallocate (e,f,g)
+  deallocate (e,f,g,h)
 
 contains
 
-- 
2.51.0

Reply via email to