Skip to content

Casting from AnyHashable to AnyHashable should never create another wrapper #36470

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 2 commits into from
Mar 22, 2021
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
7 changes: 7 additions & 0 deletions stdlib/public/core/AnyHashable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,13 @@ extension AnyHashable: CustomReflectable {
}
}

@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
extension AnyHashable: _HasCustomAnyHashableRepresentation {
public __consuming func _toCustomAnyHashable() -> AnyHashable? {
return self
}
}

/// Returns a default (non-custom) representation of `self`
/// as `AnyHashable`.
///
Expand Down
23 changes: 23 additions & 0 deletions test/Casting/Casts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -924,4 +924,27 @@ CastsTests.test("Optional cast to AnyHashable") {
expectNotEqual(xh, yh)
}

// Repeatedly casting to AnyHashable should still test equal.
// (This was broken for a while because repeatedly casting to
// AnyHashable could end up with multiple nested AnyHashables.)
// rdar://75180619
CastsTests.test("Recursive AnyHashable") {
struct P: Hashable {
var x: Int
}
struct S {
var x: AnyHashable?
init<T: Hashable>(_ x: T?) {
self.x = x
}
}
let p = P(x: 0)
let hp = p as AnyHashable?
print(hp.debugDescription)
let s = S(hp)
print(s.x.debugDescription)
expectEqual(s.x, hp)
expectEqual(s.x, p)
}

runAllTests()
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Func Collection.subranges(where:) has been removed
Func MutableCollection.moveSubranges(_:to:) has been removed
Func MutableCollection.removeSubranges(_:) has been removed
Func RangeReplaceableCollection.removeSubranges(_:) has been removed
Struct AnyHashable has added a conformance to an existing protocol _HasCustomAnyHashableRepresentation
Struct DiscontiguousSlice has been removed
Struct RangeSet has been removed
Subscript Collection.subscript(_:) has been removed
Expand Down