Skip to content

Commit f8d72f8

Browse files
committed
[stdlib] Allow RawRepresentable types to customize their hashing without implementing hashValue
Before this change, `RawRepresentable`'s custom `hashValue` implementation used to forward to `rawValue.hashValue`. (See swiftlang#20705.) This sort of made sense at the time (`hash(into:)` was very new and `hashValue` was still in heavy use): it allowed raw representable values to return the exact same hash value as their `rawValue`, which some code used to (mistakenly) rely on. The big drawback of this is that to customize the Hashable implementation of a RawRepresentable type, people need to implement both `hashValue` and `hash(into:)` -- the former does not otherwise pick up customizations to the latter. This change makes the default `RawRepresentable.hashValue` implementation call `self.hash(into:)`, just like it would on non-RawRepresentable types. rdar://82651116
1 parent 5b10335 commit f8d72f8

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

stdlib/public/core/CompilerProtocols.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,11 @@ public func != <T: Equatable>(lhs: T, rhs: T) -> Bool
185185
extension RawRepresentable where RawValue: Hashable, Self: Hashable {
186186
@inlinable // trivial
187187
public var hashValue: Int {
188-
return rawValue.hashValue
188+
// Note: in Swift 5.5 and below, this used to return `rawValue.hashValue`.
189+
// The current definition matches the default `hashValue` implementation,
190+
// so that RawRepresentable types don't need to implement both `hashValue`
191+
// and `hash(into:)` to customize their hashing.
192+
_hashValue(for: self)
189193
}
190194

191195
@inlinable // trivial

0 commit comments

Comments
 (0)