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-13105] "Switch must be exhaustive" fixit produces invalid swift code for enums with associated values #55551

Closed
cltnschlosser opened this issue Jun 28, 2020 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@cltnschlosser
Copy link
Collaborator

Previous ID SR-13105
Radar None
Original Reporter @cltnschlosser
Type Bug
Status Closed
Resolution Invalid
Additional Detail from JIRA
Votes 0
Component/s Source Tooling
Labels Bug
Assignee None
Priority Medium

md5: f794ad288ced9a41039625f6927bf9d0

Issue Description:

The fixit provides cases like: case .sourceInput(path: let path).
Correct swift would be case .sourceInput(let path) or case let .sourceInput(path)

// Type:
public enum Filelist: Hashable {
  case input(path: VirtualPath, type: FileType)
  case sourceInput(path: VirtualPath)
  case inputAndSourceInput(path: VirtualPath, type: FileType)
  case output(path: VirtualPath, type: FileType)
  case supplementaryOutput(path: VirtualPath)

  var path: VirtualPath {
    switch self {
    case .input(let path, _),
         .sourceInput(let path),
         .inputAndSourceInput(let path, _),
         .output(let path, _),
         .supplementaryOutput(let path):
      return path
    }
  }
}


// Input:
switch self {

}


/*
Diagnostic:
Switch must be exhaustive
Do you want to add missing cases?
*/


// Output:
switch self {

case .input(path: let path, type: let type):
  <#code#>
case .sourceInput(path: let path):
  <#code#>
case .inputAndSourceInput(path: let path, type: let type):
  <#code#>
case .output(path: let path, type: let type):
  <#code#>
case .supplementaryOutput(path: let path):
  <#code#>
}


// Expected:
switch self {
case let .input(path, type):
  <#code#>
case let .sourceInput(path):
  <#code#>
case let .inputAndSourceInput(path, type):
  <#code#>
case let .output(path, type):
  <#code#>
case let .supplementaryOutput(path):
  <#code#>
}
@typesanitizer
Copy link

I think using the label is ok, it does compile (and improves clarity at the usage site). Consider the following simplified example:

enum X {
    case a(b: Int)
}
func f(_ x: X) {
    switch x {
    case .a(b: let b): print(b)
    }
}
 

This compiles fine with a recent Swift compiler. Try testing it in a Playground! 🙂

@cltnschlosser
Copy link
Collaborator Author

Huh, yeah you're right. Is that a recent addition? or has it just always bothered me, so I constantly change it before giving it a chance.

@typesanitizer
Copy link

Nope, it has always worked AFAICT. For example, you can see that it works for Swift 3.1.1 here: https://godbolt.org/z/sbvQUU

@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.
Projects
None yet
Development

No branches or pull requests

2 participants