Bug 7835 – switch case fallthrough error despite a break inside static foreach
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2012-04-05T17:40:00Z
Last change time
2016-09-21T17:27:28Z
Keywords
diagnostic
Assigned to
nobody
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2012-04-05T17:40:12Z
This D2 program compiles with no warnings or errors with DMD 2.059beta:
import core.stdc.stdio: printf;
template TypeTuple(TList...) {
alias TList TypeTuple;
}
void main() {
char c = 'b';
switch (c) {
case 'a': printf("1 a\n"); break;
foreach (o; TypeTuple!('b', 'c')) {
case o: printf("2 %c\n", c); break;
}
default: printf("default");
}
}
But it runs in a wrong way, as you see:
...>dmd -run test.d
2 b
default
...>dmd -w -run test.d
test.d(12): Error: switch case fallthrough - use 'goto default;' if intended
So the break inside the static foreach is ignored.
(This idiom of using a static foreach inside a switch is handy to generate switch cases.)
Note: adding a second break, like this, doesn't improve the situation:
import core.stdc.stdio: printf;
template TypeTuple(TList...) {
alias TList TypeTuple;
}
void main() {
char c = 'b';
switch (c) {
case 'a': printf("1 a\n"); break;
foreach (o; TypeTuple!('b', 'c')) {
case o: printf("2 %c\n", c); break; break;
}
default: printf("default");
}
}
test.d(10): Warning: statement is not reachable
test.d(10): Warning: statement is not reachable
test.d(12): Error: switch case fallthrough - use 'goto default;' if intended
Comment #1 by dmitry.olsh — 2012-04-06T07:15:55Z
(In reply to comment #0)
> This D2 program compiles with no warnings or errors with DMD 2.059beta:
>
>
> import core.stdc.stdio: printf;
> template TypeTuple(TList...) {
> alias TList TypeTuple;
> }
> void main() {
> char c = 'b';
L_MySwitch:
> switch (c) {
> case 'a': printf("1 a\n"); break;
> foreach (o; TypeTuple!('b', 'c')) {
> case o: printf("2 %c\n", c); break L_MySwitch;
> }
> default: printf("default");
> }
> }
>
>
For these cases I recommend to use labeled breaks
so that it's more clear for humans and compiler alike.
>
> But it runs in a wrong way, as you see:
>
> ...>dmd -run test.d
> 2 b
> default
>
> ...>dmd -w -run test.d
> test.d(12): Error: switch case fallthrough - use 'goto default;' if intended
>
>
> So the break inside the static foreach is ignored.
>
> (This idiom of using a static foreach inside a switch is handy to generate
> switch cases.)
>
>
>
> Note: adding a second break, like this, doesn't improve the situation:
>
> import core.stdc.stdio: printf;
> template TypeTuple(TList...) {
> alias TList TypeTuple;
> }
> void main() {
> char c = 'b';
> switch (c) {
> case 'a': printf("1 a\n"); break;
> foreach (o; TypeTuple!('b', 'c')) {
> case o: printf("2 %c\n", c); break; break;
> }
> default: printf("default");
> }
> }
It can't help because the second break is by definition unreachable.
Comment #2 by timon.gehr — 2012-04-06T07:38:55Z
Not a bug. break applies to the innermost statement that can be broken out from. This includes foreach.
(I use this idiom often. Use labeled break to break from the switch.)
Please reopen as enhancement if you think the code should be illegal.
Comment #3 by bearophile_hugs — 2012-04-06T10:06:56Z
(In reply to comment #2)
> Not a bug. break applies to the innermost statement that can be broken out
> from. This includes foreach.
>
> (I use this idiom often. Use labeled break to break from the switch.)
You are right, thank you.
(My error was to think that "static foreach" doesn't support break.)
Comment #4 by bearophile_hugs — 2012-04-06T10:18:05Z
Reopened, because you have missed the error message in my bug report.
Using a labeled break:
import core.stdc.stdio: printf;
template TypeTuple(TList...) {
alias TList TypeTuple;
}
void main() {
char c = 'b';
MySwitch: switch (c) {
case 'a': printf("1 a\n"); break;
foreach (o; TypeTuple!('b', 'c')) {
case o: printf("2 %c\n", c); break MySwitch;
}
default: printf("default");
}
}
DMD 2.059 beta gives (compiling with -w):
test.d(12): Error: switch case fallthrough - use 'goto default;' if intended
I have also changed the issue title to better reflect the problem, now the Keywords is 'diagnostic' because it's giving a warning where there is nothing to warn against.
Comment #5 by bearophile_hugs — 2012-04-06T11:32:39Z
This compiles with no warnings and it seems to work correctly, but I don't fully understand it:
import core.stdc.stdio: printf;
template TypeTuple(TList...) {
alias TList TypeTuple;
}
void main() {
char c = 'b';
MySwitch: switch (c) {
case 'a': printf("1 a\n"); break;
foreach (o; TypeTuple!('b', 'c')) {
case o: printf("2 %c\n", c); break;
}
break;
default: printf("default");
}
}
Is it correct? if the break inside here is meant to be the foreach break:
{ case o: printf("2 %c\n", c); break; }
Then why a single break is enough after:
foreach (o; TypeTuple!('b', 'c')) {
case o: printf("2 %c\n", c); break;
}
break;
despite the foreach synthesizes more than one switch case?
Comment #6 by dmitry.olsh — 2012-04-06T11:37:01Z
(In reply to comment #5)
> This compiles with no warnings and it seems to work correctly, but I don't
> fully understand it:
>
>
> import core.stdc.stdio: printf;
> template TypeTuple(TList...) {
> alias TList TypeTuple;
> }
> void main() {
> char c = 'b';
> MySwitch: switch (c) {
> case 'a': printf("1 a\n"); break;
> foreach (o; TypeTuple!('b', 'c')) {
> case o: printf("2 %c\n", c); break;
> }
> break;
> default: printf("default");
> }
> }
>
>
> Is it correct? if the break inside here is meant to be the foreach break:
Yes.
> { case o: printf("2 %c\n", c); break; }
No it's {case 0: printf("2 %c\n", c); }
the break did his job already, it can't work twice.
>
> Then why a single break is enough after:
>
> foreach (o; TypeTuple!('b', 'c')) {
> case o: printf("2 %c\n", c); break;
> }
Then the code below gives you one break after that statement.
> break;
>
> despite the foreach synthesizes more than one switch case?
foreach synthesizes exactly one statement here.
Comment #7 by timon.gehr — 2012-04-06T11:41:49Z
(In reply to comment #5)
> This compiles with no warnings and it seems to work correctly, but I don't
> fully understand it:
>
>
> import core.stdc.stdio: printf;
> template TypeTuple(TList...) {
> alias TList TypeTuple;
> }
> void main() {
> char c = 'b';
> MySwitch: switch (c) {
> case 'a': printf("1 a\n"); break;
> foreach (o; TypeTuple!('b', 'c')) {
> case o: printf("2 %c\n", c); break;
> }
> break;
> default: printf("default");
> }
> }
>
>
> Is it correct? if the break inside here is meant to be the foreach break:
> { case o: printf("2 %c\n", c); break; }
>
> Then why a single break is enough after:
>
> foreach (o; TypeTuple!('b', 'c')) {
> case o: printf("2 %c\n", c); break;
> }
> break;
>
> despite the foreach synthesizes more than one switch case?
Your code is expanded to:
void main() {
char c = 'b';
switch (c) {
case 'a': printf("1 a\n"); break;
{case 'b': printf("2 %c\n", c); goto break_foreach;}
{case 'c': printf("2 %c\n", c); goto break_foreach;}
break_foreach: break;
default: printf("default");
}
}
Comment #8 by bearophile_hugs — 2012-04-06T13:12:01Z
(In reply to comment #7)
> Your code is expanded to:
>
> void main() {
> char c = 'b';
> switch (c) {
> case 'a': printf("1 a\n"); break;
> {case 'b': printf("2 %c\n", c); goto break_foreach;}
> {case 'c': printf("2 %c\n", c); goto break_foreach;}
> break_foreach: break;
> default: printf("default");
> }
> }
Thank you again Timon :-) So there is no bug here.
This was not easy to understand for me. (Maybe D newbies will enjoy to read an example of this in some D tips&trickls somewhere, or maybe it was just a conceptualization problem of mine.)
Issue closed again, as invalid.
Comment #9 by braddr — 2012-04-06T15:11:51Z
I think that the expansion of the static foreach is wrong. It explains the behavior, but doesn't excuse it.
I think the bug report is valid.
Comment #10 by timon.gehr — 2012-04-06T15:21:47Z
(In reply to comment #9)
> I think that the expansion of the static foreach is wrong. It explains the
> behavior, but doesn't excuse it.
>
> I think the bug report is valid.
What would be your expected behavior?
Comment #11 by initrd.gz — 2015-05-12T16:00:16Z
This also happens if you use return instead of break in the foreach loop, which should be valid (since return doesn't care about loops):
import std.stdio;
import std.typetuple;
alias SwitchCases = TypeTuple!("a", "b", "c");
int main() {
string s = "a";
switch(s) {
case "special":
writeln("Special case!");
return 0;
foreach(c; SwitchCases) {
case c:
writeln(c);
return 1;
}
default:
writeln("default case");
return 2;
}
}
$ rdmd -w ~/test.d
/home/col/test.d(21): Warning: switch case fallthrough - use 'goto default;' if intended
The switch works properly even if you ignore the warning, and moving the special case to after the foreach loop removes the warning.
Comment #12 by mathias.lang — 2016-09-21T17:27:28Z
I think the bug report from c4 and c11 is actually a diagnostic issue: https://issues.dlang.org/show_bug.cgi?id=7390
The original motivation for this bug report was actually not a bug. It has been proposed as an ER by Martin here: https://issues.dlang.org/show_bug.cgi?id=14887
So, since both the ER and the bug found are reported, I'll close this again. Feel free to direct any further discussion to the bug report or the ER, or reopen if I missed something.