forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.swift
322 lines (279 loc) · 6.58 KB
/
errors.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// RUN: %target-run-simple-swift
// REQUIRES: executable_test
//
// Tests for error handling.
//
import StdlibUnittest
enum Excuse : Error { case CatAteHomework(LifetimeTracked) }
var ErrorHandlingTests = TestSuite("ErrorHandling")
func furball(_ b: Bool) throws -> LifetimeTracked {
if b {
throw Excuse.CatAteHomework(LifetimeTracked(0))
} else {
return LifetimeTracked(1)
}
}
ErrorHandlingTests.test("tryCatch") {
do {
try expectEqual(furball(false), LifetimeTracked(1))
} catch {
expectUnreachable()
}
do {
try furball(true)
expectUnreachable()
} catch let e {
if case Excuse.CatAteHomework(let c) = e {
expectEqual(c, LifetimeTracked(0))
} else {
expectUnreachable()
}
}
}
ErrorHandlingTests.test("tryOptional") {
expectEqual(LifetimeTracked(1), try? furball(false))
expectEqual(Optional<LifetimeTracked>.none, try? furball(true))
}
ErrorHandlingTests.test("fallthroughInCatch") {
switch 1 {
case 1:
do {
try furball(true)
expectUnreachable()
} catch Excuse.CatAteHomework(_) {
fallthrough
} catch {
expectUnreachable()
}
case 0:
return
default:
expectUnreachable()
}
expectUnreachable()
}
ErrorHandlingTests.test("breakInCatch") {
switch 1 {
case 1:
do {
try furball(true)
} catch {
break
}
expectUnreachable() // break out of the case, not the catch
default:
break
}
}
enum Foo: Error {
case a(LifetimeTracked)
case b(LifetimeTracked)
}
func baz(_ x: Foo, _ condition: (LifetimeTracked) -> Bool) -> Bool {
do {
throw x
} catch Foo.a(let obj) where condition(obj),
Foo.b(let obj) where condition(obj) {
return true
} catch {}
return false
}
ErrorHandlingTests.test("multiPatternWherePaths") {
_ = baz(.a(LifetimeTracked(1)), { _ in true })
_ = baz(.b(LifetimeTracked(2)), { _ in true })
_ = baz(.a(LifetimeTracked(3)), { _ in false })
_ = baz(.b(LifetimeTracked(4)), { _ in false })
}
public enum Phase<Value>: Error {
case possible
case active(Value)
case paused(Value)
case ended(Value)
case failed
}
extension Phase {
public var valueLet: Value? {
do {
throw self
}
catch Phase.possible, Phase.failed {
return nil
}
catch let Phase.active(value), let Phase.paused(value), let Phase.ended(value) {
return value
} catch { expectUnreachable() }
expectUnreachable()
return nil
}
public var valueVar: Value? {
do {
throw self
}
catch Phase.possible, Phase.failed {
return nil
}
catch var Phase.active(value), var Phase.paused(value), var Phase.ended(value) {
return value
} catch { expectUnreachable() }
expectUnreachable()
return nil
}
}
enum K {
case A, B
}
enum A<K>: Error {
case left(a: K, b: K)
case right(a: K, b: K)
var valueLet: [K] {
do {
throw self
}
catch let A.left(a, b), let A.right(a, b) {
return [a, b]
} catch { expectUnreachable() }
expectUnreachable()
return []
}
var valueVar: [K] {
do {
throw self
}
catch var A.left(a, b), var A.right(a, b) {
return [a, b]
} catch { expectUnreachable() }
expectUnreachable()
return []
}
}
ErrorHandlingTests.test("GenericLet") {
do {
expectEqual(1.0, Phase.active(1.0).valueLet)
expectEqual(2.0, Phase.paused(2.0).valueLet)
expectEqual(3.0, Phase.ended(3.0).valueLet)
}
do {
let l = LifetimeTracked(0)
expectTrue(l === Phase.active(l).valueLet)
expectTrue(l === Phase.paused(l).valueLet)
expectTrue(l === Phase.ended(l).valueLet)
}
do {
expectEqual([K.A, K.B], A.left(a: K.A, b: K.B).valueLet)
expectEqual([K.A, K.B], A.right(a: K.A, b: K.B).valueLet)
}
do {
let l = LifetimeTracked(0)
let r = LifetimeTracked(0)
let arr = A.left(a: l, b: r).valueLet
expectTrue(arr[0] === l)
expectTrue(arr[1] === r)
}
do {
let l = LifetimeTracked(0)
let r = LifetimeTracked(0)
let arr = A.right(a: l, b: r).valueLet
expectTrue(arr[0] === l)
expectTrue(arr[1] === r)
}
}
ErrorHandlingTests.test("GenericVar") {
do {
expectEqual(1.0, Phase.active(1.0).valueVar)
expectEqual(2.0, Phase.paused(2.0).valueVar)
expectEqual(3.0, Phase.ended(3.0).valueVar)
}
do {
let l = LifetimeTracked(0)
expectTrue(l === Phase.active(l).valueVar)
expectTrue(l === Phase.paused(l).valueVar)
expectTrue(l === Phase.ended(l).valueVar)
}
do {
expectEqual([K.A, K.B], A.left(a: K.A, b: K.B).valueVar)
expectEqual([K.A, K.B], A.right(a: K.A, b: K.B).valueVar)
}
do {
let l = LifetimeTracked(0)
let r = LifetimeTracked(0)
let arr = A.left(a: l, b: r).valueVar
expectTrue(arr[0] === l)
expectTrue(arr[1] === r)
}
do {
let l = LifetimeTracked(0)
let r = LifetimeTracked(0)
let arr = A.right(a: l, b: r).valueVar
expectTrue(arr[0] === l)
expectTrue(arr[1] === r)
}
}
enum Gesture: Error {
case pan(Any)
case pinch(Any)
}
extension Gesture {
var valueLet: Any {
do {
throw self
}
catch Gesture.pan(let data),
Gesture.pinch(let data) {
return data
} catch { expectUnreachable() }
expectUnreachable()
return 42
}
var valueVar: Any {
do { throw self }
catch Gesture.pan(var data),
Gesture.pinch(var data) {
return data
} catch { expectUnreachable() }
expectUnreachable()
return 42
}
}
ErrorHandlingTests.test("GenericLet") {
expectEqual(1, Gesture.pan(1).valueLet as! Int)
expectEqual(2, Gesture.pinch(2).valueLet as! Int)
let l = LifetimeTracked(0)
expectTrue(l === Gesture.pan(l).valueLet as! LifetimeTracked)
expectTrue(l === Gesture.pinch(l).valueLet as! LifetimeTracked)
}
ErrorHandlingTests.test("GenericVar") {
expectEqual(1, Gesture.pan(1).valueVar as! Int)
expectEqual(2, Gesture.pinch(2).valueVar as! Int)
let l = LifetimeTracked(0)
expectTrue(l === Gesture.pan(l).valueVar as! LifetimeTracked)
expectTrue(l === Gesture.pinch(l).valueVar as! LifetimeTracked)
}
ErrorHandlingTests.test("Enum Initialization Leaks") {
enum Enum1 {
case case1(LifetimeTracked)
case case2(LifetimeTracked, Int)
}
enum Enum2: Error {
case case1(LifetimeTracked)
case case2(Enum1, LifetimeTracked)
}
struct Struct {
var value: Enum2 = .case2(.case1(LifetimeTracked(0)), LifetimeTracked(1))
func doSomethingCatch() {
do {
throw value
}
catch let Enum2.case2(.case2(k, _), _) {
return
} catch {
return
}
return
}
}
do {
let s = Struct()
s.doSomethingCatch()
}
}
runAllTests()