|
| 1 | +// RUN: %target-run-simple-swift | %FileCheck %s |
| 2 | +// REQUIRES: executable_test |
| 3 | + |
| 4 | +// RawRepresentable is not Equatable itself, but it does provide a generic |
| 5 | +// implementation of == based on rawValues. This gets picked up as the |
| 6 | +// implementation of Equatable.== when a concrete RawRepresentable type conforms |
| 7 | +// to Equatable without providing its own implementation. |
| 8 | +// |
| 9 | +// However, RawRepresentable used to not provide equivalent implementations for |
| 10 | +// hashing, allowing the compiler to synthesized hashing as usual, based on the |
| 11 | +// actual contents of the type rather than its rawValue. Thus, the definitions |
| 12 | +// of equality and hashing may not actually match, leading to broken hashes. |
| 13 | +// |
| 14 | +// The difference between rawValue and the actual contents is subtle, and it |
| 15 | +// only causes problems in custom RawRepresentable implementations where the |
| 16 | +// rawValue isn't actually the storage representation, like the weird struct |
| 17 | +// below. |
| 18 | +// |
| 19 | +// rdar://problem/45308741 |
| 20 | + |
| 21 | +struct TrickyRawRepresentable: RawRepresentable, Hashable { |
| 22 | + var value: [Unicode.Scalar] |
| 23 | + |
| 24 | + var rawValue: String { |
| 25 | + return String(String.UnicodeScalarView(value)) |
| 26 | + } |
| 27 | + |
| 28 | + init?(rawValue: String) { |
| 29 | + self.value = Array(rawValue.unicodeScalars) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +let s1 = TrickyRawRepresentable(rawValue: "café")! |
| 34 | +let s2 = TrickyRawRepresentable(rawValue: "cafe\u{301}")! |
| 35 | + |
| 36 | +// CHECK: s1 == s2: true |
| 37 | +print("s1 == s2: \(s1 == s2)") |
| 38 | + |
| 39 | +// CHECK: hashValue matches: true |
| 40 | +print("hashValue matches: \(s1.hashValue == s2.hashValue)") |
| 41 | + |
| 42 | +extension Hasher { |
| 43 | + static func hash<H: Hashable>(_ value: H) -> Int { |
| 44 | + var hasher = Hasher() |
| 45 | + hasher.combine(value) |
| 46 | + return hasher.finalize() |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// CHECK: hash(into:) matches: true |
| 51 | +print("hash(into:) matches: \(Hasher.hash(s1) == Hasher.hash(s2))") |
| 52 | + |
| 53 | +// CHECK: _rawHashValue(seed:) matches: true |
| 54 | +let r1 = s1._rawHashValue(seed: 42) |
| 55 | +let r2 = s2._rawHashValue(seed: 42) |
| 56 | +print("_rawHashValue(seed:) matches: \(r1 == r2)") |
0 commit comments