Bug 14887 – break in static foreach should apply to outer scope

Status
NEW
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2015-08-07T21:16:46Z
Last change time
2024-12-13T18:43:59Z
Assigned to
No Owner
Creator
Martin Nowak
Moved to GitHub: dmd#19022 →

Comments

Comment #0 by code — 2015-08-07T21:16:46Z
A break inside a static foreach loop is interpreted as "breaking" the foreach loop. The static foreach doesn't have a scope and one cannot break it during compile time. Instead the break will work at runtime. ---- import std.stdio; void foo(Args...)() { foreach (A; Args) { pragma(msg, A); writeln("runtime ", A.stringof); break; } } void main() { foo!(int, long, float, double)(); } ---- int long float double runtime int ---- This is highly confusing, particularly when combined with switch (see issue 7835). It would be best to deprecate the existing break and require a break with label instead.
Comment #1 by mathias.lang — 2016-09-21T17:16:26Z
I tend to disagree here. The break will happen at runtime and skip the other iterations of that loop, and to me that's exactly what it should do, even if it might sound confusing at first. If anything, we could improve flow analysis to skip this specific code, but having its behaviour changed would be highly confusing.
Comment #2 by robert.schadek — 2024-12-13T18:43:59Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/19022 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB