+2017-12-19 Paolo Carlini <paolo.carlini@oracle.com>
+
+ PR c++/82593
+ * decl.c (check_array_designated_initializer): Not static.
+ * cp-tree.h (check_array_designated_initializer): Declare.
+ * typeck2.c (process_init_constructor_array): Call the latter.
+ * parser.c (cp_parser_initializer_list): Check the return value
+ of require_potential_rvalue_constant_expression.
+
2017-12-19 Martin Sebor <msebor@redhat.com>
PR c++/83394
extern tree finish_case_label (location_t, tree, tree);
extern tree cxx_maybe_build_cleanup (tree, tsubst_flags_t);
+extern bool check_array_designated_initializer (constructor_elt *,
+ unsigned HOST_WIDE_INT);
/* in decl2.c */
extern void record_mangling (tree, bool);
initializer. If it does, an error is issued. Returns true if CE
is valid, i.e., does not have a designated initializer. */
-static bool
+bool
check_array_designated_initializer (constructor_elt *ce,
unsigned HOST_WIDE_INT index)
{
if (!cp_parser_parse_definitely (parser))
designator = NULL_TREE;
- else if (non_const)
- require_potential_rvalue_constant_expression (designator);
+ else if (non_const
+ && (!require_potential_rvalue_constant_expression
+ (designator)))
+ designator = NULL_TREE;
if (designator)
/* Warn the user that they are using an extension. */
pedwarn (loc, OPT_Wpedantic,
FOR_EACH_VEC_SAFE_ELT (v, i, ce)
{
- if (ce->index)
- {
- gcc_assert (TREE_CODE (ce->index) == INTEGER_CST);
- if (compare_tree_int (ce->index, i) != 0)
- {
- ce->value = error_mark_node;
- sorry ("non-trivial designated initializers not supported");
- }
- }
- else
+ if (!ce->index)
ce->index = size_int (i);
+ else if (!check_array_designated_initializer (ce, i))
+ ce->index = error_mark_node;
gcc_assert (ce->value);
ce->value
= massage_init_elt (TREE_TYPE (type), ce->value, nested, complain);
--- /dev/null
+// PR c++/82593
+// { dg-do compile { target c++11 } }
+// { dg-options "" }
+
+enum {
+ INDEX1 = 0,
+ INDEX2
+};
+
+class SomeClass {
+public:
+ SomeClass();
+private:
+ struct { int field; } member[2];
+};
+
+SomeClass::SomeClass()
+ : member({
+ [INDEX1] = { .field = 0 },
+ [INDEX2] = { .field = 1 }
+ })
+{
+}
--- /dev/null
+// PR c++/82593
+// { dg-do compile { target c++11 } }
+// { dg-options "" }
+
+const int INDEX1 = 0;
+const int INDEX2 = 1;
+
+class SomeClass {
+public:
+ SomeClass();
+private:
+ struct { int field; } member[2];
+};
+
+SomeClass::SomeClass()
+ : member({
+ [INDEX1] = { .field = 0 },
+ [INDEX2] = { .field = 1 }
+ })
+{
+}
--- /dev/null
+// PR c++/82593
+// { dg-do compile { target c++11 } }
+// { dg-options "" }
+
+int INDEX1 = 0;
+int INDEX2 = 1;
+
+class SomeClass {
+public:
+ SomeClass();
+private:
+ struct { int field; } member[2];
+};
+
+SomeClass::SomeClass()
+ : member({
+ [INDEX1] = { .field = 0 }, // { dg-error "constant expression" }
+ [INDEX2] = { .field = 1 } // { dg-error "constant expression" }
+ })
+{
+}