You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: proposals/NNNN-nonisolated-for-global-actor-cutoff.md
+18-13Lines changed: 18 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -221,36 +221,41 @@ Because `MyClass` does not conform to `Sendable`, it cannot be accessed from mul
221
221
For global-actor-isolated value types, [SE-0434: Usability of global-actor-isolated types](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0434-global-actor-isolated-types-usability.md) allows accessing `var` stored properties with `Sendable` type from within the module as `nonisolated`. This proposal extends this rule to **all**`Sendable` value types:
222
222
223
223
```swift
224
-
structS {
225
-
var x: Int=0// okay ('nonisolated' is inferred within the module)
224
+
protocolP {
225
+
@MainActorvar x: Int{ get }
226
226
}
227
227
228
-
actorMyActor {
229
-
functest(s: S) {
230
-
print(s.x) // synchronous access to 'x' after sending `S` to `MyActor` is okay.
228
+
structS: P {
229
+
var x: Int// 'nonisolated' is inferred within the module
230
+
231
+
init(x: Int) {
232
+
self.x= x // nonisolated access of x is okay
231
233
}
232
234
}
233
235
```
234
236
235
-
In the above code, the value type `S` is implicitly `Sendable` within the module and its storage `x` is of `Sendable` type `Int`. When `Sendable` value types are passed between isolation domains, each isolation domain has an independent copy of the value. Accessing properties stored on a value type from across isolation domains is safe as long as the stored property type is also `Sendable`. Even if the stored property is a `var`, assigning to the property will not risk a data race, because the assignment cannot have effects on copies in other isolation domains. Therefore, synchronous access of `x` from within the module is okay.
237
+
In the above code, the value type `S` is implicitly `Sendable` and its protocol requirement stored property `x` is of `Sendable` type `Int`. While the protocol `P` requires `x` to be globally isolated,
238
+
under this proposal, the witness `x` is treated as non-isolated within the module.
239
+
When `Sendable` value types are passed between isolation domains, each isolation domain has an independent copy of the value. Accessing properties stored on a value type from across isolation domains is safe as long as the stored property type is also `Sendable`. Even if the stored property is a `var`, assigning to the property will not risk a data race, because the assignment cannot have effects on copies in other isolation domains. Therefore, synchronous access of `x` is okay.
236
240
237
-
Additionally, [SE-0434](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0434-global-actor-isolated-types-usability.md) allows explicitly annotating globally-isolated value types' properties such as `x` in the previous example with `nonisolated` for synchronous access from outside the module. This proposal extends this rule to **all**`Sendable` value types:
241
+
Additionally, [SE-0434](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0434-global-actor-isolated-types-usability.md) allows explicitly annotating globally-isolated value types' properties such as `x` in the previous example with `nonisolated` for enabling synchronous access from outside the module. This proposal extends this rule to **all**`Sendable` value types:
238
242
239
243
```swift
240
244
// In Module A
241
-
publicstructS: Sendable {
242
-
nonisolatedpublicvar x: Int=0// okay
243
-
publicinit() {}
245
+
publicprotocolP {
246
+
@MainActorvar x: Int { get }
244
247
}
245
248
```
246
249
247
250
```swift
248
251
// In Module B
249
252
importA
250
253
251
-
actorMyActor {
252
-
functest(s: S) {
253
-
print(s.x) // synchronous access to 'x' after sending `S` to `MyActor` is okay.
254
+
structS: P {
255
+
nonisolatedvar x: Int// 'nonisolated' is explicitly spelled
0 commit comments