|
| 1 | +// RUN: %empty-directory(%t) |
| 2 | +// RUN: %target-build-swift -o %t/a.out.fragile %s |
| 3 | +// RUN: %target-run %t/a.out.fragile | %FileCheck %s |
| 4 | + |
| 5 | +// FIXME: miscompiles cause extra deinits with library evolution enabled |
| 6 | + |
| 7 | +// R/UN: %target-build-swift -enable-library-evolution -o %t/a.out.resilient %s |
| 8 | +// R/UN: %target-run %t/a.out.resilient | %FileCheck %s |
| 9 | + |
| 10 | +// REQUIRES: executable_test |
| 11 | + |
| 12 | +// CHECK: starting |
| 13 | + |
| 14 | +struct MyError: Error {} |
| 15 | + |
| 16 | +public struct Resilient: ~Copyable { |
| 17 | + static var nextValue: Int = 0 |
| 18 | + |
| 19 | + private(set) public var value: Int |
| 20 | + public init(nonthrowing: ()) { |
| 21 | + value = Self.nextValue |
| 22 | + Self.nextValue += 1 |
| 23 | + } |
| 24 | + deinit { print("resilient deinit \(value)") } |
| 25 | + |
| 26 | + public init(throwing: Bool) throws { |
| 27 | + if throwing { |
| 28 | + throw MyError() |
| 29 | + } |
| 30 | + self = .init(nonthrowing: ()) |
| 31 | + } |
| 32 | + public init(throwingAfterInit: Bool) throws { |
| 33 | + self = .init(nonthrowing: ()) |
| 34 | + if throwingAfterInit { |
| 35 | + throw MyError() |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public static func instanceCount() -> Int { |
| 40 | + return nextValue |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func makeItem1() throws -> Resilient { |
| 45 | + return Resilient(nonthrowing: ()) |
| 46 | +} |
| 47 | + |
| 48 | +func test1a() throws { |
| 49 | + // CHECK-NEXT: resilient deinit 0 |
| 50 | + _ = try makeItem1() |
| 51 | +} |
| 52 | +func test1b() throws { |
| 53 | + // CHECK-NEXT: resilient deinit 1 |
| 54 | + let x = try makeItem1() |
| 55 | +} |
| 56 | + |
| 57 | +func makeItem2(throwing: Bool) throws -> Resilient { |
| 58 | + return try Resilient(throwing: throwing) |
| 59 | +} |
| 60 | + |
| 61 | +func test2aa() throws { |
| 62 | + // CHECK-NEXT: resilient deinit 2 |
| 63 | + _ = try makeItem2(throwing: false) |
| 64 | +} |
| 65 | + |
| 66 | +func test2ab() throws { |
| 67 | + _ = try makeItem2(throwing: true) |
| 68 | +} |
| 69 | + |
| 70 | +func test2ba() throws { |
| 71 | + // CHECK-NEXT: resilient deinit 3 |
| 72 | + let x = try makeItem2(throwing: false) |
| 73 | +} |
| 74 | + |
| 75 | +func test2bb() throws { |
| 76 | + let x = try makeItem2(throwing: true) |
| 77 | +} |
| 78 | + |
| 79 | +func makeItem3(throwing: Bool) throws -> Resilient { |
| 80 | + return try Resilient(throwingAfterInit: throwing) |
| 81 | +} |
| 82 | + |
| 83 | +func test3aa() throws { |
| 84 | + // CHECK-NEXT: resilient deinit 4 |
| 85 | + _ = try makeItem3(throwing: false) |
| 86 | +} |
| 87 | + |
| 88 | +func test3ab() throws { |
| 89 | + // CHECK-NEXT: resilient deinit 5 |
| 90 | + _ = try makeItem3(throwing: true) |
| 91 | +} |
| 92 | + |
| 93 | +func test3ba() throws { |
| 94 | + // CHECK-NEXT: resilient deinit 6 |
| 95 | + let x = try makeItem3(throwing: false) |
| 96 | +} |
| 97 | + |
| 98 | +func test3bb() throws { |
| 99 | + // CHECK-NEXT: resilient deinit 7 |
| 100 | + let x = try makeItem3(throwing: true) |
| 101 | +} |
| 102 | + |
| 103 | +func main() { |
| 104 | + print("starting") |
| 105 | + _ = try? test1a() |
| 106 | + _ = try? test1b() |
| 107 | + _ = try? test2aa() |
| 108 | + _ = try? test2ab() |
| 109 | + _ = try? test2ba() |
| 110 | + _ = try? test2bb() |
| 111 | + _ = try? test3aa() |
| 112 | + _ = try? test3ab() |
| 113 | + _ = try? test3ba() |
| 114 | + _ = try? test3bb() |
| 115 | + |
| 116 | + // CHECK-NEXT: 8 instances in total |
| 117 | + print("\(Resilient.instanceCount()) instances in total") |
| 118 | +} |
| 119 | +main() |
0 commit comments