]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fortran: Alloc comp of non-finalizable type not finalized [PR111674]
authorPaul Thomas <pault@gcc.gnu.org>
Wed, 4 Oct 2023 07:26:35 +0000 (08:26 +0100)
committerPaul Thomas <pault@gcc.gnu.org>
Wed, 4 Oct 2023 07:26:35 +0000 (08:26 +0100)
2023-10-04  Paul Thomas  <pault@gcc.gnu.org>

gcc/fortran
PR fortran/37336
PR fortran/111674
* trans-expr.cc (gfc_trans_scalar_assign): Finalize components
on deallocation if derived type is not finalizable.

gcc/testsuite/
PR fortran/37336
PR fortran/111674
* gfortran.dg/allocate_with_source_25.f90: Final count in tree
dump reverts from 4 to original 6.
* gfortran.dg/finalize_38.f90: Add test for fix of PR111674.

gcc/fortran/trans-expr.cc
gcc/testsuite/gfortran.dg/allocate_with_source_25.f90
gcc/testsuite/gfortran.dg/finalize_38.f90

index cca2f4e186275447c2e9d8fdda5202540288374d..860b73c496839b9b50335ce9f12520b1b1d10519 100644 (file)
@@ -10723,7 +10723,7 @@ gfc_trans_scalar_assign (gfc_se * lse, gfc_se * rse, gfc_typespec ts,
        {
          tmp_var = gfc_evaluate_now (lse->expr, &lse->pre);
          tmp = gfc_deallocate_alloc_comp_no_caf (ts.u.derived, tmp_var,
-                                                 0, true);
+                                                 0, gfc_may_be_finalized (ts));
          if (deep_copy)
            tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
                            tmp);
index de20a147842cbb181730b7b60650b81ad501acb8..92dc50756d4946554c0b0b9f964a32e5a3339278 100644 (file)
@@ -68,4 +68,4 @@ contains
   end function func_foo_a
 
 end program simple_leak
-! { dg-final { scan-tree-dump-times "\>_final" 4 "original" } }
+! { dg-final { scan-tree-dump-times "\>_final" 6 "original" } }
index f4b00a16a5404109d1fde8c74693df9a38740156..85334890035f0bade512389769ee9e87de16a9eb 100644 (file)
@@ -4,6 +4,8 @@
 ! With -std=gnu, no finalization of array or structure constructors should occur.
 ! See finalize_38a.f90 for the result with f2008.
 ! Tests fix for PR64290 as well.
+! Extended to test that nonfinalizable types with allocatable finalizable components
+! are finalized before deallocation (PR111674).
 !
 module testmode
   implicit none
@@ -20,6 +22,10 @@ module testmode
     final :: destructor3, destructor4
   end type complicated
 
+  type :: notfinalizable
+    type(simple), allocatable :: aa
+  end type
+
   integer :: check_scalar
   integer :: check_array(4)
   real :: check_real
@@ -114,6 +120,7 @@ program test_final
   type(simple), allocatable :: MyType, MyType2
   type(simple), allocatable :: MyTypeArray(:)
   type(simple) :: ThyType = simple(21), ThyType2 = simple(22)
+  type(notfinalizable) :: MyNf
   class(simple), allocatable :: MyClass
   class(simple), allocatable :: MyClassArray(:)
 
@@ -214,6 +221,15 @@ program test_final
   deallocate (MyClassArray)
   call test(2, 0, [10, 20], 170, rarray = [10.0,20.0])
 
+!******************
+! Test for PR111674
+!******************
+  final_count = 0
+  MyNf = notfinalizable (simple (42)) ! Allocatable component not finalized
+  if (final_count .ne. 0) stop 171
+  MyNf = notfinalizable (simple (84)) ! Component finalized before deallocation
+  call test(1, 42, [0,0], 180)
+
 ! Clean up for valgrind testing
   if (allocated (MyType)) deallocate (MyType)
   if (allocated (MyType2)) deallocate (MyType2)