This is taken from DIP 1035:
struct S {
private union {
int x;
int* y;
}
private int tag; // x if 0, y if 1
// public @trusted interface follows
void set(int rhs) @trusted {
if (tag == 0) x = rhs;
else *y = rhs;
}
....
}
Allowing safe code to modify tag from @safe code, followed by calling set(), results in unsafe code from safe functions.
This could be relaxed to restrict access only for types that have @trusted functions.
Comment #1 by stanislav.blinov — 2020-06-17T20:18:57Z
For clarity, you mean modifying `tag` like so?
void unsafe() @safe
{
S s;
__traits(getMember, s, "tag") = 2; // should be error
s.set(42);
}
Comment #2 by robert.schadek — 2024-12-13T19:09:16Z