Issue 207913
Summary [clang] Clarify constexpr active union member change diagnostic
Labels clang:frontend, constexpr
Assignees
Reporter tbaederr
    This is specifically about this test from `test/Sema/constant-_expression_-cxx2a.cpp`:
```c++
  template<int> struct X {};
  union V {
    int a, b;
    constexpr V(X<1>) : a(b = 1) {} // expected-note {{assignment would change active union member during the initialization of a different member}}
    constexpr V(X<3>) : a((b = 1, a = 1)) {} // expected-note {{assignment would change active union member during the initialization of a different member}}
  };
  constinit V v1 = X<1>(); // expected-error {{constant init}} expected-note {{constinit}} expected-note {{in call}}
  constinit V v3 = X<3>(); // expected-error {{constant init}} expected-note {{constinit}} expected-note {{in call}}
```

Clang is the only compiler diagnosing this: https://godbolt.org/z/WvP1PsG6W

Is this really invalid?

By contrast, initializing (and activating) the union members via normal assignments does _not_ diagnose:
```c++
  union U {
 int a;
    int b;
    constexpr U() {
        a = b = 10;
    }
 };

constexpr U u;
static_assert(u.a == 10);
```
https://godbolt.org/z/rqP9n9766

Which makes sense, I guess? We're first activating `b` and then `a` by reading from `b`.
Is the constructor field initializer case really different?

CC @zygoloid @frederick-vs-ja @Endilll 


_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to