Skip to content

[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

Merged

Conversation

DougGregor
Copy link
Member

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.

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.
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor DougGregor requested a review from lorentey July 31, 2018 18:45
@DougGregor DougGregor merged commit 3439333 into swiftlang:master Jul 31, 2018
@DougGregor DougGregor deleted the warn-override-nsobject-hashvalue branch July 31, 2018 20:14
Copy link
Contributor

@jrose-apple jrose-apple left a 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)
}
Copy link
Contributor

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") {
Copy link
Contributor

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.

Copy link
Member

@lorentey lorentey left a 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" &&
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member Author

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.

Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants