forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrethrows.swift
344 lines (274 loc) · 19.3 KB
/
rethrows.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// RUN: %target-parse-verify-swift
/** Basics *******************************************************************/
// Function types can't be rethrows right now.
let r1 = {() rethrows -> Int in 0} // expected-error {{only function declarations may be marked 'rethrows'}}
let r2 : () rethrows -> Int = { 0 } // expected-error {{only function declarations may be marked 'rethrows'}}
let r3 : Optional<() rethrows -> ()> = nil // expected-error {{only function declarations may be marked 'rethrows'}}
func f1(f: () throws -> ()) rethrows { try f() }
func f2(f: () -> ()) rethrows { f() } // expected-error {{'rethrows' function must take a throwing function argument}}
func f3(f: UndeclaredFunctionType) rethrows { f() } // expected-error {{use of undeclared type 'UndeclaredFunctionType'}}
func cf1(f: () throws -> ())() rethrows { try f() } // expected-warning{{curried function declaration syntax will be removed in a future version of Swift}}
func cf2(f: () -> ())() rethrows { f() } // expected-error {{'rethrows' function must take a throwing function argument}} expected-warning{{curried function declaration syntax will be removed in a future version of Swift}}
func cf3(f: UndeclaredFunctionType)() rethrows { f() } // expected-error {{use of undeclared type 'UndeclaredFunctionType'}} expected-warning{{curried function declaration syntax will be removed in a future version of Swift}}
func cf4(f: () ->())(g: () throws -> ()) rethrows {} // expected-warning{{curried function declaration syntax will be removed in a future version of Swift}}
func cf5() rethrows -> () throws -> () {} // expected-error {{'rethrows' function must take a throwing function argument}}
/** Protocol conformance checking ********************************************/
protocol P {
func tf() throws
func nf() // expected-note {{protocol requires}}
func thf(f: () throws -> ()) throws
func nhf(f: () throws -> ()) // expected-note 2 {{protocol requires}}
func rhf(f: () throws -> ()) rethrows // expected-note {{protocol requires}}
}
struct T0 : P { // expected-error {{type 'T0' does not conform to protocol 'P'}}
func tf() throws {}
func nf() throws {} // expected-note {{candidate throws, but protocol does not allow it}}
func thf(f: () throws -> ()) throws {}
func nhf(f: () throws -> ()) throws {} // expected-note {{candidate throws, but protocol does not allow it}}
func rhf(f: () throws -> ()) throws {} // expected-note {{candidate is not 'rethrows', but protocol requires it}}
}
struct T1 : P {
func tf() {}
func nf() {}
func thf(f: () throws -> ()) {}
func nhf(f: () throws -> ()) {}
func rhf(f: () throws -> ()) {}
}
struct T2 : P { // expected-error {{type 'T2' does not conform to protocol 'P'}}
func tf() {}
func nf() {}
func thf(f: () throws -> ()) rethrows {}
func nhf(f: () throws -> ()) rethrows {} // expected-note {{candidate throws, but protocol does not allow it}}
func rhf(f: () throws -> ()) rethrows {}
}
/** Classes ******************************************************************/
class Super {
func tf() throws {}
func nf() {} // expected-note {{overridden declaration is here}}
func thf(f: () throws -> ()) throws {}
func nhf(f: () throws -> ()) {} // expected-note 2 {{overridden declaration is here}}
func rhf(f: () throws -> ()) rethrows {} // expected-note {{overridden declaration is here}}
}
class C1 : Super {
override func tf() {}
override func nf() {}
override func thf(f: () throws -> ()) {}
override func nhf(f: () throws -> ()) {}
override func rhf(f: () throws -> ()) {}
}
class C2 : Super {
override func tf() throws {}
override func nf() throws {} // expected-error {{cannot override non-throwing method with throwing method}}
override func thf(f: () throws -> ()) throws {}
override func nhf(f: () throws -> ()) throws {} // expected-error {{cannot override non-throwing method with throwing method}}
override func rhf(f: () throws -> ()) throws {} // expected-error {{override of 'rethrows' method should also be 'rethrows'}}
}
class C3 : Super {
override func tf() {}
override func nf() {}
override func thf(f: () throws -> ()) rethrows {}
override func nhf(f: () throws -> ()) rethrows {} // expected-error {{cannot override non-throwing method with throwing method}}
override func rhf(f: () throws -> ()) rethrows {}
}
/** Semantics ****************************************************************/
func call(fn: () throws -> Int) rethrows -> Int { return try fn() }
func callAC(@autoclosure fn: () throws -> Int) rethrows -> Int { return try fn() }
func raise() throws -> Int { return 0 }
func noraise() -> Int { return 0 }
/** Global functions **/
func testCallUnhandled() {
call(noraise)
try call(noraise) // expected-warning {{no calls to throwing functions occur within 'try'}}
call(raise) // expected-error {{call can throw, but it is not marked with 'try' and the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
try call(raise) // expected-error {{call can throw, but the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
}
func testCallHandled() throws {
call(noraise)
try call(noraise) // expected-warning {{no calls to throwing functions occur within 'try'}}
call(raise) // expected-error {{call can throw but is not marked with 'try'}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
try call(raise)
}
func testCallACUnhandled() {
callAC(noraise())
try callAC(noraise()) // expected-warning {{no calls to throwing functions occur within 'try'}}
callAC(raise()) // expected-error {{call can throw but is not marked with 'try'}} \
// expected-error {{call can throw, but it is not marked with 'try' and the error is not handled}} \
// expected-note {{call is to 'rethrows' function, but argument function can throw}}
try callAC(raise()) // expected-error {{call can throw, but the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
}
func testCallACHandled() throws {
callAC(noraise())
try callAC(noraise()) // expected-warning {{no calls to throwing functions occur within 'try'}}
callAC(raise()) // expected-error 2 {{call can throw but is not marked with 'try'}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
try callAC(raise())
}
func testForward1(fn: () throws -> Int) rethrows {
try call(fn)
}
func testForward2(fn: () throws -> Int) rethrows {
try call({ try fn() })
}
/** Methods **/
struct MyStruct : MyProto {
func call(fn: () throws -> Int) rethrows -> Int { return try fn() }
func callAC(@autoclosure fn: () throws -> Int) rethrows -> Int { return try fn() }
static func static_call(fn: () throws -> Int) rethrows -> Int { return try fn() }
static func static_callAC(@autoclosure fn: () throws -> Int) rethrows -> Int { return try fn() }
}
func testMethodCallUnhandled(s: MyStruct) {
s.call(noraise)
try s.call(noraise) // expected-warning {{no calls to throwing functions occur within 'try'}}
s.call(raise) // expected-error {{call can throw, but it is not marked with 'try' and the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
try s.call(raise) // expected-error {{call can throw, but the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
MyStruct.static_call(noraise)
try MyStruct.static_call(noraise) // expected-warning {{no calls to throwing functions occur within 'try'}}
MyStruct.static_call(raise) // expected-error {{call can throw, but it is not marked with 'try' and the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
try MyStruct.static_call(raise) // expected-error {{call can throw, but the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
}
func testMethodCallHandled(s: MyStruct) throws {
s.call(noraise)
try s.call(noraise) // expected-warning {{no calls to throwing functions occur within 'try'}}
s.call(raise) // expected-error {{call can throw but is not marked with 'try'}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
try s.call(raise)
MyStruct.static_call(noraise)
try MyStruct.static_call(noraise) // expected-warning {{no calls to throwing functions occur within 'try'}}
MyStruct.static_call(raise) // expected-error {{call can throw but is not marked with 'try'}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
try MyStruct.static_call(raise)
}
func testMethodCallACUnhandled(s: MyStruct) {
s.callAC(noraise())
try s.callAC(noraise()) // expected-warning {{no calls to throwing functions occur within 'try'}}
s.callAC(raise()) // expected-error {{call can throw but is not marked with 'try'}} \
// expected-error {{call can throw, but it is not marked with 'try' and the error is not handled}} \
// expected-note {{call is to 'rethrows' function, but argument function can throw}}
try s.callAC(raise()) // expected-error {{call can throw, but the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
MyStruct.static_callAC(noraise())
try MyStruct.static_callAC(noraise()) // expected-warning {{no calls to throwing functions occur within 'try'}}
MyStruct.static_callAC(raise()) // expected-error {{call can throw but is not marked with 'try'}} \
// expected-error {{call can throw, but it is not marked with 'try' and the error is not handled}} \
// expected-note {{call is to 'rethrows' function, but argument function can throw}}
try MyStruct.static_callAC(raise()) // expected-error {{call can throw, but the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
}
func testMethodCallACHandled(s: MyStruct) throws {
s.callAC(noraise())
try s.callAC(noraise()) // expected-warning {{no calls to throwing functions occur within 'try'}}
s.callAC(raise()) // expected-error 2 {{call can throw but is not marked with 'try'}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
try s.callAC(raise())
MyStruct.static_callAC(noraise())
try MyStruct.static_callAC(noraise()) // expected-warning {{no calls to throwing functions occur within 'try'}}
MyStruct.static_callAC(raise()) // expected-error 2 {{call can throw but is not marked with 'try'}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
try MyStruct.static_callAC(raise())
}
/** Protocol methods **/
protocol MyProto {
func call(fn: () throws -> Int) rethrows -> Int
func callAC(@autoclosure fn: () throws -> Int) rethrows -> Int
static func static_call(fn: () throws -> Int) rethrows -> Int
static func static_callAC(@autoclosure fn: () throws -> Int) rethrows -> Int
}
/** Existentials **/
func testProtoMethodCallUnhandled(s: MyProto) {
s.call(noraise)
try s.call(noraise) // expected-warning {{no calls to throwing functions occur within 'try'}}
s.call(raise) // expected-error {{call can throw, but it is not marked with 'try' and the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
try s.call(raise) // expected-error {{call can throw, but the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
s.dynamicType.static_call(noraise)
try s.dynamicType.static_call(noraise) // expected-warning {{no calls to throwing functions occur within 'try'}}
s.dynamicType.static_call(raise) // expected-error {{call can throw, but it is not marked with 'try' and the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
try s.dynamicType.static_call(raise) // expected-error {{call can throw, but the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
}
func testProtoMethodCallHandled(s: MyProto) throws {
s.call(noraise)
try s.call(noraise) // expected-warning {{no calls to throwing functions occur within 'try'}}
s.call(raise) // expected-error {{call can throw but is not marked with 'try'}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
try s.call(raise)
s.dynamicType.static_call(noraise)
try s.dynamicType.static_call(noraise) // expected-warning {{no calls to throwing functions occur within 'try'}}
s.dynamicType.static_call(raise) // expected-error {{call can throw but is not marked with 'try'}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
try s.dynamicType.static_call(raise)
}
func testProtoMethodCallACUnhandled(s: MyProto) {
s.callAC(noraise())
try s.callAC(noraise()) // expected-warning {{no calls to throwing functions occur within 'try'}}
s.callAC(raise()) // expected-error {{call can throw but is not marked with 'try'}} \
// expected-error {{call can throw, but it is not marked with 'try' and the error is not handled}} \
// expected-note {{call is to 'rethrows' function, but argument function can throw}}
try s.callAC(raise()) // expected-error {{call can throw, but the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
s.dynamicType.static_callAC(noraise())
try s.dynamicType.static_callAC(noraise()) // expected-warning {{no calls to throwing functions occur within 'try'}}
s.dynamicType.static_callAC(raise()) // expected-error {{call can throw but is not marked with 'try'}} \
// expected-error {{call can throw, but it is not marked with 'try' and the error is not handled}} \
// expected-note {{call is to 'rethrows' function, but argument function can throw}}
try s.dynamicType.static_callAC(raise()) // expected-error {{call can throw, but the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
}
func testProtoMethodCallACHandled(s: MyProto) throws {
s.callAC(noraise())
try s.callAC(noraise()) // expected-warning {{no calls to throwing functions occur within 'try'}}
s.callAC(raise()) // expected-error 2 {{call can throw but is not marked with 'try'}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
try s.callAC(raise())
s.dynamicType.static_callAC(noraise())
try s.dynamicType.static_callAC(noraise()) // expected-warning {{no calls to throwing functions occur within 'try'}}
s.dynamicType.static_callAC(raise()) // expected-error 2 {{call can throw but is not marked with 'try'}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
try s.dynamicType.static_callAC(raise())
}
/** Generics **/
func testGenericMethodCallUnhandled<P: MyProto>(s: P) {
s.call(noraise)
try s.call(noraise) // expected-warning {{no calls to throwing functions occur within 'try'}}
s.call(raise) // expected-error {{call can throw, but it is not marked with 'try' and the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
try s.call(raise) // expected-error {{call can throw, but the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
P.static_call(noraise)
try P.static_call(noraise) // expected-warning {{no calls to throwing functions occur within 'try'}}
P.static_call(raise) // expected-error {{call can throw, but it is not marked with 'try' and the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
try P.static_call(raise) // expected-error {{call can throw, but the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
}
func testGenericMethodCallHandled<P: MyProto>(s: P) throws {
s.call(noraise)
try s.call(noraise) // expected-warning {{no calls to throwing functions occur within 'try'}}
s.call(raise) // expected-error {{call can throw but is not marked with 'try'}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
try s.call(raise)
P.static_call(noraise)
try P.static_call(noraise) // expected-warning {{no calls to throwing functions occur within 'try'}}
P.static_call(raise) // expected-error {{call can throw but is not marked with 'try'}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
try P.static_call(raise)
}
func testGenericMethodCallACUnhandled<P: MyProto>(s: P) {
s.callAC(noraise())
try s.callAC(noraise()) // expected-warning {{no calls to throwing functions occur within 'try'}}
s.callAC(raise()) // expected-error {{call can throw but is not marked with 'try'}} \
// expected-error {{call can throw, but it is not marked with 'try' and the error is not handled}} \
// expected-note {{call is to 'rethrows' function, but argument function can throw}}
try s.callAC(raise()) // expected-error {{call can throw, but the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
P.static_callAC(noraise())
try P.static_callAC(noraise()) // expected-warning {{no calls to throwing functions occur within 'try'}}
P.static_callAC(raise()) // expected-error {{call can throw but is not marked with 'try'}} \
// expected-error {{call can throw, but it is not marked with 'try' and the error is not handled}} \
// expected-note {{call is to 'rethrows' function, but argument function can throw}}
try P.static_callAC(raise()) // expected-error {{call can throw, but the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
}
func testGenericMethodCallACHandled<P: MyProto>(s: P) throws {
s.callAC(noraise())
try s.callAC(noraise()) // expected-warning {{no calls to throwing functions occur within 'try'}}
s.callAC(raise()) // expected-error 2 {{call can throw but is not marked with 'try'}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
try s.callAC(raise())
P.static_callAC(noraise())
try P.static_callAC(noraise()) // expected-warning {{no calls to throwing functions occur within 'try'}}
P.static_callAC(raise()) // expected-error 2 {{call can throw but is not marked with 'try'}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
try P.static_callAC(raise())
}
/** Miscellaneous bugs **/
// rdar://problem/21967164 - Non-throwing closures are incorrectly marked as throwing in rethrow contexts
func rt1(@noescape predicate: () throws -> ()) rethrows { }
rt1 { }
func rt2(predicate: () throws -> ()) rethrows { }
rt2 { }
enum SomeError : ErrorType {
case Badness
}
func testUnrelatedThrowsInRethrows(fn: () throws -> Void) rethrows {
try fn() // okay
try testUnrelatedThrowsInRethrows(fn) // okay
raise() // expected-error {{call can throw, but it is not marked with 'try' and the error is not handled; a function declared 'rethrows' may only throw if its parameter does}}
try raise() // expected-error {{call can throw, but the error is not handled; a function declared 'rethrows' may only throw if its parameter does}}
throw SomeError.Badness // expected-error {{a function declared 'rethrows' may only throw if its parameter does}}
}