Bug 7894 – [CTFE] - goto within ForStatement restarts loop
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-04-12T15:25:00Z
Last change time
2012-07-14T14:02:53Z
Assigned to
nobody
Creator
code
Comments
Comment #0 by code — 2012-04-12T15:25:07Z
cat > bug.d << CODE
int foo()
{
foreach(v; 0 .. 2)
{
auto n = v;
Lagain:
if (n--) goto Lagain;
}
return 0;
}
enum ctfe = foo();
CODE
dmd -c bug.d
--------
This will loop endlessly because the loop initializer is
reinterpreted after each goto.
Comment #1 by clugdbug — 2012-07-09T01:28:00Z
Slightly reduced test case:
int bug7894()
{
for (int k = 0; k < 2; ++k) {
goto Lagain;
Lagain: ;
}
return 1;
}
static assert( bug7894() );
Actually the loop initializer isn't reinterpreted after each goto. What's happening is that the increment is skipped.
Comment #2 by github-bugzilla — 2012-07-14T13:03:37Z