Hello All,

The attached patch is in response to a regression report from Linaro.

Jerry Delisle produced the attached patch with the help of Claude. You
will see from the date stamp at the top that I sat on it for a while
because I have been occupied by other bugs and life in general :-(
Please accept my apologies.

I have tested it thoroughly on x86_64 as did Jerry on aarch64.

Apart from gfortran.dg/pr49213.f90, which fails on a clean tree with
-O2 and higher, the patch passes regression testing.

Pushed as r17-6306.

Memory leaks present in 16-branch that are now fixed in mainline:
pdt_77.f03, pdt_82.f03, pdt_82.f03, pdt_86.f03
Real leaks remaining in mainline:
pdt_70.f03, pdt_80.f03, pdt_array_alloc_1.f90, pdt_bug3.f90 and pdt_bug17.f90

Regards

Paul


On Fri, 6 Nov 2026 at 08:32, <[email protected]> wrote:
>
> Dear contributor,
>
> Our automatic CI has detected problems related to your patch(es). Please find 
> some details below.
>
> In  aarch64 native, after:
>   | gcc patch https://patchwork.sourceware.org/patch/147969
>   | Author: Paul Thomas <[email protected]>
>   | Date:   Thu Nov 5 16:24:48 2026 +0000
>   |
>   |     [PATCH] Fortran: [PDT] (9/16) Parameterized derived type errors 
> [PR82173]
>   |
>   |     2026-11-05  Paul Thomas  <[email protected]>
>   |
>   |     gcc/fortran
>   | ... 56 lines of the commit log omitted.
>   | ... applied on top of baseline commit:
>   | 935a032c6b0 Daily bump.
>
> Produces 1 regression:
>   |
>   | regressions.sum:
>   | Running libstdc++:libstdc++-dg/conformance.exp ...
>   | FAIL: 29_atomics/atomic/wait_notify/127757.cc -std=gnu++20 execution test
>
> Used configuration :
>  *CI config* tcwg_gcc_check aarch64-linux-gnu
>  *configure and test flags:* none, autodetected on 
> aarch64-unknown-linux-gnu--disable-multilib --enable-fix-cortex-a53-835769 
> --enable-fix-cortex-a53-843419 --with-arch=armv8-a
>
> If you have any questions regarding this report, please ask on 
> [email protected] mailing list.
>
> -----------------8<--------------------------8<--------------------------8<--------------------------
>
> The information below contains the details of the failures, and the ways to 
> reproduce a debug environment:
>
> You can find the failure logs in *.log.1.xz files in
>  * 
> http://54.172.246.49:9090/jobs/tcwg_gcc_check--master-aarch64-precommit/builds/11175/archive/artifacts/artifacts.precommit/00-sumfiles/
> The full lists of regressions and improvements as well as configure and make 
> commands are in
>  * 
> http://54.172.246.49:9090/jobs/tcwg_gcc_check--master-aarch64-precommit/builds/11175/archive/artifacts/artifacts.precommit/notify/
> The list of [ignored] baseline and flaky failures are in
>  * 
> http://54.172.246.49:9090/jobs/tcwg_gcc_check--master-aarch64-precommit/builds/11175/archive/artifacts/artifacts.precommit/sumfiles/xfails.xfail
>
> Current build   : 
> http://54.172.246.49:9090/jobs/tcwg_gcc_check--master-aarch64-precommit/builds/11175/archive/artifacts/
> Reference build : 
> http://54.172.246.49:9090/jobs/tcwg_gcc_check--master-aarch64-build/builds/10778/archive/artifacts/
>
> Warning: we do not enable maintainer-mode nor automatically update
> generated files, which may lead to failures if the patch modifies the
> master files.
From e1abc488a4d11700386bed75c869970614be9edb Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Tue, 3 Nov 2026 09:09:33 -0800
Subject: [PATCH] fortran: [PR82173] Free LEN components of PDTs in CLASS(*)
 objects

Deallocating an unlimited polymorphic object that holds a PDT freed the
object but not its LEN-dependent array and string components, because
the PDT's vtab had no finalization wrapper.  Polymorphic assignment from
a structure constructor also leaked the constructor temporary's
components after their deep copy, for PDTs and for types with
allocatable components alike.

Treat LEN-dependent PDT components as finalizable so that the wrapper
is generated and frees them, and free the components of a structure
constructor temporary after a polymorphic assignment.  pdt_bug9.f90
leaked; its REAL(16) cases move to pdt_bug9a.f90.

Assisted-by: Claude Opus 5.5

2026-11-16  Jerry DeLisle <[email protected]>

	PR fortran/82173

gcc/fortran/ChangeLog:

	* class.cc (pdt_len_component, pdt_len_storage_comp): New
	functions.
	(comp_is_finalizable): Treat LEN-dependent PDT components as
	finalizable.
	(finalize_component): Deallocate them.
	(generate_finalization_wrapper): Generate the wrapper for a PDT
	with LEN-dependent components.
	* trans-expr.cc (gfc_trans_assignment_1): Free the components of
	a structure constructor temporary after a polymorphic
	assignment.

gcc/testsuite/ChangeLog:

	* gfortran.dg/allocate_with_source_14.f03: Update __builtin_free
	count.
	* gfortran.dg/class_19.f03: Likewise.
	* gfortran.dg/class_allocate_16.f90: Likewise.
	* gfortran.dg/finalize_33.f90: Likewise.
	* gfortran.dg/pdt_15.f03: Update __builtin_free and .n.data
	counts.
	* gfortran.dg/pdt_26.f03: Update __builtin_free and
	__builtin_malloc counts.
	* gfortran.dg/pdt_46.f03: Likewise.
	* gfortran.dg/pdt_50.f03: Likewise.
	* gfortran.dg/pdt_bug9.f90: Move the REAL(16) cases to
	pdt_bug9a.f90.  Check that LEN components are freed.
	* gfortran.dg/class_assign_5.f90: New test.
	* gfortran.dg/pdt_bug9a.f90: New test.
---
 gcc/fortran/class.cc                          | 40 ++++++++++++++
 gcc/fortran/trans-expr.cc                     | 33 +++++++++---
 .../gfortran.dg/allocate_with_source_14.f03   |  2 +-
 gcc/testsuite/gfortran.dg/class_19.f03        |  2 +-
 .../gfortran.dg/class_allocate_16.f90         |  2 +-
 gcc/testsuite/gfortran.dg/class_assign_5.f90  | 25 +++++++++
 gcc/testsuite/gfortran.dg/finalize_33.f90     |  2 +-
 gcc/testsuite/gfortran.dg/pdt_15.f03          |  4 +-
 gcc/testsuite/gfortran.dg/pdt_26.f03          |  4 +-
 gcc/testsuite/gfortran.dg/pdt_46.f03          |  4 +-
 gcc/testsuite/gfortran.dg/pdt_50.f03          |  4 +-
 gcc/testsuite/gfortran.dg/pdt_bug9.f90        | 11 ++--
 gcc/testsuite/gfortran.dg/pdt_bug9a.f90       | 52 +++++++++++++++++++
 13 files changed, 162 insertions(+), 23 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/class_assign_5.f90
 create mode 100644 gcc/testsuite/gfortran.dg/pdt_bug9a.f90

diff --git a/gcc/fortran/class.cc b/gcc/fortran/class.cc
index faca0b877d0..ca0e7784718 100644
--- a/gcc/fortran/class.cc
+++ b/gcc/fortran/class.cc
@@ -1004,6 +1004,41 @@ has_finalizer_component (gfc_symbol *derived)
 }
 
 
