From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2122) id 9ACD03857836; Thu, 14 Apr 2022 00:24:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9ACD03857836 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-8150] c++: NRV and ref-extended temps [PR101442] X-Act-Checkin: gcc X-Git-Author: Jason Merrill X-Git-Refname: refs/heads/master X-Git-Oldrev: 019d6d4149ee97d55ce9efe4e5e470d38130cdeb X-Git-Newrev: ad8161e6d7b26d690d90069ae9a129e7ac36892a Message-Id: <20220414002400.9ACD03857836@sourceware.org> Date: Thu, 14 Apr 2022 00:24:00 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 14 Apr 2022 00:24:00 -0000 https://gcc.gnu.org/g:ad8161e6d7b26d690d90069ae9a129e7ac36892a commit r12-8150-gad8161e6d7b26d690d90069ae9a129e7ac36892a Author: Jason Merrill Date: Wed Apr 13 13:23:08 2022 -0400 c++: NRV and ref-extended temps [PR101442] This issue goes back to r83221, where the cleanup for extended ref temps changed from being unconditional to being tied to the declaration they formed part of the initializer for. The named return value optimization changes the cleanup for the NRV variable to only run on the EH path; we don't want that change to affect temporary cleanups. The perform_member_init change isn't necessary (there 'decl' is a COMPONENT_REF), it's just for consistency. PR c++/101442 gcc/cp/ChangeLog: * decl.cc (cp_finish_decl): Don't pass decl to push_cleanup. * init.cc (perform_member_init): Likewise. * semantics.cc (push_cleanup): Adjust comment. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/initlist-nrv1.C: New test. Diff: --- gcc/cp/decl.cc | 2 +- gcc/cp/init.cc | 2 +- gcc/cp/semantics.cc | 3 ++- gcc/testsuite/g++.dg/cpp0x/initlist-nrv1.C | 34 ++++++++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 3 deletions(-) diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 5f59612fb00..d51fd75b003 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -8554,7 +8554,7 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p, { for (tree t : *cleanups) { - push_cleanup (decl, t, false); + push_cleanup (NULL_TREE, t, false); /* As in initialize_local_var. */ wrap_temporary_cleanups (init, t); } diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc index 7ce8d3a46e5..75ab965a218 100644 --- a/gcc/cp/init.cc +++ b/gcc/cp/init.cc @@ -1066,7 +1066,7 @@ perform_member_init (tree member, tree init, hash_set &uninitialized) init = build2 (INIT_EXPR, type, decl, init); finish_expr_stmt (init); FOR_EACH_VEC_ELT (*cleanups, i, t) - push_cleanup (decl, t, false); + push_cleanup (NULL_TREE, t, false); } else if (type_build_ctor_call (type) || (init && CLASS_TYPE_P (strip_array_types (type)))) diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 43627ed30af..f5ec808b1bc 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -666,7 +666,8 @@ do_pushlevel (scope_kind sk) /* Queue a cleanup. CLEANUP is an expression/statement to be executed when the current scope is exited. EH_ONLY is true when this is not - meant to apply to normal control flow transfer. */ + meant to apply to normal control flow transfer. DECL is the VAR_DECL + being cleaned up, if any, or null for temporaries or subobjects. */ void push_cleanup (tree decl, tree cleanup, bool eh_only) diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist-nrv1.C b/gcc/testsuite/g++.dg/cpp0x/initlist-nrv1.C new file mode 100644 index 00000000000..e44dbecfece --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/initlist-nrv1.C @@ -0,0 +1,34 @@ +// PR c++/101442 +// { dg-do run { target c++11 } } + +bool destroyed = false; + +struct A +{ + A() {} + A(const A &) = delete; + A &operator=(const A &) = delete; + ~A() {destroyed = true;} +}; + +struct B +{ + const A &a; + struct string { + string(const char*) { } + ~string() { } + } s; +}; + +B foo() +{ + B ret{ A{}, "" }; + return ret; +} + +int main() +{ + B b = foo(); + if (!destroyed) + __builtin_abort(); +}