See attached patch. Since r17-4394 (PR125690), gfc_get_pdt_instance
marks an explicit-shape CHARACTER array component whose length depends
on a type parameter as pdt_array, and an allocatable one as
deferred-length, even when the length depends only on KIND parameters
and folds to a constant. Such a component then takes the run-time PDT
path, and a structure constructor that broadcasts a scalar into it
hits an ICE in walk_coarray. The patch sets pdt_array and deferred
only under the existing check for a non-constant length, which already
guards pdt_string, so a KIND-length component is laid out as an
ordinary fixed-length array.

The fix is in gfc_get_pdt_instance in decl.cc; the rest is the new
test, which also checks that nothing is allocated for these
components.

Regression tested on x86_64.

OK for mainline?

Regards,

Jerry

---

fortran: [PR128763] ICE for a PDT character array with a KIND length

gfc_get_pdt_instance marked an explicit-shape CHARACTER array component
whose length depends on a type parameter as pdt_array, and an
allocatable one as deferred-length, even when the length depends only
on KIND parameters and folds to a constant.  A structure constructor
assigning to such a component then hit an ICE in walk_coarray.  Set
pdt_array and deferred only when the length is not constant, as for
pdt_string.

Assisted-by: Claude Opus 5.5

        PR fortran/128763

gcc/fortran/ChangeLog:

        * decl.cc (gfc_get_pdt_instance): Set pdt_array and ts.deferred
        for a CHARACTER component only when its length is not constant.

gcc/testsuite/ChangeLog:

        * gfortran.dg/pdt_97.f90: New test.
>From c138793f10da27c770db3f17a7881b567ead4179 Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Sat, 21 Nov 2026 11:23:16 -0800
Subject: [PATCH] fortran: [PR128763] ICE for a PDT character array with a KIND
 length

gfc_get_pdt_instance marked an explicit-shape CHARACTER array component
whose length depends on a type parameter as pdt_array, and an
allocatable one as deferred-length, even when the length depends only
on KIND parameters and folds to a constant.  A structure constructor
assigning to such a component then hit an ICE in walk_coarray.  Set
pdt_array and deferred only when the length is not constant, as for
pdt_string.

Assisted-by: Claude Opus 5.5

	PR fortran/128763

gcc/fortran/ChangeLog:

	* decl.cc (gfc_get_pdt_instance): Set pdt_array and ts.deferred
	for a CHARACTER component only when its length is not constant.

gcc/testsuite/ChangeLog:

	* gfortran.dg/pdt_97.f90: New test.
---
 gcc/fortran/decl.cc                  | 12 +++++++-----
 gcc/testsuite/gfortran.dg/pdt_97.f90 | 28 ++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+), 5 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/pdt_97.f90

diff --git a/gcc/fortran/decl.cc b/gcc/fortran/decl.cc
index 01b7434444f..736f410170b 100644
--- a/gcc/fortran/decl.cc
+++ b/gcc/fortran/decl.cc
@@ -4604,11 +4604,13 @@ gfc_get_pdt_instance (gfc_actual_arglist *param_list, gfc_symbol **sym,
 	  else
 	    gfc_free_expr (e);
 	  if (c2->ts.u.cl->length->expr_type != EXPR_CONSTANT)
-	    c2->attr.pdt_string = 1;
-	  if (c1->as && c1->as->type == AS_EXPLICIT)
-	    c2->attr.pdt_array = 1;
-	  else if (c1->attr.allocatable)
-	    c2->ts.deferred = 1;
+	    {
+	      c2->attr.pdt_string = 1;
+	      if (c1->as && c1->as->type == AS_EXPLICIT)
+		c2->attr.pdt_array = 1;
+	      else if (c1->attr.allocatable)
+		c2->ts.deferred = 1;
+	    }
 	}
 
       /* Recurse into this function for PDT components.  */
diff --git a/gcc/testsuite/gfortran.dg/pdt_97.f90 b/gcc/testsuite/gfortran.dg/pdt_97.f90
new file mode 100644
index 00000000000..2ea6ab23a86
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pdt_97.f90
@@ -0,0 +1,28 @@
+! { dg-do compile }
+! { dg-options "-fdump-tree-original" }
+!
+! PR 128763
+
+module m
+  type t1(k)
+    integer, kind :: k
+    character(len=k) :: ch(2)
+  end type
+  type t2(k1, k2)
+    integer, kind :: k1, k2
+    character(len=k2+2) :: ch(k1:k2)
+  end type
+  type t3(k3, k4)
+    integer, kind :: k3, k4
+    type(t2(k3, k4)) :: c(k3:k4)
+  end type
+end module
+
+program p
+  use m
+  type(t1(4)) :: x
+  type(t3(2,5)) :: y
+  x = t1(4)('HI-PDT')
+  y = t3(2,5)(t2(2,5)('HI-PDT'))
+end program
+! { dg-final { scan-tree-dump-not "__builtin_malloc" "original" } }
-- 
2.56.1

Reply via email to