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 1 commit
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
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)
} else {
return _handle.map { $0.hash }
hasher.combine(_handle.map { $0.hash })
}
}

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.hashValue })
}

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.hash })
}

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.hash })
}

/// 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)
} else {
return _wrapped.hash
hasher.combine(_wrapped.hash)
}
}

Expand Down
4 changes: 2 additions & 2 deletions Foundation/NSObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@ open class NSObject : NSObjectProtocol, Equatable, Hashable {
return self
}

open var hashValue: Int {
return hash
open func hash(into hasher: inout Hasher) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For compatibility with the Foundation overlay on Cocoa platforms, we should not remove the existing hashValue definition, but we should make it public final instead of open. This also applies tohash(into:) -- it must also be declared public final. (See swiftlang/swift#20129)

The NSObject hashing interface is hash; subclasses must override that instead of hashValue or hash(into:). Sadly, this includes subclasses defined within this module -- I think there might be a couple of subclasses that do currently the wrong thing.

(On Darwin, NSObject.hashValue and hash(into:) are defined public because the enclosing module is small, and doesn't define subclasses. The appropriate qualifier in swift-corelibs-foundation is public final.)

hasher.combine(hash)
}

/// Returns a Boolean value indicating whether two values are equal.
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.hash })
}

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)
} else {
return _wrapped.hash
hasher.combine(_wrapped.hash)
}
}

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.hash)
}

// 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.hash })
}

// 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.hash)
}

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.hashValue })
}

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