------
void fun() @safe {
union U {
immutable int x;
int y;
}
U u;
u.y = 1;
assert(u.x == 1);
u.y = 2;
assert(u.x == 2); // look ma! I broke immutability!
}
------
Comment #1 by hsteoh — 2014-10-02T03:41:30Z
Basically, immutable cannot be allowed to overlap with anything mutable, otherwise there will be a way to break the immutability guarantee.
Comment #2 by hsteoh — 2016-02-19T00:30:55Z
Unfortunately, fixing this may prove to be a challenge, because std.typecons.Rebindable uses a union of (possibly) immutable and mutable fields in order to achieve rebindability. Arguably, though, some kind of explicit cast ought to be required in that case. Either that, or we impose the non-overlapping restriction only in @safe code, so that @system code can continue using unions to do black magic with immutability.