skip to content

What is Embedded Swift, how do you enable it, and which language features does it still not support as of Swift 6.4?

level: seniorimportance: nice to knowfreq 10%

answer

  1. no Swift runtime in the binary
  2. an experimental feature flag
  3. whole-module optimization is mandatory
  4. every generic must be specialized
  5. 6.4 widened what is allowed

basics

~20 s

Embedded Swift is an experimental, runtime-free compilation mode for tiny binaries, enabled with -enable-experimental-feature Embedded and -wmo; as of Swift 6.4 it still does not support weak and unowned, protocol-type casts or unspecializable generic calls.

solid answer

~50 s

Embedded Swift compiles a subset of Swift without the Swift runtime, specializing every generic and emitting only the code and metadata actually used, so it fits microcontrollers, firmware and small Wasm modules. It is still an experimental feature: with `swiftc` you pass `-enable-experimental-feature Embedded` together with `-wmo` (the driver errors "Whole module optimization (wmo) must be enabled with embedded Swift"), plus an embedded target and the embedded standard library from a swift.org toolchain; the Wasm SDK's `_wasm-embedded` ID packages all of that. As of 6.4 it still does not support `weak` and `unowned` (a compile error), dynamic casts to types involving a protocol, non-final generic methods in classes, or generic methods called on `any P` values. Swift 6.4 lifted older limits: every `any` type, `Any` included, untyped `throws`, metatype values and throwing tasks now work. Running `-Wwarning EmbeddedRestrictions` on a normal build lists what would fail.

code

swift · 22 lines
swift
protocol Sensor { func read() -> Int }
protocol Calibrating: AnyObject { func calibrate() }

final class Thermometer: Sensor, Calibrating {
    weak var hub: Hub?                       // flagged
    func read() -> Int { 21 }
    func calibrate() {}
}

final class Hub {
    var sensors: [any Sensor] = []           // fine in Swift 6.4
    func describe<T>(_ value: T) -> String { "\(value)" }
}

class Store<Element> {
    func convert<U>(_ f: (Element) -> U) -> [U] { [] }   // flagged: not final
}

func poll(_ object: AnyObject, hub: Hub) throws -> Int {  // untyped throws: fine in 6.4
    if let c = object as? Calibrating { c.calibrate() }   // flagged
    return hub.sensors.reduce(0) { $0 + $1.read() }
}

go deeper

for a junior

Recall that Embedded Swift is a subset of Swift for tiny targets such as microcontrollers, built without the Swift runtime.

for a middle

Explain how it is turned on: an experimental feature flag with whole-module optimization, an embedded standard library, or the Wasm SDK's embedded ID.

for a senior

Tie each restriction to its cause, full specialization or missing runtime metadata, know what 6.4 lifted, and audit a module with the EmbeddedRestrictions warning group.

for a principal

Judge whether to commit a product to a still-experimental mode: weigh rapid release-to-release change against the size and safety gains over C.

