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

[SR-8861] NSObject.hashValue is overridable in corelibs-Foundation #3627

Closed
kevints mannequin opened this issue Sep 26, 2018 · 3 comments
Closed

[SR-8861] NSObject.hashValue is overridable in corelibs-Foundation #3627

kevints mannequin opened this issue Sep 26, 2018 · 3 comments
Assignees

Comments

@kevints
Copy link
Mannequin

kevints mannequin commented Sep 26, 2018

Previous ID SR-8861
Radar None
Original Reporter @kevints
Type Bug
Status Resolved
Resolution Done
Environment

Ubuntu 14.04
Swift 4.2-RELEASE

Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug
Assignee @lorentey
Priority Medium

md5: 89402437120ed0c994dfd9e9e03e8ea8

Issue Description:

In swift-corelibs-foundation, NSObject.hashValue is left overridable, and is in fact overridden by most (all?) Foundation-defined subclasses.

This causes the following compiler warning:

Foundation/NSError.swift:165:23: warning: override of 'NSObject.hashValue' is deprecated; override 'NSObject.hash' to get consistent hashing behavior
    override open var hashValue: Int {
                      ^

Subclasses of NSObject are expected to customize hashing by overriding the hash property, not hashValue.

(The Swift-only hashValue property was accidentally left open in the Cocoa Foundation overlays in Swift 4.2 and below, which was causing a great deal of confusion – overriding NSObject.hashValue instead of NSObject.hash made the customization invisible to Cocoa collections, breaking hashing. In Swift 5, NSObject.hashValue is non-overridable on Apple platforms. swift-corelibs-foundation should be updated to follow suit, and to make its hashing APIs consistent with Darwin's.)

@lorentey
Copy link
Member

Swift's `Equatable` (and consequently, `Hashable`) protocols aren't directly applicable to class hierarchies like `NSObject`. In particular, Equatable provides no way for subclasses to customize equality – the root class needs to provide some customization hook for this.

`NSObject` does this by exposing the `isEqual(_🙂` method and the `hash` property, respectively. To change how an `NSObject` subclass compares and hashes, override these two methods.

The compiler warning is about an unfortunate footgun – `NSObject.hashValue` was accidentally left overridable in Swift 4.1 and below, even though overriding it has no effect as far as NSObject is concerned. We're planning to turn it into an error in Swift 5.

See the forum discussion below for more details, including sample code for custom NSObject subclasses.

https://forums.swift.org/t/xcode-10-gm-hash-into-issue-from-nsobject-class/16141

@lorentey
Copy link
Member

I updated the description to reflect the underlying issue – I'm hijacking this bug to track the swift-corelibs-foundation side of the NSObject.hashValue vs. NSObject.hash cleanup. (rdar://problem/45674813)

@lorentey
Copy link
Member

lorentey commented Jun 1, 2019

This was resolved for 5.0 in #1869

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant