Skip to content

Conversation

@naveen-seth
Copy link
Contributor

@naveen-seth naveen-seth commented Nov 8, 2025

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.
This fixes the assertion.

@naveen-seth naveen-seth assigned Fznamznon and Sirraide and unassigned Fznamznon and Sirraide Nov 8, 2025
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Nov 8, 2025
@naveen-seth naveen-seth requested a review from Sirraide November 8, 2025 19:12
@llvmbot
Copy link
Member

llvmbot commented Nov 8, 2025

@llvm/pr-subscribers-clang

Author: Naveen Seth Hanig (naveen-seth)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/167171.diff

2 Files Affected:

  • (modified) clang/lib/Sema/SemaInit.cpp (+4-1)
  • (added) clang/test/SemaCXX/crash-union-designated-initializer.cpp (+23)
diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index cc6ddf568d346..13fb22ebfe0ef 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -2402,8 +2402,11 @@ void InitListChecker::CheckStructUnionTypes(
         CheckEmptyInitializable(
             InitializedEntity::InitializeMember(*Field, &Entity),
             IList->getEndLoc());
-        if (StructuredList)
+        if (StructuredList) {
           StructuredList->setInitializedFieldInUnion(*Field);
+          StructuredList->resizeInits(SemaRef.Context,
+                                      StructuredList->getNumInits() + 1);
+        }
         break;
       }
     }
diff --git a/clang/test/SemaCXX/crash-union-designated-initializer.cpp b/clang/test/SemaCXX/crash-union-designated-initializer.cpp
new file mode 100644
index 0000000000000..7ba40311eac0b
--- /dev/null
+++ b/clang/test/SemaCXX/crash-union-designated-initializer.cpp
@@ -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}}
+}

@naveen-seth naveen-seth requested review from Fznamznon and removed request for Fznamznon and Sirraide November 8, 2025 19:13
…subobject

Fixes llvm#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.
This fixes the assertion.
@naveen-seth naveen-seth force-pushed the fix-crash-sema-designated-init-union branch from c896266 to 3fbca9a Compare November 8, 2025 20:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category crash-on-valid

Projects

None yet

Development

Successfully merging this pull request may close these issues.

crash in CheckDesignatedInitializer since clang 3.7

4 participants