mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
* Dynamic Cast Rework: Runtime This is a completely refactored version of the core swift_dynamicCast runtime method. This fixes a number of bugs, especially in the handling of multiply-wrapped types such as Optional within Any. The result should be much closer to the behavior specified by `docs/DynamicCasting.md`. Most of the type-specific logic is simply copied over from the earlier implementation, but the overall structure has been changed to be uniformly recursive. In particular, this provides uniform handling of Optional, existentials, Any and other common "box" types along all paths. The consistent structure should also be easier to update in the future with new general types. Benchmarking does not show any noticable performance implications. **Temporarily**, the old implementation is still available. Setting the environment variable `SWIFT_OLD_DYNAMIC_CAST_RUNTIME` before launching a program will use the old runtime implementation. This is only to facilitate testing; once the new implementation is stable, I expect to completely remove the old implementation.
72 lines
1.6 KiB
Swift
72 lines
1.6 KiB
Swift
// RUN: %target-run-simple-swift
|
|
// REQUIRES: executable_test
|
|
|
|
// REQUIRES: objc_interop
|
|
// REQUIRES: OS=macosx
|
|
|
|
// rdar://20990451 is tracking the fix for compiling this test optimized.
|
|
// XFAIL: swift_test_mode_optimize
|
|
// XFAIL: swift_test_mode_optimize_size
|
|
// XFAIL: swift_test_mode_optimize_unchecked
|
|
|
|
import Foundation
|
|
import StdlibUnittest
|
|
|
|
protocol Fooable {
|
|
func foo() -> String
|
|
}
|
|
|
|
func fooify<T>(_ x: T) -> String {
|
|
if let foo = x as? Fooable {
|
|
return foo.foo()
|
|
} else {
|
|
return "not fooable"
|
|
}
|
|
}
|
|
|
|
extension NSRect: Fooable {
|
|
func foo() -> String { return "NSRect" }
|
|
}
|
|
|
|
extension CFSet: Fooable {
|
|
func foo() -> String { return "CFSet" }
|
|
}
|
|
|
|
extension NSString: Fooable {
|
|
func foo() -> String { return "NSString" }
|
|
}
|
|
|
|
var ProtocolLookupForeign = TestSuite("ProtocolLookupForeign")
|
|
|
|
ProtocolLookupForeign.test("NSRect") {
|
|
expectEqual("NSRect", fooify(NSRect()))
|
|
}
|
|
|
|
ProtocolLookupForeign.test("NSPoint") {
|
|
expectEqual("not fooable", fooify(NSPoint()))
|
|
}
|
|
|
|
ProtocolLookupForeign.test("CFSet") {
|
|
if #available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) {
|
|
expectEqual("CFSet", fooify(CFSetCreate(kCFAllocatorDefault, nil, 0, nil)!))
|
|
}
|
|
}
|
|
|
|
ProtocolLookupForeign.test("CFArray") {
|
|
expectEqual("not fooable", fooify(CFArrayCreate(kCFAllocatorDefault, nil, 0, nil)!))
|
|
}
|
|
|
|
ProtocolLookupForeign.test("NSString") {
|
|
expectEqual("NSString", fooify(NSString()))
|
|
}
|
|
|
|
ProtocolLookupForeign.test("NSMutableString") {
|
|
expectEqual("NSString", fooify(NSMutableString()))
|
|
}
|
|
|
|
ProtocolLookupForeign.test("NSSet") {
|
|
expectEqual("not fooable", fooify(NSSet()))
|
|
}
|
|
|
|
runAllTests()
|