You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
--> 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:
letmyAge = -3switchmyAge {
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:
letmyAge = -3switchmyAge {
case -3: print("?")
case0...17: print("I'm young")
case18...60: print("I'm an adult")
case61...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.
--> 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:
letmyAge = 4switchmyAge {
case -3: print("?")
case0...17: print("I'm young")
case18...60: print("I'm an adult")
case61...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:
letmyAge = -4switchmyAge {
case -3: print("?")
case0...17: print("I'm young")
case18...60: print("I'm an adult")
case61...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.
The text was updated successfully, but these errors were encountered:
Unfortunately, integer operations like '...' and '<' are just plain functions to Swift, so it'd be difficult to do this kind of analysis. Even with special case understanding of integer intervals, I think there are still cases in the full generality of pattern matching for which exhaustiveness matching would be undecidable. We may eventually be able to handle some cases, but there will always be special cases involved in doing so.
Environment
Mac OSX El Capitan 10.11.3
Xcode 7.2.1 (7C1002)
MacBook Pro 13" mid 2009
Additional Detail from JIRA
md5: 4b529bffcbdbff8bd6095f1ea7a23423
is duplicated by:
relates to:
Issue Description:
Example 1:
--> 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:
--> 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:
--> 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:
--> 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:
--> No errors. Warning in the line with case -3: "Will never be executed". Prints "I'm young".
Example 6:
--> 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.
The text was updated successfully, but these errors were encountered: