Wednesday, March 6, 2024

Swift 5.10

Holly Borla (Mastodon):

Swift 5.10 accomplishes full data isolation in the concurrency language model. This important milestone has taken years of active development over many releases. The concurrency model was introduced in Swift 5.5 including async/await, actors, and structured concurrency. Swift 5.7 introduced Sendable as the fundamental concept for thread-safe types whose values can be shared across arbitrary concurrent contexts without introducing a risk of data races. And now, in Swift 5.10, full data isolation is enforced at compile time in all areas of the language when the complete concurrency checking option is enabled.

Full data isolation in Swift 5.10 sets the stage for the next major release, Swift 6. The Swift 6.0 compiler will offer a new, opt-in Swift 6 language mode that will enforce full data isolation by default, and we will embark upon the transition to eliminate data races across all software written in Swift.

[…]

When building code with the compiler flag -strict-concurrency=complete, Swift 5.10 will diagnose the potential for data races at compile time except where an explicit unsafe opt-out, such as nonisolated(unsafe) or @unchecked Sendable, is used.

[…]

Previously:

Update (2024-03-07): Rob Jonson:

Swift 5.10 with full concurrency checking warns for most of the @MainActor failures I have found in the past.

But you can still accidentally call a @MainActor function from a background thread if you use ObjectiveC or Framework code like Notifications.

Jesse Squires:

Swift classes with isolation annotations behave differently when inheriting from an ObjC superclass.

Donny Wals:

With Swift 5.10, Apple has managed to close some large gaps that existed in Swift Concurrency’s data safety features. In short, this means that the compiler will be able to catch more possible thread safety issue by enforcing actor isolation and Sendability in more places.

Let’s take a look at the two features that make this possible.

Wade Tregaskis:

I really wish Swift releases would stop having major regressions in the ability to parse non-trivial expressions. The installation of a new version of Xcode – with a corresponding new Swift version & toolchain – should be a joyous occasion, not one I increasingly dread.

Update (2024-03-08): Rob Napier (Mastodon):

In the following simplified code, we’re getting stuck with MainActor being both forbidden and required. We have a ViewModifier that observes a ObservableObject. Therefore it must be MainActor (and in fact, becomes MainActor automatically due to the ObservedObject wrapper).

However, it is used within a ButtonStyle. ButtonStyle requires a non-isololated makeBody method. But if it’s nonisolated, then it can’t access the ViewModifier. Under “complete” concurrency, the following either generates “Call to main actor-isolated initializer ‘init()’ in a synchronous nonisolated context” or “Main actor-isolated instance method ‘makeBody(configuration:)’ cannot be used to satisfy nonisolated protocol requirement.”

Holly Borla:

Yeah, the recommended way to do this is using MainActor.assumeIsolated.

1 Comment RSS · Twitter · Mastodon

Wake me up when "it just works".

It should probably happen during the year of Linux on Desktop.

Leave a Comment