Bug 4131 – break does not work correctly with foreach and associative arrays
Status
RESOLVED
Resolution
FIXED
Severity
regression
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Windows
Creation time
2010-04-27T15:01:00Z
Last change time
2015-06-09T01:27:40Z
Keywords
wrong-code
Assigned to
nobody
Creator
sludwig
Comments
Comment #0 by sludwig — 2010-04-27T15:01:57Z
Issuing a break statement will not cause the foreach loop to exit but instead will perform another iteration before jumping out.
---
import std.stdio;
void main()
{
int[int] test;
test[0] = 0;
test[1] = 1;
bool flag = false;
foreach( k, v; test){
writefln("loop: %s %s", k, v);
assert(!flag); // fails on second loop!
flag = true;
break; // should exit here after the first iteration
}
}
---
output:
---
loop: 0 0
loop: 1 1
core.exception.AssertError@test(12): Assertion failure
---
Comment #1 by bearophile_hugs — 2010-04-27T15:39:44Z
This bug is present in dmd 2.043, and it's absent in dmd 1.058 and 1.042.