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

Walter Bright <[email protected]> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |WORKSFORME

--- Comment #40 from Walter Bright <[email protected]> ---
Note that dgList exceeds the lifetime of b, and should fail to compile for that
reason.

Let's try it (adding import and @safe and updating the code to D2):
----
import std.stdio;

@safe:

void delegate()[] dgList;

void test() {

        foreach(int i; [1, 2, 3]) {
                int b = 2;
                dgList ~= { writeln(b); };
                writeln(&b); // b will be the *same* on each iteration
        }
}
----

> dmd test.c -preview=dip1000
test.d(13): Error: reference to local variable `b` assigned to non-scope
parameter `_param_0`

This is correct behavior. There is no way D should allow references to
variables that went out of scope.

--

Reply via email to