Uploaded image for project: 'Swift'
  1. Swift
  2. SR-766

Switch doesn't need to cover negative Int cases

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Medium
    • Resolution: Done
    • Component/s: Compiler
    • Labels:
      None
    • Environment:

      Mac OSX El Capitan 10.11.3
      Xcode 7.2.1 (7C1002)
      MacBook Pro 13" mid 2009

      Description

      Example 1:

              let myAge = 4
              
              switch myAge {
              case 0...17: print("I'm young")
              case 18...60: print("I'm an adult")
              case 61...Int.max: print("I'm old")
              }
      

      --> No errors or warnings. Prints "I'm young". Although Alt-Click shows that myAge is an Int, Xcode doesn't insist on handling the possible negative cases.

      Example 2:

              let myAge = -3
              
              switch myAge {
              case -3: print("?")
              }
      

      --> No errors or warnings. Prints "?". Like in the first example, Xcode should complain and ask for a default case, but it doesn't.

      Example 3:

              let myAge = -3
              
              switch myAge {
              case -3: print("?")
              case 0...17: print("I'm young")
              case 18...60: print("I'm an adult")
              case 61...Int.max: print("I'm old")
              }
      

      --> No errors. Prints "?". Warning in the line with case 0...17: "Will never be executed". Deleting that line pushes the warning to the next line, and so on, until only the negative case is left.

      Example 4:

              let myAge = -3
              
              switch myAge {
              case 0...17: print("I'm young")
              case 18...60: print("I'm an adult")
              }
      

      --> As soon as I delete the negative case, the expected error appears: "Switch must be exhaustive, consider adding a default clause". (Note that I left out the positive cases 61...Int.max.)

      Example 5:

              let myAge = 4
              
              switch myAge {
              case -3: print("?")
              case 0...17: print("I'm young")
              case 18...60: print("I'm an adult")
              case 61...Int.max: print("I'm old")
              }
      

      --> No errors. Warning in the line with case -3: "Will never be executed". Prints "I'm young".

      Example 6:

              let myAge = -4
              
              switch myAge {
              case -3: print("?")
              case 0...17: print("I'm young")
              case 18...60: print("I'm an adult")
              case 61...Int.max: print("I'm old")
              }
      

      --> Warning in the line with case -3: "Will never be executed". Compiles, but crashes on runtime: "fatal error: Range end index has no valid successor" marking the line with "case 61...Int.max" with: "Thread 1: EXC_BAD_INSTRUCTION (code=I386_INVOP, subcode=0x0)"

      It seems that negative Ints in a switch are not handled correctly. It could be a swift problem or just Xcode not warning and showing errors correctly. Xcode shouldn't allow for a switch to not handle all possible cases.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                lennartwisbar Lennart Wisbar
                Reporter:
                lennartwisbar Lennart Wisbar
              • Votes:
                0 Vote for this issue
                Watchers:
                5 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: