mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
We claim to handle optional input types but not all paths of this function actually do. rdar://39195672
79 lines
1.6 KiB
Swift
79 lines
1.6 KiB
Swift
// RUN: %target-run-simple-swift
|
|
// REQUIRES: executable_test
|
|
// REQUIRES: objc_interop
|
|
|
|
import StdlibUnittest
|
|
import Foundation
|
|
|
|
protocol P : class { }
|
|
protocol C : class { }
|
|
|
|
class Foo : NSObject {}
|
|
var Casts = TestSuite("Casts")
|
|
|
|
|
|
@inline(never)
|
|
func castit<ObjectType>(_ o: NSObject?, _ t: ObjectType.Type) -> ObjectType? {
|
|
return o as? ObjectType
|
|
}
|
|
|
|
@inline(never)
|
|
func castitExistential<ObjectType>(_ o: C?, _ t: ObjectType.Type) -> ObjectType? {
|
|
return o as? ObjectType
|
|
}
|
|
|
|
Casts.test("cast optional<nsobject> to protocol") {
|
|
if let obj = castit(nil, P.self) {
|
|
print("fail")
|
|
expectUnreachable()
|
|
} else {
|
|
print("success")
|
|
}
|
|
}
|
|
|
|
Casts.test("cast optional<nsobject> to protocol meta") {
|
|
if let obj = castit(nil, P.Type.self) {
|
|
print("fail")
|
|
expectUnreachable()
|
|
} else {
|
|
print("success")
|
|
}
|
|
}
|
|
Casts.test("cast optional<protocol> to protocol") {
|
|
if let obj = castitExistential(nil, P.self) {
|
|
print("fail")
|
|
expectUnreachable()
|
|
} else {
|
|
print("success")
|
|
}
|
|
}
|
|
|
|
Casts.test("cast optional<protocol> to class") {
|
|
if let obj = castitExistential(nil, Foo.self) {
|
|
print("fail")
|
|
expectUnreachable()
|
|
} else {
|
|
print("success")
|
|
}
|
|
}
|
|
|
|
Casts.test("cast optional<protocol> to protocol meta") {
|
|
if let obj = castitExistential(nil, P.Type.self) {
|
|
expectUnreachable()
|
|
print("fail")
|
|
} else {
|
|
print("success")
|
|
}
|
|
}
|
|
|
|
Casts.test("cast optional<protocol> to class meta") {
|
|
if let obj = castitExistential(nil, Foo.Type.self) {
|
|
expectUnreachable()
|
|
print("fail")
|
|
} else {
|
|
print("success")
|
|
}
|
|
}
|
|
|
|
runAllTests()
|