lowering: a mut assigned inside a CE block gets its global/nonlocal (#122) - #124
Merged
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A built-in CE body (
async/seq/result/option) lowers to its own nesteddef/async def, but the closure-capture declarations (DESIGN.md§3) were only computed for function bodies. Soflag <- trueinside anasync { }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. Theresult { }read-modify-write form (attempts <- attempts + 1) hit the louderUnboundLocalErrorinstead; every built-in CE had one form or the other.The fix computes the same
global/nonlocalprelude a function body gets, for all four built-in CE frames:scan_ce_scopemirrorsscan_scopeover CE items —let/let!/fortargets are the frame's binders, aforbody is the same Python frame so it is entered, nested functions and CEs still are not.nonlocal, elseglobal) is factored out oflower_fn_bodyintoclassify_captured/capture_declsand reused; declarations are named after lowering so a renamedmutslot is declared under the name the closure assigns, as before.fn_local_stackwhile 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 flaginside theasync defand printsTrue. User builders were already covered: they desugar to lambdas, whose statement-bodied form goes throughlower_fn_body.Tests: emitted-shape assertions for the
global(async, result) andnonlocal(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.mdgets the feedback item.Closes #122