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
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
4 changes: 4 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -4142,6 +4142,10 @@ NOTE(redundant_particular_literal_case_here,none,

WARNING(non_exhaustive_switch_warn,none, "switch must be exhaustive", ())

WARNING(override_nsobject_hashvalue,none,
"override of 'NSObject.hashValue' is deprecated; "
"override 'NSObject.hash' to get consistent hashing behavior", ())

#ifndef DIAG_NO_UNDEF
# if defined(DIAG)
# undef DIAG
Expand Down
12 changes: 12 additions & 0 deletions lib/Sema/TypeCheckDeclOverride.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1519,6 +1519,18 @@ static bool checkSingleOverride(ValueDecl *override, ValueDecl *base) {
diagnoseOverrideForAvailability(override, base);
}

// Overrides of NSObject.hashValue are deprecated; one should override
// NSObject.hash instead.
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.

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.

override->diagnose(diag::override_nsobject_hashvalue);
}
}
}

/// Check attributes associated with the base; some may need to merged with
/// or checked against attributes in the overriding declaration.
AttributeOverrideChecker attrChecker(base, override);
Expand Down
8 changes: 8 additions & 0 deletions test/ClangImporter/objc_override.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ class CallbackSubC : CallbackBase {
override func perform(optNonescapingHandler: @escaping () -> Void) {} // expected-error {{method does not override any method from its superclass}}
}

//
class MyHashableNSObject: NSObject {
override var hashValue: Int { // expected-warning{{override of 'NSObject.hashValue' is deprecated}}
return 0
}
}


// FIXME: Remove -verify-ignore-unknown.
// <unknown>:0: error: unexpected note produced: overridden declaration is here
// <unknown>:0: error: unexpected note produced: setter for 'boolProperty' declared here
10 changes: 8 additions & 2 deletions test/Inputs/clang-importer-sdk/swift-modules/Foundation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@
@_exported import CoreGraphics
@_exported import Foundation

public func == (lhs: NSObject, rhs: NSObject) -> Bool {
return lhs.isEqual(rhs)
extension NSObject : Equatable, Hashable {
@objc open var hashValue: Int {
return hash
}

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.

}

public let NSUTF8StringEncoding: UInt = 8
Expand Down
10 changes: 0 additions & 10 deletions test/Inputs/clang-importer-sdk/swift-modules/ObjectiveC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,3 @@ public func _convertObjCBoolToBool(_ x: ObjCBool) -> Bool {
public func ~=(x: NSObject, y: NSObject) -> Bool {
return true
}

extension NSObject : Equatable, Hashable {
public var hashValue: Int {
return hash
}
}

public func == (lhs: NSObject, rhs: NSObject) -> Bool {
return lhs.isEqual(rhs)
}