Skip to content

lowering: a mut assigned inside a CE block gets its global/nonlocal (#122) - #124

Merged
simontreanor merged 1 commit into
mainfrom
fix/mut-in-ce-frame
Aug 30, 2026
Merged

lowering: a mut assigned inside a CE block gets its global/nonlocal (#122)#124
simontreanor merged 1 commit into
mainfrom
fix/mut-in-ce-frame

Conversation

@simontreanor

Copy link
Copy Markdown
Owner

A built-in CE body (async/seq/result/option) lowers to its own nested def/async def, but the closure-capture declarations (DESIGN.md §3) were only computed for function bodies. So flag <- true inside an async { } block bound a silent local in the coroutine and the module binding never changed — the quiet miscompile #122 reports, where a finished host idled forever on a flag that could not flip. The result { } read-modify-write form (attempts <- attempts + 1) hit the louder UnboundLocalError instead; every built-in CE had one form or the other.

The fix computes the same global/nonlocal prelude a function body gets, for all four built-in CE frames:

  • scan_ce_scope mirrors scan_scope over CE items — let/let!/for targets are the frame's binders, a for body is the same Python frame so it is entered, nested functions and CEs still are not.
  • The split (captured name in an enclosing function frame → nonlocal, else global) is factored out of lower_fn_body into classify_captured/capture_decls and reused; declarations are named after lowering so a renamed mut slot is declared under the name the closure assigns, as before.
  • The CE's own binders are pushed on fn_local_stack while its items lower, so a lambda nested inside the block classifies its captures against the CE frame too (and the fold/decode inlining guards, which union the stack as an over-approximation, only get more conservative).

The issue's repro now emits global flag inside the async def and prints True. User builders were already covered: they desugar to lambdas, whose statement-bodied form goes through lower_fn_body.

Tests: emitted-shape assertions for the global (async, result) and nonlocal (enclosing-function mut) cases, plus e2e runs of the filed repro and a seq counter. DESIGN.md §3 records that a CE body is a frame under the same rule; ROADMAP.md gets the feedback item.

Closes #122

…122)

A built-in CE body lowers to its own nested def/async def, but the
closure-capture declarations were only computed for function bodies, so
`flag <- true` inside an async { } block bound a silent local and the
module binding never changed (result { }'s read-modify-write form hit
UnboundLocalError instead). All four built-in CE frames now compute the
same global/nonlocal prelude a function body gets, and the CE's own
binders join the frame stack so a lambda nested in the block classifies
its captures against them.

Closes #122
@simontreanor
simontreanor merged commit 5a13c92 into main Aug 30, 2026
16 checks passed
@simontreanor
simontreanor deleted the fix/mut-in-ce-frame branch August 30, 2026 23:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Assigning a module mut from inside an async block silently binds a local: the nested async def lacks the global

1 participant