swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.38k stars 10.34k forks source link

[SR-7900] Protocol class should conform to Hashable #50435

Open swift-ci opened 6 years ago

swift-ci commented 6 years ago
Previous ID SR-7900
Radar rdar://problem/56835199
Original Reporter RndmTsk (JIRA User)
Type Bug
Environment Apple Swift version 4.0 (swiftlang-900.0.65 clang-900.0.37) Target: x86_64-apple-macosx10.9 Apple Swift version 4.1 (swiftlang-902.0.48 clang-902.0.37.1) Target: x86_64-apple-darwin17.5.0 Apple Swift version 4.2 (swiftlang-1000.0.16.7 clang-1000.10.25.3) Target: x86_64-apple-darwin18.0.0
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug, ClangImporter | |Assignee | None | |Priority | Medium | md5: 0824e58f4798fbbf56cc0dc19a2d115f

Issue Description:

When attempting to use `Protocol` as a key-type in a Swift dictionary an error is produced when it is directly included (add2).

If used as a restriction on a generic type (add1) it compiles fine.

NOTE: If the generic function is made to consider Protocol & Hashable, Swift versions \< 4.1 reported a warning: "Redundant conformance constraint 'T' : 'Hashable'"

import Foundation

func add1<T: Protocol>(contentsOf: [T: AnyObject]) {
    // OK!
}

func add2(contentsOf: [Protocol: AnyObject]) {
    // Error: Type 'Protocol' does not conform to protocol 'Hashable'
}

func add3<T: Protocol & Hashable>(contentsOf: [T: AnyObject]) {
    // Warning: Redundant conformance contstraint 'T': 'Hashable'
    // Only manifests in Swift < 4.1
}
belkadan commented 5 years ago

Protocol is implemented as an NSObject these days; maybe we can just do that.

belkadan commented 4 years ago

@swift-ci create