File: distributed_protocol_isolation.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (283 lines) | stat: -rw-r--r-- 13,012 bytes parent folder | download
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
// 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 -strict-concurrency=targeted -disable-availability-checking -I %t 2>&1 %s
// REQUIRES: concurrency
// REQUIRES: distributed

import Distributed
import FakeDistributedActorSystems

/// Use the existential wrapper as the default actor system.
typealias DefaultDistributedActorSystem = FakeActorSystem

// ==== -----------------------------------------------------------------------
// MARK: Distributed actor protocols

protocol WrongDistFuncs {
    distributed func notDistActor() // expected-error{{'distributed' method can only be declared within 'distributed actor'}}{{5-17=}} {{-1:25-25=: DistributedActor}}
}

protocol DistProtocol: DistributedActor {
  // FIXME(distributed): avoid issuing these warnings, these originate from the call on the DistProtocol where we marked this func as dist isolated,
  func local() -> String
  // (the note appears a few times, because we misuse the call many times)
  // expected-note@-2{{distributed actor-isolated instance method 'local()' declared here}}
  // expected-note@-3{{distributed actor-isolated instance method 'local()' declared here}}
  // expected-note@-4{{distributed actor-isolated instance method 'local()' declared here}}

  distributed func dist() -> String
  distributed func dist(string: String) -> String

  distributed func distAsync() async -> String
  distributed func distThrows() throws -> String
  distributed func distAsyncThrows() async throws -> String
}

distributed actor SpecificDist: DistProtocol {

  nonisolated func local() -> String { "hi" }

  distributed func dist() -> String { "dist!" }
  distributed func dist(string: String) -> String { string }

  distributed func distAsync() async -> String { "dist!" }
  distributed func distThrows() throws -> String { "dist!" }
  distributed func distAsyncThrows() async throws -> String { "dist!" }

  func inside() async throws {
    _ = self.local() // ok

    _ = self.dist() // ok
    _ = self.dist(string: "") // ok
    _ = await self.distAsync() // ok
    _ = try self.distThrows() // ok
    _ = try await self.distAsyncThrows() // ok
  }
}

func outside_good(dp: SpecificDist) async throws {
  _ = dp.local()

  _ = try await dp.dist() // implicit async throws
  _ = try await dp.dist(string: "") // implicit async throws
  _ = try await dp.distAsync() // implicit throws
  _ = try await dp.distThrows() // implicit async
  _ = try await dp.distAsyncThrows() // ok
}

func outside_good_generic<DP: DistProtocol>(dp: DP) async throws {
  _ = dp.local() // expected-error{{only 'distributed' instance methods can be called on a potentially remote distributed actor}}
  _ = await dp.local() // expected-error{{only 'distributed' instance methods can be called on a potentially remote distributed actor}}
  // the below warning is expected because we don't apply the "implicitly async" to the not-callable func
  // expected-warning@-2{{no 'async' operations occur within 'await' expression}}

  _ = try dp.local() // expected-error{{only 'distributed' instance methods can be called on a potentially remote distributed actor}}
  // the below warning is expected because we don't apply the "implicitly throwing" to the not-callable func
  // expected-warning@-2{{no calls to throwing functions occur within 'try' expression}}

  _ = try await dp.dist() // implicit async throws
  _ = try await dp.dist(string: "") // implicit async throws
  _ = try await dp.distAsync() // implicit throws
  _ = try await dp.distThrows() // implicit async
  _ = try await dp.distAsyncThrows() // ok
}

func outside_good_ext<DP: DistProtocol>(dp: DP) async throws {
  _ = try await dp.dist() // implicit async throws
  _ = try await dp.dist(string: "") // implicit async throws
  _ = try await dp.distAsync() // implicit throws
  _ = try await dp.distThrows() // implicit async
  _ = try await dp.distAsyncThrows() // ok
}

// ==== ------------------------------------------------------------------------
// MARK: General protocols implemented by distributed actors

/// A distributed actor could only conform to this by making everything 'nonisolated':
protocol StrictlyLocal {
  func local()
  // expected-note@-1 2{{mark the protocol requirement 'local()' 'async throws' to allow actor-isolated conformances}}{{15-15= async throws}}

  func localThrows() throws
  // expected-note@-1 2{{mark the protocol requirement 'localThrows()' 'async' to allow actor-isolated conformances}}{{22-22=async }}

