aboutsummaryrefslogtreecommitdiff
path: root/gcc/cp/except.cc
diff options
context:
space:
mode:
authorJason Merrill <jason@redhat.com>2023-10-30 17:44:54 -0400
committerJason Merrill <jason@redhat.com>2023-11-02 16:01:38 -0400
commitae07265381d934ee97fb1ce8915731158c91babc (patch)
tree0ea0046259c3306f7c1fdb50be7e043f7df363e6 /gcc/cp/except.cc
parent6afa984f47e16e8bd958646d7407b74e61041f5d (diff)
downloadgcc-ae07265381d934ee97fb1ce8915731158c91babc.zip
gcc-ae07265381d934ee97fb1ce8915731158c91babc.tar.gz
gcc-ae07265381d934ee97fb1ce8915731158c91babc.tar.bz2
c++: retval dtor on rethrow [PR112301]
In r12-6333 for PR33799, I fixed the example in [except.ctor]/2. In that testcase, the exception is caught and the function returns again, successfully. In this testcase, however, the exception is rethrown, and hits two separate cleanups: one in the try block and the other in the function body. So we destroy twice an object that was only constructed once. Fortunately, the fix for the normal case is easy: we just need to clear the "return value constructed by return" flag when we do it the first time. This gets more complicated with the named return value optimization, since we don't want to destroy the return value while the NRV variable is still in scope. PR c++/112301 PR c++/102191 PR c++/33799 gcc/cp/ChangeLog: * except.cc (maybe_splice_retval_cleanup): Clear current_retval_sentinel when destroying retval. * semantics.cc (nrv_data): Add in_nrv_cleanup. (finalize_nrv): Set it. (finalize_nrv_r): Fix handling of throwing cleanups. gcc/testsuite/ChangeLog: * g++.dg/eh/return1.C: Add more cases.
Diffstat (limited to 'gcc/cp/except.cc')
-rw-r--r--gcc/cp/except.cc18
1 files changed, 17 insertions, 1 deletions
diff --git a/gcc/cp/except.cc b/gcc/cp/except.cc
index e32efb3..d966725 100644
--- a/gcc/cp/except.cc
+++ b/gcc/cp/except.cc
@@ -1284,7 +1284,15 @@ build_noexcept_spec (tree expr, tsubst_flags_t complain)
current_retval_sentinel so that we know that the return value needs to be
destroyed on throw. Do the same if the current function might use the
named return value optimization, so we don't destroy it on return.
- Otherwise, returns NULL_TREE. */
+ Otherwise, returns NULL_TREE.
+
+ The sentinel is set to indicate that we're in the process of returning, and
+ therefore should destroy a normal return value on throw, and shouldn't
+ destroy a named return value variable on normal scope exit. It is set on
+ return, and cleared either by maybe_splice_retval_cleanup, or when an
+ exception reaches the NRV scope (finalize_nrv_r). Note that once return
+ passes the NRV scope, it's effectively a normal return value, so cleanup
+ past that point is handled by maybe_splice_retval_cleanup. */
tree
maybe_set_retval_sentinel ()
@@ -1361,6 +1369,14 @@ maybe_splice_retval_cleanup (tree compound_stmt, bool is_try)
tsi_delink (&iter);
}
tree dtor = build_cleanup (retval);
+ if (!function_body)
+ {
+ /* Clear the sentinel so we don't try to destroy the retval again on
+ rethrow (c++/112301). */
+ tree clear = build2 (MODIFY_EXPR, boolean_type_node,
+ current_retval_sentinel, boolean_false_node);
+ dtor = build2 (COMPOUND_EXPR, void_type_node, clear, dtor);
+ }
tree cond = build3 (COND_EXPR, void_type_node, current_retval_sentinel,
dtor, void_node);
tree cleanup = build_stmt (loc, CLEANUP_STMT,