https://bugs.llvm.org/show_bug.cgi?id=51788
Bug ID: 51788
Summary: Trivial assignment of inactive member doesn't begin
the lifetime in constant evaluation
Product: clang
Version: trunk
Hardware: PC
OS: Linux
Status: NEW
Keywords: compile-fail
Severity: enhancement
Priority: P
Component: C++2a
Assignee: unassignedclangb...@nondot.org
Reporter: johel...@gmail.com
CC: blitzrak...@gmail.com, erik.pilking...@gmail.com,
johel...@gmail.com, llvm-bugs@lists.llvm.org,
richard-l...@metafoo.co.uk
In accordance to https://eel.is/c++draft/class.union#general-6.sentence-3, this
should begin the lifetime of these unions. See https://godbolt.org/z/88dPzxjYz
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:
https://godbolt.org/z/7a6fohPGK.
```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
```
--
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs