-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathdistributed_actor_transfernonsendable.swift
84 lines (66 loc) · 3.51 KB
/
distributed_actor_transfernonsendable.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
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend-emit-module -emit-module-path %t/FakeDistributedActorSystems.swiftmodule -module-name FakeDistributedActorSystems -target %target-swift-5.7-abi-triple %S/Inputs/FakeDistributedActorSystems.swift
// RUN: %target-swift-frontend -swift-version 6 -verify -verify-ignore-unknown -target %target-swift-5.7-abi-triple -I %t %s -emit-sil
// REQUIRES: concurrency
// REQUIRES: distributed
import Distributed
import FakeDistributedActorSystems
////////////////////////
// MARK: Declarations //
////////////////////////
@available(SwiftStdlib 5.5, *)
typealias DefaultDistributedActorSystem = FakeActorSystem
final class NonSendableKlass {}
extension NonSendableKlass : Codable {}
@MainActor func transferToMain<T>(_ t: T) async {}
/////////////////
// MARK: Tests //
/////////////////
distributed actor MyDistributedActor {
let x: NonSendableKlass
init(system: FakeActorSystem, y: NonSendableKlass) {
x = NonSendableKlass()
actorSystem = system
_ = { @MainActor in
// TODO: This should error saying 'y' is actor isolated.
print(y) // expected-error {{sending 'y' risks causing data races}}
// expected-note @-1 {{task-isolated 'y' is captured by a main actor-isolated closure. main actor-isolated uses in closure may race against later nonisolated uses}}
}
}
init(system: FakeActorSystem, y2: NonSendableKlass) {
x = y2
actorSystem = system
_ = { @MainActor in
print(y2) // expected-error {{sending 'y2' risks causing data races}}
// expected-note @-1 {{'self'-isolated 'y2' is captured by a main actor-isolated closure. main actor-isolated uses in closure may race against later nonisolated uses}}
}
}
distributed func transferActorField() async {
await transferToMain(x) // expected-error {{sending 'self.x' risks causing data races}}
// expected-note @-1 {{sending 'self'-isolated 'self.x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and 'self'-isolated uses}}
}
distributed func transferActorIsolatedArg(_ x: NonSendableKlass) async {
await transferToMain(x) // expected-error {{sending 'x' risks causing data races}}
// expected-note @-1 {{sending 'self'-isolated 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and 'self'-isolated uses}}
}
distributed func transferActorIsolatedArgIntoClosure(_ x: NonSendableKlass) async {
_ = { @MainActor in
// TODO: In 2nd part of message should say actor-isolated instead of later
// nonisolated uses in the case of a closure.
print(x) // expected-error {{sending 'x' risks causing data races}}
// expected-note @-1 {{'self'-isolated 'x' is captured by a main actor-isolated closure. main actor-isolated uses in closure may race against later nonisolated uses}}
}
}
}
/////////////////////////////////
// MARK: Associated Type Tests //
/////////////////////////////////
protocol AssociatedTypeTestProtocol {
associatedtype A: DistributedActor
}
func associatedTypeTestBasic<T: AssociatedTypeTestProtocol>(_: T, _: isolated T.A) {
}
func associatedTypeTestBasic2<T: AssociatedTypeTestProtocol>(_: T, iso: isolated T.A, x: NonSendableKlass) async {
await transferToMain(x) // expected-error {{sending 'x' risks causing data races}}
// expected-note @-1 {{sending 'iso'-isolated 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and 'iso'-isolated uses}}
}