Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-766] Switch doesn't need to cover negative Int cases #43378

Closed
swift-ci opened this issue Feb 18, 2016 · 5 comments
Closed

[SR-766] Switch doesn't need to cover negative Int cases #43378

swift-ci opened this issue Feb 18, 2016 · 5 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-766
Radar None
Original Reporter lennartwisbar (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

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

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee lennartwisbar (JIRA)
Priority Medium

md5: 4b529bffcbdbff8bd6095f1ea7a23423

is duplicated by:

  • SR-13516 Exhaustive switch not recognized as exhaustive

relates to:

  • SR-5088 Integer switch not seen as exhaustive

Issue 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.

@belkadan
Copy link
Contributor

@jckarter, where does switch exhaustiveness checking happen? Sema or SIL?

@jckarter
Copy link
Member

It's handled in SIL diagnostics.

@benrimmington
Copy link
Collaborator

All examples now have the error:
Switch must be exhaustive, consider adding a default clause

Tested with:

  • Apple Swift version 3.0 (swiftlang-800.0.46.2 clang-800.0.38)

  • Xcode version 8.0 (8A218a)

  • macOS Sierra version 10.12 (16A323)

@jckarter
Copy link
Member

jckarter commented Oct 3, 2016

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.

@jckarter
Copy link
Member

jckarter commented Oct 4, 2016

At least in all your examples, though, raising a "must be exhaustive" error looks like the correct thing to do. Sounds like this is fixed.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

4 participants