@@ -5503,48 +5503,54 @@ InjectAnonymousStructOrUnionMembers(Sema &SemaRef, Scope *S, DeclContext *Owner,
55035503 if ((isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D)) &&
55045504 cast<NamedDecl>(D)->getDeclName()) {
55055505 ValueDecl *VD = cast<ValueDecl>(D);
5506- if (CheckAnonMemberRedeclaration(SemaRef, S, Owner, VD->getDeclName(),
5507- VD->getLocation(), AnonRecord->isUnion(),
5508- SC)) {
5509- // C++ [class.union]p2:
5510- // The names of the members of an anonymous union shall be
5511- // distinct from the names of any other entity in the
5512- // scope in which the anonymous union is declared.
5506+ // C++ [class.union]p2:
5507+ // The names of the members of an anonymous union shall be
5508+ // distinct from the names of any other entity in the
5509+ // scope in which the anonymous union is declared.
5510+
5511+ bool FieldInvalid = CheckAnonMemberRedeclaration(
5512+ SemaRef, S, Owner, VD->getDeclName(), VD->getLocation(),
5513+ AnonRecord->isUnion(), SC);
5514+ if (FieldInvalid)
55135515 Invalid = true;
5514- } else {
5515- // C++ [class.union]p2:
5516- // For the purpose of name lookup, after the anonymous union
5517- // definition, the members of the anonymous union are
5518- // considered to have been defined in the scope in which the
5519- // anonymous union is declared.
5520- unsigned OldChainingSize = Chaining.size();
5521- if (IndirectFieldDecl *IF = dyn_cast<IndirectFieldDecl>(VD))
5522- Chaining.append(IF->chain_begin(), IF->chain_end());
5523- else
5524- Chaining.push_back(VD);
55255516
5526- assert(Chaining.size() >= 2);
5527- NamedDecl **NamedChain =
5528- new (SemaRef.Context)NamedDecl*[Chaining.size()];
5529- for (unsigned i = 0; i < Chaining.size(); i++)
5530- NamedChain[i] = Chaining[i];
5517+ // Inject the IndirectFieldDecl even if invalid, because later
5518+ // diagnostics may depend on it being present, see findDefaultInitializer.
5519+
5520+ // C++ [class.union]p2:
5521+ // For the purpose of name lookup, after the anonymous union
5522+ // definition, the members of the anonymous union are
5523+ // considered to have been defined in the scope in which the
5524+ // anonymous union is declared.
5525+ unsigned OldChainingSize = Chaining.size();
5526+ if (IndirectFieldDecl *IF = dyn_cast<IndirectFieldDecl>(VD))
5527+ Chaining.append(IF->chain_begin(), IF->chain_end());
5528+ else
5529+ Chaining.push_back(VD);
55315530
5532- IndirectFieldDecl *IndirectField = IndirectFieldDecl::Create(
5533- SemaRef.Context, Owner, VD->getLocation(), VD->getIdentifier(),
5534- VD->getType(), {NamedChain, Chaining.size()});
5531+ assert(Chaining.size() >= 2);
5532+ NamedDecl **NamedChain =
5533+ new (SemaRef.Context) NamedDecl *[Chaining.size()];
5534+ for (unsigned i = 0; i < Chaining.size(); i++)
5535+ NamedChain[i] = Chaining[i];
55355536
5536- for (const auto *Attr : VD->attrs())
5537- IndirectField->addAttr(Attr->clone(SemaRef.Context));
5537+ IndirectFieldDecl *IndirectField = IndirectFieldDecl::Create(
5538+ SemaRef.Context, Owner, VD->getLocation(), VD->getIdentifier(),
5539+ VD->getType(), {NamedChain, Chaining.size()});
55385540
5539- IndirectField->setAccess(AS);
5540- IndirectField->setImplicit();
5541- SemaRef.PushOnScopeChains(IndirectField, S);
5541+ for (const auto *Attr : VD->attrs())
5542+ IndirectField->addAttr(Attr->clone(SemaRef.Context));
55425543
5543- // That includes picking up the appropriate access specifier.
5544- if (AS != AS_none) IndirectField->setAccess(AS);
5544+ IndirectField->setAccess(AS);
5545+ IndirectField->setImplicit();
5546+ IndirectField->setInvalidDecl(FieldInvalid);
5547+ SemaRef.PushOnScopeChains(IndirectField, S);
55455548
5546- Chaining.resize(OldChainingSize);
5547- }
5549+ // That includes picking up the appropriate access specifier.
5550+ if (AS != AS_none)
5551+ IndirectField->setAccess(AS);
5552+
5553+ Chaining.resize(OldChainingSize);
55485554 }
55495555 }
55505556
0 commit comments