From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2122) id 8835C3858280; Fri, 23 Jun 2023 16:43:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8835C3858280 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1687538611; bh=w8YI+TileZYsHzn55nKSFkGh1t95dRtJtQN0fKgr41I=; h=From:To:Subject:Date:From; b=ubF6hbjc9XP4QIydfCkrxyIZhtb9QVeeYoN0O6XutJ7wpGByfLbxgiMcd4qbwevFA O0XIQvFu67+HfCMYlt+qmnlvn20b1t4TJZKNWiUYoZHRsX0gnZ9PuweT6ZTsIhKPHE 4dJ/sjZHApipGN8QyLXG1Bi4p6DmtM20Ngm4f9E0= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jason Merrill To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-7471] c++: init-list of uncopyable type [PR110102] X-Act-Checkin: gcc X-Git-Author: Jason Merrill X-Git-Refname: refs/heads/releases/gcc-13 X-Git-Oldrev: dc7f1bfbe5999e4639cf3f3afe70043b49352fdf X-Git-Newrev: be1e122bd20c17aa0b57fc40cbd64f9e9a889aa2 Message-Id: <20230623164331.8835C3858280@sourceware.org> Date: Fri, 23 Jun 2023 16:43:31 +0000 (GMT) List-Id: https://gcc.gnu.org/g:be1e122bd20c17aa0b57fc40cbd64f9e9a889aa2 commit r13-7471-gbe1e122bd20c17aa0b57fc40cbd64f9e9a889aa2 Author: Jason Merrill Date: Thu Jun 8 16:21:38 2023 -0400 c++: init-list of uncopyable type [PR110102] 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. Diff: --- gcc/cp/call.cc | 8 ++++++++ gcc/testsuite/g++.dg/cpp0x/initlist-opt1.C | 15 +++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index 867d7a5bee6..6dc925ecc5b 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -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 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 + +struct A { + A(int) {} + A(const A&) = delete; // { dg-message "declared here" } + A(A&&) {} +}; +int main() { + std::list v = {1,2,3}; +}