Hello All, I have had this patch on my tree since Jerry sent it to me. It has been regression tested several times since then and is a necessary prequel for some of my own patches.
Initially I had some concerns about using the finalization wrapper to do this but I have now convinced myself that all is well and that this is even quite neat. I will be pushing it just as soon as I get my act together. Thanks, Jerry! Cheers Paul ---------- Forwarded message --------- From: Jerry DeLisle <[email protected]> Date: Mon, 16 Nov 2026 at 17:56 Subject: [PATCH] fortran: [PR82173] Free PDT LEN storage only on deallocation To: Paul Richard Thomas <[email protected]> Hi Paul, Here is the fix for the fin1.f90 case. Since r17-6306 the finalization wrapper of a PDT frees its LEN-dependent components, but the wrapper also finalizes objects that continue to exist, such as an INTENT(OUT) actual argument, so s_out then wrote to freed storage. The wrapper now frees these components only when its fini_coarray argument is true, as it already did for coarray components. gfc_deallocate_with_status, gfc_deallocate_scalar_with_status and gfc_add_comp_finalizer_call, for an allocatable component, pass true; INTENT(OUT) and intrinsic assignment still pass false. An allocatable object cannot have coarray ultimate components, so coarrays are not affected. finalize_18.f90 has one count raised, because the wrapper's DEALLOCATE of an allocatable component now passes true. pdt_bug9b.f90 is the fin1.f90 case plus an extended type and a CHARACTER(n) component. pdt_bug9.f90, pdt_bug9a.f90 and class_assign_5.f90 stay leak-free under valgrind. I could not try your s_out from pdt-class-array-dummy-ice.f90, because a CLASS INTENT(OUT) PDT dummy still ICEs in gfc_trans_deferred_vars here without your pending patch; please check it with that applied. Regression tested on x86_64. The only failures are pr49213.f90 at -O2 and above, which come from the recent IPA changes upstream. Regards, Jerry --- fortran: [PR82173] Free PDT LEN storage only on deallocation Since r17-6306 the finalization wrapper of a PDT frees its LEN-dependent components. The wrapper also finalizes objects that continue to exist, such as an INTENT(OUT) actual argument (F2023 7.5.6.3), so the procedure then accessed freed storage. Free these components only when the wrapper's fini_coarray argument is true, as is already done for coarray components, and pass true wherever the finalized object is deallocated. An allocatable object cannot have coarray ultimate components, so this does not change the handling of coarrays. Assisted-by: Claude Opus 5.5 PR fortran/82173 gcc/fortran/ChangeLog: * class.cc (finalize_component): Deallocate LEN-dependent PDT components only if fini_coarray is true. (generate_finalization_wrapper): Document fini_coarray. * trans.cc (gfc_add_comp_finalizer_call): Pass true for fini_coarray if the component is allocatable. (gfc_add_finalizer_call): Add argument FINI_COARRAY and pass it to the finalizer. (gfc_deallocate_with_status): Pass true for fini_coarray. (gfc_deallocate_scalar_with_status): Likewise. * trans.h (gfc_add_finalizer_call): Add a bool argument. gcc/testsuite/ChangeLog: * gfortran.dg/finalize_18.f90: Update the count of finalizer calls with fini_coarray true. * gfortran.dg/pdt_bug9b.f90: New test.
From 0169930e6bf04d8a6219c492a84402fad90de2b4 Mon Sep 17 00:00:00 2001 From: Jerry DeLisle <[email protected]> Date: Mon, 16 Nov 2026 09:54:41 -0800 Subject: [PATCH] fortran: [PR82173] Free PDT LEN storage only on deallocation Since r17-6306 the finalization wrapper of a PDT frees its LEN-dependent components. The wrapper also finalizes objects that continue to exist, such as an INTENT(OUT) actual argument (F2023 7.5.6.3), so the procedure then accessed freed storage. Free these components only when the wrapper's fini_coarray argument is true, as is already done for coarray components, and pass true wherever the finalized object is deallocated. An allocatable object cannot have coarray ultimate components, so this does not change the handling of coarrays. Assisted-by: Claude Opus 5.5 PR fortran/82173 gcc/fortran/ChangeLog: * class.cc (finalize_component): Deallocate LEN-dependent PDT components only if fini_coarray is true. (generate_finalization_wrapper): Document fini_coarray. * trans.cc (gfc_add_comp_finalizer_call): Pass true for fini_coarray if the component is allocatable. (gfc_add_finalizer_call): Add argument FINI_COARRAY and pass it to the finalizer. (gfc_deallocate_with_status): Pass true for fini_coarray. (gfc_deallocate_scalar_with_status): Likewise. * trans.h (gfc_add_finalizer_call): Add a bool argument. gcc/testsuite/ChangeLog: * gfortran.dg/finalize_18.f90: Update the count of finalizer calls with fini_coarray true. * gfortran.dg/pdt_bug9b.f90: New test. --- gcc/fortran/class.cc | 8 +++-- gcc/fortran/trans.cc | 15 +++++--- gcc/fortran/trans.h | 3 +- gcc/testsuite/gfortran.dg/finalize_18.f90 | 2 +- gcc/testsuite/gfortran.dg/pdt_bug9b.f90 | 44 +++++++++++++++++++++++ 5 files changed, 62 insertions(+), 10 deletions(-) create mode 100644 gcc/testsuite/gfortran.dg/pdt_bug9b.f90 diff --git a/gcc/fortran/class.cc b/gcc/fortran/class.cc index ca0e7784718..ccd899abd76 100644 --- a/gcc/fortran/class.cc +++ b/gcc/fortran/class.cc @@ -1132,6 +1132,7 @@ finalize_component (gfc_expr *expr, gfc_symbol *derived, gfc_component *comp, /* Add IF (fini_coarray). */ if (comp->attr.codimension + || pdt_len_storage_comp (comp) || (comp->ts.type == BT_CLASS && CLASS_DATA (comp) && CLASS_DATA (comp)->attr.codimension)) { @@ -1765,9 +1766,10 @@ finalizer_insert_packed_call (gfc_code *block, gfc_finalizer *fini, _final(assumed-rank array, stride, skip_corarray) where the array has to be contiguous (except of the lowest dimension). The stride (in bytes) is used to allow different sizes for ancestor types by - skipping over the additionally added components in the scalarizer. If - "fini_coarray" is false, coarray components are not finalized to allow for - the correct semantic with intrinsic assignment. */ + skipping over the additionally added components in the scalarizer. + "fini_coarray" is true when the object is deallocated; otherwise coarray + components and LEN-dependent PDT components, which outlive finalization + (F2023:7.5.6.3), are left alone. */ static void generate_finalization_wrapper (gfc_symbol *derived, gfc_namespace *ns, diff --git a/gcc/fortran/trans.cc b/gcc/fortran/trans.cc index ff3bea5e1be..642162b54c5 100644 --- a/gcc/fortran/trans.cc +++ b/gcc/fortran/trans.cc @@ -1362,6 +1362,8 @@ gfc_add_comp_finalizer_call (stmtblock_t *block, tree decl, gfc_component *comp, cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node, tmp, fold_convert (TREE_TYPE (tmp), null_pointer_node)); + /* The component is deallocated after it is finalized. */ + fini_coarray = true; } else cond = logical_true_node; @@ -1407,11 +1409,12 @@ gfc_add_comp_finalizer_call (stmtblock_t *block, tree decl, gfc_component *comp, /* Add a call to the finalizer, using the passed *expr. Returns - true when a finalizer call has been inserted. */ + true when a finalizer call has been inserted. FINI_COARRAY is true + when EXPR is about to be deallocated. */ bool gfc_add_finalizer_call (stmtblock_t *block, gfc_expr *expr2, - tree class_container) + tree class_container, bool fini_coarray) { tree tmp; gfc_ref *ref; @@ -1482,7 +1485,8 @@ gfc_add_finalizer_call (stmtblock_t *block, gfc_expr *expr2, tmp = build_call_expr_loc (input_location, final_se.expr, 3, desc_se.expr, size_se.expr, - boolean_false_node); + fini_coarray ? boolean_true_node + : boolean_false_node); gfc_add_expr_to_block (&tmp_block, tmp); @@ -1958,7 +1962,7 @@ gfc_deallocate_with_status (tree pointer, tree status, tree errmsg, tree errlen, gfc_start_block (&non_null); if (add_when_allocated) gfc_add_expr_to_block (&non_null, add_when_allocated); - gfc_add_finalizer_call (&non_null, expr, class_container); + gfc_add_finalizer_call (&non_null, expr, class_container, true); if (coarray_dealloc_mode == GFC_CAF_COARRAY_NOCOARRAY || flag_coarray != GFC_FCOARRAY_LIB) { @@ -2135,7 +2139,8 @@ gfc_deallocate_scalar_with_status (tree pointer, tree status, tree label_finish, gfc_start_block (&non_null); /* Free allocatable components. */ - finalizable = gfc_add_finalizer_call (&non_null, expr, class_container); + finalizable = gfc_add_finalizer_call (&non_null, expr, class_container, + true); if (!finalizable && ts.type == BT_DERIVED && ts.u.derived->attr.alloc_comp) { int caf_mode = coarray diff --git a/gcc/fortran/trans.h b/gcc/fortran/trans.h index 349df913ed4..1e3e2b78960 100644 --- a/gcc/fortran/trans.h +++ b/gcc/fortran/trans.h @@ -455,7 +455,8 @@ tree gfc_get_class_from_gfc_expr (gfc_expr *); tree gfc_get_class_from_expr (tree); tree gfc_get_vptr_from_expr (tree); tree gfc_copy_class_to_class (tree, tree, tree, bool); -bool gfc_add_finalizer_call (stmtblock_t *, gfc_expr *, tree = NULL_TREE); +bool gfc_add_finalizer_call (stmtblock_t *, gfc_expr *, tree = NULL_TREE, + bool = false); bool gfc_add_comp_finalizer_call (stmtblock_t *, tree, gfc_component *, bool); void gfc_finalize_tree_expr (gfc_se *, gfc_symbol *, const symbol_attribute &, int); diff --git a/gcc/testsuite/gfortran.dg/finalize_18.f90 b/gcc/testsuite/gfortran.dg/finalize_18.f90 index f3e3863a172..cafd6a647a4 100644 --- a/gcc/testsuite/gfortran.dg/finalize_18.f90 +++ b/gcc/testsuite/gfortran.dg/finalize_18.f90 @@ -39,7 +39,7 @@ end ! { dg-final { scan-tree-dump-times "desc.\[0-9\]+.data = \\(void . restrict\\) y.aa;" 1 "original" } } ! { dg-final { scan-tree-dump-times "desc.\[0-9\]+.data = \\(void . restrict\\) y.cc._data;" 1 "original" } } -! { dg-final { scan-tree-dump-times "__final_m_T \\(&desc.\[0-9\]+, 0, 1\\);" 1 "original" } } +! { dg-final { scan-tree-dump-times "__final_m_T \\(&desc.\[0-9\]+, 0, 1\\);" 2 "original" } } ! { dg-final { scan-tree-dump-times "__final_m_T \\(&y.bb, 0, 1\\);" 1 "original" } } ! { dg-final { scan-tree-dump "y.cc._vptr->_final \\(&desc.\[0-9\]+, (\\(integer\\(kind=8\\)\\) )?y.cc._vptr->_size, 1\\);" "original" } } ! { dg-final { scan-tree-dump "y.dd._vptr->_final \\(&y.dd._data, (\\(integer\\(kind=8\\)\\) )?y.dd._vptr->_size, 1\\);" "original" } } diff --git a/gcc/testsuite/gfortran.dg/pdt_bug9b.f90 b/gcc/testsuite/gfortran.dg/pdt_bug9b.f90 new file mode 100644 index 00000000000..a7b0f0ecf78 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/pdt_bug9b.f90 @@ -0,0 +1,44 @@ +! { dg-do run } +! +! PR 82173 +! +module m + type t(n) + integer, len :: n + integer :: a(n) + character(n) :: s + contains + final :: fin + end type + type, extends(t) :: e + integer :: b(n) + end type + integer :: nfin = 0 +contains + subroutine fin (x) + type(t(*)), intent(inout) :: x + nfin = nfin + 1 + end subroutine + subroutine s_out (x) + type(t(3)), intent(out) :: x + x%a = 7 + x%s = 'abc' + end subroutine + subroutine s_out_ext (x) + type(e(3)), intent(out) :: x + x%a = 5 + x%b = 6 + end subroutine +end module +program p + use m + type(t(3)) :: y + type(e(3)) :: z + y%a = 1 + call s_out (y) + if (any (y%a /= 7) .or. y%s /= 'abc' .or. nfin /= 1) stop 1 + y = t(3)([4,5,6], 'def') + if (any (y%a /= [4,5,6]) .or. y%s /= 'def') stop 2 + call s_out_ext (z) + if (any (z%a /= 5) .or. any (z%b /= 6)) stop 3 +end program -- 2.56.0
