5.10 · What property wrappers, result builders, async and throws compile to
Language features that look like magic are rewrites: hidden storage, calls to builder methods, a changed calling convention and a branch on an error register.
Mental model. Each feature is lowered to something ordinary. Seeing the lowered form removes most of the mystery (and explains the costs).
Sugar that rewrites code
Property wrappers and result builders.
- What does the compiler turn a property wrapper into: what storage exists, what are $name and _name, and what does it cost?
A property wrapper is a rewrite. For @Clamped var n = 5 the compiler synthesises a private stored property _n of the wrapper type, and turns n into a computed… - What does the compiler do with the statements inside a result builder body, in what order do the builder methods run, and why does it affect type checking?
A result builder rewrites a block of statements, with no explicit return, into calls on the builder type. Each expression statement is passed through…
Effects in the calling convention
async and throws.
- How is an async function different in compiled code, what does await compile to, and what is a hop_to_executor?
An async function gets a different calling convention: the SIL type is @convention(thin) @async () -> Int, and the function receives a hidden async context… - How is throws implemented: is it like C++ exceptions, what does try compile to, and what does it cost?
Swift errors are ordinary return values, not unwinding exceptions. A throwing function has an extra result channel in its type, visible in SIL as (Int, @error…
More in Compiler & language internals
- 5.1 · From source to machine code, and how to look inside
- 5.2 · Devirtualisation, whole-module optimisation, inlining, exclusivity
- 5.3 · Specialisation, witness tables, and opaque vs existential in the compiled code
- 5.4 · Library evolution, module interfaces, and the ABI boundary
- 5.5 · How Swift macros work, and the roles they play
- 5.6 · Why it compiles slowly, and what rebuilds
- 5.7 · Swift 6 language mode and upcoming features
- 5.8 · Symbol names, linkage, lazy globals and reflection metadata: what is in the binary
- 5.9 · Optimisation levels, closure contexts, ownership conventions and value layout
- 5.11 · Package access, SPI, internal imports, strict memory safety and pre-specialisation