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-13851] Enumerations with raw value types generate warnings when adopting protocols with default implementations of hash(into: ). #56249

Open
byaruhaf opened this issue Nov 13, 2020 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@byaruhaf
Copy link

Previous ID SR-13851
Radar rdar://problem/71385578
Original Reporter @byaruhaf
Type Bug

Attachment: Download

Environment

Apple Swift version 5.3.1 (swiftlang-1200.0.41 clang-1200.0.32.8)
Target: x86_64-apple-darwin20.1.0

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

md5: 073bb7eebed95e78762bd0411a5bec9c

Issue Description:

The default implementation of 'hash(into: )' for a protocol that inherits Hashable results in a warning on Custom types that they adopt that particular protocol.

protocol SettingsSelectable: Hashable {
    var display: String { get }
}
 
extension SettingsSelectable {
    func hash(into hasher: inout Hasher) {
        hasher.combine(display)
    }
}
 
enum PlaybackSpeed: Int, SettingsSelectable {
    case half
    case standard
    case onePointFive
    case double
    
    var display: String {
        switch self {
        case .half:
            return "0.5x"
        case .standard:
            return "1.0x"
        case .onePointFive:
            return "1.5x"
        case .double:
            return "2.0x"
        }
    }
}
 

Compiling the code above in the attached demo.swiftresults in the warning below:

swiftc demo.swift [13:06:57]
Swift.RawRepresentable:2:27: warning: 'Hashable.hashValue' is deprecated as a protocol requirement; conform type 'PlaybackSpeed' to 'Hashable' by implementing 'hash(into:)' instead
 @inlinable public var hashValue: Int { get }

Since enum PlaybackSpeed should have the functions hash(into: ) from the default implementation so this warning should not be generated.

When I remove the raw value types from the Enum the warning is not generated.

So enumerations with raw value types generate warnings when adopting protocols with default implementations of hash(into: ).

@typesanitizer
Copy link

@swift-ci create

@lorentey
Copy link
Member

Note: This is a compiler issue – the diagnostic is emitted incorrectly. According to @byaruhaf in #34745 this is being triggered by RawRepresentable's definition for `hashValue` in the Standard Library. (The stdlib also defines an implementation of `hash(into🙂` in the exact same extension, so the diagnostic is clearly wrong. The fact that that definition is overridden by SettingsSelectable is probably what's confusing the diagnostic logic.)

Note that AFAICT, the sample code above comes dangerously close to violating Hashable requirements, because SettingsSelectable's default implementation of hash(into🙂 does not match the default definition of equality it inherits from RawRepresentable. However, this doesn't change the fact that the diagnostic is emitted by mistake.

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

No branches or pull requests

3 participants