// RUN: %target-run-simple-swift(-Xfrontend -sil-verify-all -Xfrontend -disable-availability-checking) | %FileCheck %s // RUN: %target-run-simple-swift(-O -Xfrontend -sil-verify-all -Xfrontend -disable-availability-checking) | %FileCheck %s // REQUIRES: executable_test // UNSUPPORTED: use_os_stdlib // UNSUPPORTED: back_deployment_runtime protocol P { func speak() } extension P { func speak() { print("hello") } } struct Noncopyable: ~Copyable {} struct Ordinary {} struct Dog: Copyable {} extension Dog: P where T: Copyable {} enum Cat: Copyable { case meows init() { self = .meows } } extension Cat: P where Left: Copyable, Right: ~Copyable {} struct ConditionallyCopyable: ~Copyable { var value: Int = 17 } extension ConditionallyCopyable: Copyable where T: Copyable { } // FIXME: Not yet supported // struct VariadicCopyable: Copyable { } // extension VariadicCopyable: P where repeat each T: Copyable { } func attemptCall(_ a: Any) { if let value = a as? P { value.speak() return } print("failed to cast (attemptCall)") } @_silgen_name("swift_getExtendedFunctionTypeMetadata") func _getExtendedFunctionTypeMetadata( flags: UInt, differentiabilityKind: UInt, parameterTypes: UnsafePointer?, parameterFlags: UnsafePointer?, resultType: Any.Type, globalActorType: Any.Type? = nil, extendedFlags: UInt32, thrownErrorType: Any.Type? = nil) -> Any.Type defer { main() } func main() { // CHECK: hello attemptCall(Dog()) // CHECK: failed to cast (attemptCall) attemptCall(Dog()) // CHECK: failed to cast (attemptCall) attemptCall(Dog>()) // CHECK: hello attemptCall(Cat()) // CHECK: failed to cast (attemptCall) attemptCall(Cat()) // CHECK: failed to cast (attemptCall) attemptCall(Cat>()) // CHECK-FIXME: hello // attemptCall(VariadicCopyable>()) // CHECK-FIXME: failed to cast (attemptCall) // attemptCall(VariadicCopyable>()) // CHECK: tuple types print("tuple types") // CHECK: hello attemptCall(Dog<(Ordinary, Ordinary)>()) // CHECK-FIXME: failed to cast (attemptCall) // FIXME: Requires the ability to create such tuple types // attemptCall(Dog<(Ordinary, Noncopyable)>()) // CHECK: metatype types print("metatype types") // CHECK: hello attemptCall(Dog()) // CHECK: hello attemptCall(Dog()) // CHECK: function types print("function types") attemptCall(Dog<(Ordinary) -> Noncopyable>()) // This is a nonmovable function type, which cannot currently be // expressed in the language. let noncopyableFnType = _getExtendedFunctionTypeMetadata( flags: 0x04000000 | 0x80000000, differentiabilityKind: 0, parameterTypes: nil, parameterFlags: nil, resultType: Void.self, extendedFlags: 0x1 << 16) // CHECK: failed to cast (attemptCall) func doFuncCall(_: F.Type) { attemptCall(Dog()) } _openExistential(noncopyableFnType, do: doFuncCall) // CHECK: existential types print("existential types") // CHECK: hello attemptCall(Dog()) // CHECK-FIXME: failed to cast (attemptCall) // FIXME crashes: attemptCall(Dog()) // CHECK: cast succeeded test_radar124171788(.nothing) } // coverage for rdar://124171788 enum Maybe: ~Copyable { case just(Wrapped) case nothing } extension Maybe: Copyable where Wrapped: Copyable {} extension Maybe: CustomDebugStringConvertible { var debugDescription: String { "cast succeeded" } } func test_radar124171788(_ v: Maybe) { if let foo = v as? CustomDebugStringConvertible { print("\(foo.debugDescription)") return } print("failed to cast (test_radar124171788)") }