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-10311] Move from Swift 4.2 to 5.0 causes change in type inference #52711

Open
swift-ci opened this issue Apr 4, 2019 · 0 comments
Open
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Apr 4, 2019

Previous ID SR-10311
Radar rdar://problem/49703567
Original Reporter UXJon (JIRA User)
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug
Assignee None
Priority Medium

md5: 8c8092648877b90d8ddea7d94788e97b

Issue Description:

I have two similarly named classes, one of which is an OptionSet. They compiled fine in Swift 4.2, but upon updating to Swift 5.0, StarRatingSet had an error saying it didn't conform to SetAlgebra.

Adding a line defining the Element as StarRatingSet allowed it to work again.

Apparently the type inference changed between versions due to the overload of contains.

enum StarRating:Int,Comparable {
    case unknown = 0
    case star1
    case star2
    case star3
    case star4
    case star5
    
    static func < (lhs: StarRating, rhs: StarRating)->Bool {
        return lhs.rawValue < rhs.rawValue
    }
}

struct StarRatingSet:OptionSet {
    typealias RawValue = Int
    typealias Element = StarRatingSet //<-- This is the line I had to add
    var rawValue: Int
    
    static let unknown = StarRatingSet(rawValue: 1 << 0)
    static let star1 = StarRatingSet(rawValue: 1 << 1)
    static let star2 = StarRatingSet(rawValue: 1 << 2)
    static let star3 = StarRatingSet(rawValue: 1 << 3)
    static let star4 = StarRatingSet(rawValue: 1 << 4)
    static let star5 = StarRatingSet(rawValue: 1 << 5)
    
    static let star4Plus:StarRatingSet = [.star4,.star5]
    static let star3Plus:StarRatingSet = [.star3,.star4,.star5]
    static let star2Plus:StarRatingSet = [.star2,.star3,.star4,.star5]
    static let star1Plus:StarRatingSet = [.star1,.star2,.star3,.star4,.star5]
    static let any:StarRatingSet = [.unknown,.star1,.star2,.star3,.star4,.star5]
    
    func contains(_ rating:StarRating) -> Bool { //<- This might be the issue
        return self.contains(StarRatingSet(rating))
    }
    
    var highest:StarRating {
        if self.contains(.star5) {return .star5}
        else if self.contains(.star4) {return .star4}
        else if self.contains(.star3) {return .star3}
        else if self.contains(.star2) {return .star2}
        else if self.contains(.star1) {return .star1}
        return .unknown
    }
    
    var lowest:StarRating {
        if self.contains(.star1) {return .star1}
        else if self.contains(.star2) {return .star2}
        else if self.contains(.star3) {return .star3}
        else if self.contains(.star4) {return .star4}
        else if self.contains(.star5) {return .star5}
        return .unknown
    }
}

Swift Users thread:

https://forums.swift.org/t/changes-to-setalgebra-or-optionset-in-swift-5/22610/9

@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. standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

1 participant