+/* Return the component of a PDT instance whose storage depends on a LEN
+   parameter: a component itself, or one of its nonpointer nonallocatable
+   subcomponents.  */
+
+static gfc_component *
+pdt_len_component (gfc_symbol *derived)
+{
+  for (gfc_component *c = derived->components; c; c = c->next)
+    {
+      if (c->attr.pointer || c->attr.proc_pointer || c->attr.allocatable)
+	continue;
+      if ((c->attr.pdt_array && c->as && c->as->type == AS_EXPLICIT)
+	  || c->attr.pdt_string)
+	return c;
+      if (c->ts.type == BT_DERIVED
+	  && !gfc_compare_derived_types (derived, c->ts.u.derived)
+	  && pdt_len_component (c->ts.u.derived))
+	return c;
+    }
+  return NULL;
+}
+
+
+/* Return true if COMP is a LEN-dependent array or string of a PDT.  */
+
+static bool
+pdt_len_storage_comp (gfc_component *comp)
+{
+  return (!comp->attr.pointer && !comp->attr.allocatable
+	  && ((comp->attr.pdt_array && comp->as
+	       && comp->as->type == AS_EXPLICIT)
+	      || comp->attr.pdt_string));
+}
+
+
 static bool
 comp_is_finalizable (gfc_component *comp)
 {
@@ -1011,8 +1046,11 @@ comp_is_finalizable (gfc_component *comp)
     return false;
   else if (comp->attr.allocatable && comp->ts.type != BT_CLASS)
     return true;
+  else if (pdt_len_storage_comp (comp))
+    return true;
   else if (comp->ts.type == BT_DERIVED && !comp->attr.pointer
 	   && (comp->ts.u.derived->attr.alloc_comp
+	       || pdt_len_component (comp->ts.u.derived)
 	       || has_finalizer_component (comp->ts.u.derived)
 	       || (comp->ts.u.derived->f2k_derived
 		   && comp->ts.u.derived->f2k_derived->finalizers)))
@@ -1086,6 +1124,7 @@ finalize_component (gfc_expr *expr, gfc_symbol *derived, gfc_component *comp,
 
   /* Call DEALLOCATE (comp, stat=ignore).  */
   if (comp->attr.allocatable
+      || pdt_len_storage_comp (comp)
       || (comp->ts.type == BT_CLASS && CLASS_DATA (comp)
 	  && CLASS_DATA (comp)->attr.allocatable))
     {
@@ -1774,6 +1813,7 @@ generate_finalization_wrapper (gfc_symbol *derived, gfc_namespace *ns,
      an interface declaration.  */
   if ((!ancestor_wrapper || ancestor_wrapper->expr_type == EXPR_NULL)
       && !derived->attr.alloc_comp
+      && !pdt_len_component (derived)
       && (!derived->f2k_derived || !derived->f2k_derived->finalizers)
       && !has_finalizer_component (derived))
     {
diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc
index b206fcfcc54..c635e47fa71 100644
--- a/gcc/fortran/trans-expr.cc
+++ b/gcc/fortran/trans-expr.cc
@@ -14545,16 +14545,33 @@ gfc_trans_assignment_1 (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
 						      && !lhs_attr.dimension),
 				    !realloc_flag && flag_realloc_lhs
 				    && !lhs_attr.pointer);
-      if (expr2->expr_type == EXPR_FUNCTION
+      /* Free the components of an rhs temporary after its deep copy.  */
+      if ((expr2->expr_type == EXPR_FUNCTION
+	   || (expr2->expr_type == EXPR_STRUCTURE && !expr2->rank
+	       && (use_vptr_copy
+		   || (lhs_attr.allocatable && !lhs_attr.dimension))))
 	  && expr2->ts.type == BT_DERIVED
-	  && expr2->ts.u.derived->attr.alloc_comp)
+	  && (expr2->ts.u.derived->attr.alloc_comp
+	      || (expr2->expr_type == EXPR_STRUCTURE && IS_PDT (expr2))))
 	{
-	  tree tmp2 = gfc_deallocate_alloc_comp (expr2->ts.u.derived,
-						 rse.expr, expr2->rank);
-	  if (lss == gfc_ss_terminator)
-	    gfc_add_expr_to_block (&rse.post, tmp2);
-	  else
-	    gfc_add_expr_to_block (&loop.post, tmp2);
+	  gfc_symbol *rhs_type = expr2->ts.u.derived;
+	  stmtblock_t free_rhs;
+	  gfc_init_block (&free_rhs);
+	  tree tmp2;
+	  if (rhs_type->attr.alloc_comp)
+	    {
+	      tmp2 = gfc_deallocate_alloc_comp (rhs_type, rse.expr,
+						expr2->rank);
+	      gfc_add_expr_to_block (&free_rhs, tmp2);
+	    }
+	  if (expr2->expr_type == EXPR_STRUCTURE && IS_PDT (expr2))
+	    {
+	      tmp2 = gfc_deallocate_pdt_comp (rhs_type, rse.expr, 0);
+	      gfc_add_expr_to_block (&free_rhs, tmp2);
+	    }
+	  gfc_add_expr_to_block (lss == gfc_ss_terminator ? &rse.post
+						     : &loop.post,
+				 gfc_finish_block (&free_rhs));
 	}
 
       expr1->must_finalize = 0;
diff --git a/gcc/testsuite/gfortran.dg/allocate_with_source_14.f03 b/gcc/testsuite/gfortran.dg/allocate_with_source_14.f03
index 71f9068f3ea..36c1245ccdd 100644
--- a/gcc/testsuite/gfortran.dg/allocate_with_source_14.f03
+++ b/gcc/testsuite/gfortran.dg/allocate_with_source_14.f03
@@ -211,4 +211,4 @@ program main
   deallocate(av)
 end program
 ! { dg-final { scan-tree-dump-times "__builtin_malloc" 23 "original" } }
-! { dg-final { scan-tree-dump-times "__builtin_free" 28 "original" } }
+! { dg-final { scan-tree-dump-times "__builtin_free" 29 "original" } }
diff --git a/gcc/testsuite/gfortran.dg/class_19.f03 b/gcc/testsuite/gfortran.dg/class_19.f03
index 8ae48fa1b06..441642bba16 100644
--- a/gcc/testsuite/gfortran.dg/class_19.f03
+++ b/gcc/testsuite/gfortran.dg/class_19.f03
@@ -39,4 +39,4 @@ program main
 
 end program main
 
-! { dg-final { scan-tree-dump-times "__builtin_free" 12 "original" } }
+! { dg-final { scan-tree-dump-times "__builtin_free" 14 "original" } }
diff --git a/gcc/testsuite/gfortran.dg/class_allocate_16.f90 b/gcc/testsuite/gfortran.dg/class_allocate_16.f90
index 57fcc447449..65a3939c3f7 100644
--- a/gcc/testsuite/gfortran.dg/class_allocate_16.f90
+++ b/gcc/testsuite/gfortran.dg/class_allocate_16.f90
@@ -24,4 +24,4 @@
 
 end
 
-! { dg-final { scan-tree-dump-times "__builtin_free" 4 "original" } }
+! { dg-final { scan-tree-dump-times "__builtin_free" 5 "original" } }
diff --git a/gcc/testsuite/gfortran.dg/class_assign_5.f90 b/gcc/testsuite/gfortran.dg/class_assign_5.f90
new file mode 100644
index 00000000000..85ecf12995f
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/class_assign_5.f90
@@ -0,0 +1,25 @@
+! { dg-do run }
+! { dg-options "-fdump-tree-original" }
+! PR 82173
+!
+! Assigning a structure constructor with an allocatable component to an
+! unlimited polymorphic variable leaked the constructor's component.
+!
+module mb
+  type :: t
+    real, allocatable :: v(:)
+  end type
+end module
+program p
+  use mb
+  class(*), allocatable :: u
+  u = t([1.,2.,3.])
+  select type (u)
+    type is (t)
+      if (any (u%v /= [1.,2.,3.])) stop 1
+    class default
+      stop 2
+  end select
+  deallocate (u)
+end program
+! { dg-final { scan-tree-dump "__builtin_free \\(\\(void \\*\\) t\\.\[0-9\]+\\.v\\.data\\)" "original" } }
diff --git a/gcc/testsuite/gfortran.dg/finalize_33.f90 b/gcc/testsuite/gfortran.dg/finalize_33.f90
index 3857e4485ee..95abccaba0c 100644
--- a/gcc/testsuite/gfortran.dg/finalize_33.f90
+++ b/gcc/testsuite/gfortran.dg/finalize_33.f90
@@ -116,4 +116,4 @@ contains
                                                ! (iii) mci_template
 end program main_ut
 ! { dg-final { scan-tree-dump-times "__builtin_malloc" 17 "original" } }
-! { dg-final { scan-tree-dump-times "__builtin_free" 20 "original" } }
+! { dg-final { scan-tree-dump-times "__builtin_free" 21 "original" } }
diff --git a/gcc/testsuite/gfortran.dg/pdt_15.f03 b/gcc/testsuite/gfortran.dg/pdt_15.f03
index f7ee691cfce..224dd556e8c 100644
--- a/gcc/testsuite/gfortran.dg/pdt_15.f03
+++ b/gcc/testsuite/gfortran.dg/pdt_15.f03
@@ -102,5 +102,5 @@ contains
   end subroutine
 end program ch2701
 ! { dg-final { scan-tree-dump-times "PDTlink_8._deallocate " 5 "original" } }
-! { dg-final { scan-tree-dump-times ".n.data = 0B" 9 "original" } }
-! { dg-final { scan-tree-dump-times "__builtin_free" 15 "original" } }
+! { dg-final { scan-tree-dump-times ".n.data = 0B" 10 "original" } }
+! { dg-final { scan-tree-dump-times "__builtin_free" 16 "original" } }
diff --git a/gcc/testsuite/gfortran.dg/pdt_26.f03 b/gcc/testsuite/gfortran.dg/pdt_26.f03
index 86a585ad262..fb211859c28 100644
--- a/gcc/testsuite/gfortran.dg/pdt_26.f03
+++ b/gcc/testsuite/gfortran.dg/pdt_26.f03
@@ -42,5 +42,5 @@ program test_pdt
   c = addvv(a,b)
   if (any (c(1)%foo .ne. [13,15,17])) STOP 2
 end program test_pdt
-! { dg-final { scan-tree-dump-times "__builtin_free" 8 "original" } }
-! { dg-final { scan-tree-dump-times "__builtin_malloc" 8 "original" } }
+! { dg-final { scan-tree-dump-times "__builtin_free" 11 "original" } }
+! { dg-final { scan-tree-dump-times "__builtin_malloc" 10 "original" } }
diff --git a/gcc/testsuite/gfortran.dg/pdt_46.f03 b/gcc/testsuite/gfortran.dg/pdt_46.f03
index fdccbc5cded..42185970f6d 100644
--- a/gcc/testsuite/gfortran.dg/pdt_46.f03
+++ b/gcc/testsuite/gfortran.dg/pdt_46.f03
@@ -58,6 +58,6 @@ contains
   end subroutine main2
 
 end program test
-! { dg-final { scan-tree-dump-times "__builtin_free" 16 "original" } }
-! { dg-final { scan-tree-dump-times "__builtin_malloc" 8 "original" } }
+! { dg-final { scan-tree-dump-times "__builtin_free" 19 "original" } }
+! { dg-final { scan-tree-dump-times "__builtin_malloc" 10 "original" } }
 ! { dg-final { scan-tree-dump-times "__builtin_calloc" 4 "original" } }
diff --git a/gcc/testsuite/gfortran.dg/pdt_50.f03 b/gcc/testsuite/gfortran.dg/pdt_50.f03
index 270989d2fe9..dd92f9ddf7a 100644
--- a/gcc/testsuite/gfortran.dg/pdt_50.f03
+++ b/gcc/testsuite/gfortran.dg/pdt_50.f03
@@ -50,6 +50,6 @@
     call sr
     deallocate (d%p_t1)
 end
-! { dg-final { scan-tree-dump-times "__builtin_malloc" 7 "original" } }
+! { dg-final { scan-tree-dump-times "__builtin_malloc" 9 "original" } }
 ! { dg-final { scan-tree-dump-times "__builtin_calloc" 1 "original" } }
-! { dg-final { scan-tree-dump-times "__builtin_free" 9 "original" } }
+! { dg-final { scan-tree-dump-times "__builtin_free" 12 "original" } }
diff --git a/gcc/testsuite/gfortran.dg/pdt_bug9.f90 b/gcc/testsuite/gfortran.dg/pdt_bug9.f90
index afa400b062f..37820621f81 100644
--- a/gcc/testsuite/gfortran.dg/pdt_bug9.f90
+++ b/gcc/testsuite/gfortran.dg/pdt_bug9.f90
@@ -1,5 +1,7 @@
 ! { dg-do run }
+! { dg-options "-fdump-tree-original" }
 ! Based on attachment to PR82173 comment 10 - reproducers/bug9-class-is-kind-ice.f90
+! pdt_bug9a.f90 adds the REAL(16) cases.
 !
 ! This was straightforward to fix; apart from line 36, which required considerable
 ! assistance from Claude Haiku  <[email protected]>.
@@ -33,8 +35,6 @@ contains
       class is (base(8, *))     ! distinct guards - C1168 permits this
         if (size(u%v, 1) /= sz) stop 3
         if (kind (u%v) /= 8) stop 4
-      class is (base(16, *))    ! Keyword argument ‘k’ at (1) is not in the procedure
-        if (size(u%v, 1) /= sz) stop 5
     end select
     if (allocated (u)) deallocate (u)
   end
@@ -43,9 +43,14 @@ contains
     call selector (u, 5)
     allocate (u, source = base(4, 3)([1,2,3]))
     call selector (u, 3)
-    allocate (u, mold = base(16, 4)([1.,2.,3.,4.])) ! This failed
+    allocate (u, mold = base(8, 4)([1.,2.,3.,4.])) ! This failed
     call selector (u, 4)
     u = base(4, 3)([1,2,3])
     call selector (u, 3)
   end
 end program
+! DEALLOCATE of u must free the LEN-dependent component v.
+! { dg-final { scan-tree-dump "_final=__final_mj_PDTbase_4\[,\}\]" "original" } }
+! { dg-final { scan-tree-dump "_final=__final_mj_PDTbase_8\[,\}\]" "original" } }
+! The constructor temporary's v is freed after its copy into u.
+! { dg-final { scan-tree-dump "__builtin_free \\(\\(void \\*\\) PDTbase_4\\.\[0-9\]+\\.v\\.data\\)" "original" } }
diff --git a/gcc/testsuite/gfortran.dg/pdt_bug9a.f90 b/gcc/testsuite/gfortran.dg/pdt_bug9a.f90
new file mode 100644
index 00000000000..ed9ff7218ad
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pdt_bug9a.f90
@@ -0,0 +1,52 @@
+! { dg-do run }
+! { dg-require-effective-target fortran_real_16 }
+! Based on attachment to PR82173 comment 10 - reproducers/bug9-class-is-kind-ice.f90
+!
+! This was straightforward to fix; apart from line 36, which required considerable
+! assistance from Claude Haiku  <[email protected]>.
+!
+! Contributed by Jerry Delisle  <[email protected]>
+!
+module mj
+  type :: base(k, n)
+    integer, kind :: k = 4
+    integer, len :: n = 1
+    real(k) :: v(n)
+  end type
+end module
+program j1
+  use mj
+  class(*), allocatable :: u
+  allocate (base(4, 3) :: u) ! This failed "Double CLASS IS block"
+  call selector (u, 3)
+  allocate (base(4, 5) :: u) ! n = 3 was returned
+  call selector (u, 5)
+  call the_rest ()
+contains
+  subroutine selector (u, sz)
+    class(*), allocatable :: u
+    integer :: sz
+    if (.not.allocated (u)) return
+    select type (u)
+      class is (base(4, *))     ! distinct kind parameter values are
+        if (size (u%v, 1) /= sz) stop 1
+        if (kind (u%v) /= 4) stop 2
+      class is (base(8, *))     ! distinct guards - C1168 permits this
+        if (size(u%v, 1) /= sz) stop 3
+        if (kind (u%v) /= 8) stop 4
+      class is (base(16, *))    ! Keyword argument ‘k’ at (1) is not in the procedure
+        if (size(u%v, 1) /= sz) stop 5
+    end select
+    if (allocated (u)) deallocate (u)
+  end
+  subroutine the_rest ()
+    allocate (base(8, 5) :: u) ! This failed
+    call selector (u, 5)
+    allocate (u, source = base(4, 3)([1,2,3]))
+    call selector (u, 3)
+    allocate (u, mold = base(16, 4)([1.,2.,3.,4.])) ! This failed
+    call selector (u, 4)
+    u = base(4, 3)([1,2,3])
+    call selector (u, 3)
+  end
+end program
-- 
2.56.0

Reply via email to