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-4468] "Switch must be exhaustive" should suggest at least 1 incompletely handled case. #47045

Closed
swift-ci opened this issue Apr 2, 2017 · 2 comments
Assignees
Labels
compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation improvement

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Apr 2, 2017

Previous ID SR-4468
Radar None
Original Reporter mattgallagher (JIRA User)
Type Improvement
Status Resolved
Resolution Done

Attachment: Download

Environment

Xcode 8.3, Swift 3.1

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

md5: 952fe45970b7ad1ce8b237d8be29d7ba

Issue Description:

The error message "Switch must exhaustive, consider adding a default clause" should be improved by stating at least one case that is incompletely handled so the programmer can immediately do something to start fixing the problem.

Consider the following code in Swift 3.1, which will give the error "Switch must exhaustive, consider adding a default clause":

enum Something {
case a, b, c, d, r, e, f, g
}

let s: Something = someFunctionReturningSomething()
switch s {
case .a: print("a")
case .b: print("b")
case .c: print("c")
case .d: print("d")
case .e: print("e")
case .f: print("f")
case .g: print("g")
}

Upon seeing the error message, you can't immediately fix the problem. Instead, you need to slowly and painstakingly analyze the code, comparing cases in the enum to cases in the switch to find which case is accidentally omitted. This can be difficult in the presence of where clauses and can be very time consuming for enums with a large number of cases.

This is work that the compiler should be able to do (theoretically, it has already done the work to recognize the need for the error in the first place).

A more helpful message would be: "Switch must be exhaustive; cases, including 'r', are incompletely handled. Consider adding a default clause."

@CodaFi
Copy link
Member

CodaFi commented Apr 3, 2017

"It has already done the work" to determine that there is a control flow path out of this switch - the check is not semantic and it needs to be.

@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
compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation improvement
Projects
None yet
Development

No branches or pull requests

2 participants