Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Swift 5.10 Brings Full Data Isolation Compile-Time Safety to Concurrent Code

Swift 5.10 Brings Full Data Isolation Compile-Time Safety to Concurrent Code

This item in japanese


The latest Swift release, Swift 5.10, includes only a few new proposals. However, this update represents an important achievement for the language concurrency model, which can now ensure full data isolation at the compiler level, explains Swift team engineer Holly Borla.

As Borla recounts, the Swift concurrency model has matured since the introduction of async/await, actors, and structured concurrency in Swift 5.5, through the adoption of the Sendable protocol as the cornerstone of thread-safe types in Swift 5.7, up to compile-time full data isolation in Swift 5.10.

Swift 5.10 rounds out the data-race safety semantics in all corners of the language, and fixes numerous bugs in Sendable and actor isolation checking to strengthen the guarantees of complete concurrency checking.

Indeed, the Swift 5.10 compiler adds a new flag -strict-concurrency=complete that will make it detect potential data races at compile time. This means that isolation violations that were detected at runtime and produced an assert when using Swift 5.9 are now flagged as potential programming errors by the 5.10 compiler and raise a warning.

The new stricter behavior includes the possibility of false positives, meaning correct code that will be mistakenly flagged by the compiler. As Borla remarks, this is basically due to the need to reduce the amount of analysis the compiler does, which costs compilation time and will be improved in future releases of the language.

To handle such cases, full data isolation checks can be disabled locally by using nonisolated(unsafe) or @unchecked Sendable for specific actors or classes implementing the Sendable protocol. Of course, this will bring your program to unsafe land, so you better know what you are doing when opting out of full data isolation checks. As iOS developer and Swift book author Donny Wals suggests:

Whenever you’re tempted to use nonisolated(unsafe) you should always ask yourself whether it’s possible for you to actually make the type you’re marking isolated to a global actor or maybe you can make the type of the property Sendable and immutable.

Using immutable state along with Sendable or encapsulating shared state in a global actor using @MainActor are the only two ways you can access shared state concurrently without the Swift 5.10 compiler considering it unsafe.

One notable case where the unsafe opt-out directives are necessary is when you are compiling legacy code using semaphores of DispatchQueues to manually ensure isolation.

As mentioned, Swift 5.10 concurrency model is not yet perfect, in that it may generate too many data-race false positives. According to Borla, the Swift team is actively working on improving it for the Swift 6 release, which is likely to be announced at next Apple WWDC conference.

About the Author

Rate this Article