Skip to content

Commit 08c3f8a

Browse files
committed
[stdlib] Use _Hasher in conditional Hashable implementations
Implement _hash(into:) rather than hashValue.
1 parent c196667 commit 08c3f8a

File tree

7 files changed

+55
-32
lines changed

7 files changed

+55
-32
lines changed

stdlib/public/core/Arrays.swift.gyb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2297,15 +2297,17 @@ extension ${Self} : Equatable where Element : Equatable {
22972297
}
22982298
}
22992299

2300-
extension ${Self} : Hashable where Element : Hashable {
2300+
extension ${Self}: Hashable where Element: Hashable {
23012301
@_inlineable // FIXME(sil-serialize-all)
23022302
public var hashValue: Int {
2303-
// FIXME(ABI)#177: <rdar://problem/18915294> Issue applies to Array too
2304-
var result = 0
2303+
return _hashValue(for: self)
2304+
}
2305+
2306+
@_inlineable // FIXME(sil-serialize-all)
2307+
public func _hash(into hasher: inout _Hasher) {
23052308
for element in self {
2306-
result = _combineHashValues(result, element.hashValue)
2309+
hasher.append(element)
23072310
}
2308-
return result
23092311
}
23102312
}
23112313

stdlib/public/core/ClosedRange.swift

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,20 @@ extension ClosedRange.Index : Comparable {
167167
}
168168

169169
extension ClosedRange.Index: Hashable
170-
where Bound: Strideable, Bound.Stride: SignedInteger, Bound: Hashable {
170+
where Bound: Strideable, Bound.Stride: SignedInteger, Bound: Hashable {
171171
@_inlineable // FIXME(sil-serialize-all)
172172
public var hashValue: Int {
173+
return _hashValue(for: self)
174+
}
175+
176+
@_inlineable // FIXME(sil-serialize-all)
177+
public func _hash(into hasher: inout _Hasher) {
173178
switch self {
174179
case .inRange(let value):
175-
return value.hashValue
180+
hasher.append(0 as Int8)
181+
hasher.append(value)
176182
case .pastEnd:
177-
return .max
183+
hasher.append(1 as Int8)
178184
}
179185
}
180186
}
@@ -382,13 +388,16 @@ extension ClosedRange: Equatable {
382388
}
383389
}
384390

385-
extension ClosedRange : Hashable where Bound : Hashable {
391+
extension ClosedRange: Hashable where Bound: Hashable {
386392
@_inlineable // FIXME(sil-serialize-all)
387393
public var hashValue: Int {
388-
var result = 0
389-
result = _combineHashValues(result, lowerBound.hashValue)
390-
result = _combineHashValues(result, upperBound.hashValue)
391-
return result
394+
return _hashValue(for: self)
395+
}
396+
397+
@_inlineable // FIXME(sil-serialize-all)
398+
public func _hash(into hasher: inout _Hasher) {
399+
hasher.append(lowerBound)
400+
hasher.append(upperBound)
392401
}
393402
}
394403

stdlib/public/core/Dictionary.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,13 +1449,19 @@ extension Dictionary: Equatable where Value: Equatable {
14491449
extension Dictionary: Hashable where Value: Hashable {
14501450
@_inlineable // FIXME(sil-serialize-all)
14511451
public var hashValue: Int {
1452-
// FIXME(ABI)#177: <rdar://problem/18915294> Cache Dictionary<T> hashValue
1453-
var result = 0
1452+
return _hashValue(for: self)
1453+
}
1454+
1455+
@_inlineable // FIXME(sil-serialize-all)
1456+
public func _hash(into hasher: inout _Hasher) {
1457+
var commutativeHash = 0
14541458
for (k, v) in self {
1455-
let combined = _combineHashValues(k.hashValue, v.hashValue)
1456-
result ^= _mixInt(combined)
1459+
var elementHasher = _Hasher()
1460+
elementHasher.append(k)
1461+
elementHasher.append(v)
1462+
commutativeHash ^= elementHasher.finalize()
14571463
}
1458-
return result
1464+
hasher.append(commutativeHash)
14591465
}
14601466
}
14611467

stdlib/public/core/Flatten.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,10 @@ extension FlattenCollection.Index : Hashable
237237
return _hashValue(for: self)
238238
}
239239

240+
@_inlineable // FIXME(sil-serialize-all)
240241
public func _hash(into hasher: inout _Hasher) {
241242
hasher.append(_outer)
242-
if let inner = _inner {
243-
hasher.append(inner)
244-
}
243+
hasher.append(_inner)
245244
}
246245
}
247246

stdlib/public/core/Optional.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ extension Optional : Equatable where Wrapped : Equatable {
409409
}
410410
}
411411

412-
extension Optional : Hashable where Wrapped : Hashable {
412+
extension Optional: Hashable where Wrapped: Hashable {
413413
/// The hash value for the optional instance.
414414
///
415415
/// Two optionals that are equal will always have equal hash values.
@@ -418,15 +418,18 @@ extension Optional : Hashable where Wrapped : Hashable {
418418
/// your program. Do not save hash values to use during a future execution.
419419
@_inlineable // FIXME(sil-serialize-all)
420420
public var hashValue: Int {
421-
var result: Int
421+
return _hashValue(for: self)
422+
}
423+
424+
@_inlineable // FIXME(sil-serialize-all)
425+
public func _hash(into hasher: inout _Hasher) {
422426
switch self {
423427
case .none:
424-
result = 0
428+
hasher.append(0 as UInt8)
425429
case .some(let wrapped):
426-
result = 1
427-
result = _combineHashValues(result, wrapped.hashValue)
430+
hasher.append(1 as UInt8)
431+
hasher.append(wrapped)
428432
}
429-
return result
430433
}
431434
}
432435

stdlib/public/core/Range.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -398,13 +398,16 @@ extension Range: Equatable {
398398
}
399399
}
400400

401-
extension Range : Hashable where Bound : Hashable {
401+
extension Range: Hashable where Bound: Hashable {
402402
@_inlineable // FIXME(sil-serialize-all)
403403
public var hashValue: Int {
404-
var result = 0
405-
result = _combineHashValues(result, lowerBound.hashValue)
406-
result = _combineHashValues(result, upperBound.hashValue)
407-
return result
404+
return _hashValue(for: self)
405+
}
406+
407+
@_inlineable // FIXME(sil-serialize-all)
408+
public func _hash(into hasher: inout _Hasher) {
409+
hasher.append(lowerBound)
410+
hasher.append(upperBound)
408411
}
409412
}
410413

stdlib/public/core/Reverse.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ extension ReversedCollection.Index: Hashable where Base.Index: Hashable {
199199
return base.hashValue
200200
}
201201

202+
@_inlineable // FIXME(sil-serialize-all)
202203
public func _hash(into hasher: inout _Hasher) {
203204
hasher.append(base)
204205
}

0 commit comments

Comments
 (0)