"H. S. Teoh via Digitalmars-d" wrote in message
news:[email protected]...
Argh, looks like another incompletely-implemented part of the compiler:
void fun() @safe {
union U {
int p;
int* q;
}
U u;
u.p++; // compiles
u.q = null; // compiles
*u.q++; // x.d(9): Error: field U.q cannot be accessed in @safe code
because > it overlaps with a pointer
}
This looks perfectly fine to me. Consider if you replaced all uses of the
union with multiple variables and explicit casts:
int p;
int* q;
p++; // fine, of course
q++; // also fine
p = 0;
q = null;
// both of these are fine, because they don't read anything from the union.
// worst case, p was the last value assigned to
*(*cast(int**)&p)++; // The cast can't be @safe
Without unions, you can't create invalid pointers in @safe code.
With unions, you can, but you can't access them.