Skip to content

[ObjectiveC] Make NSObject.hashValue non-overridable #20129

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
Oct 30, 2018
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
24 changes: 12 additions & 12 deletions stdlib/public/SDK/ObjectiveC/ObjectiveC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,34 +216,34 @@ extension NSObject : Equatable, Hashable {

/// The hash value.
///
/// `NSObject` implements this by returning `self.hash`. Subclasses can
/// customize hashing by overriding the `hash` property.
/// `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.
@objc open // FIXME: Should be @nonobjc public. rdar://problem/42623458
var hashValue: Int {
@nonobjc
public 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. Subclasses
/// can customize hashing by overriding the `hash` property.
/// 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 func hash(into hasher: inout Hasher) {
// FIXME: We should combine self.hash here, but hashValue is currently
// overridable.
hasher.combine(hashValue)
hasher.combine(self.hash)
}

public func _rawHashValue(seed: Int) -> Int {
// FIXME: We should use self.hash here, but hashValue is currently
// overridable.
return self.hashValue._rawHashValue(seed: seed)
return self.hash._rawHashValue(seed: seed)
}
}

Expand Down
15 changes: 15 additions & 0 deletions test/stdlib/NSObject-hashing.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// RUN: %target-typecheck-verify-swift
// RUN: %target-swift-frontend -c -verify -verify-ignore-unknown %s -o /dev/null

// REQUIRES: objc_interop

import ObjectiveC

class Foo: NSObject {
override var hashValue: Int { // expected-error {{overriding non-open property outside of its defining module}} expected-error {{overriding non-@objc declarations from extensions is not supported}}
return 0
}

override func hash(into hasher: inout Hasher) { // expected-error {{overriding non-open instance method outside of its defining module}} expected-error {{overriding declarations in extensions is not supported}}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe something here should show the correct way to override hash?

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed! The warning we have in 4.2 is extremely helpful. I have a pending task to improve error diagnostics -- it's not ABI impacting though.

warning: override of 'NSObject.hashValue' is deprecated; override 'NSObject.hash' to get consistent hashing behavior