* [pushed] c++: throwing cleanup after return [PR113347]
@ 2024-01-24 19:37 Jason Merrill
0 siblings, 0 replies; only message in thread
From: Jason Merrill @ 2024-01-24 19:37 UTC (permalink / raw)
To: gcc-patches
Tested x86_64-pc-linux-gnu, applying to 12/13.
-- 8< --
Here we were assuming that current_retval_sentinel would be set if we have
seen a throwing cleanup, but that's not the case if the cleanup is after all
returns.
This change isn't needed on trunk, where current_retval_sentinel is set for
all NRV functions.
PR c++/113347
gcc/cp/ChangeLog:
* semantics.cc (finalize_nrv_r): Handle null
current_retval_sentinel.
gcc/testsuite/ChangeLog:
* g++.dg/eh/return3.C: New test.
---
gcc/cp/semantics.cc | 3 ++-
gcc/testsuite/g++.dg/eh/return3.C | 17 +++++++++++++++++
2 files changed, 19 insertions(+), 1 deletion(-)
create mode 100644 gcc/testsuite/g++.dg/eh/return3.C
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 02838775003..ff5252905fe 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -4953,7 +4953,8 @@ finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
/* If a cleanup might throw, we need to clear current_retval_sentinel on
the exception path so an outer cleanup added by
maybe_splice_retval_cleanup doesn't run. */
- if (cp_function_chain->throwing_cleanup)
+ if (current_retval_sentinel
+ && cp_function_chain->throwing_cleanup)
{
tree clear = build2 (MODIFY_EXPR, boolean_type_node,
current_retval_sentinel,
diff --git a/gcc/testsuite/g++.dg/eh/return3.C b/gcc/testsuite/g++.dg/eh/return3.C
new file mode 100644
index 00000000000..76aa50d523d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/eh/return3.C
@@ -0,0 +1,17 @@
+// PR c++/113347
+
+#if __cplusplus < 201103L
+#define THROWS
+#else
+#define THROWS noexcept(false)
+#endif
+
+struct A { ~A(); };
+struct B { ~B() THROWS; };
+
+A f()
+{
+ A a;
+ return a;
+ B();
+}
base-commit: 46874833290ee5fcd94c6e184fcd51fcd2c74e28
--
2.39.3
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2024-01-24 19:37 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-24 19:37 [pushed] c++: throwing cleanup after return [PR113347] Jason Merrill
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).