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-12908] Enum comparison diagnostics not helpful #55354

Open
swift-ci opened this issue May 30, 2020 · 5 comments
Open

[SR-12908] Enum comparison diagnostics not helpful #55354

swift-ci opened this issue May 30, 2020 · 5 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-12908
Radar rdar://problem/64129806
Original Reporter stevex (JIRA User)
Type Bug
Environment

Xcode 11.5

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, DiagnosticsQoI, TypeChecker
Assignee None
Priority Medium

md5: 8da8c52d1253a9ea892c633357d60129

Issue Description:

Not a bug, but maybe an enum usability enhancement via better diagnostics. I went through some head scratching trying to figure out how to use an if statement to check a Result (or any enum with an associated value) for success.

The message that mentioned DispatchTimeoutResult seems out of left field, but neither message was actually helpful.

import Cocoa
import Foundation

enum TwoValues {
    case one
    case two
}


let one = TwoValues.one


if one == .one {
    print("As expected")
}


if case .one = one {
    print("Also ok but not how you'd usually use an enum")
}


let r = Result<String, Error>.success("Yay")


// Cannot convert value of type 'Result<String, Error>' to expected argument type 'DispatchTimeoutResult'
if r == .success {
    print("yay?")
}


// Binary operator '==' cannot be applied to two 'Result<String, Error>' operands
if r == .success(_) {
    print("yay?")
}


// This works
if case .success(_) = r {
    print("yay!")
}

Thanks

@LucianoPAlmeida
Copy link
Collaborator

cc @xedin @hamishknight

@LucianoPAlmeida
Copy link
Collaborator

The problem here seems like for the case:

// Cannot convert value of type 'Result<String, Error>' to expected argument type 'DispatchTimeoutResult'
if r == .success {
    print("yay?")
}

The solver is inferring .success as DispatchTimeoutResult.result, I didn't debug it, but apparently that's the best solution it is finding of all disjunction choices.
So maybe adjusting this to favor the binding Result<String, Error> in some way (since the type of r is known upfront) could improve the situation? @xedin

For the other case

// Binary operator '==' cannot be applied to two 'Result<String, Error>' operands
if r == .success(_) {
    print("yay?")
}

Just tweak the diagnostics when involving an enum with value?

@xedin
Copy link
Member

xedin commented Jun 8, 2020

Diagnostic for `r == .success` is definitely incorrect it should at least be `cannot be applied to operands`, but the main issue here is that `==` can't be used to pattern match `r` to `.success(_)`, that's the job of `if case`.

@xedin
Copy link
Member

xedin commented Jun 8, 2020

@swift-ci create

@LucianoPAlmeida
Copy link
Collaborator

@xedin Yeah, there is something interesting happening here... if we remove the Foundation import, we got a `cannot be applied to operands` for `r == .success` that is why I mentioned that it could be that the solver is finding `DispatchTimeoutResult` defined in Foundation as the solution for the base type of `.success`(DispatchTimeoutResult.success that is a case without associated value) which leads to bad diagnostic.

I still didn't debug it so this is just a guess, but I was curious about why this may be happening.

@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
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants