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-6619] Unexpected error, bad fix-its with enum comparison #49168

Closed
swift-ci opened this issue Dec 15, 2017 · 5 comments
Closed

[SR-6619] Unexpected error, bad fix-its with enum comparison #49168

swift-ci opened this issue Dec 15, 2017 · 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-6619
Radar rdar://problem/36077302
Original Reporter rgov (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

Apple Swift version 4.0.3 (swiftlang-900.0.74.1 clang-900.0.39.2)
Target: x86_64-apple-macosx10.9

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

md5: df064141c8c7ed883594dd34703a74d5

Issue Description:

enum GameState: Int {
    case Started = 0
    case Initialized
    case BookAppeared
    case BookPickedUp
    case BookOpened
    case PageTurnedOnce
    case PageTurnedTwice
}

var state: GameState = .Started
...
if state > .BookOpened && state < .PageTurnedTwice {
    ...
}

Since my enum cases have integer values, I would expect to be able to compare them this way. Instead, I get this confusing error:

Binary operator '>' cannot be applied to operands of type 'GameState' and '_'

If I turn the comparison condition into

state > GameState.BookOpened && state < GameState.PageTurnedTwice

then the first `s` becomes underlined in Xcode and it suggests a Fix-it of

Self(state.rawValue) > GameState.BookOpened && Self(state.rawValue) < GameState.PageTurnedTwice

I can kind of understand the `.rawValue` access, though I don't agree it should be required, but I don't understand the call to `Self`. The compiler doesn't either, it asks me if I wanted `self`, and of course that doesn't work either.

@swift-ci
Copy link
Collaborator Author

Comment by Ryan Govostes (JIRA)

A solution to the problem is to make the enum conform to the Comparable protocol, which requires a bit of boilerplate per this answer. I wonder what other implementation of the < operator you would ever use on an enum, though.

@belkadan
Copy link
Contributor

Enums aren't Comparable by default because you are normally allowed to reorder cases. Because cases can also have custom raw values, it's not clear whether you want to order by case order or by raw value order. (This is more likely to come up with String raw values.)

The diagnostics could definitely be better. cc @xedin

@xedin
Copy link
Member

xedin commented Dec 15, 2017

@swift-ci create

@xedin
Copy link
Member

xedin commented Feb 5, 2020

Compiler now produces a much better diagnostic which actually tells you why `>` can't be used:

error: protocol 'Comparable' requires that 'GameState' conform to 'Comparable'
if state > .BookOpened && state < .PageTurnedTwice {
         ^
note: where 'Self' = 'GameState'
public protocol Comparable: Equatable {
                ^

But I think we could go even further and actually point to the `>` in the diagnostic...

@hborla
Copy link
Member

hborla commented Aug 31, 2020

This is fixed in Swift 5.3. The compiler now reports:

error: referencing operator function '>' on 'Comparable' requires that 'GameState' conform to 'Comparable'
if state > .BookOpened && state < .PageTurnedTwice {
         ^
note: where 'Self' = 'GameState'
public protocol Comparable : Equatable {
                ^

Could you please verify using the latest Xcode 12 beta? Thank you!

@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 diagnostics QoI Bug: Diagnostics Quality of Implementation type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

4 participants