You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
A for loop in a generator loses its iteration state at every yield (range counter, index, iterator not carried) #1534
Spec: §13.4 (generators) — a gen fn body is ordinary code that suspends at each yield; §13.5 — for is iteration. A for loop in a generator body loses its iteration state at every yield.
gen fn upto(count: i32) -> i32:
for i in 0..count:
yield i * 10
fn main:
var n = 0
for v in upto(4):
n = n + v
print(n)
Actual (stage1 of origin/main ba2d5d1): panic: integer overflow: i32 addition out of range. Expected 60.
MIR of __with_generator_next_…: the range loop's counter _3 and end _4 are unnamed temps; the entry block restores only the state's named fields (_2 = copy _1.count) and dispatches the resume to bb6: goto -> bb4, where bb4: _3 = binop(add, copy _3, const 1ty3) reads a counter no path on this call initialized. The yield block saves only _1.count.
Exact site: the generator state is Sema's finalize_generator_state_type — __with_generator_resume, the params, and every let found by generator_collect_state_fields (src/SemaCheck.w), which skips an NK_FOR's binding and cannot see MirLower's hidden loop locals (range counter/end, a slice/Vec index and length, a generic iterator). lower_generator_next_body (src/MirLower.w) saves/restores locals by matching those names (gen_save_generator_fields / gen_restore_generator_fields), so everything else live across a yield is lost. The same holds for a for over a Vec, a slice or an iterator, and for the loop's binding.
Tool gap: --validate-ownership / --validate-all print ok for this body — nothing checks a read of a local on a path that never initialized it (the drop check covers drops only).
Fix direction: the state must carry every local live across a yield, not only named lets — either Sema adds synthetic state fields for each for in a generator body (binding + hidden loop locals, named so MirLower's loop lowering in a generator binds them), or the next-body transform derives the carried set from MIR liveness and the state layout follows it.
Found while fixing #1412 (the nebula extract_packets generator is a for loop, so after #1412's fix it still reads garbage on resume).
Superseded in design by D69 / spec v7.3 (#1723, merged): generators are push-based, so there is no state struct to carry locals across a yield. This issue closes when the push lowering lands (branch gen-push-impl); its repro becomes a behavior test there.
Activity
MIR: a yield drops its statement temporaries before suspending; the n…
MIR: a yield drops its statement temporaries before suspending; the n…
ehartford commentedon Sep 26, 2026
Superseded in design by D69 / spec v7.3 (#1723, merged): generators are push-based, so there is no state struct to carry locals across a yield. This issue closes when the push lowering lands (branch gen-push-impl); its repro becomes a behavior test there.
D69: generators lower as calls — a gen fn calls the consumer's loop b…
D69 tests: push generators — loops in generators, stops, views, pipel…
D69: generators lower as calls — a gen fn calls the consumer's loop b…
D69 tests: push generators — loops in generators, stops, views, pipel…
D69: generators lower as calls — a gen fn calls the consumer's loop b…
D69 tests: push generators — loops in generators, stops, views, pipel…
D69: generators lower as calls — a gen fn calls the consumer's loop b…
D69 tests: push generators — loops in generators, stops, views, pipel…
D69: generators lower as calls — a gen fn calls the consumer's loop b…
D69 tests: push generators — loops in generators, stops, views, pipel…
D69: generators lower as calls — a gen fn calls the consumer's loop b…
D69 tests: push generators — loops in generators, stops, views, pipel…
D69: generators lower as calls — push generators, std.generators pipe…