diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc index d2586fad86b12da82863c4cfca5d76a1e5b980f9..dbd37d47cbf584329399e9fe3b9f1cb12206e21c 100644 --- a/gcc/cp/init.cc +++ b/gcc/cp/init.cc @@ -934,6 +934,33 @@ find_uninit_fields (tree *t, hash_set<tree> *uninitialized, tree member) } } +/* Return true if it's OK to initialize an array TYPE from INIT. Mere mortals + can't copy arrays, but the compiler can do so with a VEC_INIT_EXPR in + certain cases. */ + +static bool +can_init_array_with_p (tree type, tree init) +{ + if (!init) + /* Value-init, OK. */ + return true; + if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))) + return false; + /* We're called from synthesize_method, and we're processing the + mem-initializers of a constructor. */ + if (DECL_DEFAULTED_FN (current_function_decl)) + return true; + /* As an extension, we allow copying from a compound literal. */ + if (TREE_CODE (init) == TARGET_EXPR) + { + init = TARGET_EXPR_INITIAL (init); + if (TREE_CODE (init) == CONSTRUCTOR) + return CONSTRUCTOR_C99_COMPOUND_LITERAL (init); + } + + return false; +} + /* Initialize MEMBER, a FIELD_DECL, with INIT, a TREE_LIST of arguments. If TREE_LIST is void_type_node, an empty initializer list was given; if NULL_TREE no initializer was given. UNINITIALIZED @@ -1087,9 +1114,7 @@ perform_member_init (tree member, tree init, hash_set<tree> &uninitialized) { if (TREE_CODE (type) == ARRAY_TYPE) { - if (init == NULL_TREE - || same_type_ignoring_top_level_qualifiers_p (type, - TREE_TYPE (init))) + if (can_init_array_with_p (type, init)) { if (TYPE_DOMAIN (type) && TYPE_MAX_VALUE (TYPE_DOMAIN (type))) { diff --git a/gcc/testsuite/g++.dg/init/array62.C b/gcc/testsuite/g++.dg/init/array62.C new file mode 100644 index 0000000000000000000000000000000000000000..2a786a36e4e8cd3e77b8e1a01bb988f8fc78deb2 --- /dev/null +++ b/gcc/testsuite/g++.dg/init/array62.C @@ -0,0 +1,19 @@ +// PR c++/59465 +// { dg-do compile } + +struct string {} a[1]; +struct pair { + string s[1]; + pair() : s(a) {} // { dg-error "invalid initializer for array member" } +}; + +struct S { + char s[10]; + S() : s("aaa") {} +}; + +void +g () +{ + string x[1](a); // { dg-error "array must be initialized" } +} diff --git a/gcc/testsuite/g++.dg/init/array63.C b/gcc/testsuite/g++.dg/init/array63.C new file mode 100644 index 0000000000000000000000000000000000000000..57e9805616804e4838acdfb11d022a8a6825018e --- /dev/null +++ b/gcc/testsuite/g++.dg/init/array63.C @@ -0,0 +1,13 @@ +// PR c++/59465 +// { dg-do compile } + +struct I { + const bool b; +}; +struct O { + I a[2]; + static I const data[2]; + O() : a(data){} // { dg-error "invalid initializer for array member" } +}; + +I const O::data[2] = {true, false}; diff --git a/gcc/testsuite/g++.dg/init/array64.C b/gcc/testsuite/g++.dg/init/array64.C new file mode 100644 index 0000000000000000000000000000000000000000..e0afdfab39a59678fa63bd9dd180ac382f0c2d6a --- /dev/null +++ b/gcc/testsuite/g++.dg/init/array64.C @@ -0,0 +1,22 @@ +// PR c++/59465 +// { dg-do compile } + +static const int my_size = 10; + +class UserType +{ +public: + UserType(): f_(){} +private: +int f_; +}; + +typedef UserType Array[my_size]; + +class Foo +{ +public: + Foo(Array& m) : m_(m) {}; // { dg-error "invalid initializer for array member" } +private: + Array m_; +};