|
61 | 61 |
|
62 | 62 | ## Swift 5.10
|
63 | 63 |
|
| 64 | +* Swift 5.10 closes all known static data-race safey holes in complete strict |
| 65 | +concurrency checking. |
| 66 | + |
| 67 | + When writing code against `-strict-concurrency=complete`, Swift 5.10 will |
| 68 | + diagnose all potential for data races at compile time unless an explicit |
| 69 | + unsafe opt out, such as `nonisolated(unsafe)` or `@unchecked Sendable`, is |
| 70 | + used. |
| 71 | + |
| 72 | + For example, in Swift 5.9, the following code crashes at runtime due to a |
| 73 | + `@MainActor`-isolated initializer being evaluated outside the actor, but it |
| 74 | + was not diagnosed under `-strict-concurrency=complete`: |
| 75 | + |
| 76 | + ```swift |
| 77 | + @MainActor |
| 78 | + class MyModel { |
| 79 | + init() { |
| 80 | + MainActor.assertIsolated() |
| 81 | + } |
| 82 | + |
| 83 | + static let shared = MyModel() |
| 84 | + } |
| 85 | + |
| 86 | + func useShared() async { |
| 87 | + let model = MyModel.shared |
| 88 | + } |
| 89 | + |
| 90 | + await useShared() |
| 91 | + ``` |
| 92 | + |
| 93 | + The above code admits data races because a `@MainActor`-isolated static |
| 94 | + variable, which evaluates a `@MainActor`-isolated initial value upon first |
| 95 | + access, is accessed synchronously from a `nonisolated` context. In Swift |
| 96 | + 5.10, compiling the code with `-strict-concurrency=complete` produces a |
| 97 | + warning that the access must be done asynchronously: |
| 98 | + |
| 99 | + ``` |
| 100 | + warning: expression is 'async' but is not marked with 'await' |
| 101 | + let model = MyModel.shared |
| 102 | + ^~~~~~~~~~~~~~ |
| 103 | + await |
| 104 | + ``` |
| 105 | + |
| 106 | + Swift 5.10 fixed numerous other bugs in `Sendable` and actor isolation |
| 107 | + checking to strengthen the guarantees of complete concurrency checking. |
| 108 | + |
| 109 | + Note that the complete concurrency model in Swift 5.10 is conservative. |
| 110 | + Several Swift Evolution proposals are in active development to improve the |
| 111 | + usability of strict concurrency checking ahead of Swift 6. |
| 112 | + |
64 | 113 | * [SE-0412][]:
|
65 | 114 |
|
66 | 115 | 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.
|
@@ -9998,4 +10047,4 @@ using the `.dynamicType` member to retrieve the type of an expression should mig
|
9998 | 10047 | [#57225]: <https://github.com/apple/swift/issues/57225>
|
9999 | 10048 | [#56139]: <https://github.com/apple/swift/issues/56139>
|
10000 | 10049 | [#70065]: <https://github.com/apple/swift/pull/70065>
|
10001 |
| -[swift-syntax]: https://github.com/apple/swift-syntax |
| 10050 | +[swift-syntax]: https://github.com/apple/swift-syntax |
0 commit comments