-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathconcurrent_value_checking.swift
481 lines (382 loc) · 19.2 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
480
481
// 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
class NotConcurrent { } // 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
// ----------------------------------------------------------------------
actor A1 {
let localLet: NotConcurrent = NotConcurrent()
func synchronous() -> NotConcurrent? { nil }
func asynchronous(_: NotConcurrent?) async { }
}
@MainActor var booleanValue: Bool { false }
// Actor initializers and Sendable
actor A2 {
var localVar: 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 {
await self.init(valueAsync: value)
}
nonisolated init(nonisoAsync value: NotConcurrent, _ c: Int) async {
if c == 0 {
await self.init(valueAsync: value)
} else {
self.init(value: value)
}
}
init(delegatingAsync value: NotConcurrent, _ c: Int) async {
if c == 0 {
await self.init(valueAsync: value)
} else if c == 1 {
self.init(value: value)
} else if c == 2 {
await self.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}}
_ = await A2(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}}
_ = await A2(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}}
_ = await A2(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}}
}
extension A1 {
func testIsolation(other: A1) async {
// All within the same actor domain, so the Sendable restriction
// does not apply.
_ = localLet
_ = synchronous()
_ = await asynchronous(nil)
_ = self.localLet
_ = self.synchronous()
_ = await self.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
// ----------------------------------------------------------------------
actor TestActor {}
@globalActor
struct SomeGlobalActor {
static var shared: TestActor { TestActor() }
}
@SomeGlobalActor
let globalValue: NotConcurrent? = nil
@SomeGlobalActor
func globalSync(_: NotConcurrent?) {
}
@SomeGlobalActor
func globalAsync(_: NotConcurrent?) async {
if await booleanValue {
return
}
await globalAsync(globalValue) // both okay because we're in the actor
globalSync(nil)
}
enum E {
@SomeGlobalActor static let notSafe: NotConcurrent? = nil
}
func globalTest() async {
// expected-warning@+2 {{expression is 'async' but is not marked with 'await'}}
// expected-note@+1 {{property access is 'async'}}
let a = globalValue // expected-warning{{non-sendable type 'NotConcurrent?' of let 'globalValue' cannot exit global actor 'SomeGlobalActor'-isolated context}}
await globalAsync(a) // expected-tns-warning {{sending 'a' risks causing data races}}
// expected-tns-note @-1 {{sending global actor 'SomeGlobalActor'-isolated 'a' to global actor 'SomeGlobalActor'-isolated global function 'globalAsync' risks causing data races between global actor 'SomeGlobalActor'-isolated and local nonisolated uses}}
await globalSync(a) // expected-tns-note {{access can happen concurrently}}
// 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
}
struct HasSubscript {
@SomeGlobalActor
subscript (i: Int) -> NotConcurrent? { nil }
}
class ClassWithGlobalActorInits { // 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'}}
let a = globalValue // expected-warning {{non-sendable type 'NotConcurrent?' of let 'globalValue' cannot exit global actor 'SomeGlobalActor'-isolated context}}
await globalAsync(a) // expected-tns-warning {{sending 'a' risks causing data races}}
// expected-tns-note @-1 {{sending global actor 'SomeGlobalActor'-isolated 'a' to global actor 'SomeGlobalActor'-isolated global function 'globalAsync' risks causing data races between global actor 'SomeGlobalActor'-isolated and local main actor-isolated uses}}
await globalSync(a) // expected-tns-note {{access can happen concurrently}}
_ = await ClassWithGlobalActorInits(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}}
_ = await ClassWithGlobalActorInits() // 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) {
let hs = HasSubscript()
let _ = hs[0] // okay
_ = ClassWithGlobalActorInits(nc)
}
// ----------------------------------------------------------------------
// Sendable restriction on captures.
// ----------------------------------------------------------------------
func acceptNonConcurrent(_: () -> Void) { }
func acceptConcurrent(_: @Sendable () -> Void) { }
func testConcurrency() {
let x = NotConcurrent()
var y = 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}}
}
}
@preconcurrency func acceptUnsafeSendable(_ fn: @Sendable () -> Void) { }
func testUnsafeSendableNothing() {
var x = 5
acceptUnsafeSendable {
x = 17 // expected-warning{{mutation of captured var 'x' in concurrently-executing code}}
}
print(x)
}
func testUnsafeSendableInAsync() async {
var x = 5
acceptUnsafeSendable {
x = 17 // expected-warning{{mutation of captured var 'x' in concurrently-executing code}}
}
print(x)
}
// ----------------------------------------------------------------------
// Sendable restriction on key paths.
// ----------------------------------------------------------------------
class NC: Hashable { // expected-note 3{{class 'NC' does not conform to the 'Sendable' protocol}}
func hash(into: inout Hasher) { }
static func==(_: NC, _: NC) -> Bool { true }
}
class HasNC {
var dict: [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.
// ----------------------------------------------------------------------
actor ANI {
nonisolated let nc = 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}}
nonisolated func 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.
// ----------------------------------------------------------------------
protocol AsyncProto {
func asyncMethod(_: NotConcurrent) async
}
extension A1: 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}}
}
protocol MainActorProto {
func asyncMainMethod(_: NotConcurrent) async
}
class SomeClass: 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.
// ----------------------------------------------------------------------
@Sendable func concurrentFunc() -> NotConcurrent? { nil }
// ----------------------------------------------------------------------
// No Sendable restriction on @Sendable function types.
// ----------------------------------------------------------------------
typealias CF = @Sendable () -> NotConcurrent?
typealias BadGenericCF<T> = @Sendable () -> T?
typealias GoodGenericCF<T: Sendable> = @Sendable () -> T? // okay
var concurrentFuncVar: (@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>(_: T) { // expected-note{{consider making generic parameter 'T' conform to the 'Sendable' protocol}} {{26-26=: Sendable}}
acceptConcurrentUnary { (x: T) in
_ = x // ok
acceptConcurrentUnary { _ in x } // expected-warning{{capture of 'x' with non-sendable type 'T' in a '@Sendable' closure}}
let y: T? = nil
return y!
}
}
// ----------------------------------------------------------------------
// Sendable checking
// ----------------------------------------------------------------------
struct S1: Sendable {
var nc: NotConcurrent // expected-warning{{stored property 'nc' of 'Sendable'-conforming struct 'S1' has non-sendable type 'NotConcurrent'}}
}
struct S2<T>: Sendable { // expected-note{{consider making generic parameter 'T' conform to the 'Sendable' protocol}} {{12-12=: Sendable}}
var nc: T // expected-warning{{stored property 'nc' of 'Sendable'-conforming generic struct 'S2' has non-sendable type 'T'}}
}
struct S3<T> {
var c: T
var array: [T]
}
extension S3: Sendable where T: Sendable { }
enum E1: Sendable {
case payload(NotConcurrent) // expected-warning{{associated value 'payload' of 'Sendable'-conforming enum 'E1' has non-sendable type 'NotConcurrent'}}
}
enum E2<T> {
case payload(T)
}
extension E2: Sendable where T: Sendable { }
final class C1: Sendable {
let nc: NotConcurrent? = nil // expected-warning{{stored property 'nc' of 'Sendable'-conforming class 'C1' has non-sendable type 'NotConcurrent?'}}
var x: Int = 0 // expected-warning{{stored property 'x' of 'Sendable'-conforming class 'C1' is mutable}}
let i: Int = 0
}
final class C2: Sendable {
let x: Int = 0
}
class C3 { }
class C4: C3, @unchecked Sendable {
var y: Int = 0 // okay
}
class C5: @unchecked Sendable {
var x: Int = 0 // okay
}
// expected-warning@+1 {{class 'C6' must restate inherited '@unchecked Sendable' conformance}}{{13-13=, @unchecked Sendable}}
class C6: C5 {
var y: Int = 0 // still okay, it's unsafe
}
class C6_Restated: C5, @unchecked Sendable {
var y: Int = 0 // still okay, it's unsafe
}
class C6_Restated_Extension: C5 {
var y: Int = 0 // still okay, it's unsafe
}
extension C6_Restated_Extension: @unchecked Sendable {}
final class C7<T>: Sendable { }
class C9: Sendable { } // expected-warning{{non-final class 'C9' cannot conform to 'Sendable'; use '@unchecked Sendable'}}
@available(*, unavailable)
extension HasUnavailableSendable : @unchecked Sendable { }
class HasUnavailableSendable {
}
class NoRestated: HasUnavailableSendable {} // okay
@globalActor
struct SomeActor {
static let shared = A1()
}
class NotSendable {}
// actor-isolated mutable properties are valid
final class C10: Sendable { // expected-warning{{default initializer for 'C10' cannot be both nonisolated and main actor-isolated}}
@MainActor var x = 0
@MainActor var ns1 : NotSendable? // expected-note{{initializer for property 'ns1' is main actor-isolated}}
@MainActor let ns : NotSendable? = nil
}
final class C14: Sendable { // expected-warning{{default initializer for 'C14' cannot be both nonisolated and global actor 'SomeActor'-isolated}}
@SomeActor var y = 1
@SomeActor var nc = NotConcurrent() // expected-note{{initializer for property 'nc' is global actor 'SomeActor'-isolated}}
@SomeActor let nc1 = NotConcurrent()
}
extension NotConcurrent {
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
// ----------------------------------------------------------------------
struct S11: @unchecked Sendable {
var nc: NotConcurrent // okay
}
struct S12<T>: @unchecked Sendable {
var nc: T // okay
}
enum E11<T>: @unchecked Sendable {
case payload(NotConcurrent) // okay
case other(T) // okay
}
#if ALLOW_TYPECHECKER_ERRORS
class C11 { }
class C12: @unchecked C11 { } // expected-typechecker-error{{'unchecked' attribute cannot apply to non-protocol type 'C11'}}
protocol P { }
protocol Q: @unchecked Sendable { } // expected-typechecker-error{{'unchecked' attribute only applies in inheritance clauses}}
typealias TypeAlias1 = @unchecked P // expected-typechecker-error{{'unchecked' attribute only applies in inheritance clauses}}
#endif
// ----------------------------------------------------------------------
// UnsafeSendable historical name
// ----------------------------------------------------------------------
enum E12<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) {
var fn: (@Sendable () -> Void)? = nil
fn = {
print(nc) // expected-warning{{capture of 'nc' with non-sendable type 'NotConcurrent' in a '@Sendable' closure}}
}
_ = fn
}