www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 13640] New: can break immutability with inout

https://issues.dlang.org/show_bug.cgi?id=13640

          Issue ID: 13640
           Summary: can break immutability with inout
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: DMD
          Assignee: nobody puremagic.com
          Reporter: ag0aep6g gmail.com

struct Array()
{
    int* p;
    static struct Range
    {
        int* p;
        inout this(inout ref int* p) {this.p = p;}
    }
    /* Return type is not inout; shouldn't compile: */
    Range r() inout
    {
        return inout(Range)(p);
    }
}
void main()
{
    immutable int x = 42;
    auto a = immutable(Array!())(&x);
    auto r = a.r();
    pragma(msg, typeof(r.p)); /* "int*" -- mutable, woops */
    *r.p = 13; /* Shouldn't compile. */
    assert(&x == r.p); /* Passes as it's supposed to. */
    assert(x == 42); /* Passes, wat. */
    assert(*r.p == 13); /* Passes, wat. */
    assert(x == *r.p); /* Finally an assert that fails. */
}

--
Oct 19 2014