Skip to content

Commit 3012e21

Browse files
committed
Clarify examples per feedback.
1 parent 77dbc93 commit 3012e21

File tree

1 file changed

+57
-11
lines changed

1 file changed

+57
-11
lines changed

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

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,27 @@ struct A: Refined {
127127
}
128128
```
129129

130-
In the above code, the protocol `Refined` is refining the `GloballyIsolated` protocol, but is declared non-isolated. This means that the `Refined` still has the same requirements as `GloballyIsolated`, but they are not isolated. Therefore, a struct `A` conforming to it is also non-isolated, which allows the programmer for more flexibility when implementing the requirements of a protocol.
130+
In the above code, the protocol `Refined` is refining the `GloballyIsolated` protocol, but is declared non-isolated. This means that the `Refined` still has the same requirements as `GloballyIsolated`, but they are not isolated. Therefore, a struct `A` conforming to it is also non-isolated, which allows the programmer for more flexibility when implementing the requirements of a protocol.
131131

132132
#### Extensions
133133

134+
Today, it is possible for extensions to be globally-isolated:
135+
136+
```swift
137+
struct X {}
138+
139+
@MainActor extension X {
140+
func f() {} // implicitly globally-isolated
141+
var x: Int { get { 1 } } // implicitly globally-isolated
142+
}
143+
```
144+
145+
In the above code, `X` is a non-isolated struct, and extension members
146+
`f()` and `x` are globally-isolated.
147+
148+
However, if `X` was globally-isolated, before this proposal, the only way to stop extension members from inferring the global actor would be to mark every extension member with
149+
`nonisolated`.
150+
134151
This proposal allows for `nonisolated` attribute to be applied on extension declarations:
135152

136153
```swift
@@ -147,7 +164,7 @@ struct C: GloballyIsolated {
147164
}
148165
```
149166

150-
In the code above, the `nonisolated` attribute is applied to an extension declaration for a `GloballyIsolated` protocol. When applied to an extension, `nonisolated` applies to all of its members. In this case, `implicitlyNonisolated` method and the computed property `x` are both nonisolated, and therefore are able to be accessed from a nonisolated context in the body of `explicitlyNonisolated` method of a globally-isolated struct `C`.
167+
In the code above, the `nonisolated` attribute is applied to an extension declaration for a `GloballyIsolated` protocol. When applied to an extension, `nonisolated` applies to all of its members. In this case, `implicitlyNonisolated` method and the computed property `x` are both nonisolated, and therefore are able to be accessed from a nonisolated context in the body of `explicitlyNonisolated` method of a globally-isolated struct `C`.
151168

152169
#### Classes, structs, and enums
153170

@@ -222,14 +239,16 @@ For global-actor-isolated value types, [SE-0434: Usability of global-actor-isola
222239

223240
```swift
224241
protocol P {
225-
@MainActor var x: Int { get }
242+
@MainActor var y: Int { get }
226243
}
227244

228245
struct S: P {
229-
var x: Int // 'nonisolated' is inferred within the module
246+
var y: Int // 'nonisolated' is inferred within the module
247+
}
230248

231-
init(x: Int) {
232-
self.x = x // nonisolated access of x is okay
249+
struct F {
250+
nonisolated func getS(_ s: S) {
251+
let x = s.y // okay
233252
}
234253
}
235254
```
@@ -243,19 +262,46 @@ Additionally, [SE-0434](https://github.com/swiftlang/swift-evolution/blob/main/p
243262
```swift
244263
// In Module A
245264
public protocol P {
246-
@MainActor var x: Int { get }
265+
@MainActor var y: Int { get }
266+
}
267+
268+
public struct S: P {
269+
public nonisolated var y: Int // 'y' is explicitly non-isolated
247270
}
248271
```
249272

250273
```swift
251274
// In Module B
252275
import A
253276

254-
struct S: P {
255-
nonisolated var x: Int // 'nonisolated' is explicitly spelled
277+
struct F {
278+
nonisolated func getS(_ s: S) {
279+
let x = s.y // okay
280+
}
281+
}
282+
```
283+
284+
In contrast, `y` is still treated as globally-isolated without the explicit
285+
`nonisolated` attribute:
286+
287+
```swift
288+
// In Module A
289+
public protocol P {
290+
@MainActor var y: Int { get }
291+
}
292+
293+
public struct S: P {
294+
public var y: Int // globally-isolated outside of the module
295+
}
296+
```
297+
298+
```swift
299+
// In Module B
300+
import A
256301

257-
init(x: Int) {
258-
self.x = x // x is non-isolated
302+
struct F {
303+
nonisolated func getS(_ s: S) {
304+
let x = s.y // error: main actor-isolated property 'y' can not be referenced from a nonisolated context
259305
}
260306
}
261307
```

0 commit comments

Comments
 (0)