[Issue 14618] can break immutable with inout and a delegate

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Mar 4 11:50:12 UTC 2018


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

--- Comment #4 from ag0aep6g at gmail.com ---
(In reply to Walter Bright from comment #2)
> If I prepend:
> 
>   @safe:
> 
> to the code:
[...]
>    Error: cannot take address of local x in @safe function main

Compile with `-dip1000` and add `scope` to `f`'s parameter and it compiles
again:

----
@safe:

struct S
{
    immutable(int)* p;
}

inout(int)* f(scope inout S s)
{
    inout(int)* result;
    auto dg = (inout(int)* p) {result = p;};
    dg(s.p);
    return result;
}

void main()
{
    immutable int x = 42;
    immutable int* p = &x;
    assert(*p == 42); /* passes */
    scope(exit) assert(*p == 42); /* fails */
    int* m = f(S(p)); /* uh-oh */
    *m = 13; /* writing over immutable *p */
}
----

The `scope` on the parameter is wrong here, of course, but the compiler doesn't
catch it.

--


More information about the Digitalmars-d-bugs mailing list