swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.66k stars 10.38k forks source link

[SR-6441] Optionals used as a Generic types fail casting nil #48991

Open swift-ci opened 7 years ago

swift-ci commented 7 years ago
Previous ID SR-6441
Radar rdar://problem/35700189
Original Reporter Jan Mazurczak (JIRA User)
Type Bug
Environment Xcode version 9.1 (9B55) Swift 4 Tested in Swift Playgrounds on iOS too
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Standard Library | |Labels | Bug | |Assignee | None | |Priority | Medium | md5: 8bf4f977f4fc88a46b367d7a04a9bb0f

Issue Description:

struct A<Message> {
    func received(message: Any?) {
        if let message = message as? Message {
            print("success")
        } else {
            print("fail")
        }
    }
}

let a = A<Int?>()
a.received(message: nil)

In above example I didn't find a way to recognise message as generic type when that type is an optional.

Expected:

When generic type Message is Optional casting nil as? Message should always succeed with nil result.

Actual result:

Casting fails.

belkadan commented 7 years ago

cc @jckarter

jckarter commented 7 years ago

@swift-ci create