Skip to content

Commit 01baa5a

Browse files
committed
[ObjectiveC] NSObject: Define _hash(into:)
Add FIXMEs for potential correctness issues with the existing hashValue definition.
1 parent df17ae8 commit 01baa5a

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

stdlib/public/SDK/ObjectiveC/ObjectiveC.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,19 @@ extension NSObject : Equatable, Hashable {
211211
/// different invocations of the same program. Do not persist the
212212
/// hash value across program runs.
213213
@objc
214-
open var hashValue: Int {
214+
open // FIXME: should be @nonobjc public.
215+
var hashValue: Int {
215216
return hash
216217
}
218+
219+
public final func _hash(into hasher: inout _Hasher) {
220+
// FIXME(hasher): We should call self.hash here; however, that would cause
221+
// compatibility issues with code that mistakenly overrides hashValue rather
222+
// than hash. (Types that do this break Foundation's hashing, but as long as
223+
// they correctly override isEqual, they may still conform to Swift's
224+
// Hashable.)
225+
hasher.combine(self.hashValue)
226+
}
217227
}
218228

219229
extension NSObject : CVarArg {

0 commit comments

Comments
 (0)