A binary Swift framework built with an older compiler still loads and compiles in an app built with Swift 6.4; what do ABI stability and module stability each guarantee?
answer
- run time versus compile time
- the OS ships the Swift runtime
- a textual module description
- library evolution switched on
- enums that may gain cases
basics
~20 sABI stability (Swift 5.0, Apple platforms) lets binaries from different Swift compilers link and run together. Module stability (Swift 5.1) lets a newer compiler import the framework through its textual .swiftinterface file, which needs library evolution enabled.
solid answer
~50 sThey solve two different problems. **ABI stability**, declared for Swift 5 on Apple platforms, fixes the binary contract (calling conventions, type layout, metadata and name mangling), so code compiled by different Swift compilers links and runs together, and the Swift runtime and standard library ship in the OS. **Module stability**, added in Swift 5.1, is the compile-time half: the binary `.swiftmodule` is tied to the compiler that wrote it, so a framework for distribution also emits a textual `.swiftinterface` that a later compiler can read. You get it with `-enable-library-evolution -emit-module-interface`, or Xcode's `BUILD_LIBRARY_FOR_DISTRIBUTION`. Library evolution has a visible cost for clients: a `switch` over the framework's non-`@frozen` public enum needs `@unknown default`, a warning in Swift 5 mode and an error in Swift 6 mode. On Linux and Windows the ABI is not declared stable, so binaries there should come from one compiler version.
code
bash · 3 linesswiftc Receipts.swift -module-name Receipts -swift-version 5 \
-emit-module -emit-library \
-emit-module-interface -enable-library-evolutiongo deeper
Remember that ABI stability is about running binaries together and module stability is about a newer compiler importing a framework.
Explain the .swiftmodule versus .swiftinterface split and why the Swift runtime ships inside Apple's operating systems since Swift 5.0.
Diagnose the vendor-framework scenario: missing interface after a compiler update, @unknown default in Swift 6 mode, and library evolution only for separately shipped binaries.
Weigh what a binary framework commits to: library evolution and @frozen choices are permanent ABI promises, so a vendor should decide them before the first release.
## Two kinds of compatibility Swift's ABI Stability Manifesto separates **source compatibility** (a newer compiler still compiles older code) from **binary framework compatibility**. A binary framework ships two artifacts: a **module file** that tells the compiler what the framework's API looks like, and a **shared library** loaded at run time. Each needs its own guarantee, and the manifesto states that ABI stability is "necessary, though not sufficient" for binary frameworks. | Guarantee | Since | What it lets you change | Scope | |---|---|---|---| | ABI stability | Swift 5.0 | the Swift runtime and standard library under an app | macOS, iOS, tvOS, watchOS | | Module stability | Swift 5.1 | the compiler that builds the client | requires library evolution | | Library evolution | Swift 5.1 (SE-0260) | the framework's own API, without recompiling clients | opt-in per framework | ## ABI stability: the run-time contract The **ABI** (application binary interface) is how independently compiled code agrees on low-level details: how functions are called, how values are laid out in memory, where type metadata lives and how symbols are named (**name mangling**). With Swift 5.0 the ABI was declared stable on Apple platforms: - An app built with one Swift compiler can talk to a library built with another. - The Swift runtime and standard library ship inside the OS from macOS 10.14.4, iOS 12.2, tvOS 12.2 and watchOS 5.2, so an app whose deployment target is at least those releases no longer embeds them. - The flip side: a feature that needs new runtime support can require a newer OS, checked with availability annotations. - The language mode is compile-time only. A framework in Swift 5 mode and an app in Swift 6 mode work together. - On Linux, Windows and other platforms the ABI is **not** declared stable; an application and a library built with different compiler versions are not guaranteed to link or behave correctly there. ## Module stability: the compile-time contract The `.swiftmodule` format is tied to the compiler that wrote it. Importing one produced by a different compiler fails with an error such as `compiled module was created by a different version of the compiler`. **Module stability** adds a textual **module interface**, `Module.swiftinterface`: a Swift-syntax summary of the public API whose header records the compiler and flags that produced it. A later compiler parses that text and builds its own module from it. You produce it in one of two ways: 1. Calling `swiftc` with `-emit-module-interface` and `-enable-library-evolution`, plus an explicit language mode such as `-swift-version 5`, which the Swift 6.4 compiler insists on before it writes an interface. 2. Setting `BUILD_LIBRARY_FOR_DISTRIBUTION` on the framework target in Xcode, which turns on both. Asking for an interface without library evolution draws the warning `module interfaces are only supported with -enable-library-evolution`. ## What a module interface contains - Every `public` and `open` declaration, with its full signature, plus the bodies of `@inlinable` functions, which clients compile into their own code. - A header that records the compiler version, the target and the flags, including the language mode the framework was built in (`-language-mode 5` in the example below). - No `internal` or `private` code, except `internal` declarations marked `@usableFromInline` that inlinable bodies call: the interface is a public contract, not the source. On Apple platforms such a framework is usually shipped as an `.xcframework`, the bundle format Xcode introduced alongside `BUILD_LIBRARY_FOR_DISTRIBUTION`. ## Library evolution: what it costs **Library evolution** (also called resilience) lets a framework add public API in a new version without clients recompiling. Clients access non-`@frozen` public types indirectly, so the framework may add stored properties to a struct or cases to an enum. Consequences: - A client `switch` over a non-`@frozen` public enum from such a framework needs `@unknown default`. Swift 5 mode warns "switch covers known cases, but 'PaymentMethod' may have additional unknown values"; Swift 6 mode reports the same text as an error. - `@frozen` gives up that flexibility for performance; the lesson on compiler optimization covers it. - Turning library evolution on is itself binary-incompatible, so it belongs in a framework's first release. The Swift project's guidance is that frameworks built and shipped together with their app, such as packages, should not enable it. ## Diagnosing the scenario If a vendor's framework fails to import after an Xcode update, it most likely shipped only a `.swiftmodule`. The fix belongs to the vendor: rebuild with library evolution so the framework carries a `.swiftinterface`. If it imports but a once-exhaustive `switch` now fails in Swift 6 mode, add `@unknown default`.
- Why does a switch that lists every case of a framework's enum fail in Swift 6 language mode?When the framework is built with library evolution, a public enum not marked `@frozen` may gain cases in a later version without clients recompiling. The compiler therefore asks for `@unknown default` to handle values the client has never seen: Swift 5 mode warns, Swift 6 mode makes it an error. A `@frozen` enum promises no new cases, so listing every case is enough.
- Should every internal framework in an app's workspace enable library evolution?No. The Swift project's guidance is that frameworks built and shipped together with their clients, such as packages or an app's internal frameworks, should not use it. It adds indirection for non-frozen types, changes enum exhaustiveness for clients, and switching it on later is itself binary-incompatible. Enable it for a framework distributed as a binary and updated separately from its clients.
- What does ABI stability mean for adopting a new standard library API on Apple platforms?Because the runtime and standard library ship in the OS, a feature that needs new runtime support can require a newer OS version, so code checks availability or the app raises its deployment target. An app cannot bundle a newer Swift runtime to get around that.
- ABI stability lets any compiler import any precompiled .swiftmodule file.
- Module stability is automatic for every framework built with Swift 5.1 or later.
- Swift's ABI is declared stable on Linux and Windows just as on Apple platforms.
- A framework and an app must use the same Swift language mode to work together.
- Library evolution should be enabled on every internal framework, just in case.
Assumes Swift 6.4; ABI stability dates from Swift 5.0 and module stability from 5.1 on Apple platforms, and the unknown-case switch is a warning in Swift 5 mode and an error in Swift 6 mode.