forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistributed_actor_isolation.swift
200 lines (158 loc) · 8.51 KB
/
distributed_actor_isolation.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
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend-emit-module -emit-module-path %t/FakeDistributedActorSystems.swiftmodule -module-name FakeDistributedActorSystems -disable-availability-checking %S/Inputs/FakeDistributedActorSystems.swift
// RUN: %target-swift-frontend -typecheck -verify -enable-experimental-distributed -disable-availability-checking -I %t 2>&1 %s
// REQUIRES: concurrency
// REQUIRES: distributed
// TODO(distributed): rdar://82419661 remove -verify-ignore-unknown here, no warnings should be emitted for our
// generated code but right now a few are, because of Sendability checks -- need to track it down more.
import _Distributed
import FakeDistributedActorSystems
@available(SwiftStdlib 5.5, *)
typealias DefaultDistributedActorSystem = FakeActorSystem
// ==== ----------------------------------------------------------------------------------------------------------------
actor LocalActor_1 {
let name: String = "alice"
var mutable: String = ""
distributed func nope() {
// expected-error@-1{{'distributed' method can only be declared within 'distributed actor'}}
}
}
struct NotCodableValue { }
distributed actor DistributedActor_1 {
let name: String = "alice" // expected-note{{access to property 'name' is only permitted within distributed actor 'DistributedActor_1'}}
var mutable: String = "alice" // expected-note{{access to property 'mutable' is only permitted within distributed actor 'DistributedActor_1'}}
var computedMutable: String {
get {
"hey"
}
set {
_ = newValue
}
}
distributed let letProperty: String = "" // expected-error{{property 'letProperty' cannot be 'distributed', only computed properties can}}
distributed var varProperty: String = "" // expected-error{{property 'varProperty' cannot be 'distributed', only computed properties can}}
distributed static func distributedStatic() {} // expected-error{{'distributed' method cannot be 'static'}}
distributed class func distributedClass() {}
// expected-error@-1{{class methods are only allowed within classes; use 'static' to declare a static method}}
// expected-error@-2{{'distributed' method cannot be 'static'}} // TODO(distributed): should call out 'class' instead?
func hello() {} // expected-note{{distributed actor-isolated instance method 'hello()' declared here}}
func helloAsync() async {} // expected-note{{distributed actor-isolated instance method 'helloAsync()' declared here}}
func helloAsyncThrows() async throws {} // expected-note{{distributed actor-isolated instance method 'helloAsyncThrows()' declared here}}
distributed func distHello() { } // ok
distributed func distHelloAsync() async { } // ok
distributed func distHelloThrows() throws { } // ok
distributed func distHelloAsyncThrows() async throws { } // ok
distributed func distInt() async throws -> Int { 42 } // ok
distributed func distInt(int: Int) async throws -> Int { int } // ok
distributed func distIntString(int: Int, two: String) async throws -> (String) { "\(int) + \(two)" } // ok
distributed func dist(notCodable: NotCodableValue) async throws {
// expected-error@-1 {{parameter 'notCodable' of type 'NotCodableValue' in distributed instance method does not conform to serialization requirement 'Codable'}}
}
distributed func distBadReturn(int: Int) async throws -> NotCodableValue {
// expected-error@-1 {{result type 'NotCodableValue' of distributed instance method 'distBadReturn' does not conform to serialization requirement 'Codable'}}
fatalError()
}
distributed func varargs(int: Int...) {
// expected-error@-1{{cannot declare variadic argument 'int' in distributed instance method 'varargs(int:)'}}
}
distributed func closure(close: () -> String) {
// expected-error@-1{{parameter 'close' of type '() -> String' in distributed instance method does not conform to serialization requirement 'Codable'}}
}
distributed func noInout(inNOut burger: inout String) {
// expected-error@-1{{cannot declare 'inout' argument 'burger' in distributed instance method 'noInout(inNOut:)'}}{{43-49=}}
}
distributed func distReturnGeneric<T: Codable & Sendable>(item: T) async throws -> T { // ok
item
}
distributed func distReturnGenericWhere<T: Sendable>(item: Int) async throws -> T where T: Codable { // ok
fatalError()
}
distributed func distBadReturnGeneric<T: Sendable>(int: Int) async throws -> T {
// expected-error@-1 {{result type 'T' of distributed instance method 'distBadReturnGeneric' does not conform to serialization requirement 'Codable'}}
fatalError()
}
distributed func distGenericParam<T: Codable & Sendable>(value: T) async throws { // ok
fatalError()
}
distributed func distGenericParamWhere<T: Sendable>(value: T) async throws -> T where T: Codable { // ok
value
}
distributed func distBadGenericParam<T: Sendable>(int: T) async throws {
// expected-error@-1 {{parameter 'int' of type 'T' in distributed instance method does not conform to serialization requirement 'Codable'}}
fatalError()
}
static func staticFunc() -> String { "" } // ok
@MainActor
static func staticMainActorFunc() -> String { "" } // ok
static distributed func staticDistributedFunc() -> String {
// expected-error@-1{{'distributed' method cannot be 'static'}}{10-21=}
fatalError()
}
func test_inside() async throws {
_ = self.name
_ = self.computedMutable
_ = try await self.distInt()
_ = try await self.distInt(int: 42)
self.hello()
_ = await self.helloAsync()
_ = try await self.helloAsyncThrows()
self.distHello()
await self.distHelloAsync()
try self.distHelloThrows()
try await self.distHelloAsyncThrows()
// Hops over to the global actor.
_ = await DistributedActor_1.staticMainActorFunc()
}
}
func test_outside(
local: LocalActor_1,
distributed: DistributedActor_1
) async throws {
// ==== properties
_ = distributed.id // ok
distributed.id = ActorAddress(parse: "mock://1.1.1.1:8080/#123121") // expected-error{{cannot assign to property: 'id' is immutable}}
_ = local.name // ok, special case that let constants are okey
let _: String = local.mutable // ok, special case that let constants are okey
_ = distributed.name // expected-error{{distributed actor-isolated property 'name' can not be accessed from a non-isolated context}}
_ = distributed.mutable // expected-error{{distributed actor-isolated property 'mutable' can not be accessed from a non-isolated context}}
// ==== special properties (nonisolated, implicitly replicated)
// the distributed actor's special fields may always be referred to
_ = distributed.id
_ = distributed.actorSystem
// ==== static functions
_ = distributed.staticFunc() // expected-error{{static member 'staticFunc' cannot be used on instance of type 'DistributedActor_1'}}
_ = DistributedActor_1.staticFunc()
// ==== non-distributed functions
distributed.hello() // expected-error{{only 'distributed' instance methods can be called on a potentially remote distributed actor}}
_ = await distributed.helloAsync() // expected-error{{only 'distributed' instance methods can be called on a potentially remote distributed actor}}
_ = try await distributed.helloAsyncThrows() // expected-error{{only 'distributed' instance methods can be called on a potentially remote distributed actor}}
}
// ==== Protocols and static (non isolated functions)
protocol P {
static func hello() -> String
}
extension P {
static func hello() -> String { "" }
}
distributed actor ALL: P {
}
// ==== Codable parameters and return types ------------------------------------
func test_params(
distributed: DistributedActor_1
) async throws {
_ = try await distributed.distInt() // ok
_ = try await distributed.distInt(int: 42) // ok
_ = try await distributed.dist(notCodable: .init())
}
// Actor initializer isolation (through typechecking only!)
distributed actor DijonMustard {
nonisolated init(system: FakeActorSystem) {} // expected-warning {{'nonisolated' on an actor's synchronous initializer is invalid; this is an error in Swift 6}} {{3-15=}}
convenience init(conv: FakeActorSystem) {
self.init(system: conv)
self.f() // expected-error {{actor-isolated instance method 'f()' can not be referenced from a non-isolated context}}
}
func f() {} // expected-note {{distributed actor-isolated instance method 'f()' declared here}}
nonisolated convenience init(conv2: FakeActorSystem) { // expected-warning {{'nonisolated' on an actor's synchronous initializer is invalid; this is an error in Swift 6}} {{3-15=}}
self.init(system: conv2)
}
}