<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/60803>60803</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Clang 16-rc2 regression: confusion around single-element array class variant member in constexpr
</td>
</tr>
<tr>
<th>Labels</th>
<td>
bug,
c++20,
clang:frontend,
regression,
rejects-valid
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
CaseyCarter
</td>
</tr>
</table>
<pre>
Caught this testing MSVCSTL with 16-rc2. One of the libc++ tests instantiates `std:basic_string` with a 16-byte `struct` type, which results in an SSO buffer large enough to hold a single character. I suspect this is a vanishingly rare use case outside of test suites, so this is not a release blocker, but a "would-be-nice" for LLVM 16.
Compiling this well-formed TU:
```c++
struct CharT { int x; };
struct basic_string {
constexpr basic_string() { u_.buf_[0] = CharT{}; }
union U {
constexpr U() : buf_() {}
CharT buf_[1];
} u_;
};
constexpr bool test() {
basic_string s;
return true;
}
static_assert(test());
```
with `clang++ -std=c++20` diagnoses:
```
repro.cpp:18:15: error: static assertion expression is not an integral constant expression
static_assert(test());
^~~~~~
repro.cpp:4:41: note: member call on object outside its lifetime is not allowed in a constant expression
constexpr basic_string() { u_.buf_[0] = CharT{}; }
^
repro.cpp:14:16: note: in call to 'basic_string()'
basic_string s;
^
repro.cpp:18:15: note: in call to 'test()'
static_assert(test());
^
1 error generated.
```
This is particularly strange given that `U`'s default constructor activates `buf_`, and that the assignment `u_.buf_[0] = CharT{}` should activate `buf_` even were it not already the active member of `u_`. Indeed, changing `U`'s default constructor so as to not activate `buf_` - or removing the constructor entirely - results in successful compilation.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVl1v4joT_jXmZkQUnJLABRcF3kor7Wov2u5t5SSTxO8xNvLYdLk5v_1okgCpylG10kEhX555nvl2FJFuLeJGLLdiuZ-pGDrnNztFeN4pH9DPSlefNzsV2y5A6DRBQAratvDj-dfu-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