c++: retval dtor on rethrow [PR112301]
commitae07265381d934ee97fb1ce8915731158c91babc
authorJason Merrill <jason@redhat.com>
Mon, 30 Oct 2023 21:44:54 +0000 (30 17:44 -0400)
committerJason Merrill <jason@redhat.com>
Thu, 2 Nov 2023 20:01:38 +0000 (2 16:01 -0400)
tree0ea0046259c3306f7c1fdb50be7e043f7df363e6
parent6afa984f47e16e8bd958646d7407b74e61041f5d
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.
gcc/cp/except.cc
gcc/cp/semantics.cc
gcc/testsuite/g++.dg/eh/return1.C