Skip to content

ADR 0064: Generators consumed in place

Status: Final Type: Feature Created: 2026-10-26 Amends: the spec's §5.2 (PP030, PP031) Amended-By: 0072 (a state machine for the generators the inlining cannot take) Related-ADRs: 0028 (a generator expression in a consuming argument, and the comprehension a loop is), 0007 (an exception leaving the generator), 0033 (a name read before it is bound). Provenance: the Shed Skin census (sunfish, mwmatching, sudoku5, life).

Context

The spec excludes generators (PP030, PP031): a generator suspends at each yield and resumes on the next request, which needs a frame that outlives the call. This compiler has none. Real programs write them anyway, mostly in one shape: a function that walks something and yields what it finds, called where a loop, a comprehension or a reducer consumes it whole. sunfish's gen_moves is consumed by for, by a comprehension and by any. life's generator is consumed by a for.

In that shape nothing is suspended across anything the program can see. The consumer asks for the next value, the generator runs to the next yield, the consumer's body runs, then the generator resumes. Those turns, taken in one place, are a loop written out.

Decision

A generator function is accepted when each call to it is consumed where it is made: the iterable of a for (which a comprehension and a generator expression are), or the argument of list, sorted, sum, min, max, set, any, all or join, which become the generator expression (v for v in g(...)) that ADR 0028 already lowers. The generator is never a function of its own; each consuming loop gets its body written in:

  1. Names renamed apart. The generator's parameters and locals take a prefix fresh to the loop, so they meet no name of the consumer, as a comprehension's target does (0028's "Comprehensions"). The parameters are bound from the call's arguments in CPython's order, each through its annotation, as a call would type it.
  2. A yield v binds the loop's target and runs the body. The body is lowered once. Each yield records which one it is and jumps to the body; the body's end and a continue in it go to a dispatch that resumes after that yield. A break in the body leaves the walk, skipping the loop's else; the generator's end, or a return, runs the else. yield from xs is for v in xs: yield v.
  3. Exceptions are the consumer's. An exception the generator raises leaves through the consumer's handlers, as it leaves CPython's generator through the for. The raiser walk counts a generator as the function it was written as, so the callers check the pending slot.

The inlining takes only the shapes it can write in exactly. A generator held in a name and consumed later needs a suspended frame. So does one that consumes itself, directly or through another, which would be written into itself without end. 0072 lowers both as a state machine when they qualify. A yield inside a try or a with would put the consumer's body inside them, where CPython runs it outside, so the body's exception would meet the generator's handler and a break would skip a finally that CPython runs when it closes the generator; it is refused. Writing the body in would also differ from CPython for a yield whose value is used (x = yield v, send), and for a generator that defines a function or a class, declares global, takes *args, **kwargs or keyword-only parameters, or has a default that is not a constant. These are refused.

A generator expression held in a name (gen = (x * 2 for x in xs), then print(sorted(gen))) is a generator expression consumed in place with a name in between, and lowers as one: a rewrite before lowering (frontend/held_generators.py) puts the expression at the one read and drops the binding. CPython evaluates the first iterable when the generator is created and the rest as it is consumed. The rewrite is therefore exact only where moving the expression changes nothing CPython shows. The consumer must be the next statement. The name must be bound once and read once in the function, as the argument of a call that consumes a whole iterable. The first iterable must be a name, or range(...) over constants and names; each of those names is a parameter or assigned earlier in the same block, never deleted, and rebound by nothing in the consumer statement.

Consequences

  • No new runtime, IR or backend line: a generator is control flow in the consumer, which all backends already lower.
  • Each consuming loop holds a copy of the generator's body, so code grows with the number of consuming sites. A generator's locals live in the consumer's frame and are released with it, rather than when the generator finishes; nothing a POST program can observe depends on when, files aside, and a with around a yield is refused.
  • A generator that no loop consumes is never lowered, so an error in the body is not reported until a loop consumes it.

Alternatives considered

  • Every generator as a state machine: a frame per generator object, holding its locals and the yield it stopped at. It covers every shape, at the cost of a heap object per call where the inlining is exact and free; 0072 takes it for the shapes the inlining cannot.
  • Materialising the generator into a list: wrong for any/all and for a consumer that breaks, since the generator would run past where CPython stops it and its side effects would reorder against the consumer's.
  • Keeping PP030: a refusal of well-typed code is distance to record.