Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions clang/lib/Sema/SemaInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3029,10 +3029,11 @@ InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity,
if (StructuredList) {
FieldDecl *CurrentField = StructuredList->getInitializedFieldInUnion();
if (CurrentField && !declaresSameEntity(CurrentField, *Field)) {
assert(StructuredList->getNumInits() == 1
&& "A union should never have more than one initializer!");
const auto NumInits = StructuredList->getNumInits();
assert(StructuredList->getNumInits() <= 1 &&
"A union should never have more than one initializer!");

Expr *ExistingInit = StructuredList->getInit(0);
Expr *ExistingInit = NumInits ? StructuredList->getInit(0) : nullptr;
if (ExistingInit) {
// We're about to throw away an initializer, emit warning.
diagnoseInitOverride(
Expand Down
23 changes: 23 additions & 0 deletions clang/test/SemaCXX/crash-union-designated-initializer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Ensures that Clang does not crash in C++ mode, when a nested initializer
// is followed by a designated initializer for a union member of that same
// subobject.
// See issue #166327.

// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify %s

auto main(void) -> int {
struct Point {
float x;
float y;
union {
int idx;
char label;
} extra;
};

struct SavePoint {
struct Point p;
};

SavePoint save = {.p = {.x = 3.0, .y = 4.0}, .p.extra.label = 'p'}; // expected-warning {{nested designators are a C99 extension}}
}