From: Paul Richard Thomas <paul.richard.thomas@gmail.com>
To: "fortran@gcc.gnu.org" <fortran@gcc.gnu.org>,
gcc-patches <gcc-patches@gcc.gnu.org>
Subject: [Patch, fortran] PR112459 - gfortran -w option causes derived-type finalization at creation time
Date: Sat, 11 Nov 2023 10:15:45 +0000 [thread overview]
Message-ID: <CAGkQGiJ9KDmOZqgHH9eM1ytPGTtjG_-F+ekP0f5w46OjcEZNkw@mail.gmail.com> (raw)
[-- Attachment #1.1: Type: text/plain, Size: 660 bytes --]
Hi All,
Evidently -w causes gfc_option.allow_std to be set to default, which allows
anything and everything to happen, including these f2003/8 finalizations.
The fix is trivial.
Regtests fine - OK for mainline and -13 branch?
Paul
Fortran: Prevent unwanted finalization with -w option [PR112459]
2023-11-11 Paul Thomas <pault@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-array.cc (gfc_conv_expr): Ditto.
gcc/testsuite/
PR fortran/112459
* gfortran.dg/pr112459.f90: New test.
[-- Attachment #2: pr112459.f90 --]
[-- Type: text/x-fortran, Size: 1047 bytes --]
! { dg-do compile }
! { dg-options "-w -fdump-tree-original" }
!
! Correct unexpected finalization with -std=default/gnu and -w
!
! Contributed by Sebastian Bardeau <bardeau@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" } }
[-- Attachment #3: submit.diff --]
[-- Type: text/x-patch, Size: 1443 bytes --]
diff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc
index bbb81f40aa9..ef54a20dafd 100644
--- a/gcc/fortran/trans-array.cc
+++ b/gcc/fortran/trans-array.cc
@@ -2311,7 +2311,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 1b8be081a17..a69d7c7114d 100644
--- a/gcc/fortran/trans-expr.cc
+++ b/gcc/fortran/trans-expr.cc
@@ -9698,7 +9698,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"
next reply other threads:[~2023-11-11 10:15 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-11 10:15 Paul Richard Thomas [this message]
2023-11-11 19:10 ` Harald Anlauf
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAGkQGiJ9KDmOZqgHH9eM1ytPGTtjG_-F+ekP0f5w46OjcEZNkw@mail.gmail.com \
--to=paul.richard.thomas@gmail.com \
--cc=fortran@gcc.gnu.org \
--cc=gcc-patches@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).