-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[SR-11588]Warn about derived Hashable implementation if there’s a custom Equatable #27801
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -7356,6 +7356,15 @@ WARNING(executor_enqueue_unused_implementation, none, | |||||
"'Executor.enqueue(ExecutorJob)' will never be used, due to the presence of " | ||||||
"'enqueue(UnownedJob)'", | ||||||
()) | ||||||
WARNING(synthesized_hashable_may_not_match_custom_equatable, none, | ||||||
"automatically generated 'Hashable' implementation for type %0 " | ||||||
"may not match the behavior of custom '==' operator", (Type)) | ||||||
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
(Reduced written register doesn't use a definite article here.) More pertinently, is there a word to describe what is meant better than "match"? To me, a simple reading would suggest that 'Hashable' should behave like '==', and of course that doesn't make sense; a reader might ignore the warning because why would they want two disparate things (a protocol and an operator) to "match"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "be compatible with" might be more precise, but that's still not really actionable for the user unless we spell out what "compatible" actually means. I'm not sure how verbose we want this warning to be, however. Ideally we'd be able to just stick in a reference to some relevant documentation or something. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe, “aligned”? Instead of “behavior,” maybe “semantics”? Not sure but I think we can get a little closer with some wordsmithing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So if we were going to really spell it out precisely, what would we say? Let's start with that and try to back off. Something like "Any two values that compare equal with
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, you beat me to the punch. I was going to suggest something similar: "custom 'Equatable' conformance without custom 'Hashable' conformance may cause equivalent values to have different hashes" (fixit: insert missing declarations required by 'Hashable') [Edit: or, "custom 'Equatable' conformance with synthesized 'Hashable' conformance may cause unexpected behavior; equivalent values must have same hash"] |
||||||
NOTE(add_custom_hashable, none, "add a custom 'hash(into:)' method", ()) | ||||||
|
||||||
WARNING(hashvalue_implementation,Deprecation, | ||||||
"'Hashable.hashValue' is deprecated as a protocol requirement; " | ||||||
"conform type %0 to 'Hashable' by implementing 'hash(into:)' instead", | ||||||
(Type)) | ||||||
|
||||||
//------------------------------------------------------------------------------ | ||||||
// MARK: property wrapper diagnostics | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
public struct ImplicitEquatable: ExpressibleByIntegerLiteral, Equatable { | ||
public init(integerLiteral: Int) {} | ||
} | ||
|
||
public struct ExplicitEquatable: ExpressibleByIntegerLiteral, Equatable { | ||
public init(integerLiteral: Int) {} | ||
|
||
public static func == (lhs: ExplicitEquatable, rhs: ExplicitEquatable) -> Bool { | ||
return true | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// RUN: %empty-directory(%t) | ||
// RUN: %target-swift-frontend -emit-module -o %t/NormalLibrary.swiftmodule %S/Inputs/external_equatable_conformance.swift | ||
|
||
// RUN: %target-typecheck-verify-swift -I %t | ||
|
||
import NormalLibrary | ||
|
||
// All conformance synthesized no warning emitted | ||
struct NoCustomEquatable: Hashable { | ||
let a:Int | ||
} | ||
|
||
// Equatable implemented and Hashable synthesized raise a warning | ||
enum CustomEquatable: Hashable { | ||
case a | ||
|
||
static func == (lhs: CustomEquatable, rhs: CustomEquatable) -> Bool { | ||
// expected-warning@-1 {{automatically generated 'Hashable' implementation for type 'CustomEquatable' may not match the behavior of custom '==' operator}} | ||
// expected-note@-2 {{add a custom 'hash(into:)' method}} | ||
return true | ||
} | ||
} | ||
|
||
// All conformance implemented no warning emitted | ||
enum CustomHashable: Hashable { | ||
case a | ||
|
||
func hash(into hasher: inout Hasher) {} | ||
|
||
static func == (lhs: CustomHashable, rhs: CustomHashable) -> Bool { | ||
return true | ||
} | ||
} | ||
|
||
struct ExtendedConformance: Hashable { | ||
let a:Int | ||
} | ||
|
||
extension ExtendedConformance { | ||
static func == (lhs: ExtendedConformance, rhs: ExtendedConformance) -> Bool { | ||
// expected-warning@-1 {{automatically generated 'Hashable' implementation for type 'ExtendedConformance' may not match the behavior of custom '==' operator}} | ||
// expected-note@-2 {{add a custom 'hash(into:)' method}} | ||
return true | ||
} | ||
} | ||
|
||
enum ImportedConformance: ImplicitEquatable { | ||
case x = 1 | ||
} | ||
|
||
enum ImportedExplicitConformance: ExplicitEquatable { | ||
case x = 1 | ||
} | ||
|
||
protocol DefaultedImplementationProtocol: Equatable {} | ||
extension DefaultedImplementationProtocol { | ||
static func == (lhs: Self, rhs: Self) -> Bool { true } | ||
// expected-warning@-1 {{automatically generated 'Hashable' implementation for type 'DefaultedImplementation' may not match the behavior of custom '==' operator}} | ||
// expected-note@-2 {{add a custom 'hash(into:)' method}} | ||
} | ||
|
||
struct DefaultedImplementation: DefaultedImplementationProtocol, Hashable { | ||
let a: Int | ||
} | ||
|
||
protocol ConditionalImplementationProtocol {} | ||
extension ConditionalImplementationProtocol where Self: Equatable { | ||
static func == (lhs: Self, rhs: Self) -> Bool { true } | ||
// expected-warning@-1 {{automatically generated 'Hashable' implementation for type 'ConditionalImplementation' may not match the behavior of custom '==' operator}} | ||
// expected-note@-2 {{add a custom 'hash(into:)' method}} | ||
} | ||
|
||
struct ConditionalImplementation: ConditionalImplementationProtocol, Hashable { | ||
let a: Int | ||
} | ||
|
||
protocol ConditionalHashableImplementationProtocol {} | ||
extension ConditionalHashableImplementationProtocol where Self: Equatable { | ||
static func == (lhs: Self, rhs: Self) -> Bool { true } | ||
// expected-warning@-1 {{automatically generated 'Hashable' implementation for type 'ConditionalHashableImplementation' may not match the behavior of custom '==' operator}} | ||
// expected-note@-2 {{add a custom 'hash(into:)' method}} | ||
} | ||
|
||
extension ConditionalHashableImplementationProtocol where Self: Hashable { | ||
func hash(into hasher: Hasher) {} | ||
} | ||
|
||
struct ConditionalHashableImplementation: ConditionalHashableImplementationProtocol, Hashable { | ||
let a: Int | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
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.
Is there a reason to avoid the commonly used word 'synthesized' here?