In Swift, how does the language mode differ from the compiler version, and what does moving a module to Swift 6 mode change?
answer
- one toolchain, several dialects
- -swift-version or -language-mode
- modes 4, 4.2, 5 and 6
- only source-breaking changes wait
- #file becomes #fileID
basics
~20 sThe compiler version is the toolchain, such as Swift 6.4; the language mode is the dialect it compiles each module in: 4, 4.2, 5 or 6. Swift 6 mode turns on its scheduled source-breaking changes, most visibly data-race errors.
solid answer
~40 sThe **compiler version** is the toolchain, for example `swiftc` from Swift 6.4. The **language mode** is the source-compatibility dialect it applies to one module, chosen with `-swift-version 6` or, since Swift 6.1, the same option spelled `-language-mode 6`. The 6.4 compiler accepts modes `4`, `4.2`, `5` and `6`, and plain `swiftc` uses Swift 5 mode. Additive features such as typed throws or `InlineArray` arrive with the compiler and work in every mode; a mode only gates *source-breaking* changes. Swift 6 mode switches on the upcoming features scheduled for it: complete concurrency checking with data-race errors, region-based isolation, `#file` meaning `#fileID` and others. Warnings that end "this is an error in the Swift 6 language mode" become errors. The mode is set per module, and modules in different modes link together.
code
swift · 12 lines// Sources/Billing/main.swift in the module Billing
func trace(_ message: String, file: String = #file, line: Int = #line) -> String {
"[\(file):\(line)] \(message)"
}
print(trace("invoice saved"))
#if swift(>=6.0)
print("language mode: 6")
#else
print("language mode: 5 or earlier")
#endifgo deeper
Recall that the toolchain version and the language mode are separate numbers, and that the Swift 6 compiler still builds code in Swift 5 mode.
Explain that a mode gates only source-breaking changes, name several things Swift 6 mode turns on, and show how -swift-version or -language-mode picks the mode per module.
Read warnings ending 'this is an error in the Swift 6 language mode' as a migration backlog, and move modules one at a time because modes interoperate across module boundaries.
Weigh flipping each module to Swift 6 mode against staying in Swift 5 mode with chosen upcoming features, given the dependency graph and how many errors strict concurrency will raise.
## Two numbers that both get called "Swift version" Swift developers say "Swift version" for two different things. SE-0441 (Swift 6.1) settled the vocabulary to end the confusion: - The **compiler version**, also called the tools version, is the toolchain you run, such as `swiftc` from Swift 6.4. It decides which language features exist at all, which diagnostics are produced, and which standard library you build against. - The **language mode** is the source-compatibility dialect the compiler applies to one module. Language modes have existed since Swift 4.0, and the Swift 6.4 compiler accepts four of them: `4`, `4.2`, `5` and `6`. One compiler serves several modes. Asking Swift 6.4 for `-swift-version 7`, or for `-swift-version 6.4`, fails with "invalid value" and the note that the valid arguments to `-language-mode` are '4', '4.2', '5', '6'. A mode is a major dialect, not a copy of every release number. ## Choosing the mode On the command line the mode is `-swift-version 6`, or `-language-mode 6`, the spelling Swift 6.1 introduced with identical behaviour. `-swift-version` still works, but it is no longer listed in the compiler's top-level help. A plain `swiftc` or `swift` run without either flag compiles in **Swift 5 mode**. Xcode and Swift Package Manager set the same choice per target through their own settings. The mode is chosen **per module**. An app module in Swift 6 mode can import a library still in Swift 5 mode: SE-0362 states that code written to the Swift 6 language mode interoperates with code written to 4, 4.2 or 5. That is what makes a gradual, module-by-module migration possible. ## What a mode gates, and what it does not | | Compiler version | Language mode | |---|---|---| | What it is | The toolchain release, such as 6.4 | A dialect: 4, 4.2, 5 or 6 | | How you pick it | Install or select a toolchain | `-swift-version` / `-language-mode`, per module | | What it decides | Which features and diagnostics exist | Which source-breaking changes are on | | Tested in source with | `#if compiler(>=6.4)` | `#if swift(>=6.0)` | **Additive features** arrive with the compiler and work in every mode. Typed throws (`throws(ParseError)`), `sending` parameters and `InlineArray` all compile in Swift 5 mode with the 6.4 compiler, because they break no existing code. A mode holds back only **source-breaking** changes: rules that reject code that used to compile, or change what it means. Each of those is staged as an **upcoming feature** tied to the mode that turns it on by default. ## What Swift 6 mode turns on Swift 6 mode enables the upcoming features scheduled for it: - `StrictConcurrency`, complete concurrency checking, so data-race diagnostics become errors (the rules themselves belong to the material on Sendable and data-race safety); - `RegionBasedIsolation`, `InferSendableFromCaptures`, `GlobalConcurrency`, `IsolatedDefaultValues`, `DisableOutwardActorInference`, `DynamicActorIsolation` and `GlobalActorIsolatedTypesUsability`; - `ConciseMagicFile`: `#file` now means `#fileID` (`Billing/main.swift`) rather than the path handed to the compiler; - `ForwardTrailingClosures`, `BareSlashRegexLiterals`, `ImplicitOpenExistentials`, `NonfrozenEnumExhaustivity`, `DeprecateApplicationMain` and `ImportObjcForwardDeclarations`. It does **not** turn on the features scheduled for a later mode, such as `ExistentialAny` or `MemberImportVisibility`; those need `-enable-upcoming-feature` even in Swift 6 mode. The diagnostics show the gap. In Swift 5 mode with complete checking switched on, a nonisolated global `var` produces a warning ending "this is an error in the Swift 6 language mode"; in Swift 6 mode the same line is an error. The compiler's own diagnostic documentation describes such a warning as one that will be promoted to an error once the code moves to that mode. ## Moving a module 1. Build with the new compiler while keeping Swift 5 mode, and fix what the new compiler itself reports. 2. Turn on the Swift 6 upcoming features one at a time with `-enable-upcoming-feature`, clearing the warnings each one produces. 3. Switch the module to `-language-mode 6` and delete the feature flags the mode now implies. 4. Repeat module by module; modules in different modes keep linking together. ## Common confusions - Installing the Swift 6 toolchain does not move any target to Swift 6 mode. - Swift 6 mode is not needed for new syntax or library additions; it is needed for the stricter rules. - `swift(>=6.0)` in an `#if` tests the mode; `compiler(>=6.0)` tests the toolchain. - A higher mode is not where new features live: new features ship with the compiler, and the mode decides which source-breaking changes apply.
- Does the Swift 6.4 compiler in Swift 5 mode give you features added after Swift 5.10?Yes, the additive ones. Typed throws, `sending` parameters, `InlineArray` and Swift 6.4's `@diagnose` ship with the compiler and work in Swift 5 mode, because they break no existing code. Only source-breaking changes wait for a mode. Those are the upcoming features, and Swift 5 mode leaves them off unless you enable each one with `-enable-upcoming-feature`.
- Can a Swift 6 mode app module import a library module that is still in Swift 5 mode?Yes. The language mode is a per-module setting, and SE-0362 states that code written to Swift 6 interoperates with code written to 4, 4.2 or 5. Each module's own source is checked under its own mode. How the Swift 6 module treats unannotated types it imports is a concurrency-checking question, not a mode-compatibility one.
- Why did Swift 6.1 add -language-mode when -swift-version already worked?Because "Swift version" was ambiguous. SE-0441 adopted the term language mode in tools and APIs so nobody confuses the dialect with the toolchain release. `-language-mode` behaves exactly like `-swift-version`; the old spelling still works but is hidden from the compiler's top-level help.
The compiler version is the edition of the dictionary on your desk; the language mode is the style guide each chapter follows. A new edition adds words every chapter may use, while moving a chapter to the stricter style guide is a separate decision, made one chapter at a time.
- Installing the Swift 6 compiler switches every target to Swift 6 mode.
- You need Swift 6 language mode to use typed throws or InlineArray.
- Every module in an app must use the same language mode.
- Swift 6 mode turns on every upcoming feature, ExistentialAny included.
- -language-mode and -swift-version select different things.
Assumes the Swift 6.4 compiler, which accepts language modes 4, 4.2, 5 and 6 and uses Swift 5 mode when swiftc gets no mode flag; -language-mode is the Swift 6.1 spelling of -swift-version.