Skip to content

Commit 5f2b6ae

Browse files
committed
Add a few more explanations to the code examples in the data race safety
section.
1 parent 4e3974b commit 5f2b6ae

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

_posts/NNNN-NN-NN-swift-5.10-released.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ For example, in Swift 5.9, the following code fails an isolation assertion at ru
2929
```swift
3030
@MainActor
3131
class MyModel {
32-
init() {
32+
private init() {
3333
MainActor.assertIsolated()
3434
}
3535

@@ -43,7 +43,7 @@ func useShared() async {
4343
await useShared()
4444
```
4545

46-
The above code admits data races. `MyModel.shared` is a `@MainActor`-isolated static variable, which evaluates a `@MainActor`-isolated initial value upon first access. `MainActor.shared` is accessed synchronously from a `nonisolated` context inside the `useShared()` function, so the initial value is computed off the main actor. In Swift 5.10, compiling the code with `-strict-concurrency=complete` produces a warning that the access must be done asynchronously:
46+
The above code admits data races. `MyModel.shared` is a `@MainActor`-isolated static variable, which evaluates a `@MainActor`-isolated initial value upon first access. `MyModel.shared` is accessed synchronously from a `nonisolated` context inside the `useShared()` function, so the initial value is computed off the main actor. In Swift 5.10, compiling the code with `-strict-concurrency=complete` produces a warning that the access must be done asynchronously:
4747

4848
```
4949
warning: expression is 'async' but is not marked with 'await'
@@ -52,6 +52,8 @@ The above code admits data races. `MyModel.shared` is a `@MainActor`-isolated st
5252
await
5353
```
5454

55+
The possible fixes for resolving the data race are 1) access `MyModel.shared` asynchronously using `await`, 2) make `MyModel.init` and `MyModel.shared` both `nonisolated` and move the code that requires the main actor into a separate isolated method, or 3) isolated `useShared()` to `@MainActor`.
56+
5557
You can find more details about the changes and additions to the full data isolation programming model in the [Swift 5.10 release notes](https://github.com/apple/swift/blob/release/5.10/CHANGELOG.md).
5658

5759
### Unsafe opt-outs
@@ -60,7 +62,7 @@ Unsafe opt-outs, such as `@unchecked Sendable` conformances, are important for c
6062

6163
Swift 5.10 introduces a new `nonisolated(unsafe)` keyword to opt out of actor isolation checking for stored properties and variables. `nonisolated(unsafe)` can be used on any form of storage, including stored properties, local variables, and static or global variables.
6264

63-
`nonisolated(unsafe)` can be used as a more granular opt-out for `Sendable` checking, eliminating the need for `@unchecked Sendable` wrapper types in many use cases:
65+
`nonisolated(unsafe)` can be used as a more granular opt-out for `Sendable` checking, eliminating the need for `@unchecked Sendable` conformances in many use cases. For example, the following code defines a `MyModel` class that contains a non-`Sendable` stored property `protectedState` with all access to `protectedState` guarded by a dispatch queue. `MyModel` can still use a checked `Sendable` conformance by applying `nonisolated(unsafe)` to `protectedState` so that all other properties are checked by the compiler:
6466

6567
```swift
6668
import Dispatch

0 commit comments

Comments
 (0)