Jump to page: 1 2
Thread overview
[Issue 14411] switch statement: docs/behavior differ
Apr 05, 2015
Cody Casterline
Apr 05, 2015
Ketmar Dark
Apr 05, 2015
Cody Casterline
Apr 05, 2015
Ketmar Dark
Jan 15, 2016
Jake Drahos
Jun 14, 2016
Nemanja Boric
Jun 14, 2016
Mathias Lang
April 05, 2015
https://issues.dlang.org/show_bug.cgi?id=14411

--- Comment #1 from Cody Casterline <cody.casterline+dlang@gmail.com> ---
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

--
April 05, 2015
https://issues.dlang.org/show_bug.cgi?id=14411

--- Comment #2 from Ketmar Dark <ketmar@ketmar.no-ip.org> ---
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.

--
April 05, 2015
https://issues.dlang.org/show_bug.cgi?id=14411

--- Comment #3 from Cody Casterline <cody.casterline+dlang@gmail.com> ---
> 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"?

--
April 05, 2015
https://issues.dlang.org/show_bug.cgi?id=14411

--- Comment #4 from Ketmar Dark <ketmar@ketmar.no-ip.org> ---
(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.

--
January 15, 2016
https://issues.dlang.org/show_bug.cgi?id=14411

Jake Drahos <j@kedrahos.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |j@kedrahos.com

--- Comment #5 from Jake Drahos <j@kedrahos.com> ---
*** Issue 15569 has been marked as a duplicate of this issue. ***

--
June 14, 2016
https://issues.dlang.org/show_bug.cgi?id=14411

Nemanja Boric <4burgos@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |4burgos@gmail.com

--- Comment #6 from Nemanja Boric <4burgos@gmail.com> ---
*** Issue 16173 has been marked as a duplicate of this issue. ***

--
June 14, 2016
https://issues.dlang.org/show_bug.cgi?id=14411

Andrei Alexandrescu <andrei@erdani.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrei@erdani.com
                 OS|Mac OS X                    |All

--- Comment #7 from Andrei Alexandrescu <andrei@erdani.com> ---
This has been the behavior for a while. It's time to enact this in the "main" language.

--
June 14, 2016
https://issues.dlang.org/show_bug.cgi?id=14411

Mathias Lang <mathias.lang@sociomantic.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mathias.lang@sociomantic.co
                   |                            |m

--- Comment #8 from Mathias Lang <mathias.lang@sociomantic.com> ---
https://github.com/dlang/dmd/pull/5866

--
June 14, 2016
https://issues.dlang.org/show_bug.cgi?id=14411

--- Comment #9 from github-bugzilla@puremagic.com ---
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

--
October 01, 2016
https://issues.dlang.org/show_bug.cgi?id=14411

--- Comment #10 from github-bugzilla@puremagic.com ---
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

--
« First   ‹ Prev
1 2