|
| 1 | +// RUN: %target-run-simple-swiftgyb(-Xfrontend -sil-verify-all) | %FileCheck %t/main.swift |
| 2 | +// RUN: %target-run-simple-swiftgyb(-O -Xfrontend -sil-verify-all) | %FileCheck %t/main.swift |
| 3 | + |
| 4 | +// This test uses gyb substitute let and var in as the introducers for the |
| 5 | +// vardecls that are consumed. This guarantees that the same ordering results |
| 6 | +// from checking the consume operator when applied to lets as when applied to |
| 7 | +// lets. |
| 8 | + |
| 9 | +// REQUIRES: executable_test |
| 10 | + |
| 11 | +class X { |
| 12 | + let number: Int |
| 13 | + let function: StaticString |
| 14 | + let line: UInt |
| 15 | + init(_ number: Int, function: StaticString = #function, line: UInt = #line) { |
| 16 | + self.number = number |
| 17 | + self.function = function |
| 18 | + self.line = line |
| 19 | + print("hi - \(function) - \(number) - \(line)") |
| 20 | + } |
| 21 | + deinit { |
| 22 | + print("adios - \(function) - \(number) - \(line)") |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func barrier(_ name: String = "", function: StaticString = #function, line: UInt = #line) { |
| 27 | + print("barrier - \(name) - \(function) - \(line)") |
| 28 | +} |
| 29 | + |
| 30 | +func endcap(name: String, _ args: [Any], function: StaticString = #function, line: UInt = #line) { |
| 31 | + print("\(name) \(function)(", terminator: "") |
| 32 | + for (index, arg) in args.enumerated() { |
| 33 | + print(arg, terminator: "") |
| 34 | + if index != args.count - 1 { |
| 35 | + print(", ", terminator: "") |
| 36 | + } |
| 37 | + } |
| 38 | + print(") - \(line)") |
| 39 | +} |
| 40 | + |
| 41 | +func begin(_ args: [Any], function: StaticString = #function, line: UInt = #line) { |
| 42 | + endcap(name: "begin", args, function: function, line: line) |
| 43 | +} |
| 44 | + |
| 45 | +func end(_ args: [Any], function: StaticString = #function, line: UInt = #line) { |
| 46 | + endcap(name: "end", args, function: function, line: line) |
| 47 | +} |
| 48 | + |
| 49 | +func takeX(_ x: consuming X) {} |
| 50 | + |
| 51 | +% for introducer in ["let", "var"]: |
| 52 | + |
| 53 | +func simple_${introducer}() { |
| 54 | + // CHECK-LABEL: begin simple_{{let|var}} |
| 55 | + begin([]) |
| 56 | + do { |
| 57 | + // CHECK: hi |
| 58 | + ${introducer} x = X(0) |
| 59 | + // CHECK: adios |
| 60 | + takeX(consume x) |
| 61 | + // CHECK: barrier |
| 62 | + barrier() |
| 63 | + } |
| 64 | + // CHECK-LABEL: end simple_{{let|var}} |
| 65 | + end([]) |
| 66 | +} |
| 67 | + |
| 68 | +// CHECK-LABEL: begin testLeft_{{let|var}}(_:)(true) |
| 69 | +// CHECK: hi |
| 70 | +// CHECK: adios |
| 71 | +// CHECK: barrier - left |
| 72 | +// CHECK: barrier - end |
| 73 | +// CHECK-LABEL: end testLeft_{{let|var}}(_:)(true) |
| 74 | +// CHECK-LABEL: begin testLeft_{{let|var}}(_:)(false) |
| 75 | +// CHECK: hi |
| 76 | +// CHECK: barrier - right |
| 77 | +// CHECK: adios |
| 78 | +// CHECK: barrier - end |
| 79 | +// CHECK-LABEL: end testLeft_{{let|var}}(_:)(false) |
| 80 | +func testLeft_${introducer}(_ condition: Bool) { |
| 81 | + begin([condition]) |
| 82 | + do { |
| 83 | + ${introducer} x = X(0) |
| 84 | + if condition { |
| 85 | + takeX(consume x) |
| 86 | + barrier("left") |
| 87 | + } else { |
| 88 | + barrier("right") |
| 89 | + } |
| 90 | + barrier("end") |
| 91 | + } |
| 92 | + end([condition]) |
| 93 | +} |
| 94 | + |
| 95 | +// CHECK-LABEL: begin testNested1_{{let|var}}(_:_:)(false, false) |
| 96 | +// CHECK: hi |
| 97 | +// CHECK: barrier - not c1, not c2 |
| 98 | +// CHECK: barrier - not c1 |
| 99 | +// CHECK: adios |
| 100 | +// CHECK-LABEL: end testNested1_{{let|var}}(_:_:)(false, false) |
| 101 | +// CHECK-LABEL: begin testNested1_{{let|var}}(_:_:)(false, true) |
| 102 | +// CHECK: hi |
| 103 | +// CHECK: barrier - not c1, c2 |
| 104 | +// CHECK: barrier - not c1 |
| 105 | +// CHECK: adios |
| 106 | +// CHECK-LABEL: end testNested1_{{let|var}}(_:_:)(false, true) |
| 107 | +// CHECK-LABEL: begin testNested1_{{let|var}}(_:_:)(true, false) |
| 108 | +// CHECK: hi |
| 109 | +// CHECK: adios |
| 110 | +// CHECK: barrier - c2 |
| 111 | +// CHECK-LABEL: end testNested1_{{let|var}}(_:_:)(true, false) |
| 112 | +// CHECK-LABEL: begin testNested1_{{let|var}}(_:_:)(true, true) |
| 113 | +// CHECK: hi |
| 114 | +// CHECK: adios |
| 115 | +// CHECK: barrier - c2 |
| 116 | +// CHECK-LABEL: end testNested1_{{let|var}}(_:_:)(true, true) |
| 117 | +func testNested1_${introducer}(_ c1: Bool, _ c2: Bool) { |
| 118 | + begin([c1, c2]) |
| 119 | + do { |
| 120 | + ${introducer} x = X(0) |
| 121 | + if c1 { |
| 122 | + takeX(consume x) |
| 123 | + barrier("c2") |
| 124 | + } else { |
| 125 | + if c2 { |
| 126 | + takeX(x) |
| 127 | + barrier("not c1, c2") |
| 128 | + } else { |
| 129 | + barrier("not c1, not c2") |
| 130 | + } |
| 131 | + barrier("not c1") |
| 132 | + } |
| 133 | + } |
| 134 | + end([c1, c2]) |
| 135 | +} |
| 136 | + |
| 137 | +// CHECK-LABEL: begin testNested2_{{let|var}}(_:_:)(false, false) |
| 138 | +// CHECK: hi |
| 139 | +// CHECK: barrier - not c1, not c2 |
| 140 | +// CHECK: adios |
| 141 | +// CHECK: barrier - not c1 |
| 142 | +// CHECK-LABEL: end testNested2_{{let|var}}(_:_:)(false, false) |
| 143 | +// CHECK-LABEL: begin testNested2_{{let|var}}(_:_:)(false, true) |
| 144 | +// CHECK: hi |
| 145 | +// CHECK: adios |
| 146 | +// CHECK: barrier - not c1, c2 |
| 147 | +// CHECK: barrier - not c1 |
| 148 | +// CHECK-LABEL: end testNested2_{{let|var}}(_:_:)(false, true) |
| 149 | +// CHECK-LABEL: begin testNested2_{{let|var}}(_:_:)(true, false) |
| 150 | +// CHECK: hi |
| 151 | +// CHECK: adios |
| 152 | +// CHECK: barrier - c1 |
| 153 | +// CHECK-LABEL: end testNested2_{{let|var}}(_:_:)(true, false) |
| 154 | +// CHECK-LABEL: begin testNested2_{{let|var}}(_:_:)(true, true) |
| 155 | +// CHECK: hi |
| 156 | +// CHECK: adios |
| 157 | +// CHECK: barrier - c1 |
| 158 | +// CHECK-LABEL: end testNested2_{{let|var}}(_:_:)(true, true) |
| 159 | +func testNested2_${introducer}(_ c1: Bool, _ c2: Bool) { |
| 160 | + begin([c1, c2]) |
| 161 | + do { |
| 162 | + ${introducer} x = X(0) |
| 163 | + if c1 { |
| 164 | + takeX(consume x) |
| 165 | + barrier("c1") |
| 166 | + } else { |
| 167 | + if c2 { |
| 168 | + takeX(consume x) |
| 169 | + barrier("not c1, c2") |
| 170 | + } else { |
| 171 | + barrier("not c1, not c2") |
| 172 | + } |
| 173 | + barrier("not c1") |
| 174 | + } |
| 175 | + } |
| 176 | + end([c1, c2]) |
| 177 | +} |
| 178 | + |
| 179 | +func main_${introducer}() { |
| 180 | + simple_${introducer}() |
| 181 | + testLeft_${introducer}(true) |
| 182 | + testLeft_${introducer}(false) |
| 183 | + testNested1_${introducer}(false, false) |
| 184 | + testNested1_${introducer}(false, true) |
| 185 | + testNested1_${introducer}(true, false) |
| 186 | + testNested1_${introducer}(true, true) |
| 187 | + testNested2_${introducer}(false, false) |
| 188 | + testNested2_${introducer}(false, true) |
| 189 | + testNested2_${introducer}(true, false) |
| 190 | + testNested2_${introducer}(true, true) |
| 191 | +} |
| 192 | + |
| 193 | +% end |
| 194 | + |
| 195 | +main_let() |
| 196 | +main_var() |
0 commit comments