Skip to content

Commit 77dbc93

Browse files
committed
Update the examples for 'nonisolated' on mutable storage.
1 parent 68481e1 commit 77dbc93

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

proposals/NNNN-nonisolated-for-global-actor-cutoff.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -221,36 +221,41 @@ Because `MyClass` does not conform to `Sendable`, it cannot be accessed from mul
221221
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:
222222

223223
```swift
224-
struct S {
225-
var x: Int = 0 // okay ('nonisolated' is inferred within the module)
224+
protocol P {
225+
@MainActor var x: Int { get }
226226
}
227227

228-
actor MyActor {
229-
func test(s: S) {
230-
print(s.x) // synchronous access to 'x' after sending `S` to `MyActor` is okay.
228+
struct S: 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
231233
}
232234
}
233235
```
234236

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.
236240

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:
238242

239243
```swift
240244
// In Module A
241-
public struct S: Sendable {
242-
nonisolated public var x: Int = 0 // okay
243-
public init() {}
245+
public protocol P {
246+
@MainActor var x: Int { get }
244247
}
245248
```
246249

247250
```swift
248251
// In Module B
249252
import A
250253

251-
actor MyActor {
252-
func test(s: S) {
253-
print(s.x) // synchronous access to 'x' after sending `S` to `MyActor` is okay.
254+
struct S: P {
255+
nonisolated var x: Int // 'nonisolated' is explicitly spelled
256+
257+
init(x: Int) {
258+
self.x = x // x is non-isolated
254259
}
255260
}
256261
```

0 commit comments

Comments
 (0)