Comment #0 by cody.casterline+dlang — 2015-04-05T13:31:25Z
Created attachment 1507
example case
http://dlang.org/statement.html#SwitchStatement says:
"A ScopeStatementList must either be empty, or be ended with a ContinueStatement, BreakStatement, ReturnStatement, GotoStatement, ThrowStatement or assert(0) expression unless this is the last case. This is to set apart with C's error-prone implicit fall-through behavior."
And yet, implicit fall-through behavior is what I'm seeing with this example. (attached)
Comment #1 by cody.casterline+dlang — 2015-04-05T13:32:39Z
Aw. You have to download the example to see it. To save some time:
$ cat switch.d
#!/usr/bin/env rdmd
import std.stdio;
void main()
{
foreach (i; 1..7)
{
fn(i);
}
}
void fn(int i)
{
writeln("fn(", i, ")");
switch(i)
{
case 1, 2:
writeln(" A");
// break; not required!?
case 3:
case 4:
writeln(" B");
// break; not required!?
case 5:
writeln(" C");
// break; not required!?
default:
writeln(" D");
}
}
[codyc@eteco-2:~/test/dlang 01:32:02]
$ ./switch.d
fn(1)
A
B
C
D
fn(2)
A
B
C
D
fn(3)
B
C
D
fn(4)
B
C
D
fn(5)
C
D
fn(6)
D
Comment #2 by ketmar — 2015-04-05T17:09:50Z
specs says nothing about other cases. turn on compiler warnings and you will see something like this:
z00.d(25): Warning: switch case fallthrough - use 'goto case;' if intended
z00.d(28): Warning: switch case fallthrough - use 'goto default;' if intended
what compiler doesn't tell you, however, is that "case 1, 2:" is not what you may think, it equals to "case 2:".
p.s. no, i still don't know why warnings aren't turned off by default.
Comment #3 by cody.casterline+dlang — 2015-04-05T17:19:50Z
> specs says nothing about other cases.
Well, it says that ScopeStatementList "must" be empty, or be ended [...]. Which I read to mean there are no other cases. "This is to set apart with C's error-prone implicit fall-through behavior." so I would not expect fall-through to work like in C.
> "case 1, 2:" is not what you may think, it equals to "case 2:".
Oh? Then why did fn(1) run "A"?
Comment #4 by ketmar — 2015-04-05T19:25:23Z
(In reply to Cody Casterline from comment #3)
> > specs says nothing about other cases.
>
> Well, it says that ScopeStatementList "must" be empty, or be ended [...].
> Which I read to mean there are no other cases. "This is to set apart with
> C's error-prone implicit fall-through behavior." so I would not expect
> fall-through to work like in C.
somehow that means "programmer must adhere to specs", but not "compiler must not accept that code". i don't know why.
> > "case 1, 2:" is not what you may think, it equals to "case 2:".
>
> Oh? Then why did fn(1) run "A"?
hm. 'cause i misread the specs. sorry. i'm too used to the "in comma expressions only the last result matters" rule.
Comment #5 by j — 2016-01-15T22:24:22Z
*** Issue 15569 has been marked as a duplicate of this issue. ***
Comment #6 by 4burgos — 2016-06-14T16:09:18Z
*** Issue 16173 has been marked as a duplicate of this issue. ***
Comment #7 by andrei — 2016-06-14T18:53:48Z
This has been the behavior for a while. It's time to enact this in the "main" language.