Files
swift-mirror/test/Interpreter/casts.swift
Arnold Schwaighofer 0f8548153f IRGen: Add nil checks to scalarCheckedCasts
We claim to handle optional input types but not all paths of this function actually do.

rdar://39195672
2018-04-05 13:04:27 -07:00

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()