<html>
<head>
<base href="https://bugs.llvm.org/">
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW - Trivial assignment of inactive member doesn't begin the lifetime in constant evaluation"
href="https://bugs.llvm.org/show_bug.cgi?id=51788">51788</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Trivial assignment of inactive member doesn't begin the lifetime in constant evaluation
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>trunk
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>Linux
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Keywords</th>
<td>compile-fail
</td>
</tr>
<tr>
<th>Severity</th>
<td>enhancement
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>C++2a
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>johelegp@gmail.com
</td>
</tr>
<tr>
<th>CC</th>
<td>blitzrakete@gmail.com, erik.pilkington@gmail.com, johelegp@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
</td>
</tr></table>
<p>
<div>
<pre>In accordance to <a href="https://eel.is/c++draft/class.union#general-6.sentence-3">https://eel.is/c++draft/class.union#general-6.sentence-3</a>, this
should begin the lifetime of these unions. See <a href="https://godbolt.org/z/88dPzxjYz">https://godbolt.org/z/88dPzxjYz</a>
for the example adjusted from the Standard:
```C++
constexpr void f() {
struct foo_t { int z; };
union A { int x; foo_t y[4]; };
struct B { A a; };
union C { B b; int k; };
C c;
c.b.a.y[0] = foo_t{};
(void)+c.b.a.y[0].z;
}
int main() { []() consteval { f(); }(); }
```
```
<source>:1:16: error: constexpr function never produces a constant expression
[-Winvalid-constexpr]
constexpr void f() {
^
<source>:7:14: note: member call on member 'b' of union with no active member
is not allowed in a constant expression
c.b.a.y[0] = foo_t{};
^
<source>:10:14: error: call to consteval function 'main()::(anonymous
class)::operator()' is not a constant expression
int main() { []() consteval { f(); }(); }
^
<source>:7:14: note: member call on member 'b' of union with no active member
is not allowed in a constant expression
c.b.a.y[0] = foo_t{};
^
<source>:10:31: note: in call to 'f()'
int main() { []() consteval { f(); }(); }
^
<source>:10:14: note: in call to '&[]() {
f();
}->operator()()'
int main() { []() consteval { f(); }(); }
^
2 errors generated.
Compiler returned: 1
```
Making `A` a struct doesn't help, but it works if `x` is initialized first:
<a href="https://godbolt.org/z/7a6fohPGK">https://godbolt.org/z/7a6fohPGK</a>.
```C++
constexpr void f() {
struct foo_t { int z; };
struct A { int x; foo_t y[4]; };
struct B { A a; };
union C { B b; int k; };
C c;
// c.b.a.x = 0;
c.b.a.y[0] = foo_t{};
(void)+c.b.a.y[0].z;
}
int main() { []() consteval { f(); }(); }
```
```
<source>:1:16: error: constexpr function never produces a constant expression
[-Winvalid-constexpr]
constexpr void f() {
^
<source>:8:14: note: member call on member 'b' of union with no active member
is not allowed in a constant expression
c.b.a.y[0] = foo_t{};
^
<source>:11:14: error: call to consteval function 'main()::(anonymous
class)::operator()' is not a constant expression
int main() { []() consteval { f(); }(); }
^
<source>:8:14: note: member call on member 'b' of union with no active member
is not allowed in a constant expression
c.b.a.y[0] = foo_t{};
^
<source>:11:31: note: in call to 'f()'
int main() { []() consteval { f(); }(); }
^
<source>:11:14: note: in call to '&[]() {
f();
}->operator()()'
int main() { []() consteval { f(); }(); }
^
2 errors generated.
Compiler returned: 1
```</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>