[llvm-bugs] [Bug 51788] New: Trivial assignment of inactive member doesn't begin the lifetime in constant evaluation

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Sep 7 19:14:20 PDT 2021


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: unassignedclangbugs at nondot.org
          Reporter: johelegp at gmail.com
                CC: blitzrakete at gmail.com, erik.pilkington at gmail.com,
                    johelegp at gmail.com, llvm-bugs at lists.llvm.org,
                    richard-llvm at 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210908/74b1960f/attachment.html>


More information about the llvm-bugs mailing list