Thread overview
[Issue 6226] New: Switch with impossible cases
Jun 30, 2011
kennytm@gmail.com
June 30, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6226

           Summary: Switch with impossible cases
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: diagnostic
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2011-06-29 17:05:15 PDT ---
In the following code the cases 400 and 200 can't happen, because they are ouside the values range of char and byte. I suggest to raise a warning in such cases (this compiles with no errors on DMD 2.053):


void main() {
    char c;
    switch (c) {
        case 'a': break;
        case 400: break;
        default:
    }
    byte x;
    switch (x) {
        case 10: break;
        case 200: break;
        default:
    }
}


See here for real world bug cases: http://www.viva64.com/en/d/0142/


This too generates no errors, but I think this is less often a bug:

void main() {
    char c;
    if (c == 400) {}
}

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


kennytm@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kennytm@gmail.com


--- Comment #1 from kennytm@gmail.com 2011-06-30 01:46:49 PDT ---
V551 happens because in C a 'char' can be signed and people forget that. I doubt if the same argument could apply to D.

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



--- Comment #2 from bearophile_hugs@eml.cc 2011-06-30 03:34:31 PDT ---
(In reply to comment #1)
> V551 happens because in C a 'char' can be signed and people forget that. I doubt if the same argument could apply to D.

Mistakes happen in D too, you use a variable with a range smaller than the cases you have used in the switch. I'd like the compiler to tell me when a case is impossible, because it's probably a bug, and this warning/error doesn't damage generic code a lot because in generic code you are always able to add cases using a "static if":

switch (foo) {
  case 0: break;
  static if (typeof(foo).max >= 200)
    case 200: break;
  default:
}


Regarding your specific comment, in my second example I have used a byte. In another bug report (that's now a WONTFIX) I have argued that for the mind of most programmers (me too) a byte is an unsigned value (this is also why in C# you don't have just byte and ubyte, there is sbyte). If by mistake you think of a D byte value as an unsigned value it's easy to add a case 200, that can't happen.

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