  func localAsync() async
  // expected-note@-1 2{{mark the protocol requirement 'localAsync()' 'throws' to allow actor-isolated conformances}}
}

distributed actor Nope1_StrictlyLocal: StrictlyLocal {
  // expected-note@-1{{add '@preconcurrency' to the 'StrictlyLocal' conformance to defer isolation checking to run time}}

  func local() {}
  // expected-error@-1{{distributed actor-isolated instance method 'local()' cannot be used to satisfy nonisolated protocol requirement}}
  // expected-note@-2{{add 'nonisolated' to 'local()' to make this instance method not isolated to the actor}}
  func localThrows() throws {}
  // expected-error@-1{{distributed actor-isolated instance method 'localThrows()' cannot be used to satisfy nonisolated protocol requirement}}
  // expected-note@-2{{add 'nonisolated' to 'localThrows()' to make this instance method not isolated to the actor}}
  func localAsync() async {}
  // expected-error@-1{{distributed actor-isolated instance method 'localAsync()' cannot be used to satisfy nonisolated protocol requirement}}
  // expected-note@-2{{add 'nonisolated' to 'localAsync()' to make this instance method not isolated to the actor}}
}
distributed actor Nope2_StrictlyLocal: StrictlyLocal {
  distributed func local() {}
  // expected-error@-1{{actor-isolated distributed instance method 'local()' cannot be used to satisfy nonisolated protocol requirement}}
  distributed func localThrows() throws {}
  // expected-error@-1{{actor-isolated distributed instance method 'localThrows()' cannot be used to satisfy nonisolated protocol requirement}}
  distributed func localAsync() async {}
  // expected-error@-1{{actor-isolated distributed instance method 'localAsync()' cannot be used to satisfy nonisolated protocol requirement}}
}
distributed actor OK_StrictlyLocal: StrictlyLocal {
  nonisolated func local() {}
  nonisolated func localThrows() throws {}
  nonisolated func localAsync() async {}
}

protocol Server {
  func send<Message: Codable & Sendable>(message: Message) async throws -> String
}
actor MyServer : Server {
  func send<Message: Codable & Sendable>(message: Message) throws -> String { "" } // OK
}

protocol AsyncThrowsAll {
  func maybe(param: String, int: Int) async throws -> Int
  // expected-note@-1{{'maybe(param:int:)' declared here}}
}

actor LocalOK_AsyncThrowsAll: AsyncThrowsAll {
  func maybe(param: String, int: Int) async throws -> Int { 1111 }
}

actor LocalOK_ImplicitlyThrows_AsyncThrowsAll: AsyncThrowsAll {
  func maybe(param: String, int: Int) async -> Int { 1111 }
}
actor LocalOK_ImplicitlyAsync_AsyncThrowsAll: AsyncThrowsAll {
  func maybe(param: String, int: Int) throws -> Int { 1111 }
}
actor LocalOK_ImplicitlyThrowsAsync_AsyncThrowsAll: AsyncThrowsAll {
  func maybe(param: String, int: Int) -> Int { 1111 }
}

distributed actor Nope1_AsyncThrowsAll: AsyncThrowsAll {
  // expected-note@-1{{add '@preconcurrency' to the 'AsyncThrowsAll' conformance to defer isolation checking to run time}}

  func maybe(param: String, int: Int) async throws -> Int { 111 }
  // expected-error@-1{{distributed actor-isolated instance method 'maybe(param:int:)' cannot be used to satisfy nonisolated protocol requirement}}
  // expected-note@-2{{add 'nonisolated' to 'maybe(param:int:)' to make this instance method not isolated to the actor}}
  // expected-note@-3{{add 'distributed' to 'maybe(param:int:)' to make this instance method satisfy the protocol requirement}}
}

distributed actor OK_AsyncThrowsAll: AsyncThrowsAll {
  distributed func maybe(param: String, int: Int) async throws -> Int { 222 }
}
distributed actor OK_Implicitly_AsyncThrowsAll: AsyncThrowsAll {
  distributed func maybe(param: String, int: Int) -> Int { 333 }
}

func testAsyncThrowsAll(p: AsyncThrowsAll,
                        dap: OK_AsyncThrowsAll,
                        dapi: OK_Implicitly_AsyncThrowsAll) async throws {
  _ = try await p.maybe(param: "", int: 0)
  _ = try await dap.maybe(param: "", int: 0)
  _ = try await dapi.maybe(param: "", int: 0)

  // Such conversion is sound:
  let pp: AsyncThrowsAll = dapi
  _ = try await pp.maybe(param: "", int: 0)
}

