Skip to content

Commit 1ef6cf4

Browse files
authored
Merge pull request swiftlang#20866 from lorentey/deprecate-hashValue-1
[test] Modernize hashing throughout the test suite
2 parents 67ec3b7 + 666a22f commit 1ef6cf4

File tree

65 files changed

+246
-200
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+246
-200
lines changed

stdlib/private/StdlibCollectionUnittest/MinimalCollections.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,8 @@ public struct _CollectionState : Equatable, Hashable {
305305
}
306306
}
307307

308-
public var hashValue: Int {
309-
return _id.hashValue
308+
public func hash(into hasher: inout Hasher) {
309+
hasher.combine(_id)
310310
}
311311
}
312312

stdlib/private/StdlibUnicodeUnittest/Collation.swift

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -181,42 +181,11 @@ let ducetExtractData: [CollationTableEntry] = [
181181
CollationTableEntry([0xE01EF], [0x0000_0000_0000], "VARIATION SELECTOR-256"),
182182
]
183183

184-
public struct HashableArray<Element : Hashable> : Hashable {
185-
internal var _elements: [Element]
186-
187-
public init(_ elements: [Element]) {
188-
_elements = elements
189-
}
190-
191-
public var hashValue: Int {
192-
// FIXME: this is a bad approach to combining hash values.
193-
var result = 0
194-
for x in _elements {
195-
result ^= x.hashValue
196-
result = result &* 997
197-
}
198-
return result
199-
}
200-
}
201-
202-
public func == <Element>(
203-
lhs: HashableArray<Element>,
204-
rhs: HashableArray<Element>
205-
) -> Bool {
206-
return lhs._elements.elementsEqual(rhs._elements)
207-
}
208-
209-
extension HashableArray : ExpressibleByArrayLiteral {
210-
public init(arrayLiteral elements: Element...) {
211-
self._elements = elements
212-
}
213-
}
214-
215-
let ducetExtract: [HashableArray<Unicode.Scalar> : CollationTableEntry] = {
184+
let ducetExtract: [[Unicode.Scalar]: CollationTableEntry] = {
216185
() in
217-
var result: [HashableArray<Unicode.Scalar> : CollationTableEntry] = [:]
186+
var result: [[Unicode.Scalar]: CollationTableEntry] = [:]
218187
for entry in ducetExtractData {
219-
result[HashableArray(entry.scalars)] = entry
188+
result[entry.scalars] = entry
220189
}
221190
return result
222191
}()
@@ -232,7 +201,7 @@ extension String {
232201
internal var _collationElements: [UInt64] {
233202
var result: [UInt64] = []
234203
for us in self.unicodeScalars {
235-
let scalars: HashableArray<Unicode.Scalar> = [us]
204+
let scalars: [Unicode.Scalar] = [us]
236205
let collationElements = ducetExtract[scalars]!.collationElements
237206
if collationElements[0] != 0 {
238207
result += collationElements

stdlib/private/StdlibUnittest/LifetimeTracked.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ extension LifetimeTracked : Hashable {
5151
public var hashValue: Int {
5252
return value
5353
}
54+
public func hash(into hasher: inout Hasher) {
55+
hasher.combine(value)
56+
}
5457
}
5558

5659
extension LifetimeTracked : Strideable {

stdlib/private/StdlibUnittest/StdlibCoreExtras.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,20 @@ public func <=> <T: Comparable>(lhs: T, rhs: T) -> ExpectedComparisonResult {
110110
}
111111

112112
public struct TypeIdentifier : Hashable, Comparable {
113+
public var value: Any.Type
114+
113115
public init(_ value: Any.Type) {
114116
self.value = value
115117
}
116118

117119
public var hashValue: Int { return objectID.hashValue }
118-
public var value: Any.Type
119-
120-
internal var objectID : ObjectIdentifier { return ObjectIdentifier(value) }
120+
public func hash(into hasher: inout Hasher) {
121+
hasher.combine(objectID)
122+
}
123+
124+
internal var objectID : ObjectIdentifier {
125+
return ObjectIdentifier(value)
126+
}
121127
}
122128

123129
public func < (lhs: TypeIdentifier, rhs: TypeIdentifier) -> Bool {

stdlib/private/StdlibUnittest/StringConvertible.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ public struct CustomPrintableValue
6565
return value.hashValue
6666
}
6767

68+
public func hash(into hasher: inout Hasher) {
69+
hasher.combine(value)
70+
}
71+
6872
public typealias Stride = Int
6973

7074
public func distance(to other: CustomPrintableValue) -> Stride {

test/ClangImporter/objc_override.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class CallbackSubC : CallbackBase {
110110

111111
//
112112
class MyHashableNSObject: NSObject {
113-
override var hashValue: Int { // expected-warning{{override of 'NSObject.hashValue' is deprecated}}
113+
override var hashValue: Int { // expected-error{{overriding non-open property outside of its defining module}} expected-error{{overriding non-@objc declarations from extensions is not supported}}
114114
return 0
115115
}
116116
}

test/Constraints/bridging-nsnumber-and-nsvalue.swift.gyb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ extension Equatable {
3838
fatalError("hella cray")
3939
}
4040
}
41-
extension Hashable { public var hashValue: Int { fatalError("trill hiphy") } }
41+
extension Hashable {
42+
public func hash(into hasher: inout Hasher) {
43+
fatalError("trill hiphy")
44+
}
45+
}
46+
4247
extension CGSize: Hashable {}
4348
extension CGPoint: Hashable {}
4449
extension CGRect: Hashable {}

test/Constraints/bridging.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ extension LazyFilterSequence.Iterator : _ObjectiveCBridgeable { // expected-erro
4343

4444

4545
struct BridgedStruct : Hashable, _ObjectiveCBridgeable {
46-
var hashValue: Int { return 0 }
46+
func hash(into hasher: inout Hasher) {}
4747

4848
func _bridgeToObjectiveC() -> BridgedClass {
4949
return BridgedClass()
@@ -71,14 +71,14 @@ struct BridgedStruct : Hashable, _ObjectiveCBridgeable {
7171

7272
func ==(x: BridgedStruct, y: BridgedStruct) -> Bool { return true }
7373

74-
struct NotBridgedStruct : Hashable {
75-
var hashValue: Int { return 0 }
74+
struct NotBridgedStruct : Hashable {
75+
func hash(into hasher: inout Hasher) {}
7676
}
7777

7878
func ==(x: NotBridgedStruct, y: NotBridgedStruct) -> Bool { return true }
7979

8080
class OtherClass : Hashable {
81-
var hashValue: Int { return 0 }
81+
func hash(into hasher: inout Hasher) {}
8282
}
8383
func ==(x: OtherClass, y: OtherClass) -> Bool { return true }
8484

test/DebugInfo/DumpTypeFromMangledName.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ let _ = main()
5353
public struct tinky : Equatable, Hashable {
5454
internal let _value: Int
5555

56-
public var hashValue: Int {
57-
return 0
58-
}
56+
public func hash(into hasher: inout Hasher) {}
5957
}
6058

6159
public func == (lhs: tinky, rhs: tinky) -> Bool {

test/IRGen/Inputs/ObjectiveC.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public func _convertObjCBoolToBool(_ x: ObjCBool) -> Bool {
4747
}
4848

4949
extension NSObject : Hashable {
50-
@objc open var hashValue: Int { return 0 }
50+
public func hash(into hasher: inout Hasher) {}
5151
}
5252
public func ==(x: NSObject, y: NSObject) -> Bool { return x === y }
5353

0 commit comments

Comments
 (0)