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-9014] Pattern matching doesn't work for overloaded enum cases #51517

Open
swift-ci opened this issue Oct 16, 2018 · 3 comments
Open

[SR-9014] Pattern matching doesn't work for overloaded enum cases #51517

swift-ci opened this issue Oct 16, 2018 · 3 comments
Labels
compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-9014
Radar None
Original Reporter aaargh! (JIRA User)
Type Sub-task
Environment

Swift 4.1 Playground in Xcode 10.0 (10A254a) on macOS 10.14 (18A391)

Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Sub-task
Assignee None
Priority Medium

md5: bea00aad8d9891b0317e78ea34c47706

Parent-Task:

Issue Description:

When overloading enum cases with labelled associated values, pattern matching only works for the last case defined with the same base name.

Minimal test-case:

public enum Result {
    case Succes
    case NonFatalError(message: String)
    case NonFatalError(messages: [String])
    case FatalError
}


var test1:Result = .NonFatalError(message: "line1")
var test2:Result = .NonFatalError(messages: ["line1", "line2"])


if case .NonFatalError(let message) = test1 {
    print("message: \(message)")
}


if case .NonFatalError(let messages) = test2 {
    print("message: \(messages)")
}

Only the second pattern matches.

When I reverse the order of the two NonFatalError cases, only the first if-case matches.

Expected behaviour: either the enum should not compile, or the pattern matching should work.

@belkadan
Copy link
Contributor

You didn't put a label in your pattern-match: case .NonFatalError(messages: let messages). This should probably complain about being ambiguous, though. cc CodaFi (JIRA User)

@swift-ci
Copy link
Collaborator Author

swift-ci commented Jan 9, 2019

Comment by Nikola Lajic (JIRA)

@belkadan I ran into the same issue today. Your suggestion throws another unrelated error. In addition to that, a non-exhaustive warning is not shown for the switch.

import UIKit


enum Result {
    case finished(error: Error)
    case finished(response: String)
}


var a: Result = .finished(response: "A")


switch a {
case .finished(let response): // type is string
    print(response)
    // missing warning for non exhaustive switch
}


switch a {
case .finished(response: let response):
    print(response)
case .finished(error: let error): // error: tuple pattern element label 'error' must be 'response'
    print(error)
}

@belkadan
Copy link
Contributor

belkadan commented Jan 9, 2019

I think we just haven't finished implementing SE-0155.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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
Projects
None yet
Development

No branches or pull requests

2 participants