// ParameterizedExistentials.swift - Casting tests for generalized existentials // // This source file is part of the Swift.org open source project // // Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See https://swift.org/LICENSE.txt for license information // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors // // ----------------------------------------------------------------------------- /// /// Tests for parameterized existential type conversions. /// // ----------------------------------------------------------------------------- // RUN: %empty-directory(%t) // // RUN: %target-build-swift -swift-version 5 -g -Onone -Xfrontend -disable-availability-checking -module-name a -c %s -o %t/ParameterizedExistentials.swift.Onone.o // RUN: %target-swiftc_driver %t/ParameterizedExistentials.swift.Onone.o -o %t/a.swift5.Onone.out // RUN: %target-codesign %t/a.swift5.Onone.out // RUN: %target-run %t/a.swift5.Onone.out // // RUN: %target-build-swift -swift-version 5 -g -O -Xfrontend -disable-availability-checking -module-name a -c %s -o %t/ParameterizedExistentials.swift.O.o // RUN: %target-swiftc_driver %t/ParameterizedExistentials.swift.O.o -o %t/a.swift5.O.out // RUN: %target-codesign %t/a.swift5.O.out // RUN: %target-run %t/a.swift5.O.out // // REQUIRES: executable_test // This test requires the new existential shape metadata accessors which are // not available in on-device runtimes, or in the back-deployment runtime. // UNSUPPORTED: use_os_stdlib // UNSUPPORTED: back_deployment_runtime import Swift import StdlibUnittest protocol Holder { associatedtype T var value: T { get } } struct GenericHolder: Holder { var value: T init(value: T) { self.value = value} } protocol PairType< T, U, > { associatedtype T associatedtype U var first: T { get } var second: U { get } } struct Pair: PairType { var value: (T, U) var first: T { self.value.0 } var second: U { self.value.1 } init(value: (T, U)) { self.value = value} } final class ReferencePair: PairType { var first: T var second: U init(value: (T, U)) { (self.first, self.second) = value } } let tests = TestSuite("ParameterizedExistentials") tests.test("Parameterized existential casting basics work") { let a = GenericHolder(value: 5) as any Holder let b = GenericHolder(value: 5) as! any Holder expectEqual(a.value, b.value) let c = GenericHolder(value: 5) as? any Holder expectNotNil(c) let d = GenericHolder(value: 5) as? any Holder expectNil(d) } tests.test("Metatype existential casting basics work") { let a = GenericHolder.self as any Holder.Type let b = GenericHolder.self as! any Holder.Type expectTrue(a == b) let c = GenericHolder.self as? any Holder.Type expectNotNil(c) let d = GenericHolder.self as? any Holder.Type expectNil(d) } tests.test("Existential box should maintain identity") { let a = Pair(value: ("Hello", 42)) var b/*ox*/ = a as? any PairType expectNotNil(b) expectEqual(a.value, (b!.first, b!.second)) let c = ReferencePair(value: ("Goodbye", 24)) let d = c as? any PairType expectNotNil(d) b = d! expectEqual(b!.first, d!.first) expectEqual(b!.second, d!.second) let e = b as? ReferencePair expectNotNil(e) expectTrue(e! === c) } runAllTests()