Skip to content

[Runtime] Have swift_dynamicCast unwrap multiple levels of optionals in the source. #16772

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
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
2 changes: 1 addition & 1 deletion stdlib/public/runtime/Casting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1866,7 +1866,7 @@ checkDynamicCastFromOptional(OpaqueValue *dest,
// .Some
// Single payload enums are guaranteed layout compatible with their
// payload. Only the source's payload needs to be taken or destroyed.
return {false, payloadType};
return checkDynamicCastFromOptional(dest, src, payloadType, targetType, flags);
}

/******************************************************************************/
Expand Down
20 changes: 20 additions & 0 deletions test/stdlib/Casts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,26 @@ CastsTests.test("No overrelease of existential boxes in failed casts") {

extension Int : P {}

// Test for SR-7664: Inconsistent optional casting behaviour with generics
// Runtime failed to unwrap multiple levels of Optional when casting.
CastsTests.test("Multi-level optionals can be casted") {
func testSuccess<From, To>(_ x: From, from: From.Type, to: To.Type) {
expectNotNil(x as? To)
}
func testFailure<From, To>(_ x: From, from: From.Type, to: To.Type) {
expectNil(x as? To)
}
testSuccess(42, from: Int?.self, to: Int.self)
testSuccess(42, from: Int??.self, to: Int.self)
testSuccess(42, from: Int???.self, to: Int.self)
testSuccess(42, from: Int???.self, to: Int?.self)
testSuccess(42, from: Int???.self, to: Int??.self)
testSuccess(42, from: Int???.self, to: Int???.self)
testFailure(42, from: Int?.self, to: String.self)
testFailure(42, from: Int??.self, to: String.self)
testFailure(42, from: Int???.self, to: String.self)
}

#if _runtime(_ObjC)
extension CFBitVector : P {
static func makeImmutable(from values: Array<UInt8>) -> CFBitVector {
Expand Down