public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jason Merrill <jason@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r9-10173] c++: NRV and ref-extended temps [PR101442]
Date: Fri, 13 May 2022 17:41:48 +0000 (GMT)	[thread overview]
Message-ID: <20220513174148.B3522395B43C@sourceware.org> (raw)

https://gcc.gnu.org/g:48b8d5e028abee8869de946bbd7d24d746655d88

commit r9-10173-g48b8d5e028abee8869de946bbd7d24d746655d88
Author: Jason Merrill <jason@redhat.com>
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.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:
---
 gcc/cp/decl.c                              |  2 +-
 gcc/cp/init.c                              |  2 +-
 gcc/cp/semantics.c                         |  3 ++-
 gcc/testsuite/g++.dg/cpp0x/initlist-nrv1.C | 34 ++++++++++++++++++++++++++++++
 4 files changed, 38 insertions(+), 3 deletions(-)

diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 7fb669fd48f..bf732ba65cd 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -7442,7 +7442,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 dc9145892c4..8e60b947397 100644
--- a/gcc/cp/init.c
+++ b/gcc/cp/init.c
@@ -878,7 +878,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);
       release_tree_vector (cleanups);
     }
   else if (type_build_ctor_call (type)
diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c
index 596dc4d6447..579bfcbdc5c 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 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();
+}


                 reply	other threads:[~2022-05-13 17:41 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220513174148.B3522395B43C@sourceware.org \
    --to=jason@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).