- Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathconcurrent_value_checking.swift
479 lines (380 loc) · 18.5 KB
/
concurrent_value_checking.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
// RUN: %target-swift-frontend -target %target-swift-5.1-abi-triple -strict-concurrency=complete -parse-as-library %s -emit-sil -o /dev/null -verify -DALLOW_TYPECHECKER_ERRORS -verify-additional-prefix typechecker- -verify-additional-prefix tns-allow-typechecker-
// RUN: %target-swift-frontend -target %target-swift-5.1-abi-triple -strict-concurrency=complete -parse-as-library %s -emit-sil -o /dev/null -verify -verify-additional-prefix tns-
// REQUIRES: concurrency
// REQUIRES: asserts
classNotConcurrent{} // expected-note 12{{class 'NotConcurrent' does not conform to the 'Sendable' protocol}}
// expected-tns-allow-typechecker-note @-1 {{class 'NotConcurrent' does not conform to the 'Sendable' protocol}}
// ----------------------------------------------------------------------
// Sendable restriction on actor operations
// ----------------------------------------------------------------------
actorA1{
letlocalLet:NotConcurrent=NotConcurrent()
func synchronous()->NotConcurrent?{nil}
func asynchronous(_:NotConcurrent?)async{}
}
@MainActorvarbooleanValue:Bool{false}
// Actor initializers and Sendable
actorA2{
varlocalVar:NotConcurrent
init(value:NotConcurrent){
self.localVar = value
}
init(forwardSync value:NotConcurrent){
self.init(value: value)
}
init(delegatingSync value:NotConcurrent){
self.init(forwardSync: value)
}
init(valueAsync value:NotConcurrent)async{
self.localVar = value
}
init(forwardAsync value:NotConcurrent)async{
awaitself.init(valueAsync: value)
}
nonisolatedinit(nonisoAsync value:NotConcurrent, _ c:Int)async{
if c ==0{
awaitself.init(valueAsync: value)
}else{
self.init(value: value)
}
}
init(delegatingAsync value:NotConcurrent, _ c:Int)async{
if c ==0{
awaitself.init(valueAsync: value)
}elseif c ==1{
self.init(value: value)
}elseif c ==2{
awaitself.init(forwardAsync: value)
}else{
self.init(delegatingSync: value)
}
}
}
func testActorCreation(value:NotConcurrent)async{
_ =A2(value: value)
// expected-tns-warning @-1 {{sending 'value' risks causing data races}}
// expected-tns-note @-2 {{sending task-isolated 'value' to actor-isolated initializer 'init(value:)' risks causing data races between actor-isolated and task-isolated uses}}
_ =awaitA2(valueAsync: value)
// expected-tns-warning @-1 {{sending 'value' risks causing data races}}
// expected-tns-note @-2 {{sending task-isolated 'value' to actor-isolated initializer 'init(valueAsync:)' risks causing data races between actor-isolated and task-isolated uses}}
_ =A2(delegatingSync: value)
// expected-tns-warning @-1 {{sending 'value' risks causing data races}}
// expected-tns-note @-2 {{sending task-isolated 'value' to actor-isolated initializer 'init(delegatingSync:)' risks causing data races between actor-isolated and task-isolated uses}}
_ =awaitA2(delegatingAsync: value,9)
// expected-tns-warning @-1 {{sending 'value' risks causing data races}}
// expected-tns-note @-2 {{sending task-isolated 'value' to actor-isolated initializer 'init(delegatingAsync:_:)' risks causing data races between actor-isolated and task-isolated uses}}
_ =awaitA2(nonisoAsync: value,3)
// expected-tns-warning @-1 {{sending 'value' risks causing data races}}
// expected-tns-note @-2 {{sending task-isolated 'value' to actor-isolated initializer 'init(nonisoAsync:_:)' risks causing data races between actor-isolated and task-isolated uses}}
}
extensionA1{
func testIsolation(other:A1)async{
// All within the same actor domain, so the Sendable restriction
// does not apply.
_ = localLet
_ =synchronous()
_ =awaitasynchronous(nil)
_ =self.localLet
_ =self.synchronous()
_ =awaitself.asynchronous(nil)
// Across to a different actor, so Sendable restriction is enforced.
_ = other.localLet // expected-warning{{non-sendable type 'NotConcurrent' of property 'localLet' cannot exit actor-isolated context}}
// expected-warning@-1 {{expression is 'async' but is not marked with 'await'}}
// expected-note@-2 {{property access is 'async'}}
_ =await other.synchronous() // expected-tns-warning {{non-Sendable 'NotConcurrent?'-typed result can not be returned from actor-isolated instance method 'synchronous()' to actor-isolated context}}
_ =await other.asynchronous(nil)
}
}
// ----------------------------------------------------------------------
// Sendable restriction on global actor operations
// ----------------------------------------------------------------------
actorTestActor{}
@globalActor
structSomeGlobalActor{
staticvarshared:TestActor{TestActor()}
}
@SomeGlobalActor
letglobalValue:NotConcurrent?=nil
@SomeGlobalActor
func globalSync(_:NotConcurrent?){
}
@SomeGlobalActor
func globalAsync(_:NotConcurrent?)async{
if awaitbooleanValue{
return
}
await globalAsync(globalValue) // both okay because we're in the actor
globalSync(nil)
}
enumE{
@SomeGlobalActorstaticletnotSafe:NotConcurrent?=nil
}
func globalTest()async{
// expected-warning@+2 {{expression is 'async' but is not marked with 'await'}}
// expected-note@+1 {{property access is 'async'}}
leta= globalValue // expected-warning{{non-sendable type 'NotConcurrent?' of let 'globalValue' cannot exit global actor 'SomeGlobalActor'-isolated context}}
awaitglobalAsync(a)
awaitglobalSync(a)
// expected-warning@+2 {{expression is 'async' but is not marked with 'await'}}
// expected-note@+1 {{property access is 'async'}}
let _ =E.notSafe // expected-warning{{non-sendable type 'NotConcurrent?' of static property 'notSafe' cannot exit global actor 'SomeGlobalActor'-isolated context}}
#if ALLOW_TYPECHECKER_ERRORS
// expected-typechecker-error@+3 {{expression is 'async' but is not marked with 'await'}}
// expected-typechecker-note@+2 {{call is 'async'}}
// expected-typechecker-note@+1 {{property access is 'async'}}
globalAsync(E.notSafe)
// expected-typechecker-warning@-2 {{non-sendable type 'NotConcurrent?' of static property 'notSafe' cannot exit global actor 'SomeGlobalActor'-isolated context}}
#endif
}
structHasSubscript{
@SomeGlobalActor
subscript (i:Int)->NotConcurrent?{nil}
}
classClassWithGlobalActorInits{ // expected-tns-note 2{{class 'ClassWithGlobalActorInits' does not conform to the 'Sendable' protocol}}
@SomeGlobalActor
init(_:NotConcurrent){}
@SomeGlobalActor
init(){}
}
@MainActor
func globalTestMain(nc:NotConcurrent)async{
// expected-warning@+2 {{expression is 'async' but is not marked with 'await'}}
// expected-note@+1 {{property access is 'async'}}
leta= globalValue // expected-warning {{non-sendable type 'NotConcurrent?' of let 'globalValue' cannot exit global actor 'SomeGlobalActor'-isolated context}}
awaitglobalAsync(a)
awaitglobalSync(a)
_ =awaitClassWithGlobalActorInits(nc)
// expected-tns-warning @-1 {{non-Sendable 'ClassWithGlobalActorInits'-typed result can not be returned from global actor 'SomeGlobalActor'-isolated initializer 'init(_:)' to main actor-isolated context}}
// expected-tns-warning @-2 {{sending 'nc' risks causing data races}}
// expected-tns-note @-3 {{sending main actor-isolated 'nc' to global actor 'SomeGlobalActor'-isolated initializer 'init(_:)' risks causing data races between global actor 'SomeGlobalActor'-isolated and main actor-isolated uses}}
_ =awaitClassWithGlobalActorInits() // expected-tns-warning {{non-Sendable 'ClassWithGlobalActorInits'-typed result can not be returned from global actor 'SomeGlobalActor'-isolated initializer 'init()' to main actor-isolated context}}
}
@SomeGlobalActor
func someGlobalTest(nc:NotConcurrent){
leths=HasSubscript()
let _ =hs[0] // okay
_ =ClassWithGlobalActorInits(nc)
}
// ----------------------------------------------------------------------
// Sendable restriction on captures.
// ----------------------------------------------------------------------
func acceptNonConcurrent(_:()->Void){}
func acceptConcurrent(_:@Sendable()->Void){}
func testConcurrency(){
letx=NotConcurrent()
vary=NotConcurrent()
y =NotConcurrent()
acceptNonConcurrent{
print(x) // okay
print(y) // okay
}
acceptConcurrent{
print(x) // expected-warning{{capture of 'x' with non-sendable type 'NotConcurrent' in a '@Sendable' closure}}
print(y) // expected-warning{{capture of 'y' with non-sendable type 'NotConcurrent' in a '@Sendable' closure}}
// expected-warning@-1{{reference to captured var 'y' in concurrently-executing code}}
}
}
@preconcurrencyfunc acceptUnsafeSendable(_ fn:@Sendable()->Void){}
func testUnsafeSendableNothing(){
varx=5
acceptUnsafeSendable{
x =17 // expected-warning{{mutation of captured var 'x' in concurrently-executing code}}
}
print(x)
}
func testUnsafeSendableInAsync()async{
varx=5
acceptUnsafeSendable{
x =17 // expected-warning{{mutation of captured var 'x' in concurrently-executing code}}
}
print(x)
}
// ----------------------------------------------------------------------
// Sendable restriction on key paths.
// ----------------------------------------------------------------------
classNC:Hashable{ // expected-note 3{{class 'NC' does not conform to the 'Sendable' protocol}}
func hash(into:inoutHasher){}
staticfunc==(_:NC, _:NC)->Bool{true}
}
classHasNC{
vardict:[NC:Int]=[:]
}
func testKeyPaths(dict:[NC:Int], nc:NC){
_ = \HasNC.dict[nc] // expected-warning{{cannot form key path that captures non-sendable type 'NC'}}
}
// ----------------------------------------------------------------------
// Sendable restriction on nonisolated declarations.
// ----------------------------------------------------------------------
actorANI{
nonisolatedletnc=NC() // expected-warning {{'nonisolated' can not be applied to variable with non-'Sendable' type 'NC'; this is an error in the Swift 6 language mode}}
nonisolatedfunc f()->NC?{nil}
}
func testANI(ani:ANI)async{
_ = ani.nc // expected-warning{{non-sendable type 'NC' of property 'nc' cannot exit nonisolated context}}
}
// ----------------------------------------------------------------------
// Sendable restriction on conformances.
// ----------------------------------------------------------------------
protocolAsyncProto{
func asyncMethod(_:NotConcurrent)async
}
extensionA1:AsyncProto{
func asyncMethod(_:NotConcurrent)async{} // expected-warning{{non-sendable parameter type 'NotConcurrent' cannot be sent from caller of protocol requirement 'asyncMethod' into actor-isolated implementation}}
}
protocolMainActorProto{
func asyncMainMethod(_:NotConcurrent)async
}
classSomeClass:MainActorProto{
@SomeGlobalActor
func asyncMainMethod(_:NotConcurrent)async{} // expected-warning{{non-sendable parameter type 'NotConcurrent' cannot be sent from caller of protocol requirement 'asyncMainMethod' into global actor 'SomeGlobalActor'-isolated implementation}}
}
// ----------------------------------------------------------------------
// Sendable restriction on concurrent functions.
// ----------------------------------------------------------------------
@Sendablefunc concurrentFunc()->NotConcurrent?{nil}
// ----------------------------------------------------------------------
// No Sendable restriction on @Sendable function types.
// ----------------------------------------------------------------------
typealiasCF=@Sendable()->NotConcurrent?
typealiasBadGenericCF<T>=@Sendable()->T?
typealiasGoodGenericCF<T:Sendable>=@Sendable()->T? // okay
varconcurrentFuncVar:(@Sendable(NotConcurrent)->Void)?=nil // expected-warning{{var 'concurrentFuncVar' is not concurrency-safe because it is nonisolated global shared mutable state; this is an error in the Swift 6 language mode}}
// expected-note@-1 {{add '@MainActor' to make var 'concurrentFuncVar' part of global actor 'MainActor'}}
// expected-note@-2 {{disable concurrency-safety checks if accesses are protected by an external synchronization mechanism}}
// expected-note@-3 {{convert 'concurrentFuncVar' to a 'let' constant to make 'Sendable' shared state immutable}}
// ----------------------------------------------------------------------
// Sendable restriction on @Sendable closures.
// ----------------------------------------------------------------------
func acceptConcurrentUnary<T>(_:@Sendable(T)->T){}
func concurrentClosures<T:SendableMetatype>(_:T){ // expected-note{{consider making generic parameter 'T' conform to the 'Sendable' protocol}} {{44-44= & Sendable}}
acceptConcurrentUnary{(x:T)in
_ = x // ok
acceptConcurrentUnary{ _ in x } // expected-warning{{capture of 'x' with non-sendable type 'T' in a '@Sendable' closure}}
lety:T?=nil
return y!
}
}
// ----------------------------------------------------------------------
// Sendable checking
// ----------------------------------------------------------------------
structS1:Sendable{
varnc:NotConcurrent // expected-warning{{stored property 'nc' of 'Sendable'-conforming struct 'S1' has non-sendable type 'NotConcurrent'}}
}
structS2<T>:Sendable{ // expected-note{{consider making generic parameter 'T' conform to the 'Sendable' protocol}} {{12-12=: Sendable}}
varnc:T // expected-warning{{stored property 'nc' of 'Sendable'-conforming generic struct 'S2' has non-sendable type 'T'}}
}
structS3<T>{
varc:T
vararray:[T]
}
extensionS3:Sendablewhere T:Sendable{}
enumE1:Sendable{
case payload(NotConcurrent) // expected-warning{{associated value 'payload' of 'Sendable'-conforming enum 'E1' has non-sendable type 'NotConcurrent'}}
}
enumE2<T>{
case payload(T)
}
extensionE2:Sendablewhere T:Sendable{}
finalclassC1:Sendable{
letnc:NotConcurrent?=nil // expected-warning{{stored property 'nc' of 'Sendable'-conforming class 'C1' has non-sendable type 'NotConcurrent?'}}
varx:Int=0 // expected-warning{{stored property 'x' of 'Sendable'-conforming class 'C1' is mutable}}
leti:Int=0
}
finalclassC2:Sendable{
letx:Int=0
}
classC3{}
classC4:C3,@uncheckedSendable{
vary:Int=0 // okay
}
classC5:@uncheckedSendable{
varx:Int=0 // okay
}
// expected-warning@+1 {{class 'C6' must restate inherited '@unchecked Sendable' conformance}}{{13-13=, @unchecked Sendable}}
classC6:C5{
vary:Int=0 // still okay, it's unsafe
}
classC6_Restated:C5,@uncheckedSendable{
vary:Int=0 // still okay, it's unsafe
}
classC6_Restated_Extension:C5{
vary:Int=0 // still okay, it's unsafe
}
extensionC6_Restated_Extension:@uncheckedSendable{}
finalclassC7<T>:Sendable{}
classC9:Sendable{} // expected-warning{{non-final class 'C9' cannot conform to 'Sendable'; use '@unchecked Sendable'}}
@available(*, unavailable)
extensionHasUnavailableSendable:@uncheckedSendable{}
classHasUnavailableSendable{
}
classNoRestated:HasUnavailableSendable{} // okay
@globalActor
structSomeActor{
staticletshared=A1()
}
classNotSendable{}
// actor-isolated mutable properties are valid
finalclassC10:Sendable{ // expected-warning{{default initializer for 'C10' cannot be both nonisolated and main actor-isolated}}
@MainActorvarx=0
@MainActorvarns1:NotSendable? // expected-note{{initializer for property 'ns1' is main actor-isolated}}
@MainActorletns:NotSendable?=nil
}
finalclassC14:Sendable{ // expected-warning{{default initializer for 'C14' cannot be both nonisolated and global actor 'SomeActor'-isolated}}
@SomeActorvary=1
@SomeActorvarnc=NotConcurrent() // expected-note{{initializer for property 'nc' is global actor 'SomeActor'-isolated}}
@SomeActorletnc1=NotConcurrent()
}
extensionNotConcurrent{
func f(){}
func test(){
Task{ // expected-tns-warning {{passing closure as a 'sending' parameter risks causing data races between code in the current task and concurrent execution of the closure}}
f() // expected-tns-note {{closure captures 'self' which is accessible to code in the current task}}
}
Task{ // expected-tns-warning {{passing closure as a 'sending' parameter risks causing data races between code in the current task and concurrent execution of the closure}}
self.f() // expected-tns-note {{closure captures 'self' which is accessible to code in the current task}}
}
Task{[self]in // expected-tns-warning {{passing closure as a 'sending' parameter risks causing data races between code in the current task and concurrent execution of the closure}}
f() // expected-tns-note {{closure captures 'self' which is accessible to code in the current task}}
}
Task{[self]in // expected-tns-warning {{passing closure as a 'sending' parameter risks causing data races between code in the current task and concurrent execution of the closure}}
self.f() // expected-tns-note {{closure captures 'self' which is accessible to code in the current task}}
}
}
}
// ----------------------------------------------------------------------
// @unchecked Sendable disabling checking
// ----------------------------------------------------------------------
structS11:@uncheckedSendable{
varnc:NotConcurrent // okay
}
structS12<T>:@uncheckedSendable{
varnc:T // okay
}
enumE11<T>:@uncheckedSendable{
case payload(NotConcurrent) // okay
case other(T) // okay
}
#if ALLOW_TYPECHECKER_ERRORS
classC11{}
classC12:@uncheckedC11{} // expected-typechecker-error{{'@unchecked' cannot apply to non-protocol type 'C11'}}
protocolP{}
protocolQ:@uncheckedSendable{} // expected-typechecker-error{{'@unchecked' only applies in inheritance clauses}}
typealiasTypeAlias1=@uncheckedP // expected-typechecker-error{{'@unchecked' only applies in inheritance clauses}}
#endif
// ----------------------------------------------------------------------
// UnsafeSendable historical name
// ----------------------------------------------------------------------
enumE12<T>:UnsafeSendable{ // expected-warning{{'UnsafeSendable' is deprecated: Use @unchecked Sendable instead}}
case payload(NotConcurrent) // okay
case other(T) // okay
}
// ----------------------------------------------------------------------
// @Sendable inference through optionals
// ----------------------------------------------------------------------
func testSendableOptionalInference(nc:NotConcurrent){
varfn:(@Sendable()->Void)?=nil
fn ={
print(nc) // expected-warning{{capture of 'nc' with non-sendable type 'NotConcurrent' in a '@Sendable' closure}}
}
_ = fn
}