Bug 6439 – [CTFE] union fields are initialized independently
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
All
Creation time
2011-08-05T05:18:00Z
Last change time
2013-11-16T02:05:09Z
Keywords
CTFE, wrong-code
Assigned to
nobody
Creator
dmitry.olsh
Comments
Comment #0 by dmitry.olsh — 2011-08-05T05:18:59Z
struct A
{
this(uint a, uint b)
{
begin = a;
end = b;
}
union
{
struct
{
uint begin, end;
}
uint[2] arr;
}
}
void main(){
enum y = A(10, 20);
A y2 = A(10, 20);
assert(y2.begin == y.begin && y2.end == y.end); //passes
assert(y.arr == [0,0]); //passes, wtf ?!
assert(y.arr == y2.arr); //fails
}
I dunno what was the last stance on unions in CTFE, but this bug also affects constfolded globals (which is tricky and nasty). So at minimum it's better to just reject it.
Comment #1 by github-bugzilla — 2013-11-16T00:59:27Z