- Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathasync_throwing.swift
177 lines (143 loc) · 7.21 KB
/
async_throwing.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
// RUN: %target-swift-frontend -target %target-swift-5.1-abi-triple %s -emit-sil -o /dev/null -verify
// RUN: %target-swift-frontend -target %target-swift-5.1-abi-triple %s -emit-sil -o /dev/null -verify -strict-concurrency=targeted
// RUN: %target-swift-frontend -target %target-swift-5.1-abi-triple %s -emit-sil -o /dev/null -verify -strict-concurrency=complete
// RUN: %target-swift-frontend -target %target-swift-5.1-abi-triple %s -emit-sil -o /dev/null -verify -strict-concurrency=complete -enable-upcoming-feature RegionBasedIsolation
// REQUIRES: concurrency
// REQUIRES: swift_feature_RegionBasedIsolation
// These tests cover various interactions with async functions that are
// either throws or rethrows.
// See rdar://70813762 and rdar://70751405
enumInvocationError:Error{
case ErrVal
}
func asyncThrows()asyncthrows{
throwInvocationError.ErrVal
}
// T = Int
func asyncRethrows(fn :()asyncthrows->Int)asyncrethrows->Int{
returntryawaitfn()
}
// T = String
func asyncRethrows(fn :()asyncthrows->String)asyncrethrows->String{
returntryawaitfn()
}
// Generic. NOTE the 'rethrows'
func invoke<T>(fn :()asyncthrows->T)asyncrethrows->T{
returntryawaitfn()
}
// NOTE the 'rethrows'
func invokeAuto<T>(_ val :@autoclosure()asyncthrows->T)asyncrethrows->T{
returntryawaitval()
}
func normalTask()async->Int{
return42
}
func throwingTask()asyncthrows->String{
if1.0/3.0==0.33{
throwInvocationError.ErrVal
}
return"ok!"
}
// expected-note@+1 7 {{add 'async' to function 'syncTest()' to make it asynchronous}} {{16-16= async}}
func syncTest(){
let _ =invoke(fn: normalTask) // expected-error{{'async' call in a function that does not support concurrency}}
let _ =invokeAuto(42) // expected-error{{'async' call in a function that does not support concurrency}}
let _ =invokeAuto("intuitive") // expected-error{{'async' call in a function that does not support concurrency}}
let _ =try!asyncRethrows(fn: throwingTask) // expected-error{{'async' call in a function that does not support concurrency}}
let _ =try?invoke(fn: throwingTask) // expected-error{{'async' call in a function that does not support concurrency}}
do{
let _ =tryinvoke(fn: throwingTask) // expected-error{{'async' call in a function that does not support concurrency}}
let _ =tryasyncThrows() // expected-error{{'async' call in a function that does not support concurrency}}
}catch{
// ignore it
}
}
func asyncTest()async{
///////////
// tests that also omit await
// expected-error@+2{{expression is 'async' but is not marked with 'await'}}{{11-11=await }}
// expected-note@+1{{call is 'async'}}
let _ =invoke(fn: normalTask)
// expected-error@+2{{expression is 'async' but is not marked with 'await'}}{{11-11=await }}
// expected-note@+1{{call is 'async'}}
let _ =asyncRethrows(fn: normalTask)
// expected-error@+2{{expression is 'async' but is not marked with 'await'}}{{11-11=await }}
// expected-note@+1{{call is 'async'}}
let _ =invokeAuto(42)
// expected-error@+3 {{call can throw, but it is not marked with 'try' and the error is not handled}}
// expected-error@+2 {{expression is 'async' but is not marked with 'await'}}{{11-11=await }}
// expected-note@+1 {{call is 'async'}}
let _ =asyncThrows()
// expected-note@+4{{call is to 'rethrows' function, but argument function can throw}}
// expected-error@+3{{call can throw, but it is not marked with 'try' and the error is not handled}}
// expected-error@+2{{expression is 'async' but is not marked with 'await'}}{{11-11=await }}
// expected-note@+1:11{{call is 'async'}}
let _ =invoke(fn: throwingTask)
///////////
// tests that use await and handles the exceptions
// expected-note@+2{{call is to 'rethrows' function, but argument function can throw}}
// expected-error@+1{{call can throw, but it is not marked with 'try' and the error is not handled}}
let _ =awaitinvoke(fn: throwingTask)
let _ =awaitinvoke(fn: normalTask) // ok
let _ =awaitasyncRethrows(fn: normalTask)
let _ =try!awaitasyncRethrows(fn: normalTask) // expected-warning{{no calls to throwing functions occur within 'try' expression}}
let _ =try?awaitasyncRethrows(fn: normalTask) // expected-warning{{no calls to throwing functions occur within 'try' expression}}
let _ =try!awaitasyncRethrows(fn: throwingTask)
let _ =try?awaitasyncRethrows(fn: throwingTask)
let _ =try!awaitasyncThrows()
let _ =try?awaitasyncThrows()
//////////
// some auto-closure tests
let _ =awaitinvokeAuto("intuitive")
let _ =try!awaitinvokeAuto(awaitthrowingTask())
let _ =try?awaitinvokeAuto(awaitthrowingTask())
let _ =awaitinvokeAuto(try!awaitthrowingTask())
let _ =awaitinvokeAuto(try?awaitthrowingTask())
// expected-error@+1{{expression is 'async' but is not marked with 'await'}}{{33-33=await }}
let _ =awaitinvokeAuto(try!throwingTask()) // expected-note@:33{{call is 'async' in an autoclosure argument}}
// expected-error@+1{{expression is 'async' but is not marked with 'await'}}{{33-33=await }}
let _ =awaitinvokeAuto(try?throwingTask()) // expected-note@:33{{call is 'async' in an autoclosure argument}}
let _ =awaitinvokeAuto(try!awaitthrowingTask())
let _ =awaitinvokeAuto(try?awaitthrowingTask())
/////////
do{
let _ =tryawaitasyncThrows()
let _ =tryawaitasyncRethrows(fn: throwingTask)
//////
// more auto-closure tests
// expected-note@+7 {{did you mean to disable error propagation?}}
// expected-note@+6 {{did you mean to handle error as optional value?}}
// expected-note@+5 {{did you mean to use 'try'?}}
// expected-note@+4 {{call is to 'rethrows' function, but argument function can throw}}
// expected-note@+3 {{call is 'async' in an autoclosure argument}}
// expected-error@+2 {{expression is 'async' but is not marked with 'await'}}{{30-30=await }}
// expected-error@+1 2 {{call can throw but is not marked with 'try'}}
let _ =awaitinvokeAuto(throwingTask())
// expected-error@+1:34{{expression is 'async' but is not marked with 'await'}}{{34-34=await }}
let _ =tryawaitinvokeAuto("hello"+ throwingTask()) // expected-note@:44{{call is 'async' in an autoclosure argument}}
// expected-error@+1{{expression is 'async' but is not marked with 'await'}}{{17-17=await }}
let _ =tryinvokeAuto(awaitthrowingTask()) // expected-note{{call is 'async'}}
let _ =tryawaitinvokeAuto(awaitthrowingTask())
}catch{
// ignore
}
}
// rdar://123356909 - spurious error - `call can throw, but it is not marked with 'try' and the error is not handled`
do{
structAsyncTestSequence<Element>:AsyncSequence{
typealiasElement=Element
letstream:AsyncMapSequence<AsyncStream<[String:Any]>,Element>
init(stream:AsyncMapSequence<AsyncStream<[String:Any]>,Element>){
self.stream = stream
}
func makeAsyncIterator()->AsyncIterator{
.init(iterator: stream.makeAsyncIterator())
}
structAsyncIterator:AsyncIteratorProtocol{
variterator:AsyncMapSequence<AsyncStream<[String:Any]>,Element>.AsyncIterator
mutatingfunc next()async->Element?{
await iterator.next() // Ok
}
}
}
}