Skip to content

ADR 0033: Definite assignment, checked where it cannot be proven

Status: Final Type: Feature Created: 2026-08-02 Consolidated: 2026-08-07, absorbing 0051's placeholder rule Amends: 0007 (NameError and UnboundLocalError join the vocabulary), 0021 (the definite-assignment question it left to the frontend is asked here, which also answers the deferral 0014 recorded) Related-ADRs: 0007 (the propagate path that touches placeholders). Provenance: arbitrations A53, A99, A140, A146, A157; upstream question U31.

Context

if c > 0: y = 1 then return y is ordinary Python, and CPython raises UnboundLocalError on the path the branch skipped. A compiler that gives each local a frame slot has no such moment, because the slot exists from the first instruction. The question must therefore be asked statically or not at all. Before this ADR it was answered by accident of type and binding count: one shape produced a verifier traceback, a second printed a different number on each backend, a third printed an empty string on all three, a fourth read an uninitialised slot. One mechanism explains all four: multiply-assigned names become memory slots that the dominance invariant rightly exempts, so the initialisation question had no owner. The dominance invariant's documentation named the missing check; this ADR is that check.

Decision

A source-level pass proves which reads of a local are bound: a read is proven when each path from the function's entry to it binds the name, the standard formulation (Java's JLS §16, near enough). The pass runs per function (Python's scoping rule, that a name assigned anywhere is local everywhere, makes that sound), after the script rewrite, so a whole script's top level is checked as the function it becomes.

A read the pass cannot prove compiles as a checked read, which is CPython's per-name bound flag (A146):

  • each such name gets a hidden Bool, cleared at the function's entry and set by every binding of the name: an assignment, an augmented assignment, a walrus, and a for target at the top of each iteration;
  • each such read tests it and raises CPython's error: UnboundLocalError: cannot access local variable 'x' where it is not associated with a value inside a function, and NameError: name 'x' is not defined at a script's top level, the scope it is in CPython. NameError, and UnboundLocalError as its subclass, are in the exception vocabulary (0007). except NameError: catches both. The FFI maps a CPython UnboundLocalError to its own name;
  • a counted slot under a flag starts at its type's null, which every release accepts (the placeholder discipline below); a number or a bool starts at zero. Both are declared at the frame's entry, so that the read below the branch that binds them is dominated. The flag decides whether the name is bound;
  • a read the pass proves bound has no flag and no test, so a function whose locals are all proven bound declares no flag.

Zero-filling without a flag would print 0 where CPython raises, which breaks the law. The flag costs one store per binding and one test per read, only where the pass reports a read.

The read point. A lowering often fetches a local without lowering a Name: a method's receiver, a field's instance, the list a loop iterates, the container of a store, the subject of a narrowing test. The check therefore runs where a statement, an expression or a store starts. It covers each unproven read evaluated there before anything that can raise, print or bind, which is the moment CPython looks up the name; a read after such an operation is checked where the Name is lowered. A statement lowered twice (0053's companion loops) is checked in both copies. An unproven read left without a check is a compiler crash and never a silent read: the lowering verifies this before handing the code on.

Loops are not exempted. A static analysis cannot see that for i in range(3): runs, so a read after the loop is checked even though CPython would print. The loop variable after the loop is checked with CPython's justification: an empty iterable leaves it unbound. while True: adds no check: the body always runs, so a name bound before each break is bound after the loop. That required the lowering to terminate a literal-True test with an unconditional branch, since a dead exit edge otherwise breaks the dominance the analysis relies on. while 1: and while flag: are not folded, so the exit edge stays visible.

Proven shapes. The pass proves bindings in both arms of an if. It proves an arm that returns, leaving the other the only path, and a try whose handler also binds. The state is per program point, so a binding before or after the branch counts. A with body runs, so its bindings count; a comprehension target is a separate scope. A name bound only in a try body is a checked read, as is one a match binds in every case.

A handler's name. CPython deletes the name an except ... as e: clause binds when the clause ends. The pass unbinds the name at the end of the clause, and the lowering drops it there.

del of a local (A157) removes the name from what is bound, so a read below it is unproven and compiles as a checked read. The del itself looks up the name, as CPython raises UnboundLocalError for a del of an unbound name, so a del the pass cannot prove bound is checked too. The lowering clears the name's flag, releases what a counted slot held and nulls it, which nothing can observe (__del__ is refused).

The placeholder discipline (absorbed from 0051) lets a function returning any counted type propagate an exception, returning the type's null as a placeholder. This is safe because the caller's exception check runs directly after the call and diverts before any reader touches the value; the only operations a placeholder can meet are the count helpers, which are no-ops on null. The container cases (list, dict, set) landed together as one rule for three types, because admitting one container and forgetting another leaves a blind spot. Exc keeps no placeholder, by decision.

What is refused, each with CPython's reason in the diagnostic

  • A read above every binding of the name in the source (return y then y = 1, or a loop that uses prev before the line that binds it). A local takes the type of its first assignment, and none exists above the read. mypy in strict mode refuses the same read (has-type), so it lies outside a typed program (A144).
  • A handler's name read below its clause, which always raises on the handler's path; mypy refuses it as reading a deleted variable.
  • A name bound on some paths that holds a value with no null or zero to start at (an Array[T], a CPython handle).
  • del of a name another scope uses (PP025, the checker's). A module global that a function or class uses would stay readable there. A nested function's capture would keep its value. CPython raises NameError in both.

Consequences

  • Real programs compile where CPython runs them. The benchmark warm-up, a name bound in an if inside a loop and read after it, stopped 46 of the first 56 Shed Skin programs measured while unproven reads were refused; with the checked read the census root "definite assignment" went from 51 programs to 1, and that one reads a name above its only binding (notes/measurements.md, 2026-10-11).
  • A program whose reads are all proven compiles to the IR it would have without this ADR.
  • A docstring that names a missing check amounts to a decision. Other comments of that shape may hide similar gaps.

Alternatives considered

  • Refusing every unproven read: the first answer, with the bound flag priced as its upgrade and the trigger written down (the refusals costing more than the flags would). The census fired the trigger (A146).
  • Zero-filling: rejected as a law violation: it gives a silently different answer on the input CPython raises for.
  • Exempting loops (accepting the Java-style cost is unusual enough to state): rejected: the loop body is where the wrongness table's worst row lives.