Skip to content

Commit c896266

Browse files
committed
[clang][Sema] Fix crash on designated initializer of union member in subobject
Fixes #166327 Clang previously hit an assertion in C++ mode when a nested initializer list was followed by a designated initializer that referred to a union member of the same subobject. The assertions failing this were logically sound, and this change ensures that such initializations are handled correctly during semantic analysis to avoid failing any assertion.
1 parent 5896a25 commit c896266

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

clang/lib/Sema/SemaInit.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2402,8 +2402,11 @@ void InitListChecker::CheckStructUnionTypes(
24022402
CheckEmptyInitializable(
24032403
InitializedEntity::InitializeMember(*Field, &Entity),
24042404
IList->getEndLoc());
2405-
if (StructuredList)
2405+
if (StructuredList) {
24062406
StructuredList->setInitializedFieldInUnion(*Field);
2407+
StructuredList->resizeInits(SemaRef.Context,
2408+
StructuredList->getNumInits() + 1);
2409+
}
24072410
break;
24082411
}
24092412
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Ensures that Clang does not crash in C++ mode, when a nested initializer
2+
// is followed by a designated initializer for a union member of that same
3+
// subobject.
4+
// See issue #166327.
5+
6+
// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify %s
7+
8+
auto main(void) -> int {
9+
struct Point {
10+
float x;
11+
float y;
12+
union {
13+
int idx;
14+
char label;
15+
} extra;
16+
};
17+
18+
struct SavePoint {
19+
struct Point p;
20+
};
21+
22+
SavePoint save = {.p = {.x = 3.0, .y = 4.0}, .p.extra.label = 'p'}; // expected-warning {{nested designators are a C99 extension}}
23+
}

0 commit comments

Comments
 (0)