Bug 1605 – break in switch with goto breaks in ctfe

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2007-10-21T10:34:00Z
Last change time
2014-02-24T15:30:52Z
Keywords
patch, wrong-code
Assigned to
nobody
Creator
lutger.blijdestijn

Comments

Comment #0 by lutger.blijdestijn — 2007-10-21T10:34:12Z
I stumbled upon a case where a break statement in a switch causes the outer while loop to exit. This happens only when the function is evaluated at compile time and a goto statement is involved. Forgive me the bad example: int Break() { int i = 0; while (true) { switch(i) { case 0: goto LABEL; // comment out this line and all is fine LABEL: // at compile time, this breaks out of the while loop: break; default: return i; } i = 1; } return 0; // unreachable } void main() { assert (Break() == 1); // ok static assert(Break() == 1); // not ok }
Comment #1 by clugdbug — 2009-08-24T04:21:21Z
Actually this has nothing to do with switch or break. It only requires 'goto' inside a while loop. Reduced test case: int Break() { int i = 0; while (true) { goto LABEL; LABEL: if (i!=0) return i; i = 27; } assert(i==27); // this passes, it did actually execute the loop. return 88; // unreachable } static assert(Break() == 27); ----------- It's failing because the test for continuing to execute is wrong. ---- PATCH: interpret.c, Expression *WhileStatement::interpret(InterState *istate) if (e == EXP_BREAK_INTERPRET) return NULL; - if (e != EXP_CONTINUE_INTERPRET) + if (e && e != EXP_CONTINUE_INTERPRET) return e; } while (1) { e = condition->interpret(istate);
Comment #2 by bugzilla — 2009-09-03T13:22:55Z
Fixed dmd 1.047 and 2.032