[Issue 2043] Closure outer variables in nested blocks are not allocated/instantiated correctly: should have multiple instances but only have one.
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Tue May 24 05:34:01 UTC 2022
https://issues.dlang.org/show_bug.cgi?id=2043
Walter Bright <bugzilla at digitalmars.com> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|NEW |RESOLVED
Resolution|--- |WORKSFORME
--- Comment #40 from Walter Bright <bugzilla at digitalmars.com> ---
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.
--
More information about the Digitalmars-d-bugs
mailing list