|
| 1 | +// RUN: %target-run-simple-swift(-Xfrontend -enable-experimental-feature -Xfrontend InitAccessors) | %FileCheck %s |
| 2 | +// RUN: %target-run-simple-swift(-O -Xfrontend -enable-experimental-feature -Xfrontend InitAccessors) | %FileCheck %s |
| 3 | + |
| 4 | +// REQUIRES: executable_test |
| 5 | +// REQUIRES: asserts |
| 6 | + |
| 7 | +struct TestInit { |
| 8 | + var x: Int |
| 9 | + var y: Int |
| 10 | + var full: (Int, Int) |
| 11 | + |
| 12 | + var point: (Int, Int) { |
| 13 | + init(initialValue) initializes(y, full) accesses(x) { |
| 14 | + self.y = initialValue.1 |
| 15 | + self.full = (self.x, self.y) |
| 16 | + } |
| 17 | + |
| 18 | + get { full } |
| 19 | + set { full = newValue } |
| 20 | + } |
| 21 | + |
| 22 | + init(x: Int, y: Int) { |
| 23 | + self.x = x |
| 24 | + self.point = (x, y) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +do { |
| 29 | + let test = TestInit(x: 0, y: -1) |
| 30 | + print("test-init: \(test.point)") |
| 31 | + // CHECK: test-init: (0, -1) |
| 32 | +} |
| 33 | + |
| 34 | +struct TestSetter { |
| 35 | + var x: Int |
| 36 | + var y: Int |
| 37 | + |
| 38 | + var point: (Int, Int) { |
| 39 | + init(initialValue) accesses(x, y) { |
| 40 | + } |
| 41 | + |
| 42 | + get { (x, y) } |
| 43 | + set { } |
| 44 | + } |
| 45 | + |
| 46 | + init(x: Int, y: Int) { |
| 47 | + self.x = x |
| 48 | + self.y = y |
| 49 | + self.point = (x, y) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +do { |
| 54 | + let test = TestSetter(x: 0, y: -2) |
| 55 | + print("test-setter: \(test.point)") |
| 56 | + // CHECK: test-setter: (0, -2) |
| 57 | +} |
| 58 | + |
| 59 | +struct TestInitThenSetter { |
| 60 | + var x: Int |
| 61 | + var y: Int |
| 62 | + |
| 63 | + var point: (Int, Int) { |
| 64 | + init(initialValue) initializes(x, y) { |
| 65 | + self.x = initialValue.0 |
| 66 | + self.y = initialValue.1 |
| 67 | + } |
| 68 | + |
| 69 | + get { (x, y) } |
| 70 | + |
| 71 | + set { |
| 72 | + x = newValue.0 |
| 73 | + y = newValue.1 |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + init(x: Int, y: Int) { |
| 78 | + self.point = (x, y) |
| 79 | + |
| 80 | + if x == 1 { |
| 81 | + self.point = (0, 0) |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +do { |
| 87 | + let test = TestInitThenSetter(x: 1, y: 2) |
| 88 | + print("test-init-then-setter: \(test.point)") |
| 89 | + // CHECK: test-init-then-setter: (0, 0) |
| 90 | +} |
| 91 | + |
| 92 | +struct TestPartialInt { |
| 93 | + var x: Int |
| 94 | + var y: Int |
| 95 | + |
| 96 | + var pointX: Int { |
| 97 | + init(newValue) initializes(x) { |
| 98 | + self.x = newValue |
| 99 | + } |
| 100 | + |
| 101 | + get { x } |
| 102 | + set { self.x = newValue } |
| 103 | + } |
| 104 | + |
| 105 | + var pointY: Int { |
| 106 | + init(newValue) initializes(y) { |
| 107 | + self.y = newValue |
| 108 | + } |
| 109 | + |
| 110 | + get { y } |
| 111 | + set { self.y = newValue } |
| 112 | + } |
| 113 | + |
| 114 | + init(x: Int, y: Int) { |
| 115 | + // Init |
| 116 | + self.pointX = x |
| 117 | + // Init |
| 118 | + self.pointY = y |
| 119 | + |
| 120 | + // Setter |
| 121 | + self.pointX = 1 |
| 122 | + // Setter |
| 123 | + self.pointY = 2 |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +do { |
| 128 | + let test = TestPartialInt(x: 0, y: -1) |
| 129 | + print("test-partial-init: (\(test.pointX), \(test.pointY))") |
| 130 | + // CHECK: test-partial-init: (1, 2) |
| 131 | +} |
| 132 | + |
| 133 | +struct TestNoInitAndInit { |
| 134 | + var x: Int |
| 135 | + var y: Int |
| 136 | + |
| 137 | + var pointX: Int { |
| 138 | + init(initalValue) accesses(x) { |
| 139 | + } |
| 140 | + |
| 141 | + get { x } |
| 142 | + set { } |
| 143 | + } |
| 144 | + |
| 145 | + var pointY: Int { |
| 146 | + init(initialValue) initializes(y) { |
| 147 | + self.y = initialValue |
| 148 | + } |
| 149 | + |
| 150 | + get { y } |
| 151 | + set { } |
| 152 | + } |
| 153 | + |
| 154 | + init(x: Int, y: Int) { |
| 155 | + self.x = x |
| 156 | + self.pointX = x |
| 157 | + self.pointY = y |
| 158 | + print("TestNoInitAndInit(x: \(self.x), y: \(self.y))") |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +do { |
| 163 | + _ = TestNoInitAndInit(x: 10, y: -10) |
| 164 | + // CHECK: TestNoInitAndInit(x: 10, y: -10) |
| 165 | +} |
| 166 | + |
| 167 | +class TestClass { |
| 168 | + var x: Int |
| 169 | + var y: (Int, [String]) |
| 170 | + |
| 171 | + var data: (Int, (Int, [String])) { |
| 172 | + init(initialValue) initializes(x, y) { |
| 173 | + x = initialValue.0 |
| 174 | + y = initialValue.1 |
| 175 | + } |
| 176 | + |
| 177 | + get { (x, y) } |
| 178 | + set { |
| 179 | + x = newValue.0 |
| 180 | + y = newValue.1 |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + init(x: Int, y: (Int, [String])) { |
| 185 | + self.data = (x, y) |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +do { |
| 190 | + let test = TestClass(x: 20, y: (0, ["a", "b"])) |
| 191 | + print("test-class: \(test.data)") |
| 192 | + // CHECK: test-class: (20, (0, ["a", "b"])) |
| 193 | +} |
| 194 | + |
| 195 | +struct TestGeneric<T, U> { |
| 196 | + var a: T |
| 197 | + var b: T |
| 198 | + var c: U |
| 199 | + |
| 200 | + var data: (T, T) { |
| 201 | + init(initialValue) initializes(a, b) accesses(c) { |
| 202 | + a = initialValue.0 |
| 203 | + b = initialValue.1 |
| 204 | + print("TestGeneric(c: \(c))") |
| 205 | + } |
| 206 | + |
| 207 | + get { (a, b) } |
| 208 | + set { } |
| 209 | + } |
| 210 | + |
| 211 | + init(a: T, b: T, c: U) { |
| 212 | + self.c = c |
| 213 | + self.data = (a, b) |
| 214 | + self.data = (b, a) |
| 215 | + } |
| 216 | +} |
| 217 | + |
| 218 | +do { |
| 219 | + let test = TestGeneric(a: 42, b: 0, c: [42, "a"] as [Any]) |
| 220 | + print("test-generic: data = \(test.data)") |
| 221 | + // CHECK: TestGeneric(c: [42, "a"]) |
| 222 | + // CHECK-NEXT: test-generic: data = (42, 0) |
| 223 | +} |
| 224 | + |
| 225 | +func test_local_with_memberwise() { |
| 226 | + class MyValue {} |
| 227 | + |
| 228 | + struct TestMemberwiseConcrete { |
| 229 | + var a: Int |
| 230 | + var b: String |
| 231 | + |
| 232 | + var pair: (Int, String) { |
| 233 | + init(initialValue) initializes(a, b) { |
| 234 | + a = initialValue.0 |
| 235 | + b = initialValue.1 |
| 236 | + } |
| 237 | + |
| 238 | + get { (a, b) } |
| 239 | + set { } |
| 240 | + } |
| 241 | + |
| 242 | + var c: [MyValue] |
| 243 | + } |
| 244 | + |
| 245 | + let concrete = TestMemberwiseConcrete(pair: (0, "a"), c: []) |
| 246 | + print(concrete) |
| 247 | + |
| 248 | + struct TestMemberwiseGeneric<T, C> where C: RangeReplaceableCollection, C.Element == T { |
| 249 | + var _a: T |
| 250 | + var _b: String |
| 251 | + var _c: C |
| 252 | + |
| 253 | + var a: T { |
| 254 | + init(initialValue) initializes(_a) { |
| 255 | + _a = initialValue |
| 256 | + } |
| 257 | + |
| 258 | + get { _a } |
| 259 | + set { } |
| 260 | + } |
| 261 | + |
| 262 | + var pair: (String, C) { |
| 263 | + init(initialValue) initializes(_b, _c) accesses(_a) { |
| 264 | + _b = initialValue.0 |
| 265 | + _c = initialValue.1 |
| 266 | + _c.append(_a) |
| 267 | + } |
| 268 | + |
| 269 | + get { (_b, _c) } |
| 270 | + set { } |
| 271 | + } |
| 272 | + } |
| 273 | + |
| 274 | + let generic = TestMemberwiseGeneric(a: 1, pair: ("a", [0])) |
| 275 | + print(generic) |
| 276 | +} |
| 277 | + |
| 278 | +test_local_with_memberwise() |
| 279 | +// CHECK: TestMemberwiseConcrete(a: 0, b: "a", c: []) |
| 280 | +// CHECK-NEXT: TestMemberwiseGeneric<Int, Array<Int>>(_a: 1, _b: "a", _c: [0, 1]) |
| 281 | + |
| 282 | +func test_assignments() { |
| 283 | + struct Test { |
| 284 | + var _a: Int |
| 285 | + var _b: Int |
| 286 | + |
| 287 | + var a: Int { |
| 288 | + init(initialValue) initializes(_a) { |
| 289 | + self._a = initialValue |
| 290 | + print("a-init-accessor: \(self._a)") |
| 291 | + } |
| 292 | + get { _a } |
| 293 | + set { _a = newValue + 1 } |
| 294 | + } |
| 295 | + |
| 296 | + var pair: (Int, Int) { |
| 297 | + init(initialValue) initializes(_a, _b) { |
| 298 | + _a = initialValue.0 |
| 299 | + _b = initialValue.1 |
| 300 | + } |
| 301 | + |
| 302 | + get { (_a, _b) } |
| 303 | + set { } |
| 304 | + } |
| 305 | + |
| 306 | + init(a: Int) { |
| 307 | + // init |
| 308 | + self.a = a |
| 309 | + // re-assignment |
| 310 | + self.a = a + 1 |
| 311 | + self._b = 42 |
| 312 | + // set |
| 313 | + self.a = a + 2 |
| 314 | + } |
| 315 | + |
| 316 | + init(a: Int, b: Int) { |
| 317 | + self.a = a |
| 318 | + self.pair = (0, b) |
| 319 | + } |
| 320 | + } |
| 321 | + |
| 322 | + let test1 = Test(a: 0) |
| 323 | + print("test-assignments-1: \(test1.pair)") |
| 324 | + |
| 325 | + let test2 = Test(a: 0, b: 2) |
| 326 | + print("test-assignments-2: \(test2.pair)") |
| 327 | +} |
| 328 | + |
| 329 | +test_assignments() |
| 330 | +// CHECK: a-init-accessor: 0 |
| 331 | +// CHECK-NEXT: a-init-accessor: 1 |
| 332 | +// CHECK-NEXT: test-assignments-1: (3, 42) |
| 333 | +// CHECK-NEXT: a-init-accessor: 0 |
| 334 | +// CHECK-NEXT: test-assignments-2: (0, 2) |
0 commit comments