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-2293] Detect enum cases with uninhabited types as unreachable #44900

Closed
swift-ci opened this issue Aug 7, 2016 · 3 comments
Closed

[SR-2293] Detect enum cases with uninhabited types as unreachable #44900

swift-ci opened this issue Aug 7, 2016 · 3 comments
Assignees
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

swift-ci commented Aug 7, 2016

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

Swift Development Snapshot 2016-08-04

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee @CodaFi
Priority Medium

md5: 3527892211bcbd8b2b306ab21047581f

Issue Description:

It might be useful to use uninhabited types in enums, for example:

enum NeverError: Error { }
enum Result <T, E: Error> {
    case success(T)
    case failure(E)  
} 
typealias GuaranteedResult<T> = Result<T, NeverError>

However, the compiler will not correctly consider the case failure as unreachable

func foo(result: GuaranteedResult<Int>) {
    switch result {
    case .success(let x): …
    case .failure(let n): … // should warn that this code is unreachable
    }
}

An exhaustive switch for GuaranteedResult should only consist of one case:

switch result {
    case .success(let x): …
}
// switch should be considered exhaustive
@xwu
Copy link
Collaborator

xwu commented Aug 8, 2016

Is the new `Never` type currently usable as a true bottom type in this scenario, and does it suffer from the same shortcoming?

@swift-ci
Copy link
Collaborator Author

Comment by Loïc Lecrenier (JIRA)

@xwu Never suffers from the same shortcoming

Here is a more general example:

enum Either <A, B> {
    case left(A)
    case right(B)
}

func foo(_ x: Either<Int, Never>) {
    switch x {
    case .left(let a): print(a)
    case .right(let b): print(b) // should warn that this is unreachable
    }
}

func bar(_ x: Either<Int, Never>) {
    switch x {
    case .left(let a): print(a)
    // should consider switch to be exhaustive
    }
}

@CodaFi
Copy link
Member

CodaFi commented Apr 29, 2017

Resolved on master by #8908.

@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

3 participants