From: Jason Merrill Date: Wed, 13 Apr 2022 17:23:08 +0000 (-0400) Subject: c++: NRV and ref-extended temps [PR101442] X-Git-Tag: releases/gcc-10.4.0~135 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=54d1992376d7880a88778dab20a7cfe8cde32bc3;p=thirdparty%2Fgcc.git 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.c (cp_finish_decl): Don't pass decl to push_cleanup. * init.c (perform_member_init): Likewise. * semantics.c (push_cleanup): Adjust comment. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/initlist-nrv1.C: New test. --- diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 99a4eb59ba8d..6089cd7e52df 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -7993,7 +7993,7 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p, { unsigned i; tree t; FOR_EACH_VEC_ELT (*cleanups, i, t) - push_cleanup (decl, t, false); + push_cleanup (NULL_TREE, t, false); release_tree_vector (cleanups); } diff --git a/gcc/cp/init.c b/gcc/cp/init.c index 35c142be60fd..fb62bfe7294d 100644 --- a/gcc/cp/init.c +++ b/gcc/cp/init.c @@ -888,7 +888,7 @@ perform_member_init (tree member, tree init) 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.c b/gcc/cp/semantics.c index 315f89115009..d846a1bd8f0b 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -476,7 +476,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 000000000000..e44dbecfece4 --- /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(); +}