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

[SE-0206][stdlib] hash(into:)/hashValue cleanup #16154

Merged
merged 3 commits into from
Apr 30, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions stdlib/public/core/Arrays.swift.gyb
Original file line number Diff line number Diff line change
@@ -2263,11 +2263,6 @@ extension ${Self} : Equatable where Element : Equatable {
}

extension ${Self}: Hashable where Element: Hashable {
@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
return _hashValue(for: self)
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
hasher.combine(count) // discriminator
12 changes: 0 additions & 12 deletions stdlib/public/core/Bool.swift
Original file line number Diff line number Diff line change
@@ -145,18 +145,6 @@ public // COMPILER_INTRINSIC
func _getBool(_ v: Builtin.Int1) -> Bool { return Bool(v) }

extension Bool : Equatable, Hashable {
/// The hash value for the Boolean value.
///
/// Two values that are equal always have equal hash values.
///
/// - Note: The hash value is not guaranteed to be stable across different
/// invocations of the same program. Do not persist the hash value across
/// program runs.
@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
return _hashValue(for: self)
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
hasher.combine((self ? 1 : 0) as UInt8)
10 changes: 0 additions & 10 deletions stdlib/public/core/CTypes.swift
Original file line number Diff line number Diff line change
@@ -174,16 +174,6 @@ extension OpaquePointer: Equatable {
}

extension OpaquePointer: Hashable {
/// The pointer's hash value.
///
/// The hash value is not guaranteed to be stable across different
/// invocations of the same program. Do not persist the hash value across
/// program runs.
@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
return _hashValue(for: self)
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
hasher.combine(Int(Builtin.ptrtoint_Word(_rawValue)))
11 changes: 4 additions & 7 deletions stdlib/public/core/Character.swift
Original file line number Diff line number Diff line change
@@ -477,14 +477,11 @@ extension Character : Comparable {
}

extension Character: Hashable {
/// The character's hash value.
///
/// Hash values are not guaranteed to be equal across different executions of
/// your program. Do not save hash values to use during a future execution.
@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
// not @inlinable (performance)
@effects(releasenone)
public func hash(into hasher: inout Hasher) {
// FIXME(performance): constructing a temporary string is extremely
// wasteful and inefficient.
return String(self).hashValue
hasher.combine(String(self))
}
}
10 changes: 0 additions & 10 deletions stdlib/public/core/ClosedRange.swift
Original file line number Diff line number Diff line change
@@ -168,11 +168,6 @@ extension ClosedRange.Index : Comparable {

extension ClosedRange.Index: Hashable
where Bound: Strideable, Bound.Stride: SignedInteger, Bound: Hashable {
@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
return _hashValue(for: self)
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
switch self {
@@ -395,11 +390,6 @@ extension ClosedRange: Equatable {
}

extension ClosedRange: Hashable where Bound: Hashable {
@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
return _hashValue(for: self)
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
hasher.combine(lowerBound)
5 changes: 5 additions & 0 deletions stdlib/public/core/Codable.swift.gyb
Original file line number Diff line number Diff line change
@@ -1094,6 +1094,11 @@ public struct CodingUserInfoKey : RawRepresentable, Equatable, Hashable {
public var hashValue: Int {
return self.rawValue.hashValue
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
hasher.combine(self.rawValue)
}
}

//===----------------------------------------------------------------------===//
27 changes: 16 additions & 11 deletions stdlib/public/core/Dictionary.swift
Original file line number Diff line number Diff line change
@@ -1449,11 +1449,6 @@ extension Dictionary: Equatable where Value: Equatable {
}

extension Dictionary: Hashable where Value: Hashable {
@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
return _hashValue(for: self)
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
var commutativeHash = 0
@@ -4334,18 +4329,28 @@ extension Dictionary.Index {

@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
return _hashValue(for: self)
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
#if _runtime(_ObjC)
if _fastPath(_guaranteedNative) {
return _nativeIndex.offset
hasher.combine(0 as UInt8)
hasher.combine(_nativeIndex.offset)
return
}

switch _value {
case ._native(let nativeIndex):
return nativeIndex.offset
#if _runtime(_ObjC)
hasher.combine(0 as UInt8)
hasher.combine(nativeIndex.offset)
case ._cocoa(let cocoaIndex):
return cocoaIndex.currentKeyIndex
#endif
hasher.combine(1 as UInt8)
hasher.combine(cocoaIndex.currentKeyIndex)
}
#else
hasher.combine(_nativeIndex.offset)
#endif
}
}

5 changes: 0 additions & 5 deletions stdlib/public/core/Flatten.swift
Original file line number Diff line number Diff line change
@@ -229,11 +229,6 @@ extension FlattenCollection.Index : Comparable {

extension FlattenCollection.Index : Hashable
where Base.Index : Hashable, Base.Element.Index : Hashable {
@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
return _hashValue(for: self)
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
hasher.combine(_outer)
9 changes: 0 additions & 9 deletions stdlib/public/core/FloatingPointTypes.swift.gyb
Original file line number Diff line number Diff line change
@@ -1521,15 +1521,6 @@ extension ${Self} : _ExpressibleByBuiltinFloatLiteral {
% end

extension ${Self} : Hashable {
/// The number's hash value.
///
/// Hash values are not guaranteed to be equal across different executions of
/// your program. Do not save hash values to use during a future execution.
@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
return _hashValue(for: self)
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
var v = self
13 changes: 0 additions & 13 deletions stdlib/public/core/Integers.swift.gyb
Original file line number Diff line number Diff line change
@@ -3694,19 +3694,6 @@ ${assignmentOperatorComment(x.operator, True)}
%# end of concrete type: ${Self}

extension ${Self} : Hashable {
/// The integer's hash value.
///
/// The hash value is not guaranteed to be stable across different
/// invocations of the same program. Do not persist the hash value across
/// program runs.
@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
@inline(__always)
get {
return _hashValue(for: self)
}
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
// FIXME(hasher): To correctly bridge `Set`s/`Dictionary`s containing
14 changes: 2 additions & 12 deletions stdlib/public/core/KeyPath.swift
Original file line number Diff line number Diff line change
@@ -52,7 +52,7 @@ public class AnyKeyPath: Hashable, _AppendKeyPath {
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
final public func hash(into hasher: inout Hasher) {
return withBuffer {
var buffer = $0
while true {
@@ -441,12 +441,7 @@ internal struct ComputedPropertyID: Hashable {
}

@inlinable // FIXME(sil-serialize-all)
internal var hashValue: Int {
return _hashValue(for: self)
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
internal func hash(into hasher: inout Hasher) {
hasher.combine(value)
hasher.combine(isStoredProperty)
hasher.combine(isTableOffset)
@@ -573,11 +568,6 @@ internal enum KeyPathComponent: Hashable {
}
}

@inlinable // FIXME(sil-serialize-all)
internal var hashValue: Int {
return _hashValue(for: self)
}

@inlinable // FIXME(sil-serialize-all)
internal func hash(into hasher: inout Hasher) {
var hasher = hasher
10 changes: 2 additions & 8 deletions stdlib/public/core/ObjectIdentifier.swift
Original file line number Diff line number Diff line change
@@ -84,15 +84,9 @@ extension ObjectIdentifier: Comparable {
}

extension ObjectIdentifier: Hashable {
// FIXME: Better hashing algorithm
/// The identifier's hash value.
///
/// The hash value is not guaranteed to be stable across different
/// invocations of the same program. Do not persist the hash value across
/// program runs.
@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
return Int(Builtin.ptrtoint_Word(_value))
public func hash(into hasher: inout Hasher) {
hasher.combine(Int(Builtin.ptrtoint_Word(_value)))
}
}

11 changes: 0 additions & 11 deletions stdlib/public/core/Optional.swift
Original file line number Diff line number Diff line change
@@ -410,17 +410,6 @@ extension Optional : Equatable where Wrapped : Equatable {
}

extension Optional: Hashable where Wrapped: Hashable {
/// The hash value for the optional instance.
///
/// Two optionals that are equal will always have equal hash values.
///
/// Hash values are not guaranteed to be equal across different executions of
/// your program. Do not save hash values to use during a future execution.
@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
return _hashValue(for: self)
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
switch self {
5 changes: 0 additions & 5 deletions stdlib/public/core/PrefixWhile.swift
Original file line number Diff line number Diff line change
@@ -202,11 +202,6 @@ extension LazyPrefixWhileCollection.Index: Comparable {
}

extension LazyPrefixWhileCollection.Index: Hashable where Base.Index: Hashable {
@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
return _hashValue(for: self)
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
switch _value {
5 changes: 0 additions & 5 deletions stdlib/public/core/Range.swift
Original file line number Diff line number Diff line change
@@ -405,11 +405,6 @@ extension Range: Equatable {
}

extension Range: Hashable where Bound: Hashable {
@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
return _hashValue(for: self)
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
hasher.combine(lowerBound)
5 changes: 0 additions & 5 deletions stdlib/public/core/Reverse.swift
Original file line number Diff line number Diff line change
@@ -191,11 +191,6 @@ extension ReversedCollection.Index: Comparable {
}

extension ReversedCollection.Index: Hashable where Base.Index: Hashable {
@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
return base.hashValue
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
hasher.combine(base)
35 changes: 17 additions & 18 deletions stdlib/public/core/Set.swift
Original file line number Diff line number Diff line change
@@ -484,20 +484,9 @@ extension Set: Equatable {
}

extension Set: Hashable {
/// The hash value for the set.
///
/// Two sets that are equal will always have equal hash values.
///
/// Hash values are not guaranteed to be equal across different executions of
/// your program. Do not save hash values to use during a future execution.
@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
// FIXME(ABI)#177: <rdar://problem/18915294> Cache Set<T> hashValue
return _hashValue(for: self)
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
// FIXME(ABI)#177: <rdar://problem/18915294> Cache Set<T> hashValue
var hash = 0
for member in self {
hash ^= _hashValue(for: member)
@@ -3662,18 +3651,28 @@ extension Set.Index {

@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
return _hashValue(for: self)
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
#if _runtime(_ObjC)
if _fastPath(_guaranteedNative) {
return _nativeIndex.offset
hasher.combine(0 as UInt8)
hasher.combine(_nativeIndex.offset)
return
}

switch _value {
case ._native(let nativeIndex):
return nativeIndex.offset
#if _runtime(_ObjC)
hasher.combine(0 as UInt8)
hasher.combine(nativeIndex.offset)
case ._cocoa(let cocoaIndex):
return cocoaIndex.currentKeyIndex
#endif
hasher.combine(1 as UInt8)
hasher.combine(cocoaIndex.currentKeyIndex)
}
#else
hasher.combine(_nativeIndex.offset)
#endif
}
}

14 changes: 0 additions & 14 deletions stdlib/public/core/StringHashable.swift
Original file line number Diff line number Diff line change
@@ -122,27 +122,13 @@ extension _StringGuts {
}

extension String : Hashable {
/// The string's hash value.
///
/// Hash values are not guaranteed to be equal across different executions of
/// your program. Do not save hash values to use during a future execution.
@inlinable
public var hashValue: Int {
return _hashValue(for: self)
}

@inlinable
public func hash(into hasher: inout Hasher) {
_guts.hash(into: &hasher)
}
}

extension StringProtocol {
@inlinable
public var hashValue : Int {
return _hashValue(for: self)
}

@inlinable
public func hash(into hasher: inout Hasher) {
_wholeString._guts.hash(_encodedOffsetRange, into: &hasher)
4 changes: 2 additions & 2 deletions stdlib/public/core/StringIndex.swift
Original file line number Diff line number Diff line change
@@ -66,8 +66,8 @@ extension String.Index : Comparable {

extension String.Index : Hashable {
@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
return _compoundOffset.hashValue
public func hash(into hasher: inout Hasher) {
hasher.combine(_compoundOffset)
}
}

8 changes: 2 additions & 6 deletions stdlib/public/core/UnicodeScalar.swift
Original file line number Diff line number Diff line change
@@ -311,13 +311,9 @@ extension Unicode.Scalar : LosslessStringConvertible {
}

extension Unicode.Scalar : Hashable {
/// The Unicode scalar's hash value.
///
/// Hash values are not guaranteed to be equal across different executions of
/// your program. Do not save hash values to use during a future execution.
@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
return Int(self.value)
public func hash(into hasher: inout Hasher) {
hasher.combine(self.value)
}
}

10 changes: 0 additions & 10 deletions stdlib/public/core/UnsafePointer.swift.gyb
Original file line number Diff line number Diff line change
@@ -897,16 +897,6 @@ extension ${Self}: Comparable {
}
}
extension ${Self}: Hashable {
/// The pointer's hash value.
///
/// The hash value is not guaranteed to be stable across different
/// invocations of the same program. Do not persist the hash value across
/// program runs.
@inlinable
public var hashValue: Int {
return _hashValue(for: self)
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
hasher.combine(Int(bitPattern: self))
11 changes: 3 additions & 8 deletions stdlib/public/core/UnsafeRawPointer.swift.gyb
Original file line number Diff line number Diff line change
@@ -965,14 +965,9 @@ extension ${Self}: Comparable {
}

extension ${Self}: Hashable {
/// The pointer's hash value.
///
/// The hash value is not guaranteed to be stable across different
/// invocations of the same program. Do not persist the hash value across
/// program runs.
@inlinable
public var hashValue: Int {
return Int(bitPattern: self)
@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
hasher.combine(Int(bitPattern: self))
}
}