Bug 9628 – Lambda in foreach loop Vs. lambda in static foreach loop

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-03-01T16:48:00Z
Last change time
2013-06-08T05:25:47Z
Keywords
pull, wrong-code
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2013-03-01T16:48:17Z
This program shows two similar versions of the same loop: import std.stdio, std.algorithm, std.typetuple, std.range; void main() { auto items = [[10, 20], [30]]; foreach (_; 0 .. 2) { foreach (sub; items) { iota(sub.length) .map!((i){ writeln(sub); return 0; }) .array(); } } writeln(); foreach (_; TypeTuple!(0, 1)) { foreach (sub; items) { iota(sub.length) .map!((i){ writeln(sub); return 0; }) .array(); } } } Its output (dmd 2.063alpha): [10, 20] [10, 20] [30] [10, 20] [10, 20] [30] [10, 20] [10, 20] [30] [30] [30] [30] Probably the two loops should give the same output. (Also, this program can't be compiled with -inline).
Comment #1 by maxim — 2013-03-02T00:27:30Z
Rewritten version (this can be complied with -inline): import std.stdio, std.algorithm, std.typetuple, std.range; auto items = [[10, 20], [30]]; void bar() { foreach (int dx; 0 .. 2) { foreach (sub; items) { iota(sub.length) .map!((size_t i){ writeln(dx,"--", sub,"--", items); return 0; }) .array(); }} } void foo() { foreach (int dx; TypeTuple!(0, 1)) { foreach (sub; items) { iota(sub.length) .map!((size_t i){ writeln(dx,"--", sub,"--", items); return 0; }) .array(); }} } void main() { bar(), writeln(), foo(); } dx in second loop is always zero
Comment #2 by k.hara.pg — 2013-05-13T02:39:52Z
Comment #3 by github-bugzilla — 2013-06-08T01:25:43Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/0ba974c94e61af361aa77756bfdcf4a9353b546f fix Issue 9628 - Lambda in foreach loop Vs. lambda in static foreach loop https://github.com/D-Programming-Language/dmd/commit/a8f842e12154eee7d79c1943f6e3806116cf0525 Merge pull request #2029 from 9rnsr/fix9628 Issue 9628 - Lambda in foreach loop Vs. lambda in static foreach loop
Comment #4 by bearophile_hugs — 2013-06-08T05:25:47Z
The loop can't be compiled with -inline, but the main problem seems fixed.