llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Naveen Seth Hanig (naveen-seth)

<details>
<summary>Changes</summary>

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
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}}
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/167171
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to