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

commit r13-9160-ge4276844d09f648ba010a890ce7a5bdce17abc41
Author: Paul Thomas <pa...@gcc.gnu.org>
Date:   Sat Dec 16 13:59:45 2023 +0000

    Fortran: Prevent unwanted finalization with -w option [PR112459]
    
    2023-12-16  Paul Thomas  <pa...@gcc.gnu.org>
    
    gcc/fortran
            PR fortran/112459
            * trans-array.cc (gfc_trans_array_constructor_value): Replace
            gfc_notification_std with explicit logical expression that
            selects F2003/2008 and excludes -std=default/gnu.
            * trans-expr.cc (gfc_conv_expr): Ditto.
    
    gcc/testsuite/
            PR fortran/112459
            * gfortran.dg/pr112459.f90: New test.
    
    (cherry picked from commit 9a1105b770df9a9b485705398abbb74b5c487a25)

Diff:
---
 gcc/fortran/trans-array.cc             |  4 +++-
 gcc/fortran/trans-expr.cc              |  4 +++-
 gcc/testsuite/gfortran.dg/pr112459.f90 | 37 ++++++++++++++++++++++++++++++++++
 3 files changed, 43 insertions(+), 2 deletions(-)

diff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc
index eecb342f32af..fa432505c254 100644
--- a/gcc/fortran/trans-array.cc
+++ b/gcc/fortran/trans-array.cc
@@ -2310,7 +2310,9 @@ gfc_trans_array_constructor_value (stmtblock_t * pblock,
      Corrigenda 1 TO 4 for fortran 2008 (f08/0011).
 
      Transmit finalization of this constructor through 'finalblock'. */
-  if (!gfc_notification_std (GFC_STD_F2018_DEL) && finalblock != NULL
+  if ((gfc_option.allow_std & (GFC_STD_F2008 | GFC_STD_F2003))
+      && !(gfc_option.allow_std & GFC_STD_GNU)
+      && finalblock != NULL
       && gfc_may_be_finalized (ts)
       && ctr > 0 && desc != NULL_TREE
       && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc
index 7e3c38e5f92d..46348d7df456 100644
--- a/gcc/fortran/trans-expr.cc
+++ b/gcc/fortran/trans-expr.cc
@@ -9561,7 +9561,9 @@ gfc_conv_expr (gfc_se * se, gfc_expr * expr)
         executable construct containing the reference. This, in fact,
         was later deleted by the Combined Techical Corrigenda 1 TO 4 for
         fortran 2008 (f08/0011).  */
-      if (!gfc_notification_std (GFC_STD_F2018_DEL) && expr->must_finalize
+      if ((gfc_option.allow_std & (GFC_STD_F2008 | GFC_STD_F2003))
+         && !(gfc_option.allow_std & GFC_STD_GNU)
+         && expr->must_finalize
          && gfc_may_be_finalized (expr->ts))
        {
          gfc_warning (0, "The structure constructor at %C has been"
diff --git a/gcc/testsuite/gfortran.dg/pr112459.f90 
b/gcc/testsuite/gfortran.dg/pr112459.f90
new file mode 100644
index 000000000000..7db243c224a2
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr112459.f90
@@ -0,0 +1,37 @@
+! { dg-do compile }
+! { dg-options "-w -fdump-tree-original" }
+!
+! Contributed by Sebastian Bardeau  <bard...@iram.fr>
+!
+module mymod
+  type mysubtype
+    integer(kind=4), allocatable :: a(:)
+  end type mysubtype
+  type :: mytype
+    integer :: i
+    type(mysubtype) :: sub
+  contains
+    final :: mytype_final
+  end type mytype
+contains
+  subroutine mysubtype_final(sub)
+    type(mysubtype), intent(inout) :: sub
+    print *,'MYSUBTYPE>FINAL'
+    if (allocated(sub%a)) deallocate(sub%a)
+  end subroutine mysubtype_final
+  subroutine mytype_final(typ)
+    type(mytype), intent(inout) :: typ
+    print *,"MYTYPE>FINAL"
+    call mysubtype_final(typ%sub)
+  end subroutine mytype_final
+end module mymod
+!
+program myprog
+  use mymod
+  type(mytype), pointer :: c
+  print *,"Before allocation"
+  allocate(c)
+  print *,"After allocation"
+end program myprog
+! Final subroutines were called with std=gnu and -w = > 14 "_final"s.
+! { dg-final { scan-tree-dump-times "_final" 12 "original" } }

Reply via email to