skip to content

As a lead moving a large Swift codebase to Swift 6 language mode, in what order would you migrate modules, and which shortcuts would you restrict?

level: principalimportance: should knowfreq 45%

answer

  1. language mode is per module
  2. warnings first, then errors
  3. dependencies before dependents
  4. @preconcurrency import as scaffolding
  5. a budget for unchecked escapes

basics

~20 s

Migrate module by module, dependencies first: preview Swift 6 errors as warnings with -strict-concurrency=complete in Swift 5 mode, fix them, then switch that module to Swift 6 mode. Use @preconcurrency import as temporary scaffolding and gate @unchecked Sendable and nonisolated(unsafe) behind review.

solid answer

~40 s

The language mode is chosen per module, and Swift 5 and Swift 6 mode modules link together, so there is no flag day. I would go bottom-up: shared models and utilities first, because their `Sendable` annotations remove warnings in everything that imports them, then services such as networking, then app and UI targets. For each module: turn on `-strict-concurrency=complete` in Swift 5 mode, which reports Swift 6's errors as warnings; fix them; flip it to `-swift-version 6`; and keep it there. Third-party modules without annotations get `@preconcurrency import`, tracked for removal. I would restrict `@unchecked Sendable` and `nonisolated(unsafe)` to reviewed, commented cases, decide default main-actor isolation for app targets only, and run tests under Thread Sanitizer, because the compiler cannot see through unchecked code.

go deeper

for a junior

Know that Swift 6 mode turns data-race warnings into errors and that teams usually migrate one module at a time.

for a middle

Explain how -strict-concurrency=complete previews Swift 6 errors in Swift 5 mode and what @preconcurrency import does.

for a senior

Plan one module's migration end to end: count warnings, fix with Sendable types, Mutex, actors or sending, then flip the mode.

for a principal

Own the order across the dependency graph, the rules for unchecked escapes, when default main-actor isolation applies, and how progress is measured.

## What the decision is about Swift 6 language mode (Swift 6.0) turns concurrency checking into **errors**: non-`Sendable` values crossing isolation boundaries, mutable global state, unsafe captures. The checks themselves are mechanical; the lead's decisions are about **order, pace and which escape hatches the team may use**. There is no single right plan: a codebase dominated by UI code, a server-side codebase and a set of shared packages call for different orders. ## Facts the plan rests on - **The language mode is per module** (per target). A Swift 6 mode module can import a Swift 5 mode module and the reverse, so migration can be incremental. - **Swift 5 mode can preview Swift 6 errors**: `-strict-concurrency=minimal` (the Swift 5 default) checks only code that has adopted concurrency, `targeted` a little more, and `complete` reports what Swift 6 mode would reject, as warnings suffixed "this is an error in the Swift 6 language mode". - **`@preconcurrency import Module`** tells the compiler that `Module` predates concurrency annotations: `Sendable` diagnostics about its types are silenced or downgraded to warnings. In Swift 6.4 the warning that a `@preconcurrency` import "has no effect" belongs to the `PreconcurrencyImport` group, which is **off by default**; turn it on with `-Wwarning PreconcurrencyImport` to find the stale ones. - **Region-based isolation and `sending`** (Swift 6.0) mean many non-`Sendable` types can stay non-`Sendable`: the fix is often a capture or an ownership change, not a conformance. - **Swift 6.2 options**: default main-actor isolation per module, and `nonisolated(nonsending)` with the opt-in `NonisolatedNonsendingByDefault` feature, which has a `:migrate` mode. ## A migration order that usually works 1. **Measure.** Build every module in Swift 5 mode with `-strict-concurrency=complete` and count warnings per module; that count is the backlog and the progress metric. 2. **Leaves of the dependency graph first.** Shared model, utility and persistence modules: annotating their public types `Sendable` (or deliberately not) removes many warnings in the modules above them. Library authors can audit with `-Wwarning ExplicitSendable`. 3. **Service modules next**, such as networking: replace shared mutable caches with `Mutex`-guarded or actor-isolated state, and use `sending` where ownership really moves. 4. **App and UI modules last.** Decide here whether `-default-isolation MainActor` fits; it removes most false positives in UI code and should not be applied to the shared libraries. 5. **Flip each module to Swift 6 mode as soon as its warnings reach zero**, so new violations in it become errors, rather than waiting to flip everything at once. 6. **Remove scaffolding**: drop stale `@preconcurrency` imports, revisit every `@unchecked`. Top-down (app first) is the alternative when the app target is where races hurt today: it gets errors in the riskiest code sooner, at the price of more `@preconcurrency` imports and temporary annotations that the lower modules later make unnecessary. ## Shortcuts to restrict | Shortcut | Why it is tempting | Rule I would set | |---|---|---| | `@unchecked Sendable` | makes any type pass instantly | only with a lock or documented invariant, a comment saying what guards the state, and reviewer sign-off | | `nonisolated(unsafe)` | silences a global or capture | same bar as `@unchecked`; prefer `let`, `@MainActor` or `Mutex` | | `@preconcurrency import` | silences a dependency | allowed for unannotated dependencies, tracked and removed when they update | | `@MainActor` on everything | errors vanish | fine for UI code; not for libraries or CPU-heavy work | | staying in Swift 5 mode indefinitely | nothing breaks | each module gets a target date once its count is zero | ## Tradeoffs a lead weighs - **Speed versus real safety.** A migration finished by sprinkling `@unchecked` has the same races and no warnings left to find them. Counting unchecked escapes alongside warnings keeps that visible. - **Behaviour changes.** Adding `@MainActor` or adopting `NonisolatedNonsendingByDefault` changes where code runs; that is a performance and correctness change, not just an annotation, and deserves tests. - **Dependencies.** Third-party packages set the pace for the modules that use them; wrapping a non-annotated dependency in one small module with a reviewed boundary limits the spread of `@preconcurrency`. - **Run-time evidence.** The compiler proves only what it can see. Running the test suite under Thread Sanitizer (`-sanitize=thread`) catches races hidden behind unchecked code. - **Team learning.** The first module migrated becomes the team's reference; choose one small enough to finish quickly and representative enough to teach the patterns.

  • Why migrate the shared model module before the app target?
    Every module that imports the models inherits their sendability. Once the model types are `Sendable` (or deliberately not), many warnings in the modules above disappear without edits there, and those modules need fewer `@preconcurrency` imports. Starting at the app target means fixing symptoms whose cause lives lower in the graph.
  • How do you find `@preconcurrency import` lines that are no longer needed?
    Build with `-Wwarning PreconcurrencyImport`. In Swift 6.4 the warning that a `@preconcurrency` import has no effect sits in that diagnostic group, which is off by default, so stale imports stay silent unless you enable it, for example in a periodic CI job.
  • Would you enable NonisolatedNonsendingByDefault during the Swift 6 migration?
    Usually as a separate step. It changes where existing nonisolated async functions run, so mixing it with the mode switch makes regressions hard to attribute. Its `:migrate` mode adds `@concurrent` to keep current behaviour, after which teams remove it function by function where running on the caller's actor is wanted.

  • The whole app must switch to Swift 6 mode in one release.
  • A Swift 6 mode module cannot import a Swift 5 mode module.
  • Marking types @unchecked Sendable is a fine way to finish the migration.
  • Swift 6 language mode turns on NonisolatedNonsendingByDefault.
  • Zero warnings under complete checking proves the code race-free.
  • Unneeded @preconcurrency imports always produce a warning.

Assumes Swift 6.4 tooling; Swift 6 language mode (Swift 6.0) makes the data-race checks errors, and Swift 5 mode previews them as warnings with -strict-concurrency=complete.