https://gcc.gnu.org/g:32ff72630f2e57125d44e49c48d02397b0964a5e

commit r17-6010-g32ff72630f2e57125d44e49c48d02397b0964a5e
Author: Paul Thomas <[email protected]>
Date:   Sat Nov 7 08:16:10 2026 +0000

    Fortran: [PDT] (13/16) Parameterized derived type errors [PR82173]
    
    2026-11-07  Paul Thomas  <[email protected]>
    
    gcc/fortran
            PR fortran/82173
            * simplify.cc (gfc_simplify_len): Return NULL for paramterized
            string components of nested PDTs.
    
    gcc/testsuite/
            PR fortran/82173
            * gfortran.dg/pdt_bug25.f90: New test.

Diff:
---
 gcc/fortran/simplify.cc                 | 12 ++++++++
 gcc/testsuite/gfortran.dg/pdt_bug25.f90 | 51 +++++++++++++++++++++++++++++++++
 2 files changed, 63 insertions(+)

diff --git a/gcc/fortran/simplify.cc b/gcc/fortran/simplify.cc
index 67a826a23015..f334dafed1c7 100644
--- a/gcc/fortran/simplify.cc
+++ b/gcc/fortran/simplify.cc
@@ -5032,6 +5032,18 @@ gfc_simplify_len (gfc_expr *e, gfc_expr *kind)
   if (k == -1)
     return &gfc_bad_expr;
 
+  /* Catch parameterized strings of nested PDTs and let the expansion occur
+     downstream in trans-array.cc(structure_alloc_comps).  */
+  if (e->expr_type == EXPR_VARIABLE && IS_PDT (e->symtree->n.sym)
+      && !e->param_list && e->ref)
+    {
+      gfc_ref *r = e->ref;
+      for (; r; r = r->next)
+       if (r->type == REF_COMPONENT
+           && (IS_PDT (r->u.c.component) || IS_CLASS_PDT (r->u.c.component)))
+         return NULL;
+    }
+
   if (e->expr_type == EXPR_CONSTANT
       || substring_has_constant_len (e))
     {
diff --git a/gcc/testsuite/gfortran.dg/pdt_bug25.f90 
b/gcc/testsuite/gfortran.dg/pdt_bug25.f90
new file mode 100644
index 000000000000..3cd4212892b8
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pdt_bug25.f90
@@ -0,0 +1,51 @@
+! { dg-do run }
+! F2023 7.5.4.1: a component may itself be of parameterized derived type,
+! with its parameters given by expressions in the outer parameters.
+! This is ~/pos/p22_E_nested_pdt.f90 in the attachment to comment #10
+! of PR82173.
+module m_p22
+  implicit none
+  type :: leaf(k, n)
+    integer, kind :: k = 4
+    integer, len :: n = 1
+    real(k) :: v(n)
+    character(len=n) :: s
+  end type
+  type :: branch(k, n)
+    integer, kind :: k = 4
+    integer, len :: n = 1
+    type(leaf(k, n)) :: same
+    type(leaf(k, 2*n)) :: twice
+    type(leaf(8, 3)) :: fixed
+    integer :: tag(n)
+  end type
+end module
+
+program p22
+  use m_p22
+  implicit none
+  type(branch(4, 3)) :: b
+
+  b%same%v = 1.0_4
+  b%same%s = 'abc'
+  b%twice%v = 2.0_4
+  b%twice%s = 'abcdef'
+  b%fixed%v = 3.0_8
+  b%fixed%s = 'xyz'
+  b%tag = 7
+
+  if (b%k /= 4 .or. b%n /= 3) error stop 1
+  if (b%same%k /= 4 .or. b%same%n /= 3) error stop 2
+  if (b%twice%n /= 6) error stop 3
+  if (b%fixed%k /= 8 .or. b%fixed%n /= 3) error stop 4
+
+  if (size (b%same%v) /= 3) error stop 5
+  if (size (b%twice%v) /= 6) error stop 6
+  if (kind (b%fixed%v) /= 8) error stop 7
+  if (len (b%same%s) /= 3 .or. len (b%twice%s) /= 6) error stop 8
+
+  if (abs (sum (b%same%v) - 3.0_4) > 1.0e-6_4) error stop 9
+  if (abs (sum (b%twice%v) - 12.0_4) > 1.0e-6_4) error stop 10
+  if (b%same%s /= 'abc' .or. b%twice%s /= 'abcdef') error stop 11
+  if (sum (b%tag) /= 21) error stop 12
+end program

Reply via email to