aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Merrill <jason@redhat.com>2022-08-24 16:28:32 -0400
committerJason Merrill <jason@redhat.com>2022-08-25 17:03:10 -0400
commit072d3115c0e297d91c133c4214b357b0a50557b9 (patch)
tree56aee593ee7bddd43a1bb5437d74dd52235fc1ea
parent30e160475489867a09ed89532cae135b5849cf98 (diff)
downloadgcc-072d3115c0e297d91c133c4214b357b0a50557b9.zip
gcc-072d3115c0e297d91c133c4214b357b0a50557b9.tar.gz
gcc-072d3115c0e297d91c133c4214b357b0a50557b9.tar.bz2
c++: block copy elision in delegating ctor
CWG2403 deals with the issue that copy elision is not possible when the initialized object is a potentially-overlapping subobject and the initializer is a function that returns by value. Jonathan pointed out that this also affects delegating constructors, which might be used to construct a base subobject. gcc/cp/ChangeLog: * call.cc (unsafe_return_slot_p): Return 2 for *this in a constructor. gcc/testsuite/ChangeLog: * g++.dg/init/elide8.C: New test.
-rw-r--r--gcc/cp/call.cc7
-rw-r--r--gcc/testsuite/g++.dg/init/elide8.C11
2 files changed, 18 insertions, 0 deletions
diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
index 370137e..d107a28 100644
--- a/gcc/cp/call.cc
+++ b/gcc/cp/call.cc
@@ -9022,6 +9022,13 @@ unsafe_return_slot_p (tree t)
if (is_empty_base_ref (t))
return 2;
+ /* A delegating constructor might be used to initialize a base. */
+ if (current_function_decl
+ && DECL_CONSTRUCTOR_P (current_function_decl)
+ && (t == current_class_ref
+ || tree_strip_nop_conversions (t) == current_class_ptr))
+ return 2;
+
STRIP_NOPS (t);
if (TREE_CODE (t) == ADDR_EXPR)
t = TREE_OPERAND (t, 0);
diff --git a/gcc/testsuite/g++.dg/init/elide8.C b/gcc/testsuite/g++.dg/init/elide8.C
new file mode 100644
index 0000000..31f899b
--- /dev/null
+++ b/gcc/testsuite/g++.dg/init/elide8.C
@@ -0,0 +1,11 @@
+// CWG 2403 case 3: we can't elide this copy because the delegating constructor
+// might be used to initialize a base.
+// { dg-do compile { target c++11 } }
+
+struct Noncopyable {
+ Noncopyable() = default;
+ Noncopyable(const Noncopyable &) = delete;
+ Noncopyable(int) : Noncopyable(make()) {} // { dg-error "deleted" }
+
+ static Noncopyable make();
+};