]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fortran: Fix default init of finalizable derived argus [PR116829]
authorPaul Thomas <pault@gcc.gnu.org>
Fri, 7 Feb 2025 12:46:44 +0000 (12:46 +0000)
committerPaul Thomas <pault@gcc.gnu.org>
Fri, 7 Feb 2025 12:46:56 +0000 (12:46 +0000)
2025-02-07  Tomáš Trnka  <trnka@scm.com>

gcc/fortran
PR fortran/116829
* trans-decl.cc (init_intent_out_dt): Always call
gfc_init_default_dt() for BT_DERIVED to apply s->value if the
symbol isn't allocatable. Also simplify the logic a bit.

gcc/testsuite/
PR fortran/116829
* gfortran.dg/derived_init_7.f90: New test.

gcc/fortran/trans-decl.cc
gcc/testsuite/gfortran.dg/derived_init_7.f90 [new file with mode: 0644]

index 017f184f179421123dcc311ed7a069b75e56c4b7..83f8130afd87f0d70185686ce817568422bef2b9 100644 (file)
@@ -4551,7 +4551,6 @@ init_intent_out_dt (gfc_symbol * proc_sym, gfc_wrapped_block * block)
   tree tmp;
   tree present;
   gfc_symbol *s;
-  bool dealloc_with_value = false;
 
   gfc_init_block (&init);
   for (f = gfc_sym_get_dummy_args (proc_sym); f; f = f->next)
@@ -4582,12 +4581,9 @@ init_intent_out_dt (gfc_symbol * proc_sym, gfc_wrapped_block * block)
           by the caller.  */
        if (tmp == NULL_TREE && !s->attr.allocatable
            && s->ts.u.derived->attr.alloc_comp)
-         {
-           tmp = gfc_deallocate_alloc_comp (s->ts.u.derived,
-                                            s->backend_decl,
-                                            s->as ? s->as->rank : 0);
-           dealloc_with_value = s->value;
-         }
+         tmp = gfc_deallocate_alloc_comp (s->ts.u.derived,
+                                          s->backend_decl,
+                                          s->as ? s->as->rank : 0);
 
        if (tmp != NULL_TREE && (s->attr.optional
                                 || s->ns->proc_name->attr.entry_master))
@@ -4597,14 +4593,9 @@ init_intent_out_dt (gfc_symbol * proc_sym, gfc_wrapped_block * block)
                              present, tmp, build_empty_stmt (input_location));
          }
 
-       if (tmp != NULL_TREE && !dealloc_with_value)
-         gfc_add_expr_to_block (&init, tmp);
-       else if (s->value && !s->attr.allocatable)
-         {
-           gfc_add_expr_to_block (&init, tmp);
-           gfc_init_default_dt (s, &init, false);
-           dealloc_with_value = false;
-         }
+       gfc_add_expr_to_block (&init, tmp);
+       if (s->value && !s->attr.allocatable)
+         gfc_init_default_dt (s, &init, false);
       }
     else if (f->sym && f->sym->attr.intent == INTENT_OUT
             && f->sym->ts.type == BT_CLASS
diff --git a/gcc/testsuite/gfortran.dg/derived_init_7.f90 b/gcc/testsuite/gfortran.dg/derived_init_7.f90
new file mode 100644 (file)
index 0000000..f145385
--- /dev/null
@@ -0,0 +1,58 @@
+! { dg-do run }
+! Check that finalizable intent(out) dummy arguments are first finalized
+! and then correctly default-initialized (PR116829)
+!
+! Contributed by Tomas Trnka  <trnka@scm.com>
+!
+module FinalizableIntentOutTestModule
+   implicit none
+
+   type :: AapType
+      integer  :: i = 0
+   contains
+      final    :: Finalizer
+   end type
+   integer :: ctr = 0
+   logical :: err1 = .false.
+   logical :: err2 = .false.
+contains
+
+   subroutine Finalizer(self)
+      type(AapType), intent(inout) :: self
+
+      ! Fail if Finalizer gets called again on an already finalized object
+      if (self%i == 42) err1 = .true.
+
+      self%i = 42 ! Nobody should ever see this value after finalization
+      ctr = ctr + 1
+   end subroutine
+
+end module
+
+
+program test
+   use FinalizableIntentOutTestModule
+
+   implicit none
+
+   type(AapType) :: aap
+
+   ! Set "i" to nonzero so that initialization in MakeAap has something to do
+   aap%i = 1
+
+   call MakeAap(aap)
+   
+   if (err1) stop 1
+   if (err2) stop 2      ! This was failing
+   if (ctr /= 1) stop 3  ! Belt and braces to ensure number of final calls correct.
+
+contains
+
+   subroutine MakeAap(a)
+      type(AapType), intent(out) :: a
+
+      ! Fail if "a" wasn't initialized properly
+      if (a%i /= 0)  err2 = .true.
+   end subroutine
+
+end program