D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4131 - break does not work correctly with foreach and associative arrays
Summary: break does not work correctly with foreach and associative arrays
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other Windows
: P2 regression
Assignee: No Owner
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2010-04-27 15:01 UTC by Sönke Ludwig
Modified: 2015-06-09 01:27 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Sönke Ludwig 2010-04-27 15:01:57 UTC
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 bearophile_hugs 2010-04-27 15:39:44 UTC
This bug is present in dmd 2.043, and it's absent in dmd 1.058 and 1.042.
Comment 2 Walter Bright 2010-04-29 22:13:36 UTC
changeset druntime 287
Comment 3 Don 2010-05-05 19:12:56 UTC
Fixed DMD2.044