Bug 15535 – Emit error on "goto default" in final switch
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-01-09T21:31:00Z
Last change time
2016-03-19T20:21:39Z
Assigned to
nobody
Creator
jbc.engelen
Comments
Comment #0 by jbc.engelen — 2016-01-09T21:31:34Z
This code compiles, but I think it shouldn't:
void main()
{
int i;
final switch(i) {
case 1: goto default;
}
}
When executed, the program immediately crashes.
Because a final switch cannot have a "default:" case, I think the compiler error on this code.
Comment #1 by jbc.engelen — 2016-01-09T21:43:36Z
(This is probably mostly a note to self.)
Note that "goto default" means to go to the default label of the current switch statement only. So this does not work:
void main()
{
int i = 1;
switch (i) {
case 1:
final switch(i) {
case 1: goto default;
}
default:
i = 666;
break;
}
assert(i == 666);
}
The code compiles and when run will throw
core.exception.SwitchError@gotodefault(6): No appropriate switch clause found
(line 6 contains the "goto default")
Comment #2 by github-bugzilla — 2016-01-15T17:24:57Z