From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2122) id 6171F3858420; Wed, 24 Jan 2024 19:40:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6171F3858420 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1706125256; bh=8on4qapVgt0vw510u6mNHwW0nU5/g/VYYCHpAN1T6Ts=; h=From:To:Subject:Date:From; b=KM3HTP9BMciCRTTU9eCbpC9KSl63CNrCZd+pohclLr2RlwK8iPB/aiRriAOBOPaI1 sfLjistbQEtPfWXiGWtKMwjRGeB4ADyXhPnVfXeyc42Jnzbd+1Sz5eQ5hAt3mvnvr2 fCRdzSIu+Eq67ZSm4Tl3t1K4gCbb/J2PscACfsyw= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jason Merrill To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-10112] c++: throwing cleanup after return [PR113347] X-Act-Checkin: gcc X-Git-Author: Jason Merrill X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: 0da4402bd4b350691b2301003de62268f865d91c X-Git-Newrev: 050fc885a6e324a03a6397d86134ab4b68aac944 Message-Id: <20240124194056.6171F3858420@sourceware.org> Date: Wed, 24 Jan 2024 19:40:56 +0000 (GMT) List-Id: https://gcc.gnu.org/g:050fc885a6e324a03a6397d86134ab4b68aac944 commit r12-10112-g050fc885a6e324a03a6397d86134ab4b68aac944 Author: Jason Merrill Date: Tue Jan 23 15:41:09 2024 -0500 c++: throwing cleanup after return [PR113347] 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. (cherry picked from commit 713fbaff8b17a01d3b72110f89112851ed43a90a) Diff: --- gcc/cp/semantics.cc | 3 ++- gcc/testsuite/g++.dg/eh/return3.C | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index c42402c7c14..e5db5189f33 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -4887,7 +4887,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(); +}