Enabling complete concurrency checking in a module can yield many data-race safety issues reported by the compiler. Hundreds, possibly even thousands of warnings are not uncommon. When faced with a such a large number of problems, especially if you are just beginning to learn about Swift’s data isolation model, this can feel insurmountable.
Why Swift 6
Concurrency was introduced in Swift 5.5 in 2021. Since then, It has been programmer's responsibility to ensure that our apps are safe from Data Race. Enter into the discussion - Swift 6!
- Compiler's enforce concurrent checks (at compile time)
- Data Race Safety
In Swift 6, static Data Race Safety can be done at compile-time - meaning that we can find potential data-race issues before shipping the product. Furthermore, adopting Data Race Safety checks ensures that future changes to your code stay safe.
Oh by the way, if you will take the hint! Since Swift 5.5, it seems that the elders of Swift community put more and more emphasis on Asynchronous and Concurrency programming. We'd better go with the flow.
Convinced, yet?
Steps to Migration
First off, wise words from Paul Cohen from Apple's WWDC 2024: "Don't Panic"
Next, you will have to be brave. Very Brave.
Then, perhaps, these steps can help:
- Get Xcode 16 with Swift 6 compiler
- Make sure your code compiled correctly with Swift 5
- (Optional: Enable Swift 5 Individual Concurrency Feature)
- Enable complete (concurrent) checking (still with Swift 5)
- (Optional: Think - YOLO! and man up)
- Enable Swift 6
- Audit unsafe opt-outs
Optional: Enable Swift 5 Individual Concurrency Feature
This can be done individually in any order.
- Remove Actor Isolation Inference caused by Property Wrappers, see flag:
DisableOutwardActorInference
- Strict concurrency for global variables, see flag:
GlobalConcurrency
- Inferring Sendable for methods and key path literals, see flag:
InferSendableFromCaptures
To activate each, visit Build's Setting of each target, and search for each flag, i.e. "DisableOutwardActorInference".
For more info, see: Swift.org's Migrating to Swift 6
Enable complete (concurrent) checking
- Done while using Swift 5 compiler (before switching to Swift 6)
- Per target setting
- Compiler will start to give warning of anything that would fail when switching to Swift 6's enforced data isolation. Note that these are just warning.
- Start changing/refactoring the code to remove the warnings
Enable Swift 6
- Full data-isolation enforcement/checking
- Should be done after eliminating all warnings
- Prevent future code or refactoring to use unsafe data-isolation
Audit unsafe opt-outs
- During the 2 previous steps, there might be some opt-out to make app compiled
- This step is to refactoring or re-write the code to be data-isolation safe
Problem Solving
References
- WWDC24: Migrate your app to Swift 6
- WWDC24: What's new in Swift 6
- Swift.org's Migrating to Swift 6
- Hacking with Swift: What's new in Swift 6.0
No comments:
Post a Comment