diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index a8e2e5c1e045..dc45202f6b2e 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -559,6 +559,7 @@ Bug Fixes to C++ Support - Fixed an assertion failure in debug mode, and potential crashes in release mode, when diagnosing a failed cast caused indirectly by a failed implicit conversion to the type of the constructor parameter. - Fixed an assertion failure by adjusting integral to boolean vector conversions (#GH108326) +- Fixed a crash when mixture of designated and non-designated initializers in union. (#GH113855) - Fixed an issue deducing non-type template arguments of reference type. (#GH73460) - Fixed an issue in constraint evaluation, where type constraints on the lambda expression containing outer unexpanded parameters were not correctly expanded. (#GH101754) diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp index e2a59f63ccf5..f13355bb93cb 100644 --- a/clang/lib/Sema/SemaInit.cpp +++ b/clang/lib/Sema/SemaInit.cpp @@ -2253,6 +2253,10 @@ bool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity, return FlexArrayDiag != diag::ext_flexible_array_init; } +static bool isInitializedStructuredList(const InitListExpr *StructuredList) { + return StructuredList && StructuredList->getNumInits() == 1U; +} + void InitListChecker::CheckStructUnionTypes( const InitializedEntity &Entity, InitListExpr *IList, QualType DeclType, CXXRecordDecl::base_class_const_range Bases, RecordDecl::field_iterator Field, @@ -2499,8 +2503,7 @@ void InitListChecker::CheckStructUnionTypes( StructuredList, StructuredIndex); InitializedSomething = true; InitializedFields.insert(*Field); - - if (RD->isUnion() && StructuredList) { + if (RD->isUnion() && isInitializedStructuredList(StructuredList)) { // Initialize the first field within the union. StructuredList->setInitializedFieldInUnion(*Field); } @@ -2585,7 +2588,7 @@ void InitListChecker::CheckStructUnionTypes( CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, StructuredList, StructuredIndex); - if (RD->isUnion() && StructuredList) { + if (RD->isUnion() && isInitializedStructuredList(StructuredList)) { // Initialize the first field within the union. StructuredList->setInitializedFieldInUnion(*Field); } diff --git a/clang/test/SemaCXX/PR113855.cpp b/clang/test/SemaCXX/PR113855.cpp new file mode 100644 index 000000000000..fb2a448eca04 --- /dev/null +++ b/clang/test/SemaCXX/PR113855.cpp @@ -0,0 +1,15 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +struct S {}; + +union U { + S x; + float y; +}; + +void f() { + new U{0,.y=1}; + // expected-warning@-1 {{mixture of designated and non-designated initializers in the same initializer list is a C99 extension}} + // expected-note@-2 {{first non-designated initializer is here}} + // expected-error@-3 {{initializer for aggregate with no elements requires explicit braces}} +}