Both safe and wrong?
Luís Marques
luis at luismarques.eu
Sun Feb 3 23:48:04 UTC 2019
Consider this code:
https://run.dlang.io/is/aw09pD
import std.stdio;
@safe:
const x = 42;
int* y = cast(int*) &x;
void main()
{
*y = 7;
writeln(x); // prints 42
}
I'm probably too out of touch of how the safe type system should
work, but this seems inconsistent. I would expect *either*:
1) Overriding x's const is unsafe and undefined behavior. The
cast inside a safe block should cause an error. The 42 is fine.
2) As we promised the type system, we didn't modify the 42
through a const view of that value (x), instead we used a mutable
view (y). The cast is fine. It should print 7 instead.
But I guess I'm wrong? Please clarify.
More information about the Digitalmars-d
mailing list