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

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Mar 4 01:54:26 UTC 2018


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

Walter Bright <bugzilla at digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla at digitalmars.com

--- Comment #2 from Walter Bright <bugzilla at digitalmars.com> ---
If I prepend:

  @safe:

to the code:

(In reply to ag0aep6g from comment #0)
> struct S
> {
>     immutable(int)* p;
> }
> 
> inout(int)* f(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;

   Error: cannot take address of local x in @safe function main

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

--


More information about the Digitalmars-d-bugs mailing list