From: Jason Merrill <jason@redhat.com>
To: gcc-patches@gcc.gnu.org
Subject: [pushed] c++: block copy elision in delegating ctor
Date: Thu, 25 Aug 2022 17:03:41 -0400 [thread overview]
Message-ID: <20220825210341.2217843-1-jason@redhat.com> (raw)
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.
Tested x86_64-pc-linux-gnu, applying to trunk.
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.
---
gcc/cp/call.cc | 7 +++++++
gcc/testsuite/g++.dg/init/elide8.C | 11 +++++++++++
2 files changed, 18 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/init/elide8.C
diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
index 370137ebd6d..d107a2814dc 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 00000000000..31f899b6623
--- /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();
+};
base-commit: 30e160475489867a09ed89532cae135b5849cf98
--
2.31.1
reply other threads:[~2022-08-25 21:03 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=20220825210341.2217843-1-jason@redhat.com \
--to=jason@redhat.com \
--cc=gcc-patches@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).