From: Jason Merrill <jason@redhat.com>
To: gcc-patches@gcc.gnu.org
Subject: [pushed] c++: NRV and goto [PR92407]
Date: Tue, 6 Jun 2023 21:31:16 -0400 [thread overview]
Message-ID: <20230607013116.2770869-1-jason@redhat.com> (raw)
Tested x86_64-pc-linux-gnu, applying to trunk.
-- 8< --
Here our named return value optimization was breaking the required
destructor when the goto takes 'a' out of scope. The simplest fix is to
disable the optimization in the presence of user labels.
We could do better by disabling the optimization only if there is a backward
goto across the variable declaration, but we don't currently track that.
PR c++/92407
gcc/cp/ChangeLog:
* typeck.cc (check_return_expr): Prevent NRV in the presence of
named labels.
gcc/testsuite/ChangeLog:
* g++.dg/opt/nrv22.C: New test.
---
gcc/cp/typeck.cc | 3 +++
gcc/testsuite/g++.dg/opt/nrv22.C | 30 ++++++++++++++++++++++++++++++
2 files changed, 33 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/opt/nrv22.C
diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc
index 11fcc7fcd3b..6618c6a2021 100644
--- a/gcc/cp/typeck.cc
+++ b/gcc/cp/typeck.cc
@@ -11155,6 +11155,9 @@ check_return_expr (tree retval, bool *no_warning)
if (fn_returns_value_p && flag_elide_constructors)
{
if (named_return_value_okay_p
+ /* The current NRV implementation breaks if a backward goto needs to
+ destroy the object (PR92407). */
+ && !cp_function_chain->x_named_labels
&& (current_function_return_value == NULL_TREE
|| current_function_return_value == bare_retval))
current_function_return_value = bare_retval;
diff --git a/gcc/testsuite/g++.dg/opt/nrv22.C b/gcc/testsuite/g++.dg/opt/nrv22.C
new file mode 100644
index 00000000000..eb889fa615b
--- /dev/null
+++ b/gcc/testsuite/g++.dg/opt/nrv22.C
@@ -0,0 +1,30 @@
+// PR c++/92407
+// { dg-do run }
+
+struct A
+{
+ A () { a++; }
+ A (const A &) { a++; }
+ ~A () { a--; }
+ static int a;
+};
+int A::a = 0;
+
+A
+foo ()
+{
+ int cnt = 10;
+lab:
+ A a;
+ if (cnt--)
+ goto lab;
+ return a;
+}
+
+int
+main ()
+{
+ foo ();
+ if (A::a)
+ __builtin_abort ();
+}
base-commit: 29c82c6ca929e0f5eccfe038dea71177d814c6b7
prerequisite-patch-id: aed53cdac161144c31cb1433282e1ad1d49d3770
prerequisite-patch-id: 1098cb4457a5eff90fa8176f9b0d8d2e9477596e
prerequisite-patch-id: 823f2ce422455c6c7ccbaa9938b670a600b376df
--
2.31.1
reply other threads:[~2023-06-07 1:36 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20230607013116.2770869-1-jason@redhat.com \
--to=jason@redhat.com \
--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).