Skip to content

Commit 7da78f7

Browse files
authored
Verify that nil can cast to AnyHashable (#34434)
Optional conditionally conforms to Hashable, so for example `Optional<Int>` must cast to AnyHashable _even if it contains `nil`_. This was broken up through Swift 5.3 and was fixed by the new dynamic cast runtime. Add a test to ensure this stays fixed.
1 parent 7f2185d commit 7da78f7

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

test/Casting/Casts.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,4 +761,12 @@ CastsTests.test("Async function types") {
761761
expectFalse(asyncFnType is (() -> Void).Type)
762762
}
763763

764+
// `Optional<Int>` is Hashable, so it must cast to AnyHashable,
765+
// even if it contains a nil. (This was broken in 5.3 and earlier,
766+
// but was fixed by the new dynamic cast runtime.)
767+
CastsTests.test("Optional nil -> AnyHashable") {
768+
let a : Int? = nil
769+
expectNotNil(a as? AnyHashable)
770+
}
771+
764772
runAllTests()

0 commit comments

Comments
 (0)