]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: init-list of uncopyable type [PR110102]
authorJason Merrill <jason@redhat.com>
Thu, 8 Jun 2023 20:21:38 +0000 (16:21 -0400)
committerJason Merrill <jason@redhat.com>
Fri, 23 Jun 2023 14:49:10 +0000 (10:49 -0400)
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
gcc/testsuite/g++.dg/cpp0x/initlist-opt1.C [new file with mode: 0644]

index 867d7a5bee68c49c689d4f9972113caec9985cc1..6dc925ecc5b9082bb158fba198f4b6f384ba7363 100644 (file)
@@ -4271,6 +4271,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));
   return 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 (file)
index 0000000..56de4bc
--- /dev/null
@@ -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};
+}