From: Jason Merrill <jason@redhat.com>
To: gcc-patches@gcc.gnu.org
Subject: [pushed] c++: init-list of uncopyable type [PR110102]
Date: Fri, 9 Jun 2023 11:39:43 -0400 [thread overview]
Message-ID: <20230609153943.3386685-1-jason@redhat.com> (raw)
Tested x86_64-pc-linux-gnu, applying to trunk.
-- 8< --
The maybe_init_list_as_range optimization is a form of copy elision, but we
can only elide well-formed copies.
PR c++/110102
gcc/cp/ChangeLog:
* call.cc (maybe_init_list_as_array): Check that the element type is
copyable.
gcc/testsuite/ChangeLog:
* g++.dg/cpp0x/initlist-opt1.C: New test.
---
gcc/cp/call.cc | 8 ++++++++
gcc/testsuite/g++.dg/cpp0x/initlist-opt1.C | 15 +++++++++++++++
2 files changed, 23 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/cpp0x/initlist-opt1.C
diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
index d6154f1a319..354773f00c6 100644
--- a/gcc/cp/call.cc
+++ b/gcc/cp/call.cc
@@ -4272,6 +4272,14 @@ maybe_init_list_as_array (tree elttype, tree init)
if (has_non_trivial_temporaries (first))
return NULL_TREE;
+ /* We can't do this if copying from the initializer_list would be
+ ill-formed. */
+ tree copy_argtypes = make_tree_vec (1);
+ TREE_VEC_ELT (copy_argtypes, 0)
+ = cp_build_qualified_type (elttype, TYPE_QUAL_CONST);
+ if (!is_xible (INIT_EXPR, elttype, copy_argtypes))
+ return NULL_TREE;
+
init_elttype = cp_build_qualified_type (init_elttype, TYPE_QUAL_CONST);
tree arr = build_array_of_n_type (init_elttype, CONSTRUCTOR_NELTS (init));
arr = finish_compound_literal (arr, init, tf_none);
diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist-opt1.C b/gcc/testsuite/g++.dg/cpp0x/initlist-opt1.C
new file mode 100644
index 00000000000..56de4bc0092
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/initlist-opt1.C
@@ -0,0 +1,15 @@
+// PR c++/110102
+// { dg-do compile { target c++11 } }
+
+// { dg-error "deleted|construct_at" "" { target *-*-* } 0 }
+
+#include <list>
+
+struct A {
+ A(int) {}
+ A(const A&) = delete; // { dg-message "declared here" }
+ A(A&&) {}
+};
+int main() {
+ std::list<A> v = {1,2,3};
+}
base-commit: 3e12669a0eb968cfcbe9242b382fd8020935edf8
--
2.31.1
reply other threads:[~2023-06-09 15:39 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=20230609153943.3386685-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).