Thread overview
[Issue 4349] New: Deprecate automatic case fallthrough
Sep 28, 2010
Jonathan M Davis
Jan 02, 2012
Brad Roberts
June 19, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4349

           Summary: Deprecate automatic case fallthrough
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2010-06-19 13:06:48 PDT ---
Bugs caused by unwanted fall-through between cases in switch statement are common enough to push designers of other languages (like C#) to find ways to avoid them.

D2 has already in place all is necessary to avoid this source of problems. The
only missing part is to disallow cases that miss an explicit return, goto,
break, assert(0), exit(), or similar.

C code ported to D that assumes the fall-through will just raise errors that can be solved adding "goto case;" at the end of the cases.

The usage of "goto case;" will not be excessive because D allows both ranged cases and multiple with a comma that allow to compress switch code.

case 1, 2, 3:
case 1: .. case 3:

As for the multiple returns in a function this kind of code needs some care:

switch (x) {
    case 0:
        if (y)
            goto case;
        else
            return x;
    default:
        break;
}


Bug 3536 shows an alternative proposal.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 21, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4349



--- Comment #1 from bearophile_hugs@eml.cc 2010-06-21 03:17:29 PDT ---
Sean Kelly and Michel Fortin have shown some code examples.

This D code:

switch (x) {
    case 1: case 2: case 3: case 4:
        doSomething();
        break;
    default:
        whatever();
}


With the new proposal can be translated like this (this is already D syntax):

switch (x) {
    case 1, 2, 3, 4:
        doSomething();
        break;
    default:
        whatever();
        break;
}


C/C++ programmers that will want to use D will have to learn the new rule that says: "In D there is no automatic case fallthrough". But allowing the fallthrough when there are no statements after the label is a special case to the general rule that they have to learn: ", unless there are no statements inside the case.".

Special cases are bad in a programming language, they make manuals longer, require more time and energy for the programmer to learn them, make the compiler more complex and more buggy, and often slow down the programming even whey they were created to give a handy special case.

-----------------

This D code gives a little more problems:

switch (x) {
    case 1: .. case 10:
    case 22: .. case 32:
    case 52, 64:
        doSomething();
        break;
    default:
        whatever();
}


With the new proposal it can be translated like this (this is new syntax), that considers a closed range of values one of the possible items of the list of cases:

switch (x) {
    case 1: .. case 10,
    case 22: .. case 32,
    52, 64:
        doSomething();
        break;
    default:
        whatever();
}


Or just like this (this is normal D syntax):

switch (x) {
    case 1: .. case 10: goto case;
    case 22: .. case 32: goto case;
    case 52, 64:
        doSomething();
        break;
    default:
        whatever();
}


This shorter syntax (that uses a single 'case' statement) is now not available:

switch (x) {
    case 1 ... 10, 22 ... 32, 52, 64:
        doSomething();
        break;
    default:
        whatever();
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 21, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4349


Andrei Alexandrescu <andrei@metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrei@metalanguage.com


--- Comment #2 from Andrei Alexandrescu <andrei@metalanguage.com> 2010-06-21 06:30:56 PDT ---
This is all unnecessary aggravation. I don't think it's complicated for anyone to understand that there's a requirement to insert a "break;" (or another control flow statement) in places where a "break;" should be inserted anyway.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 21, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4349



--- Comment #3 from bearophile_hugs@eml.cc 2010-06-21 11:19:01 PDT ---
Answer to Comment 2: I agree that it's easy for people to understand that there's a requirement to insert a break or similar. This is what I have written in the Description.

But this was not the point of my Comment 1. In Comment 1 I was talking about the *exception* to that easy rule. What I was trying to express is that I don't like such exception to the rule.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 28, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4349



--- Comment #4 from bearophile_hugs@eml.cc 2010-09-28 15:35:45 PDT ---
Walter has now vaguely accepted to require a control statements at the end of each case.

But some people have asked for an exception to that rule, when a case is empty.

This bug 4349 is against that exception.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 28, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4349


Jonathan M Davis <jmdavisProg@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jmdavisProg@gmx.com


--- Comment #5 from Jonathan M Davis <jmdavisProg@gmx.com> 2010-09-28 16:21:29 PDT ---
I think that the exception makes good sense and wouldn't mind having it but that it's not particularly necessary. Thanks to ranged case statements and the like, the need for multiple case statements which all fall through to a single block of code is greatly reduced if not outright eliminated.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 02, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4349


Brad Roberts <braddr@puremagic.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |braddr@puremagic.com


--- Comment #6 from Brad Roberts <braddr@puremagic.com> 2012-01-01 20:45:10 PST ---
This one is ready to be closed, right?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 02, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4349


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


--- Comment #7 from bearophile_hugs@eml.cc 2012-01-02 04:35:19 PST ---
(In reply to comment #6)
> This one is ready to be closed, right?

I don't like a lot the presence of a special case to the D rule that implicit fall-through is disallowed in D (this code compiles):


void main() {
    int x;
    switch (x) {
        case 1: /* x++; break; */
        case 2: break;
        default: break;
    }
}


But I presume you are right, so I close this issue.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------