What does the compiler guarantee through its mandatory SIL passes?

5.1.2 · L5 · Learning

Predict first

Is the missing return found when lowering to raw SIL?

func f(_ x: Int) -> Int {
    if x > 0 { return 1 }
}
  • Yes
  • No, a later pass reports it

Predict first

Does the same code compile through the SIL passes?

func f(_ x: Int) -> Int {
    if x > 0 { return 1 }
}
  • Yes
  • No

Mental model. Type checking asks whether the words make sense. The mandatory passes ask whether the story is consistent from start to end.

Mandatory passes run in every build, including -Onone, because they enforce language rules rather than speed. They include: definite initialisation (every let and stored property is set before it is read, and every path through an init initialises everything), dataflow diagnostics (“missing return”, unreachable code, unused results), static exclusivity enforcement (two overlapping accesses to the same variable), capture promotion and boxing of captured variables, and ownership verification, which checks the compiler’s own retain and release bookkeeping.

The practical consequence is that these errors are not type errors and are not affected by build settings. They also explain why the compiler can reject code that type-checks: it follows control flow, which the type checker does not. If a diagnostic mentions “used before being initialized” or “missing return”, you are looking at a SIL pass, not at Sema.

Raw SIL is produced without complaint. The “missing return” diagnostic comes from a dataflow pass that analyses the control-flow graph.

The dataflow pass finds a path that falls off the end of a function that must return an Int.

Common traps

  • Looking for these errors in Sema output. They only appear once SIL exists.
  • Assuming an optimised build is stricter. The guarantees are the same at every optimisation level.

They might follow up with

  • Why is “missing return” not a type error? It depends on control flow, which only exists once the code is lowered to a graph of basic blocks.
  • What is ownership SSA (OSSA)? A form of SIL that tracks ownership of every value so the optimiser can remove retains and releases safely.

Related questions

  • 5.1.1 What are the stages from Swift source to machine code, and why does Swift have SIL?
  • 5.2.4 What is exclusivity enforcement, and what does it cost at runtime?

← What are the stages from Swift source to machine code, and why does…   How do you look at what the compiler produced at each stage? →