-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Type checker] Warn about overrides of NSObject.hashValue. #18407
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
[Type checker] Warn about overrides of NSObject.hashValue. #18407
Conversation
NSObject.hashValue is provided to satisfy the hashValue constraint of the Hashable protocol. However, it is not the correct customization point for interoperating with Objective-C, because Objective-C code will call through the -hash method. Warn about overrides of NSObject.hashValue; users should override NSObject.hash instead. Fixes rdar://problem/42780635.
@swift-ci please smoke test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actual change looks fine, some quibbles about the approach.
|
||
public static func == (lhs: NSObject, rhs: NSObject) -> Bool { | ||
return lhs.isEqual(rhs) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should live in ObjectiveC.swift, not Foundation.swift.
if (auto classDecl = | ||
baseVar->getDeclContext()->getAsClassOrClassExtensionContext()) { | ||
if (classDecl->getBaseName().userFacingName() == "NSObject" && | ||
baseVar->getBaseName().userFacingName() == "hashValue") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: classDecl->getName().is("NSObject") && baseVar->getName() == ctx.Id_hashValue
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 LGTM, with the one question above.
if (auto baseVar = dyn_cast<VarDecl>(base)) { | ||
if (auto classDecl = | ||
baseVar->getDeclContext()->getAsClassOrClassExtensionContext()) { | ||
if (classDecl->getBaseName().userFacingName() == "NSObject" && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will also trigger on custom classes that happen to be named NSObject
, right? Would it be possible to make this a more specific match?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I can check for the ObjectiveC
module.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... or Foundation
, I guess, for corelibs Foundation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oof, that's fun. Good catch.
NSObject.hashValue is provided to satisfy the hashValue constraint of
the Hashable protocol. However, it is not the correct customization
point for interoperating with Objective-C, because Objective-C code
will call through the -hash method. Warn about overrides of
NSObject.hashValue; users should override NSObject.hash instead.
Fixes rdar://problem/42780635.