Skip to content

Use Hashable.hash(into:) over hashValue to silence the deprecation warnings #1820

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

Merged
merged 3 commits into from
Feb 14, 2019
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Foundation/AffineTransform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ open class NSAffineTransform : NSObject, NSCopying, NSSecureCoding {
return other === self || (other.affineTransform == self.affineTransform)
}

open override var hashValue: Int {
open override var hash: Int {
return affineTransform.hashValue
}

Expand Down
6 changes: 3 additions & 3 deletions Foundation/Calendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -859,12 +859,12 @@ public struct Calendar : Hashable, Equatable, ReferenceConvertible, _MutableBoxi

// MARK: -

public var hashValue: Int {
public func hash(into hasher: inout Hasher) {
// We implement hash ourselves, because we need to make sure autoupdating calendars have the same hash
if _autoupdating {
return 1
hasher.combine(1 as Int8)
} else {
return _handle.map { $0.hash }
hasher.combine(_handle.map { $0 })
}
}

Expand Down
4 changes: 2 additions & 2 deletions Foundation/CharacterSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ public struct CharacterSet : ReferenceConvertible, Equatable, Hashable, SetAlgeb
}
}

public var hashValue: Int {
return _mapUnmanaged { $0.hashValue }
public func hash(into hasher: inout Hasher) {
hasher.combine(_mapUnmanaged { $0 })
}

public var description: String {
Expand Down
4 changes: 2 additions & 2 deletions Foundation/DateComponents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,8 @@ public struct DateComponents : ReferenceConvertible, Hashable, Equatable, _Mutab

// MARK: -

public var hashValue: Int {
return _handle.map { $0.hash }
public func hash(into hasher: inout Hasher) {
hasher.combine(_handle.map { $0 })
}

public static func ==(lhs: DateComponents, rhs: DateComponents) -> Bool {
Expand Down
6 changes: 3 additions & 3 deletions Foundation/IndexSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ public struct IndexSet : ReferenceConvertible, Equatable, BidirectionalCollectio
_handle = _MutablePairHandle(NSIndexSet(), copying: false)
}

public var hashValue: Int {
return _handle.map { $0.hash }
public func hash(into hasher: inout Hasher) {
hasher.combine(_handle.map { $0 })
}

/// Returns the number of integers in `self`.
public var count: Int {
return _handle.map { $0.count }
Expand Down
6 changes: 3 additions & 3 deletions Foundation/Locale.swift
Original file line number Diff line number Diff line change
Expand Up @@ -421,11 +421,11 @@ public struct Locale : CustomStringConvertible, CustomDebugStringConvertible, Ha
return _wrapped.debugDescription
}

public var hashValue : Int {
public func hash(into hasher: inout Hasher) {
if _autoupdating {
return 1
hasher.combine(1 as Int8)
} else {
return _wrapped.hash
hasher.combine(_wrapped)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Foundation/NSCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fileprivate class NSCacheKey: NSObject {
super.init()
}

override var hashValue: Int {
override var hash: Int {
switch self.value {
case let nsObject as NSObject:
return nsObject.hashValue
Expand Down
2 changes: 1 addition & 1 deletion Foundation/NSCalendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1334,7 +1334,7 @@ internal class _NSCopyOnWriteCalendar: NSCalendar {
return backingCalendar.isEqual(value)
}

override var hashValue: Int {
override var hash: Int {
return backingCalendar.hashValue
}

Expand Down
2 changes: 1 addition & 1 deletion Foundation/NSError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ open class NSError : NSObject, NSCopying, NSSecureCoding, NSCoding {
// -- NSObject Overrides --
// The compiler has special paths for attempting to do some bridging on NSError (and equivalent Error instances) -- in particular, in the lookup of NSError objects' superclass.
// On platforms where we don't have bridging (i.e. Linux), this causes a silgen failure. We can avoid the issue by overriding methods inherited by NSObject ourselves.
override open var hashValue: Int {
override open var hash: Int {
// CFHash does the appropriate casting/bridging on platforms where we support it.
return Int(bitPattern: CFHash(self))
}
Expand Down
25 changes: 24 additions & 1 deletion Foundation/NSObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,33 @@ open class NSObject : NSObjectProtocol, Equatable, Hashable {
return self
}

open var hashValue: Int {
/// The hash value.
///
/// `NSObject` implements this by returning `self.hash`.
///
/// `NSObject.hashValue` is not overridable; subclasses can customize hashing
/// by overriding the `hash` property.
///
/// **Axiom:** `x == y` implies `x.hashValue == y.hashValue`
///
/// - 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.
public final var hashValue: Int {
return hash
}

/// Hashes the essential components of this value by feeding them into the
/// given hasher.
///
/// NSObject implements this by feeding `self.hash` to the hasher.
///
/// `NSObject.hash(into:)` is not overridable; subclasses can customize
/// hashing by overriding the `hash` property.
public final func hash(into hasher: inout Hasher) {
hasher.combine(self.hash)
}

/// Returns a Boolean value indicating whether two values are equal.
///
/// Equality is the inverse of inequality. For any values `a` and `b`,
Expand Down
8 changes: 4 additions & 4 deletions Foundation/Notification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ public struct Notification : ReferenceConvertible, Equatable, Hashable {
self.object = object
self.userInfo = userInfo
}

public var hashValue: Int {
return name.rawValue.hash
}

public func hash(into hasher: inout Hasher) {
hasher.combine(name)
}

public var description: String {
var description = "name = \(name.rawValue)"
if let obj = object { description += ", object = \(obj)" }
Expand Down
6 changes: 3 additions & 3 deletions Foundation/PersonNameComponents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ public struct PersonNameComponents : ReferenceConvertible, Hashable, Equatable,
set { _applyMutation { $0.phoneticRepresentation = newValue } }
}

public var hashValue : Int {
return _handle.map { $0.hash }
public func hash(into hasher: inout Hasher) {
hasher.combine(_handle.map { $0 })
}

public static func ==(lhs : PersonNameComponents, rhs: PersonNameComponents) -> Bool {
// Don't copy references here; no one should be storing anything
return lhs._handle._uncopiedReference().isEqual(rhs._handle._uncopiedReference())
Expand Down
6 changes: 3 additions & 3 deletions Foundation/TimeZone.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,11 @@ public struct TimeZone : Hashable, Equatable, ReferenceConvertible {

// MARK: -

public var hashValue : Int {
public func hash(into hasher: inout Hasher) {
if _autoupdating {
return 1
hasher.combine(1 as Int8)
} else {
return _wrapped.hash
hasher.combine(_wrapped)
}
}

Expand Down
4 changes: 2 additions & 2 deletions Foundation/URL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,8 @@ public struct URL : ReferenceConvertible, Equatable {
_url = URL._converted(from: NSURL(fileURLWithFileSystemRepresentation: path, isDirectory: isDirectory, relativeTo: baseURL))
}

public var hashValue: Int {
return _url.hash
public func hash(into hasher: inout Hasher) {
hasher.combine(_url)
}

// MARK: -
Expand Down
14 changes: 8 additions & 6 deletions Foundation/URLComponents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,10 @@ public struct URLComponents : ReferenceConvertible, Hashable, Equatable, _Mutabl
set { _applyMutation { $0.queryItems = newValue } }
}

public var hashValue: Int {
return _handle.map { $0.hash }
public func hash(into hasher: inout Hasher) {
hasher.combine(_handle.map { $0 })
}

// MARK: - Bridging

fileprivate init(reference: NSURLComponents) {
Expand Down Expand Up @@ -346,9 +346,11 @@ public struct URLQueryItem : ReferenceConvertible, Hashable, Equatable {
get { return _queryItem.value }
set { _queryItem = NSURLQueryItem(name: name, value: newValue) }
}

public var hashValue: Int { return _queryItem.hash }


public func hash(into hasher: inout Hasher) {
hasher.combine(_queryItem)
}

public static func ==(lhs: URLQueryItem, rhs: URLQueryItem) -> Bool {
return lhs._queryItem.isEqual(rhs._queryItem)
}
Expand Down
4 changes: 2 additions & 2 deletions Foundation/URLRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ public struct URLRequest : ReferenceConvertible, Equatable, Hashable {
}
}

public var hashValue: Int {
return _handle.map { $0.hashValue }
public func hash(into hasher: inout Hasher) {
hasher.combine(_handle.map { $0 })
}

public static func ==(lhs: URLRequest, rhs: URLRequest) -> Bool {
Expand Down