|
5 | 5 |
|
6 | 6 | ## Swift 5.10
|
7 | 7 |
|
| 8 | +* Swift 5.10 closes all known static data-race safey holes in complete strict |
| 9 | +concurrency checking. |
| 10 | + |
| 11 | + When writing code against `-strict-concurrency=complete`, Swift 5.10 will |
| 12 | + diagnose all potential for data races at compile time unless an explicit |
| 13 | + unsafe opt out, such as `nonisolated(unsafe)` or `@unchecked Sendable`, is |
| 14 | + used. |
| 15 | + |
| 16 | + For example, in Swift 5.9, the following code crashes at runtime due to a |
| 17 | + `@MainActor`-isolated initializer being evaluated outside the actor, but it |
| 18 | + was not diagnosed under `-strict-concurrency=complete`: |
| 19 | + |
| 20 | + ```swift |
| 21 | + @MainActor |
| 22 | + class MyModel { |
| 23 | + init() { |
| 24 | + MainActor.assertIsolated() |
| 25 | + } |
| 26 | + |
| 27 | + static let shared = MyModel() |
| 28 | + } |
| 29 | + |
| 30 | + func useShared() async { |
| 31 | + let model = MyModel.shared |
| 32 | + } |
| 33 | + |
| 34 | + await useShared() |
| 35 | + ``` |
| 36 | + |
| 37 | + The above code admits data races because a `@MainActor`-isolated static |
| 38 | + variable, which evaluates a `@MainActor`-isolated initial value upon first |
| 39 | + access, is accessed synchronously from a `nonisolated` context. In Swift |
| 40 | + 5.10, compiling the code with `-strict-concurrency=complete` produces a |
| 41 | + warning that the access must be done asynchronously: |
| 42 | + |
| 43 | + ``` |
| 44 | + warning: expression is 'async' but is not marked with 'await' |
| 45 | + let model = MyModel.shared |
| 46 | + ^~~~~~~~~~~~~~ |
| 47 | + await |
| 48 | + ``` |
| 49 | + |
| 50 | + Swift 5.10 fixed numerous other bugs in `Sendable` and actor isolation |
| 51 | + checking to strengthen the guarantees of complete concurrency checking. |
| 52 | + |
| 53 | + Note that the complete concurrency model in Swift 5.10 is conservative. |
| 54 | + Several Swift Evolution proposals are in active development to improve the |
| 55 | + usability of strict concurrency checking ahead of Swift 6. |
| 56 | + |
8 | 57 | * [SE-0412][]:
|
9 | 58 |
|
10 | 59 | Under strict concurrency checking, every global or static variable must be either isolated to a global actor or be both immutable and of `Sendable` type.
|
|
0 commit comments