// ==== -----------------------------------------------------------------------
// MARK: Distributed actor protocols can have non-dist requirements

protocol TerminationWatchingA {
  func terminated(a: String) async
  // expected-note@-1{{mark the protocol requirement 'terminated(a:)' 'throws' to allow actor-isolated conformances}}
}

protocol TerminationWatchingDA: DistributedActor {
  func terminated(da: String) async // expected-note 3 {{distributed actor-isolated instance method 'terminated(da:)' declared here}}
}

actor A_TerminationWatchingA: TerminationWatchingA {
  func terminated(a: String) { } // ok, since: actor -> implicitly async
}
func test_watching_A(a: A_TerminationWatchingA) async throws {
  await a.terminated(a: "normal")
}

distributed actor DA_TerminationWatchingA: TerminationWatchingA {
  // expected-note@-1{{add '@preconcurrency' to the 'TerminationWatchingA' conformance to defer isolation checking to run time}}

  func terminated(a: String) { }
  // expected-error@-1{{distributed actor-isolated instance method 'terminated(a:)' cannot be used to satisfy nonisolated protocol requirement}}
  // expected-note@-2{{add 'nonisolated' to 'terminated(a:)' to make this instance method not isolated to the actor}}
}

distributed actor DA_TerminationWatchingDA: TerminationWatchingDA {
  distributed func test() {}
  func terminated(da: String) { }
  // expected-note@-1{{distributed actor-isolated instance method 'terminated(da:)' declared here}}
}

func test_watchingDA(da: DA_TerminationWatchingDA) async throws {
  try await da.test() // ok
  da.terminated(da: "the terminated func is not distributed") // expected-error{{only 'distributed' instance methods can be called on a potentially remote distributed actor}}
}

func test_watchingDA<WDA: TerminationWatchingDA>(da: WDA) async throws {
  try await da.terminated(da: "the terminated func is not distributed")
  // expected-error@-1{{only 'distributed' instance methods can be called on a potentially remote distributed actor}}
  // expected-warning@-2{{no calls to throwing functions occur within 'try' expression}}

  let __secretlyKnownToBeLocal = da
  await __secretlyKnownToBeLocal.terminated(da: "local calls are okey!") // OK
  await da.whenLocal { __secretlyKnownToBeLocal in
    await __secretlyKnownToBeLocal.terminated(da: "local calls are okey!") // OK
  }
}

func test_watchingDA_erased(da: DA_TerminationWatchingDA) async throws {
  let wda: any TerminationWatchingDA = da
  try await wda.terminated(da: "the terminated func is not distributed")
  // expected-error@-1{{only 'distributed' instance methods can be called on a potentially remote distributed actor}}
  // expected-warning@-2{{no calls to throwing functions occur within 'try' expression}}

  let __secretlyKnownToBeLocal = wda
  await __secretlyKnownToBeLocal.terminated(da: "local calls are okey!") // OK
  await wda.whenLocal { __secretlyKnownToBeLocal in
    await __secretlyKnownToBeLocal.terminated(da: "local calls are okey!") // OK
  }
}

func test_watchingDA_any(da: any TerminationWatchingDA) async throws {
  try await da.terminated(da: "the terminated func is not distributed")
  // expected-error@-1{{only 'distributed' instance methods can be called on a potentially remote distributed actor}}
  // expected-warning@-2{{no calls to throwing functions occur within 'try' expression}}
}

// ==== ------------------------------------------------------------------------
// MARK: Distributed Actor requiring protocol witnessing async throws requirements

struct Salsa: Codable, Sendable {}

protocol TacoPreparation {
  func makeTacos(with salsa: Salsa) async throws
}

protocol DistributedTacoMaker: DistributedActor, TacoPreparation {
}

extension DistributedTacoMaker {
  distributed func makeTacos(with: Salsa) {}
}

extension TacoPreparation {
  distributed func makeSalsa() -> Salsa {}
  // expected-error@-1{{'distributed' method can only be declared within 'distributed actor'}}
}

distributed actor TacoWorker: DistributedTacoMaker {} // implemented in extensions

extension DistributedTacoMaker where SerializationRequirement == Codable {
  distributed func makeGreatTacos(with: Salsa) {}
}