## What Embedded Swift is **Embedded Swift** is a compilation mode that produces Swift code needing **no Swift runtime**: no run-time protocol-conformance metadata, no Objective-C, a simplified reference-counting model. It targets constrained environments: microcontrollers, bare-metal code with no operating system, firmware, kernels and size-critical WebAssembly. Swift's diagnostics documentation describes it as a subset in which "the vast majority of Swift language features are available", minus the ones that need the full runtime. Two compilation rules explain almost every restriction: - **Full specialization.** Every generic function and type must eventually be compiled for concrete types ("monomorphized"); there is no unspecialized generic fallback at run time. - **Whole-module, aggressive cross-module optimization.** The compiler must see definitions to specialize them, so Embedded builds use whole-module optimization and make most functions' bodies available to clients. Embedded Swift is not a language mode: Swift 5 or Swift 6 mode is chosen separately. ## How to enable it Embedded Swift is declared in the compiler's feature list as an **experimental feature** (`EXPERIMENTAL_FEATURE(Embedded, true)`, usable in release compilers), not an upcoming one. 1. **Direct `swiftc`:** `-enable-experimental-feature Embedded -wmo`, plus an embedded target triple (for example an ARM or RISC-V `-none-` triple). Without `-wmo` the driver stops with *"Whole module optimization (wmo) must be enabled with embedded Swift."* `-enable-upcoming-feature Embedded` does not turn it on. 2. **A standard library built for Embedded.** It comes with swift.org toolchains and SDKs; Xcode 27's bundled toolchain does not include it, and builds fail with "unable to load standard library". 3. **WebAssembly:** build with the Wasm SDK's `<version>_wasm-embedded` ID, which packages the mode, target and library together. 4. **A package target** can enable the experimental feature in its Swift settings; manifest details belong with the package manager. ## What Swift 6.4 still does not support In an Embedded build `weak` and `unowned` are hard errors; the other rows are reported by the `EmbeddedRestrictions` diagnostics as unsupported. | Construct | Why | |---|---| | `weak` / `unowned` references | the simplified reference counting cannot support them; an error in Embedded builds | | `x as? P` and other dynamic casts to protocol-involving types | no run-time conformance metadata | | non-final generic methods in a class | cannot be specialized for every override and call site | | generic methods called on an `any P` value | the concrete type is unknown, so no specialization exists | | a class whose superclass's generic argument is the subclass | circular metadata dependency | ## What changed in 6.3 and 6.4 - **Existentials:** before 6.4 only `AnyObject`-constrained `any` types were allowed. **Swift 6.4** supports every `any` type, including `Any`, so heterogeneous arrays like `[any Sensor]` compile. - **Untyped throws:** previously only typed throws (`throws(MyError)`) was allowed; 6.4 supports plain `throws`, which is `any Error`. Throwing an `any Error` usually heap-allocates, so allocation-sensitive code should still prefer typed throws. - **Metatypes:** 6.4 supports creating and using metatype values, including existential metatypes. - **Concurrency:** throwing tasks and task groups now work. - **Library:** floating-point parsing (`Double(someString)`) and printing are available. - **Diagnostics:** Swift 6.3 added the `EmbeddedRestrictions` diagnostic group. It is on by default in Embedded builds and can be turned on in an ordinary build with `-Wwarning EmbeddedRestrictions`. ## Auditing existing code Enabling `-Wwarning EmbeddedRestrictions` on a normal macOS or Linux build is the cheapest way to see what a shared module would need to change before anyone sets up an embedded toolchain. Typical fixes: - replace a `weak` back-reference with an index, an ID or a non-owning design; - make generic class methods `final`, or move them to a struct; - replace `as? Protocol` checks with an enum or a generic parameter; - call generic helpers through `some P` or a generic parameter rather than on `any P` values. Features with a dynamic cost (existentials, untyped throws, metatypes) are allowed, but they cost code size, metadata and indirect calls wherever they are used. Code that is highly sensitive to size or speed can turn on warnings in the `PerformanceHints` diagnostic group to find those uses, then decide one by one whether to keep them. Expect the subset to keep moving: the 6.3 and 6.4 releases each widened it, so a restriction noted in last year's design document may be gone, and a project should pin the toolchain it validated against.

  • Why is calling a generic method on an any P value unsupported while calling a non-generic one is fine?
    A non-generic requirement is called through the conformance's witness table, which Embedded Swift can emit. A generic method needs a specialized copy for each concrete type it is used with, and through `any P` the compiler does not know the concrete type, so no specialization can be produced. Calling it on a generic parameter `T: P` or `some P` works, because each call site is specialized.
  • Swift 6.4 allows untyped throws in Embedded Swift. Why might firmware code still use typed throws?
    Untyped `throws` throws an `any Error`, and boxing an error into an existential usually needs a heap allocation. With `throws(SensorError)` the concrete error type is known, nothing is boxed, and the code stays allocation-free, which matters on a microcontroller with a tiny heap or none.
  • How would you find what a shared module must change before anyone builds it for Embedded Swift?
    Compile it as ordinary Swift with `-Wwarning EmbeddedRestrictions` (Swift 6.3 and later). The compiler reports every `weak` or `unowned`, protocol cast, non-final generic class method and generic call on an existential as a warning, without needing an embedded toolchain or target.

  • Says Embedded Swift is enabled with -swift-version or an upcoming-feature flag.
  • Believes Embedded Swift in 6.4 still forbids every non-class existential.
  • Thinks weak and unowned work in Embedded Swift because ARC is still present.
  • Claims untyped throws is still disallowed in Embedded Swift 6.4.
  • Treats Embedded Swift as a separate language rather than a subset.

Assumes Swift 6.4: Embedded Swift is still an experimental feature; 6.4 added general any types, untyped throws and metatypes, and 6.3 added the EmbeddedRestrictions diagnostic group.