diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 8afda8264c4..375385e0013 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -6626,6 +6626,11 @@ reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p, return error_mark_node; } + if (!field && ANON_AGGR_TYPE_P (type)) + /* Apparently the designator isn't for a member of this anonymous + struct, so head back to the enclosing class. */ + break; + if (!field || TREE_CODE (field) != FIELD_DECL) { if (complain & tf_error) diff --git a/gcc/testsuite/g++.dg/ext/anon-struct10.C b/gcc/testsuite/g++.dg/ext/anon-struct10.C new file mode 100644 index 00000000000..9b01bf3fada --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/anon-struct10.C @@ -0,0 +1,21 @@ +// PR c++/101767 +// { dg-do compile { target c++11 } } +// { dg-additional-options "-Wno-pedantic" } + +typedef struct { + struct { + int x; + }; + union { + int y; + float z; + }; +} S; + +void foo(void) +{ + [[maybe_unused]] S a = { + .x = 1, + .y = 0 + }; +}