When one Swift codebase builds with two toolchains and two language modes, when do you use #if compiler, #if swift or #if hasFeature?
answer
- toolchain, dialect or feature
- Swift 5 mode stops at 5.10
- parsing is skipped only sometimes
- left side of && guards the right
basics
~20 s#if compiler(>=6.4) tests the toolchain, so it guards new syntax; #if swift(>=6.0) tests the language mode; #if hasFeature(X) tests whether feature X is on. Only failing compiler() and swift() checks stop the block being parsed.
solid answer
~40 s`compiler()` compares the **toolchain version** whatever the mode: the 6.4 compiler passes `compiler(>=6.4)` in Swift 4, 5 and 6 mode alike. `swift()` compares the **language mode**: under 6.4, Swift 5 mode passes `swift(>=5.10)` but fails `swift(>=5.11)` and `swift(>=6.0)`. `hasFeature(X)` is true when X is on, through a flag, the mode or a shipped feature, so `hasFeature(ExistentialAny)` stays false in Swift 6 mode until the flag is passed. Parsing decides the rest: a block behind a failing `compiler()` or `swift()` check is not parsed, which lets an older toolchain skip Swift 6.4 syntax such as `@diagnose`; a block behind `hasFeature`, `os()` or a `-D` flag is still parsed. To combine them, put `compiler(>=…)` on the left of `&&`.
code
swift · 23 lines#if compiler(>=6.4)
@diagnose(DeprecatedDeclaration, as: warning, reason: "Legacy feed until the importer ships")
func importLegacyFeed() -> Int { legacyCount() }
#else
func importLegacyFeed() -> Int { legacyCount() }
#endif
@available(*, deprecated, message: "Use the streaming importer")
func legacyCount() -> Int { 42 }
#if swift(>=6.0)
let mode = "Swift 6 language mode"
#else
let mode = "Swift 5 language mode or earlier"
#endif
#if hasFeature(ExistentialAny)
let anyRule = "ExistentialAny on"
#else
let anyRule = "ExistentialAny off"
#endif
print(mode, anyRule, importLegacyFeed())go deeper
Recall that compiler() checks the toolchain, swift() checks the language mode, and hasFeature checks one named feature.
Explain why Swift 5 mode under a 6.x compiler fails swift(>=6.0), and why hasFeature(ExistentialAny) is false in Swift 6 mode.
Use the parsing rule to keep new syntax behind compiler() checks, ordering && so older toolchains never interpret the right side.
Decide how long the codebase supports an older toolchain lane, since each supported toolchain adds #if compiler branches someone must later delete.
## Three different questions A codebase that builds in two CI lanes, one on Swift 6.4 and one on an older 6.x toolchain, and with some modules in Swift 5 mode and some in Swift 6 mode, asks three different things at compile time: | Condition | Compares | 6.4 compiler, Swift 5 mode | Body parsed when false? | |---|---|---|---| | `#if compiler(>=6.4)` | the toolchain version | true | no | | `#if swift(>=6.0)` | the language mode | false | no | | `#if hasFeature(ExistentialAny)` | whether one feature is on | false unless flagged | yes | Using the wrong one is how a build passes on one lane and fails on the other. ## `compiler()`: which toolchain `compiler()` (Swift 4.2, SE-0212) compares the compiler version "regardless of the Swift version setting passed to the compiler", in the words of the language reference. The 6.4 compiler passes `compiler(>=6.4)` in Swift 4, 5 and 6 mode alike. It is the right condition for anything a newer toolchain introduced: new syntax, a new attribute, or a workaround for one release's compiler bug. ## `swift()`: which language mode `swift()` compares the language mode being compiled, not the toolchain. With the 6.4 compiler: - in Swift 6 mode, `swift(>=6.0)` is true; - in Swift 5 mode, `swift(>=5.10)` is true but `swift(>=5.11)` and `swift(>=6.0)` are false, because Swift 5 mode tops out at 5.10; - in Swift 4 mode, `swift(>=5.10)` is false. So `swift()` is the right test for "is this module in Swift 6 mode", and the wrong test for "is this a Swift 6 toolchain". ## `hasFeature()`: which features are on `hasFeature(X)`, available since Swift 5.8 (SE-0362), is true when feature X is enabled: an upcoming feature passed with `-enable-upcoming-feature`, one the current mode includes, or a feature the compiler simply ships. With the 6.4 compiler: - `hasFeature(StrictConcurrency)` is true in Swift 6 mode, and false in Swift 5 mode unless the flag is passed; - `hasFeature(ExistentialAny)` stays false even in Swift 6 mode until the flag is passed, because that feature belongs to a future mode; - `hasFeature(SourceWarningControl)`, the identifier for `@diagnose`, is true in every mode. `hasFeature` is the precise test when code depends on one rule rather than on a whole mode. ## Why parsing decides which one you need 1. Every statement inside an `#if` block is parsed even when the block is not compiled. 2. The exception: when the condition includes a `swift()` or `compiler()` check that fails, the body is not parsed, so an older compiler never sees syntax it does not know. 3. SE-0362 extended this to `&&` and `||`: when the left side already disables parsing, a failing `compiler(>=…)` for instance, the call syntax on the right is not interpreted. `#if compiler(>=6.4) && hasFeature(X)` is therefore safe on old toolchains. 4. `hasFeature`, `os()`, `canImport()` and `-D` conditions give no such protection: the body must still parse. That is why a Swift 6.4 attribute such as `@diagnose` belongs behind `compiler(>=6.4)`, not behind `hasFeature(SourceWarningControl)` alone. ## A worked example The snippet with this question uses all three conditions in one file and was built with the Swift 6.4 compiler and `-Werror DeprecatedDeclaration`: - In Swift 5 mode it prints `Swift 5 language mode or earlier ExistentialAny off 42`. - With `-swift-version 6` it prints `Swift 6 language mode ExistentialAny off 42`: the mode changed, the future-mode feature did not. - Adding `-enable-upcoming-feature ExistentialAny` turns `off` into `on`. - In every run the `compiler(>=6.4)` branch is taken, and its `@diagnose` attribute keeps the deprecated call a warning despite `-Werror`. An older 6.x toolchain would take the `#else` branch without ever parsing the `@diagnose` line, so the same file builds on both lanes. ## Choosing - New syntax or a new attribute: `compiler(>=…)`. - Code that depends on one upcoming feature: `hasFeature(X)`, nested inside or to the right of a `compiler()` check when X brings syntax. - Code that depends on the mode as a whole: `swift(>=6.0)`, though `hasFeature` usually names the real dependency better. - The OS version the app runs on: none of these. That is `if #available` at run time. A misread here rarely fails loudly. A `swift(>=6.0)` check meant to detect the new toolchain silently picks the old branch for every module still in Swift 5 mode, and the fix lands only on the modules that happened to migrate.
- Why does `#if hasFeature(SourceWarningControl)` not protect an older compiler from the `@diagnose` attribute?Because the body of an `#if` is parsed even when its condition is false, unless the condition includes a failing `compiler()` or `swift()` check. An older compiler would still have to parse the new attribute inside the `hasFeature` block. Wrap it in `#if compiler(>=6.4)`, or write `compiler(>=6.4) && hasFeature(…)` with the compiler check on the left.
- Under the Swift 6.4 compiler in Swift 5 mode, why is swift(>=5.10) true but swift(>=5.11) false?`swift()` reports the language mode, not the toolchain, and Swift 5 mode on this compiler behaves as language version 5.10, the last 5.x. Any check above 5.10, including `swift(>=6.0)`, fails until the module moves to Swift 6 mode. Code that wants to know the toolchain must use `compiler()` instead.
- #if swift(>=6.0) is true whenever the Swift 6 compiler builds the code.
- #if hasFeature hides new syntax from older compilers.
- #if compiler(>=6.4) gives different answers in Swift 5 and Swift 6 mode.
- hasFeature(ExistentialAny) is true in Swift 6 mode by default.
- #if os(iOS) can tell whether the device runs iOS 26.
Assumes the Swift 6.4 compiler: compiler() dates from Swift 4.2 (SE-0212) and hasFeature from Swift 5.8 (SE-0362); in Swift 5 mode this compiler satisfies swift(>=5.10) but not swift(>=6.0).