Skip to content

Commit d602a25

Browse files
[Sema] Do not emit cast to unrelated warning from existential to Anyhashable
1 parent 0c67ce6 commit d602a25

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

lib/Sema/TypeCheckProtocol.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -5725,7 +5725,13 @@ TypeChecker::couldDynamicallyConformToProtocol(Type type, ProtocolDecl *Proto,
57255725
// we cannot know statically.
57265726
if (type->isExistentialType())
57275727
return true;
5728-
5728+
5729+
// The underlying concrete type may have a `Hashable` conformance that is
5730+
// not possible to know statically.
5731+
if (type->isAnyHashable()) {
5732+
return true;
5733+
}
5734+
57295735
// A generic archetype may have protocol conformances we cannot know
57305736
// statically.
57315737
if (type->is<ArchetypeType>())

test/Constraints/casts.swift

+13
Original file line numberDiff line numberDiff line change
@@ -693,3 +693,16 @@ func SR_16058_tests() {
693693
// More than one optionality wrapping
694694
let _: String? = foo.flatMap { dict.SR16058(_: $0) } as? String // OK
695695
}
696+
697+
// https://github.com/apple/swift/issues/59405
698+
func isHashable(_ error: Error) -> Bool {
699+
(error as? AnyHashable) != nil // OK
700+
}
701+
702+
func isHashable_is(_ error: Error) -> Bool {
703+
error is AnyHashable // OK
704+
}
705+
706+
func isHashable_composition(_ error: Error & AnyObject) -> Bool {
707+
error is AnyHashable // OK
708+
}

0 commit comments

Comments
 (0)