<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/62156>62156</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[Clang] Allows multiple initializers for a union when using designated initializers in C++
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang:frontend
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
shafik
</td>
</tr>
</table>
<pre>
Given the following, see godbolt: https://godbolt.org/z/TjxE3PhfK
```cpp
union U {
int x;
float y;
};
void f() {
U u{.x=2,.y=1};
}
```
We produce the following diagnostic:
```console
<source>:7:13: warning: initializer overrides prior initialization of this subobject [-Winitializer-overrides]
U u{.x=2,.y=1};
^~~~
<source>:7:11: note: previous initialization is here
U u{.x=2,.y=1};
^
1 warning generated.
Compiler returned: 0
```
This should be ill-formed and should be a hard error.
It looks like we allow this as an extension if we look at `SemaInit.cpp` and see `diagnoseInitOverride(...)` we see the following:
```cpp
// Overriding an initializer via a designator is valid with C99 designated
// initializers, but ill-formed with C++20 designated initializers.
unsigned DiagID = SemaRef.getLangOpts().CPlusPlus
? diag::ext_initializer_overrides
: diag::warn_initializer_overrides;
```
I wonder if for unions in C++ we should make this a hard error.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVU2P4jgQ_TXFpUSU2ASaQw5AmlVrV5rR7ozmODK4knja2JHtADOH_u0rJ3QTUPd-RBBHVfZz-dWrsvBe1YaogHwNeTkRXWisK3wjKvU82Vn5s_hNHclgaAgrq7U9KVMD26AnwtrKndUB-AqbEFoPfAVsC2x7cSTW1cC2v4Btv_w4P_LPTfU7pCWkq8t7ng6_fdsOls4oa_ArwmI9GBBRmYBn4K-GSlsR8OebARbl9bt_H62SWAF7ALa8QfqKHSzWyRl4yYBtkp_Ay-xm_aK8i2yM-42wdVZ2e7qlA6UStbE-qH1k4N0DWuOtpouVb7zt3J6APwJfLYCvMh5JPAlnIr18hcq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