Comment #2 by john.loughran.colvin — 2016-09-21T23:04:04Z
Simplified example:
struct S {
union {
// works if order swapped
int b;
int[1] a;
}
this(int v) {
a[0] = v;
}
}
auto s = S(2);
void main() {
assert(s.a[0] == 2); // fails
assert(S(2).a[0] == 2); // OK
}
Comment #3 by razvan.nitu1305 — 2022-03-24T15:23:58Z
(In reply to John Colvin from comment #2)
> Simplified example:
>
> struct S {
> union {
> // works if order swapped
> int b;
> int[1] a;
> }
>
> this(int v) {
> a[0] = v;
> }
> }
> auto s = S(2);
>
> void main() {
> assert(s.a[0] == 2); // fails
> assert(S(2).a[0] == 2); // OK
> }
It seems that ctfe rewrites `auto s = S(2)` to `auto s = S(0, [2)`. So it does not seem to understand that a and b are inside a union and therefore only the first initializer (0) will be taken into account.
Comment #4 by robert.schadek — 2024-12-13T18:47:32Z