Home Articles

Enabling Strict Concurrency in Swift 6

Note

Good news! Apple has released a new Xcode version Xcode 16

Xcode Releases

Xcode Releases

Xcode 16 and Swift 6 Compiler

Swift 6 compiler, is the 1st version of Swift 6 language mode means, when you install Xcode 16, you get the new llvm compiler and you get Swift 6 language support with it.

We can check your version by running swiftc –version in terminal. Because Swift 6 language mode – is an opting feature (not a mandatory one).

Xcode 16 Compiler and Language Version

Xcode 16 Compiler and Language Version

In my mac I have installed Xcode 16.0, here it includes:

  • Swift version 6.0 —> Language Version

  • swiftlang-6.0.0.9.10 clang-1600.0.26.2 —> Compiler Version

Compiler version vs Language version

Compiler version vs Language version

You may think everything is the same like the past releases, what's new here??,

This new compiler version in Xcode 16 back supports to Swift 5 and even Swift 4.2 versions

  • If your current project is running on swift 4.2

  • when you move to Xcode 16, you can still continue using swift 4.2 version

When you create a new project or package in xcode 16, The default language version assigned is Swift 5. Yes!, because Swift 6 language mode – is an opting feature (not a mandatory one)
And when you want to update your project to Swift6 language mode, this is where the break-in changes happen 🙈

Holly Bolra Tweet About Swift6 Opt in Mode

Holly Bolra Tweet About Swift6 Opt in Mode

How to enable Swift 6 upcoming features?

Note

Either in your build settings or package manifest

You can begin leveraging new features from Swift 6, such as strict concurrency checking, without fully upgrading your project to Swift 6. This process, known as Incremental adoption,

  • It allows you to gradually implement these updates rather than adopting all the changes at once.

  • To enable new language features, navigate to your project's build settings, search for Swift Compiler - Upcoming Features, and set the features you want to adopt to Yes.

  • Lets you integrate Swift 6 features progressively, ensuring a smoother transition.

For Swift Package Manager (SPM) packages, you can achieve the same incremental adoption using the .enableUpcomingFeature API.

  • Allows you to specify and enable individual features that you want to integrate into your package.

  • By using this approach, you can selectively update your package with new Swift 6 features while maintaining compatibility with earlier versions.

Enabling Swift 6 upcoming features

Enabling Swift 6 upcoming features

How to update the language version?

Note

Either in your build settings or package manifest

To upgrade your project to Swift 6 language mode, navigate to your build settings and select Swift Compiler - Language > Swift Language Version. This upgrade enables all of the new features introduced in Swift 6, including strict concurrency checking.

Note: After upgrading, you may notice that a few features planned for future Swift versions are still listed but disabled by default in Swift 6. For example, the ExistentialAny (SE-335), which was initially planned for Swift 6, was postponed by the Language Steering Group and is not enabled by default in this version.

For Swift Package Manager (SPM) packages, don’t confuse the swift-tools-version with the Swift language version. The swift-tools-version specified at the top of your Package.swift Package.swift file only sets the minimum Swift version required to build the package. To upgrade your package to Swift 6, you need to explicitly adopt the following Swift setting:

.swiftLanguageVersion(.v6)

Swift Language Version

Update Swift Language Version

Update Swift Language Version

How to enable the strict concurrency checking?

Note

Either in your build settings or package manifest

If you only want to enable strict concurrency checking, you can update your build settings accordingly. Some of you might have already experimented with this, as it was available in Xcode 15 for projects using Swift 5.10. Has anyone tried it in Xcode 15 yet?

There are three levels of concurrency checking to choose from:

  1. Minimal: This enforces Sendable constraints only where they’ve been explicitly adopted and performs actor-isolation checks for code that has adopted concurrency.

  1. Targeted: This level performs actor-isolation checks and applies Sendable constraints to explicitly adopted code.

  1. Completed: Enforces Sendable constraints and actor-isolation checks across the entire project or module.

Each step introduces stricter checking, potentially leading to more warnings. It’s important to proceed gradually, adopting each level one at a time. After resolving the warnings for each level, you can open a pull request and move on to the next level.

Enable Strict Concurrency Check

Enable Strict Concurrency Check

This is one of the codebase, where I initially tested the Swift 6 with Xcode 16 beta.

Before and After Swift 6

Before and After Swift 6

  • Before doing anything the project had 20 warnings that’s the 1st screenshot in left,

  • When I enabled the strict concurrency check at the app target, it was 400+ warnings

  • When I enabled in all the targets, with few local modules it was 1600+ warnings,

  • That’s nice, right? No sign of any peace 😂

This is a free third party commenting service we are using for you, which needs you to sign in to post a comment, but the good bit is you can stay anonymous while commenting.