Skip to content

Make NSObject.hashValue non-overridable #1850

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 1 commit into from
Jan 30, 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 @@ -387,7 +387,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
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