]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: NRV and ref-extended temps [PR101442]
authorJason Merrill <jason@redhat.com>
Wed, 13 Apr 2022 17:23:08 +0000 (13:23 -0400)
committerJason Merrill <jason@redhat.com>
Thu, 14 Apr 2022 00:22:57 +0000 (20:22 -0400)
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.

gcc/cp/decl.cc
gcc/cp/init.cc
gcc/cp/semantics.cc
gcc/testsuite/g++.dg/cpp0x/initlist-nrv1.C [new file with mode: 0644]

index 5f59612fb0015dba9fee2cb1e02b4c68bd44c0ed..d51fd75b0032c28c937744bb996a372caa06d89e 100644 (file)
@@ -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);
        }
index 7ce8d3a46e5b0156a766d0e80a7de5a7f1cfad94..75ab965a2184932a5999ed9c74679ba08d5f644d 100644 (file)
@@ -1066,7 +1066,7 @@ perform_member_init (tree member, tree init, hash_set<tree> &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))))
index 43627ed30afcb2bcab2e5a8094738f9124aacd1f..f5ec808b1bc1882ce31788e24d0b1a01af64c626 100644 (file)
@@ -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 (file)
index 0000000..e44dbec
--- /dev/null
@@ -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();
+}