Bug 5655 – Lambda inside static foreach saves wrong value of counter
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
All
Creation time
2011-02-26T09:25:00Z
Last change time
2013-08-18T22:34:55Z
Assigned to
nobody
Creator
andrei
Comments
Comment #0 by andrei — 2011-02-26T09:25:07Z
This program should print 1 but instead prints 0. Apparently all lambdas inside a static foreach loop capture 0.
import std.stdio, std.typetuple;
void main()
{
TypeTuple!(int, float) TT;
void function() funs[2];
foreach (i, T; TT)
{
funs[i] = function { writeln(i); };
}
funs[1]();
}
Comment #1 by lovelydear — 2012-04-22T16:53:08Z
Output on 2.059 Win32:
PS E:\DigitalMars\dmd2\samples> rdmd bug.d
OPTLINK (R) for Win32 Release 8.00.12
Copyright (C) Digital Mars 1989-2010 All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
I:\DOCUME~1\Nick\LOCALS~1\Temp\.rdmd\rdmd-bug.d-E7282F21F02BD0DD07E8BB39480BDC77\bug-d-E7282F21F02BD0DD07E8BB39480BDC77.obj(bug-d-E7282F21F02BD0DD07E8BB39480BDC77) Offset 00DA9H Record Type 00C3
Error 1: Previous Definition Different : _D3bug4mainFZv14__funcliteral1FZv
--- errorlevel 1
PS E:\DigitalMars\dmd2\samples>
PS E:\DigitalMars\dmd2\samples> dmd -lib bug.d
bug.lib: Error: multiple definition of bug_1_1a5: _D3bug4mainFZv14__funcliteral1FZv and bug_1_1a5: _D3bug4mainFZv14__funcliteral1FZv
PS E:\DigitalMars\dmd2\samples>
Comment #2 by yebblies — 2012-07-08T05:13:17Z
*** Issue 8351 has been marked as a duplicate of this issue. ***
Comment #3 by yebblies — 2012-07-17T10:56:55Z
*** Issue 7798 has been marked as a duplicate of this issue. ***
Comment #4 by bearophile_hugs — 2013-03-06T16:16:46Z
Related to Issue 9628 ?
Comment #5 by hsteoh — 2013-08-18T22:34:55Z
This bug appears to have been fixed in the latest git HEAD. To prove that it has really been fixed (not just have a different wrong value for i captured), I expanded the code slightly:
------
import std.stdio, std.typetuple;
void main()
{
TypeTuple!(int, float, real) TT;
void function() funs[3];
foreach (i, T; TT)
{
funs[i] = function { writeln(i); };
}
funs[1]();
funs[2]();
}
------
The output is:
1
2