Bug 14411 – switch statement: docs/behavior differ

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-04-05T13:31:25Z
Last change time
2018-01-03T22:32:02Z
Assigned to
No Owner
Creator
Cody Casterline

Attachments

IDFilenameSummaryContent-TypeSize
1507switch.dexample casetext/plain369

Comments

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.
Comment #8 by mathias.lang — 2016-06-14T19:29:33Z
Comment #9 by github-bugzilla — 2016-06-14T19:59:02Z
Commits pushed to master at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/b76b0ce720197bc58c37e706c4788f8c5517dc7c Fix issue 14411 - Make implicit switch case fallthrough an error https://github.com/dlang/dmd/commit/6e2bf76d289c128a46e50284fb632f8f0c696d31 Merge pull request #5866 from mathias-lang-sociomantic/fix-14411 Fix issue 14411 - Make implicit switch case fallthrough an error
Comment #10 by github-bugzilla — 2016-10-01T11:47:44Z
Commits pushed to stable at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/b76b0ce720197bc58c37e706c4788f8c5517dc7c Fix issue 14411 - Make implicit switch case fallthrough an error https://github.com/dlang/dmd/commit/6e2bf76d289c128a46e50284fb632f8f0c696d31 Merge pull request #5866 from mathias-lang-sociomantic/fix-14411
Comment #11 by github-bugzilla — 2018-01-03T22:32:02Z
Commit pushed to dmd-cxx at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/715b42534aa9c2b66d6f2fd08f973d3b3dfd476a Fix issue 14411/16625 - Deprecate implicit switch case fallthrough.