https://gcc.gnu.org/g:d93190f056f9725a614c731519aef5bbe4afd53c

commit r17-6422-gd93190f056f9725a614c731519aef5bbe4afd53c
Author: Jerry DeLisle <[email protected]>
Date:   Wed Nov 18 09:54:41 2026 -0800

    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
    
    2026-11-18  Jerry Delisle  <[email protected]>
    
    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.

Diff:
---
 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(-)

diff --git a/gcc/fortran/class.cc b/gcc/fortran/class.cc
index ca0e7784718a..ccd899abd763 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 ff3bea5e1be8..642162b54c5c 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 3487caf5c0e0..4237cd166bab 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 f3e3863a1725..cafd6a647a43 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 000000000000..a7b0f0ecf780
--- /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

Reply via email to