// RUN: %empty-directory(%t) // RUN: %target-swift-frontend -target %target-future-triple -I %S/Inputs/CTypes -prespecialize-generic-metadata -enable-experimental-feature LayoutStringValueWitnesses -enable-experimental-feature LayoutStringValueWitnessesInstantiation -enable-layout-string-value-witnesses -enable-layout-string-value-witnesses-instantiation -enable-type-layout -enable-autolinking-runtime-compatibility-bytecode-layouts -parse-stdlib -emit-module -emit-module-path=%t/layout_string_witnesses_types.swiftmodule %S/Inputs/layout_string_witnesses_types.swift // RUN: %target-build-swift-dylib(%t/%target-library-name(layout_string_witnesses_types)) -target %target-future-triple -I %S/Inputs/CTypes -Xfrontend -enable-experimental-feature -Xfrontend LayoutStringValueWitnesses -Xfrontend -enable-experimental-feature -Xfrontend LayoutStringValueWitnessesInstantiation -Xfrontend -enable-layout-string-value-witnesses -Xfrontend -enable-layout-string-value-witnesses-instantiation -Xfrontend -enable-type-layout -Xfrontend -parse-stdlib -parse-as-library %S/Inputs/layout_string_witnesses_types.swift // RUN: %target-codesign %t/%target-library-name(layout_string_witnesses_types) // RUN: %target-swift-frontend -target %target-future-triple -enable-experimental-feature LayoutStringValueWitnesses -enable-experimental-feature LayoutStringValueWitnessesInstantiation -enable-layout-string-value-witnesses -enable-layout-string-value-witnesses-instantiation -enable-library-evolution -enable-autolinking-runtime-compatibility-bytecode-layouts -emit-module -emit-module-path=%t/layout_string_witnesses_types_resilient.swiftmodule %S/Inputs/layout_string_witnesses_types_resilient.swift // RUN: %target-build-swift -target %target-future-triple -g -Xfrontend -enable-experimental-feature -Xfrontend LayoutStringValueWitnesses -Xfrontend -enable-experimental-feature -Xfrontend LayoutStringValueWitnessesInstantiation -Xfrontend -enable-layout-string-value-witnesses -Xfrontend -enable-layout-string-value-witnesses-instantiation -Xfrontend -enable-library-evolution -c -parse-as-library -o %t/layout_string_witnesses_types_resilient.o %S/Inputs/layout_string_witnesses_types_resilient.swift // RUN: %target-build-swift -target %target-future-triple -g -parse-stdlib -module-name layout_string_witnesses_dynamic -llayout_string_witnesses_types -L%t %t/layout_string_witnesses_types_resilient.o -I %t -o %t/main %s %target-rpath(%t) // RUN: %target-codesign %t/main // RUN: %target-run %t/main %t/%target-library-name(layout_string_witnesses_types) | %FileCheck %s --check-prefix=CHECK -check-prefix=CHECK-%target-os // REQUIRES: executable_test // REQUIRES: swift_feature_LayoutStringValueWitnesses // REQUIRES: swift_feature_LayoutStringValueWitnessesInstantiation // Requires runtime functions added in Swift 5.9. // UNSUPPORTED: use_os_stdlib // UNSUPPORTED: back_deployment_runtime import Swift import layout_string_witnesses_types import layout_string_witnesses_types_resilient class TestClass { init() {} deinit { print("TestClass deinitialized!") } } func testGeneric() { let ptr = allocateInternalGenericPtr(of: TestClass.self) do { let x = TestClass() testGenericInit(ptr, to: x) } do { let y = TestClass() // CHECK: Before deinit print("Before deinit") // CHECK-NEXT: TestClass deinitialized! testGenericAssign(ptr, from: y) } // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: TestClass deinitialized! testGenericDestroy(ptr, of: TestClass.self) ptr.deallocate() } testGeneric() func testGenericAny() { let ptr = allocateInternalGenericPtr(of: Any.self) do { let x: Any = TestClass() testGenericInit(ptr, to: x as Any) } do { let y: Any = TestClass() // CHECK: Before deinit print("Before deinit") // CHECK-NEXT: TestClass deinitialized! testGenericAssign(ptr, from: y as Any) } // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: TestClass deinitialized! testGenericDestroy(ptr, of: Any.self) ptr.deallocate() } testGenericAny() func testPrespecializedAnyObject() { let ptr = UnsafeMutablePointer>.allocate(capacity: 1) do { let x = PrespecializedStruct(x: SimpleClass(x: 23)) testInit(ptr, to: x) } do { let y = PrespecializedStruct(x: SimpleClass(x: 32)) // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: SimpleClass deinitialized! testAssign(ptr, from: y) } // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: SimpleClass deinitialized! testDestroy(ptr) ptr.deallocate() } testPrespecializedAnyObject() func testPrespecializedStructSimpleClass() { let ptr = UnsafeMutablePointer>.allocate(capacity: 1) do { let x = PrespecializedStruct(x: SimpleClass(x: 23)) testInit(ptr, to: x) } do { let y = PrespecializedStruct(x: SimpleClass(x: 32)) // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: SimpleClass deinitialized! testAssign(ptr, from: y) } // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: SimpleClass deinitialized! testDestroy(ptr) ptr.deallocate() } testPrespecializedStructSimpleClass() func testPrespecializedStructInt() { let ptr = UnsafeMutablePointer>.allocate(capacity: 1) do { let x = PrespecializedStruct(x: 23) testInit(ptr, to: x) } do { let y = PrespecializedStruct(x: 32) testAssign(ptr, from: y) } ptr.deallocate() } testPrespecializedStructInt() func testPrespecializedSingletonEnumAnyObject() { let ptr = UnsafeMutablePointer>.allocate(capacity: 1) do { let x = PrespecializedSingletonEnum.only(23, SimpleClass(x: 23)) testInit(ptr, to: x) } do { let y = PrespecializedSingletonEnum.only(32, SimpleClass(x: 32)) // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: SimpleClass deinitialized! testAssign(ptr, from: y) } // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: SimpleClass deinitialized! testDestroy(ptr) ptr.deallocate() } testPrespecializedSingletonEnumAnyObject() func testPrespecializedSingletonEnumSimpleClass() { let ptr = UnsafeMutablePointer>.allocate(capacity: 1) do { let x = PrespecializedSingletonEnum.only(23, SimpleClass(x: 23)) testInit(ptr, to: x) } do { let y = PrespecializedSingletonEnum.only(32, SimpleClass(x: 32)) // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: SimpleClass deinitialized! testAssign(ptr, from: y) } // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: SimpleClass deinitialized! testDestroy(ptr) ptr.deallocate() } testPrespecializedSingletonEnumSimpleClass() func testPrespecializedSingletonEnumInt() { let ptr = UnsafeMutablePointer>.allocate(capacity: 1) do { let x = PrespecializedSingletonEnum.only(23, 23) testInit(ptr, to: x) } do { let y = PrespecializedSingletonEnum.only(32, 32) testAssign(ptr, from: y) } ptr.deallocate() } testPrespecializedSingletonEnumInt() func testPrespecializedSinglePayloadEnumAnyObject() { let ptr = UnsafeMutablePointer>.allocate(capacity: 1) do { let x = PrespecializedSinglePayloadEnum.nonEmpty(23, SimpleClass(x: 23)) testInit(ptr, to: x) } do { let y = PrespecializedSinglePayloadEnum.nonEmpty(32, SimpleClass(x: 32)) // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: SimpleClass deinitialized! testAssign(ptr, from: y) } // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: SimpleClass deinitialized! testDestroy(ptr) ptr.deallocate() } testPrespecializedSinglePayloadEnumAnyObject() func testPrespecializedSinglePayloadEnumSimpleClass() { let ptr = UnsafeMutablePointer>.allocate(capacity: 1) do { let x = PrespecializedSinglePayloadEnum.nonEmpty(23, SimpleClass(x: 23)) testInit(ptr, to: x) } do { let y = PrespecializedSinglePayloadEnum.nonEmpty(32, SimpleClass(x: 32)) // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: SimpleClass deinitialized! testAssign(ptr, from: y) } // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: SimpleClass deinitialized! testDestroy(ptr) ptr.deallocate() } testPrespecializedSinglePayloadEnumSimpleClass() func testPrespecializedSinglePayloadEnumInt() { let ptr = UnsafeMutablePointer>.allocate(capacity: 1) do { let x = PrespecializedSinglePayloadEnum.nonEmpty(23, 23) testInit(ptr, to: x) } do { let y = PrespecializedSinglePayloadEnum.nonEmpty(32, 32) testAssign(ptr, from: y) } ptr.deallocate() } testPrespecializedSinglePayloadEnumInt() func testPrespecializedMultiPayloadEnumAnyObject() { let ptr = UnsafeMutablePointer>.allocate(capacity: 1) do { let x = PrespecializedMultiPayloadEnum.nonEmpty0(23, SimpleClass(x: 23)) testInit(ptr, to: x) } do { let y = PrespecializedMultiPayloadEnum.nonEmpty0(32, SimpleClass(x: 32)) // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: SimpleClass deinitialized! testAssign(ptr, from: y) } // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: SimpleClass deinitialized! testDestroy(ptr) ptr.deallocate() } testPrespecializedMultiPayloadEnumAnyObject() func testPrespecializedMultiPayloadEnumSimpleClass() { let ptr = UnsafeMutablePointer>.allocate(capacity: 1) do { let x = PrespecializedMultiPayloadEnum.nonEmpty0(23, SimpleClass(x: 23)) testInit(ptr, to: x) } do { let y = PrespecializedMultiPayloadEnum.nonEmpty0(32, SimpleClass(x: 32)) // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: SimpleClass deinitialized! testAssign(ptr, from: y) } // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: SimpleClass deinitialized! testDestroy(ptr) ptr.deallocate() } testPrespecializedMultiPayloadEnumSimpleClass() func testPrespecializedMultiPayloadEnumInt() { let ptr = UnsafeMutablePointer>.allocate(capacity: 1) do { let x = PrespecializedMultiPayloadEnum.nonEmpty0(23, 23) testInit(ptr, to: x) } do { let y = PrespecializedMultiPayloadEnum.nonEmpty0(32, 32) testAssign(ptr, from: y) } ptr.deallocate() } testPrespecializedMultiPayloadEnumInt() func testGenericTuple() { let ptr = allocateInternalGenericPtr(of: GenericTupleWrapper.self) do { let x = TestClass() testGenericInit(ptr, to: GenericTupleWrapper((x, 32))) } do { let y = TestClass() // CHECK: Before deinit print("Before deinit") // CHECK-NEXT: TestClass deinitialized! testGenericAssign(ptr, from: GenericTupleWrapper((y, 32))) } // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: TestClass deinitialized! testGenericDestroy(ptr, of: GenericTupleWrapper.self) ptr.deallocate() } testGenericTuple() func testGenericNested() { let ptr = allocateInternalGenericPtr(of: GenericNestedOuter.Inner.self) do { let x = TestClass() testGenericInit(ptr, to: GenericNestedOuter.Inner(x)) } do { let y = TestClass() // CHECK: Before deinit print("Before deinit") // CHECK-NEXT: TestClass deinitialized! testGenericAssign(ptr, from: GenericNestedOuter.Inner(y)) } // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: TestClass deinitialized! testGenericDestroy(ptr, of: GenericNestedOuter.Inner.self) ptr.deallocate() } testGenericNested() func testGenericNestedRef() { let ptr = allocateInternalGenericPtr(of: GenericNestedRefOuter.Inner.self) do { let x = TestClass() testGenericInit(ptr, to: GenericNestedRefOuter.Inner(x)) } do { let y = TestClass() // CHECK: Before deinit print("Before deinit") // CHECK-NEXT: TestClass deinitialized! testGenericAssign(ptr, from: GenericNestedRefOuter.Inner(y)) } // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: TestClass deinitialized! testGenericDestroy(ptr, of: GenericNestedRefOuter.Inner.self) ptr.deallocate() } testGenericNestedRef() func testGenericEnum() { let ptr = allocateInternalGenericPtr(of: GenericEnumWrapper.self) do { let x = TestClass() testGenericInit(ptr, to: GenericEnumWrapper(.a(x, 32), 32)) } do { let y = TestClass() // CHECK: Before deinit print("Before deinit") // CHECK-NEXT: TestClass deinitialized! testGenericAssign(ptr, from: GenericEnumWrapper(.a(y, 45), 45)) } // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: TestClass deinitialized! testGenericDestroy(ptr, of: GenericEnumWrapper.self) ptr.deallocate() } testGenericEnum() func testGenericEnumSingleton() { let ptr = allocateInternalGenericPtr(of: SingletonEnum.self) do { let x = TestClass() testGenericInit(ptr, to: SingletonEnum.only(x, 23)) } do { let y = TestClass() // CHECK: Before deinit print("Before deinit") // CHECK-NEXT: TestClass deinitialized! testGenericAssign(ptr, from: SingletonEnum.only(y, 32)) } // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: TestClass deinitialized! testGenericDestroy(ptr, of: SingletonEnum.self) ptr.deallocate() } testGenericEnumSingleton() func testRecursive() { let ptr = allocateInternalGenericPtr(of: Recursive.self) do { let x = TestClass() testGenericInit(ptr, to: Recursive(x, nil)) } do { let y = TestClass() // CHECK: Before deinit print("Before deinit") // CHECK-NEXT: TestClass deinitialized! testGenericAssign(ptr, from: Recursive(y, nil)) } // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: TestClass deinitialized! testGenericDestroy(ptr, of: Recursive.self) ptr.deallocate() } testRecursive() func testComplexNesting() { let ptr = allocateInternalGenericPtr(of: ComplexNesting.self) do { let x = TestClass() testGenericInit(ptr, to: ComplexNesting(34, x, x, x)) } do { let y = TestClass() // CHECK: Before deinit print("Before deinit") // CHECK-NEXT: TestClass deinitialized! testGenericAssign(ptr, from: ComplexNesting(34, y, y, y)) } // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: TestClass deinitialized! testGenericDestroy(ptr, of: ComplexNesting.self) ptr.deallocate() } testComplexNesting() enum TestEnum { case empty case nonEmpty(TestClass) } func testGenericWithEnumNonEmpty() { let ptr = allocateInternalGenericPtr(of: TestEnum.self) do { let x = TestClass() testGenericInit(ptr, to: TestEnum.nonEmpty(x)) } do { let y = TestClass() // CHECK: Before deinit print("Before deinit") // CHECK-NEXT: TestClass deinitialized! testGenericAssign(ptr, from: TestEnum.nonEmpty(y)) } // CHECK-NEXT: TestClass deinitialized! testGenericDestroy(ptr, of: TestEnum.self) ptr.deallocate() } testGenericWithEnumNonEmpty() public struct ResilientWrapper { let x: SimpleResilient let y: Int } func testResilient() { let ptr = UnsafeMutablePointer.allocate(capacity: 1) do { let x = TestClass() testInit(ptr, to: ResilientWrapper(x: SimpleResilient(x: 23, y: x), y: 5)) } do { let y = TestClass() // CHECK: Before deinit print("Before deinit") // CHECK-NEXT: TestClass deinitialized! testAssign(ptr, from: ResilientWrapper(x: SimpleResilient(x: 23, y: y), y: 7)) } // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: TestClass deinitialized! testDestroy(ptr) ptr.deallocate() } testResilient() public struct GenericResilientWrapper { let x: GenericResilient let y: Int } func testGenericResilient() { let ptr = UnsafeMutablePointer>.allocate(capacity: 1) do { let x = TestClass() testInit(ptr, to: GenericResilientWrapper(x: GenericResilient(x: x, y: 32), y: 32)) } do { let y = TestClass() // CHECK: Before deinit print("Before deinit") // CHECK-NEXT: TestClass deinitialized! testAssign(ptr, from: GenericResilientWrapper(x: GenericResilient(x: y, y: 42), y: 32)) } // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: TestClass deinitialized! testDestroy(ptr) print("Dealloc") ptr.deallocate() } testGenericResilient() func testMixedEnumWrapperWrapperGeneric() { let ptr = allocateInternalGenericPtr(of: MixedEnumWrapperWrapperGeneric.self) do { let x = MixedEnumWrapperWrapperGeneric(x: MixedEnumWrapper(x: .nonEmpty(SimpleClass(x: 23)), y: .e(SimpleClass(x: 32))), y: TestClass()) testGenericInit(ptr, to: x) } do { let y = MixedEnumWrapperWrapperGeneric(x: MixedEnumWrapper(x: .nonEmpty(SimpleClass(x: 28)), y: .e(SimpleClass(x: 82))), y: TestClass()) // CHECK: Before deinit print("Before deinit") // CHECK-NEXT: SimpleClass deinitialized! // CHECK-NEXT: SimpleClass deinitialized! // CHECK-NEXT: TestClass deinitialized! testGenericAssign(ptr, from: y) } // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: SimpleClass deinitialized! // CHECK-NEXT: SimpleClass deinitialized! // CHECK-NEXT: TestClass deinitialized! testGenericDestroy(ptr, of: MixedEnumWrapperWrapperGeneric.self) ptr.deallocate() } testMixedEnumWrapperWrapperGeneric() func testGenericSinglePayloadEnum() { let ptr = allocateInternalGenericPtr(of: SinglePayloadEnum.self) do { let x = SinglePayloadEnum.nonEmpty(23, SimpleClass(x: 23)) testGenericInit(ptr, to: x) } do { let y = SinglePayloadEnum.nonEmpty(32, SimpleClass(x: 32)) // CHECK: Before deinit print("Before deinit") // CHECK-NEXT: SimpleClass deinitialized! testGenericAssign(ptr, from: y) } // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: SimpleClass deinitialized! testGenericDestroy(ptr, of: SinglePayloadEnum.self) ptr.deallocate() } testGenericSinglePayloadEnum() func testGenericSinglePayloadEnumManyXI() { let ptr = allocateInternalGenericPtr(of: SinglePayloadEnumManyXI.self) do { let x = SinglePayloadEnumManyXI.nonEmpty(Builtin.zeroInitializer(), SimpleClass(x: 23)) testGenericInit(ptr, to: x) } do { let y = SinglePayloadEnumManyXI.nonEmpty(Builtin.zeroInitializer(), SimpleClass(x: 23)) // CHECK: Before deinit print("Before deinit") // CHECK-NEXT: SimpleClass deinitialized! testGenericAssign(ptr, from: y) } // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: SimpleClass deinitialized! testGenericDestroy(ptr, of: SinglePayloadEnumManyXI.self) ptr.deallocate() } testGenericSinglePayloadEnumManyXI() struct RefPlusEnumResolve { let x: SimpleClass let y: ResilientSinglePayloadEnumComplex } func testRefPlusEnumResolve() { let ptr = allocateInternalGenericPtr(of: RefPlusEnumResolve.self) do { let x = RefPlusEnumResolve(x: SimpleClass(x: 23), y: .nonEmpty(.nonEmpty1(SimpleClass(x: 23)))) testGenericInit(ptr, to: x) } do { let y = RefPlusEnumResolve(x: SimpleClass(x: 23), y: .nonEmpty(.nonEmpty1(SimpleClass(x: 23)))) // CHECK: Before deinit print("Before deinit") // CHECK-NEXT: SimpleClass deinitialized! // CHECK-NEXT: SimpleClass deinitialized! testGenericAssign(ptr, from: y) } // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: SimpleClass deinitialized! // CHECK-NEXT: SimpleClass deinitialized! testGenericDestroy(ptr, of: RefPlusEnumResolve.self) ptr.deallocate() } testRefPlusEnumResolve() func testResilientSingletonEnumTag() { let x = switch getResilientSingletonEnumNonEmpty(SimpleClass(x: 23)) { case .nonEmpty: 0 } // CHECK: Enum case: 0 print("Enum case: \(x)") } testResilientSingletonEnumTag() func testResilientSinglePayloadEnumSimpleTag() { let x = switch getResilientSinglePayloadEnumSimpleEmpty0() { case .nonEmpty: 0 case .empty0: 1 case .empty1: 2 } // CHECK: Enum case: 1 print("Enum case: \(x)") } testResilientSinglePayloadEnumSimpleTag() func testResilientSinglePayloadEnumSimpleTagMultiExtraTagPayload() { let x = switch getResilientSinglePayloadEnumSimpleMultiExtraTagPayloadEmpty3() { case .nonEmpty: 0 case .empty0: 1 case .empty1: 2 case .empty2: 3 case .empty3: 4 case .empty4: 5 case .empty5: 6 case .empty6: 7 case .empty7: 8 case .empty8: 9 } // CHECK: Enum case: 4 print("Enum case: \(x)") } testResilientSinglePayloadEnumSimpleTagMultiExtraTagPayload() func testResilientSinglePayloadEnumIndirectTag() { let x = switch getResilientSinglePayloadEnumIndirectNonEmpty(SimpleClass(x: 23)) { case .nonEmpty: 0 case .empty: 1 } // CHECK: Enum case: 0 print("Enum case: \(x)") let y = switch getResilientSinglePayloadEnumIndirectEmpty() { case .nonEmpty: 0 case .empty: 1 } // CHECK: Enum case: 1 print("Enum case: \(y)") } testResilientSinglePayloadEnumIndirectTag() func testResilientSinglePayloadEnumComplexTag() { let x = switch getResilientSinglePayloadEnumComplexEmpty0() { case .nonEmpty: 0 case .empty0: 1 case .empty1: 2 } // CHECK: Enum case: 1 print("Enum case: \(x)") } testResilientSinglePayloadEnumComplexTag() func testResilientMultiPayloadEnumTag() { let x = switch getResilientMultiPayloadEnumEmpty0() { case .nonEmpty0: 0 case .nonEmpty1: 1 case .empty0: 2 case .empty1: 3 } // CHECK: Enum case: 2 print("Enum case: \(x)") } testResilientMultiPayloadEnumTag() func testResilientSinglePayloadEnumGenericTag() { let x = switch getResilientSinglePayloadEnumGenericEmpty0(AnyObject.self) { case .nonEmpty: 0 case .empty0: 1 case .empty1: 2 } // CHECK: Enum case: 1 print("Enum case: \(x)") } testResilientSinglePayloadEnumGenericTag() func testResilientMultiPayloadEnumGenericTag() { let x = switch getResilientMultiPayloadEnumGenericEmpty0(AnyObject.self) { case .nonEmpty0: 0 case .nonEmpty1: 1 case .empty0: 2 case .empty1: 3 } // CHECK: Enum case: 2 print("Enum case: \(x)") } testResilientMultiPayloadEnumGenericTag() @inline(never) func matchResilientSinglePayloadEnumSimpleTag(_ x: ResilientSinglePayloadEnumSimple) -> Int { return switch x { case .nonEmpty: 0 case .empty0: 1 case .empty1: 2 } } func testResilientSinglePayloadEnumSimpleInjectTag() { let x = ResilientSinglePayloadEnumSimple.nonEmpty(SimpleClass(x: 23)) let y = ResilientSinglePayloadEnumSimple.empty0 let z = ResilientSinglePayloadEnumSimple.empty1 // CHECK: Enum case: 0 print("Enum case: \(matchResilientSinglePayloadEnumSimpleTag(x))") // CHECK: Enum case: 1 print("Enum case: \(matchResilientSinglePayloadEnumSimpleTag(y))") // CHECK: Enum case: 2 print("Enum case: \(matchResilientSinglePayloadEnumSimpleTag(z))") } testResilientSinglePayloadEnumSimpleInjectTag() @inline(never) func matchResilientSinglePayloadEnumGenericTag(_ x: ResilientSinglePayloadEnumGeneric) -> Int { return switch x { case .nonEmpty: 0 case .empty0: 1 case .empty1: 2 } } func testResilientSinglePayloadEnumGenericInjectTag() { let x = ResilientSinglePayloadEnumGeneric.nonEmpty(SimpleClass(x: 23)) let y = ResilientSinglePayloadEnumGeneric.empty0 let z = ResilientSinglePayloadEnumGeneric.empty1 // CHECK: Enum case: 0 print("Enum case: \(matchResilientSinglePayloadEnumGenericTag(x))") // CHECK: Enum case: 1 print("Enum case: \(matchResilientSinglePayloadEnumGenericTag(y))") // CHECK: Enum case: 2 print("Enum case: \(matchResilientSinglePayloadEnumGenericTag(z))") } testResilientSinglePayloadEnumGenericInjectTag() @inline(never) func matchResilientMultiPayloadEnumGenericTag(_ x: ResilientMultiPayloadEnumGeneric) -> Int { return switch x { case .nonEmpty0: 0 case .nonEmpty1: 1 case .empty0: 2 case .empty1: 3 } } func testResilientMultiPayloadEnumGenericInjectTag() { let x = ResilientMultiPayloadEnumGeneric.nonEmpty0(SimpleClass(x: 23)) let y = ResilientMultiPayloadEnumGeneric.nonEmpty1(SimpleClass(x: 32)) let z = ResilientMultiPayloadEnumGeneric.empty0 let w = ResilientMultiPayloadEnumGeneric.empty1 // CHECK: Enum case: 0 print("Enum case: \(matchResilientMultiPayloadEnumGenericTag(x))") // CHECK: Enum case: 1 print("Enum case: \(matchResilientMultiPayloadEnumGenericTag(y))") // CHECK: Enum case: 2 print("Enum case: \(matchResilientMultiPayloadEnumGenericTag(z))") // CHECK: Enum case: 3 print("Enum case: \(matchResilientMultiPayloadEnumGenericTag(w))") } testResilientMultiPayloadEnumGenericInjectTag() @inline(never) func matchResilientSingletonEnumTag(_ x: ResilientSingletonEnum) -> Int { return switch x { case .nonEmpty: 0 } } func testResilientSingletonEnumGenericInjectTag() { let x = ResilientSingletonEnum.nonEmpty(SimpleClass(x: 23)) // CHECK: Enum case: 0 print("Enum case: \(matchResilientSingletonEnumTag(x))") } testResilientSingletonEnumGenericInjectTag() enum ResilientPayloadSinglePayloadEnum { case empty0 case empty1 case empty2 case nonEmpty(ResilientSinglePayloadEnum, Int) } func testResilientPayloadSinglePayloadEnum() { let xxx = ResilientPayloadSinglePayloadEnum.nonEmpty(.empty0, 1) // CHECK: nonEmpty(layout_string_witnesses_types_resilient.ResilientSinglePayloadEnum.empty0, 1) print(xxx) } testResilientPayloadSinglePayloadEnum() struct SinglePayloadSimpleResolve { let x: ResilientSinglePayloadEnumSimple let y: ResilientSinglePayloadEnumComplex } func testSinglePayloadSimpleResolve() { let ptr = allocateInternalGenericPtr(of: SinglePayloadSimpleResolve.self) do { let x = SinglePayloadSimpleResolve(x: .nonEmpty(SimpleClass(x: 23)), y: .nonEmpty(.nonEmpty1(SimpleClass(x: 23)))) testGenericInit(ptr, to: x) } do { let y = SinglePayloadSimpleResolve(x: .nonEmpty(SimpleClass(x: 32)), y: .nonEmpty(.nonEmpty1(SimpleClass(x: 32)))) // CHECK: Before deinit print("Before deinit") // CHECK-NEXT: SimpleClass deinitialized! // CHECK-NEXT: SimpleClass deinitialized! testGenericAssign(ptr, from: y) } // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: SimpleClass deinitialized! // CHECK-NEXT: SimpleClass deinitialized! testGenericDestroy(ptr, of: SinglePayloadSimpleResolve.self) ptr.deallocate() } testSinglePayloadSimpleResolve() func testArrayDestroy() { let buffer = UnsafeMutableBufferPointer>.allocate(capacity: 20) defer { buffer.deallocate() } buffer.initialize(repeating: GenericStruct(SimpleClass(x: 23))) // CHECK: Before destroy print("Before destroy") // CHECK-NEXT: SimpleClass deinitialized! testGenericArrayDestroy(buffer) } testArrayDestroy() func testArrayInitWithCopy() { let src = UnsafeMutableBufferPointer>.allocate(capacity: 20) let dest = UnsafeMutableBufferPointer>.allocate(capacity: 20) defer { src.deallocate() dest.deallocate() } src.initialize(repeating: GenericStruct(SimpleClass(x: 23))) testGenericArrayInitWithCopy(dest: dest, src: src) // CHECK: Before src deinit print("Before src deinit") src.deinitialize() // CHECK-NEXT: Before dest deinit print("Before dest deinit") // CHECK-NEXT: SimpleClass deinitialized! dest.deinitialize() } testArrayInitWithCopy() func testArrayAssignWithCopy() { let src = UnsafeMutableBufferPointer>.allocate(capacity: 20) let dest = UnsafeMutableBufferPointer>.allocate(capacity: 20) defer { src.deallocate() dest.deallocate() } src.initialize(repeating: GenericStruct(SimpleClass(x: 23))) dest.initialize(repeating: GenericStruct(SimpleClass(x: 32))) // CHECK: Before assign print("Before assign") // CHECK-NEXT: SimpleClass deinitialized! testGenericArrayAssignWithCopy(dest: dest, src: src) // CHECK: Before src deinit print("Before src deinit") src.deinitialize() // CHECK-NEXT: Before dest deinit print("Before dest deinit") // CHECK-NEXT: SimpleClass deinitialized! dest.deinitialize() } testArrayAssignWithCopy() // This is a regression test for rdar://118366415 func testTupleAlignment() { let ptr = allocateInternalGenericPtr(of: TupleLargeAlignment.self) do { let x = TupleLargeAlignment(TestClass()) testGenericInit(ptr, to: x) } do { let y = TupleLargeAlignment(TestClass()) // CHECK: Before deinit print("Before deinit") // CHECK-NEXT: TestClass deinitialized! testGenericAssign(ptr, from: y) } // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: TestClass deinitialized! testGenericDestroy(ptr, of: TupleLargeAlignment.self) ptr.deallocate() } testTupleAlignment() func testWeakRefOptionalNative() { let ptr = allocateInternalGenericPtr(of: TestOptional.self) let ptr2 = allocateInternalGenericPtr(of: TestOptional.self) do { let classInstance = SimpleClass(x: 23) do { let x = TestOptional.nonEmpty(WeakNativeWrapper(x: classInstance)) let y = TestOptional.nonEmpty(WeakNativeWrapper(x: classInstance)) testGenericInit(ptr, to: x) testGenericInit(ptr2, to: y) } testGenericDestroy(ptr, of: TestOptional.self) testGenericDestroy(ptr2, of: TestOptional.self) // CHECK: Before deinit print("Before deinit") // CHECK-NEXT: SimpleClass deinitialized! } ptr.deallocate() } testWeakRefOptionalNative() func testGenericResilientWithUnmanagedAndWeak() { let ptr = allocateInternalGenericPtr(of: GenericResilientWithUnmanagedAndWeak.self) do { let x = GenericResilientWithUnmanagedAndWeak(x: TestClass()) testGenericInit(ptr, to: x) } do { let y = GenericResilientWithUnmanagedAndWeak(x: TestClass()) // CHECK: Before deinit print("Before deinit") // CHECK-NEXT: TestClass deinitialized! testGenericAssign(ptr, from: y) } // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: TestClass deinitialized! testGenericDestroy(ptr, of: GenericResilientWithUnmanagedAndWeak.self) ptr.deallocate() } testGenericResilientWithUnmanagedAndWeak() func testNonCopyableGenericStructSimpleClass() { let ptr = UnsafeMutableBufferPointer>.allocate(capacity: 1) let x = NonCopyableGenericStruct(x: 23, y: SimpleClass(x: 23)) ptr.initializeElement(at: 0, to: x) // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: SimpleClass deinitialized! testGenericArrayDestroy(ptr) ptr.deallocate() } testNonCopyableGenericStructSimpleClass() func testNonCopyableGenericEnumSimpleClass() { let ptr = UnsafeMutableBufferPointer>.allocate(capacity: 1) let x = NonCopyableGenericEnum.x(23, SimpleClass(x: 23)) ptr.initializeElement(at: 0, to: x) // CHECK-NEXT: Before deinit print("Before deinit") // CHECK-NEXT: SimpleClass deinitialized! testGenericArrayDestroy(ptr) ptr.deallocate() } testNonCopyableGenericEnumSimpleClass() #if os(macOS) import Foundation func testGenericObjc() { let ptr = allocateInternalGenericPtr(of: ObjcClass.self) do { let x = ObjcClass(x: 23) testGenericInit(ptr, to: x) } do { let y = ObjcClass(x: 32) // CHECK-macosx: Before deinit print("Before deinit") // CHECK-macosx-NEXT: ObjcClass deinitialized! testGenericAssign(ptr, from: y) } // CHECK-macosx-NEXT: Before deinit print("Before deinit") // CHECK-macosx-NEXT: ObjcClass deinitialized! testGenericDestroy(ptr, of: ObjcClass.self) ptr.deallocate() } testGenericObjc() import Darwin func testWeakRefOptionalObjc() { let ptr = allocateInternalGenericPtr(of: TestOptional.self) let ptr2 = allocateInternalGenericPtr(of: TestOptional.self) do { let classInstance = ObjcClass(x: 23) do { let x = TestOptional.nonEmpty(WeakObjcWrapper(x: classInstance)) let y = TestOptional.nonEmpty(WeakObjcWrapper(x: classInstance)) testGenericInit(ptr, to: x) testGenericInit(ptr2, to: y) } testGenericDestroy(ptr, of: TestOptional.self) testGenericDestroy(ptr2, of: TestOptional.self) // CHECK-macosx: Before deinit print("Before deinit") // CHECK-macosx-NEXT: ObjcClass deinitialized! } ptr.deallocate() } testWeakRefOptionalObjc() #endif