I was playing around with how T.init works. And I think I may have found a type loophole.

Given that you may initialize a pointer member to the address to a static global:

//----
__gshared int a = 0;
struct S
{
    int* p = &a;
}
//----

Then, in theory, any variable, be they mutable or const, are initialized to T.init:

//----
void main()
{
    immutable S s;
}
//----

This is an issue, because I now have an immutable pointer that points to mutable data:
//----
    immutable S s = S.init;
    immutable int* p = s.p;
    assert(*p == 0); //OK
    a = 5;
    assert(*p == 5); //OK
//----

So this violates the type system...

The question here is:
Is this "legit" code? At what point do you think my code should have been rejected?

Reply via email to