Skip to content

Commit 0f1df57

Browse files
committed
Reframe the isolation inference section to make it clear that Swift
is the subject performing inference.
1 parent 5e5349e commit 0f1df57

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

TSPL.docc/LanguageGuide/Concurrency.md

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,16 +1394,15 @@ as described in <doc:Attributes#globalActor>.
13941394

13951395
### Isolation inference
13961396

1397-
Global actor isolation can be inferred in the code you write. Isolation
1398-
is inferred from class inheritance, protocol conformances, and context
1399-
where code is written.
1397+
Swift infers global actor isolation in the code you write based on
1398+
class inheritance and protocol conformances.
14001399

14011400
#### Classes
14021401

1403-
If a superclass is isolated to a global actor, the global actor is
1404-
inferred on subclasses. For example, the code below has a main-actor
1402+
If a superclass has global actor isolation, Swift infers that global actor
1403+
on subclasses. For example, the code below has a main-actor
14051404
isolated class `Vehicle`, and a subclass `Train` that inherits
1406-
main-actor isolation:
1405+
from `Vehicle`:
14071406

14081407
```swift
14091408
@MainActor
@@ -1421,12 +1420,13 @@ class Train: Vehicle {
14211420
}
14221421
```
14231422

1424-
In the above example, `Vehicle` and all of its methods and properties
1425-
are isolated to the main actor. The `Train` subclass infers its isolation
1426-
from the superclass, so the overridden `makeNoise` method is also isolated
1427-
to the main actor.
1423+
In the above example, all methods and properties in `Vehicle`
1424+
are isolated to the main actor. The `Train` class inherits all
1425+
methods, properties, and global actor isolation from the `Vehicle`
1426+
superclass, so Swift infers main-actor isolation for the `makeNoise`
1427+
override.
14281428

1429-
Global actor isolation is also inferred from individual overridden
1429+
Swift also infers global-actor isolation from individual overridden
14301430
methods. For example, the following code isolates one method of the
14311431
`Vehicle` class to the main actor instead of the entire class:
14321432

@@ -1447,15 +1447,17 @@ class Train: Vehicle {
14471447
}
14481448
```
14491449

1450-
The override of `makeNoise` in `Train` infers main-actor isolation from
1451-
the overridden `makeNoise` method in `Vehicle`.
1450+
Swift infers main-actor isolation for the `makeNoise` override of
1451+
the `Train` subclass based on the isolation of the `makeNoise` method
1452+
in `Vehicle`.
14521453

14531454
#### Protocols
14541455

1455-
If a protocol is isolated to a global actor, the global actor is
1456-
inferred on conforming types. For example, the following code has
1457-
a main-actor isolated protocol `Togglable`, and a conforming struct
1458-
`Switch`:
1456+
Swift infers global-actor isolation from protocol conformances.
1457+
When a type conforms to a protocol, Swift infers actor isolation from
1458+
the protocol itself, and from individual protocol requirements.
1459+
For example, the following code has a main-actor isolated protocol
1460+
`Togglable`, and a conforming struct `Switch`:
14591461

14601462
```swift
14611463
@MainActor
@@ -1473,12 +1475,13 @@ struct Switch: Togglable {
14731475
```
14741476

14751477
In the above example, `Togglable` and all of its requirements
1476-
are isolated to the main actor. The `Switch` type infers its isolation
1477-
from the protocol conformance, and the implementation of `toggle` is
1478-
isolated to the main actor.
1478+
are isolated to the main actor. Swift infers main-actor isolation
1479+
on types that conform to `Togglable`, so all methods and properties
1480+
of `Switch` are isolated to the main actor, including the `isOn`
1481+
property and the `toggle` method.
14791482

1480-
Isolation is only inferred from protocols when the conformance is
1481-
written at the primary declaration. If the conformance is written in
1483+
Swift only infers isolation from protocols when you write the conformance
1484+
at the primary declaration. If you write the conformance in
14821485
an extension, then isolation inference only applies to requirements that
14831486
are implemented in the extension. For example, the following code
14841487
implements a conformance of `Switch` to `Togglable` in an extension:
@@ -1500,9 +1503,12 @@ extension Switch: Togglable {
15001503
}
15011504
```
15021505

1503-
Now, the `Switch` type itself does not have inferred isolation. The
1504-
`toggle` method inside `Switch` is isolated to the main actor, because
1505-
the protocol requirement that it implements is isolated to the main actor.
1506+
Swift does not infer global-actor isolation on the `Switch` type itself;
1507+
the `Switch` type is `nonisolated`, and the methods and properties directly
1508+
inside the type are `nonisolated`. Swift infers global-actor isolation for
1509+
the protocol requirements implemented in the extension that declares the
1510+
conformance to `Togglable`, so the `toggle` method is isolated to the
1511+
main actor.
15061512

15071513

15081514
<!--

0 commit comments

Comments
 (0)