Skip to content

Commit a1732ba

Browse files
proggeramlugRalph Küpper
andauthored
fix(transform): preserve per-iteration let/const bindings across the async state machine (#6345) (#6353)
* fix(transform): preserve per-iteration let/const bindings across the async state machine (#6345) The async/generator lowering hoisted every body Let into one activation-wide PreallocateBoxes frame, collapsing a loop's per-iteration let/const onto a single box: every closure made in the loop observed the last value. Only hoist bindings that must survive a suspend. * fix(transform): snapshot per-iteration captures that outlive an await (#6345) A binding read after an await keeps its cross-state box, which is shared by every iteration. Copy it into a per-state local at each closure-creation site so each closure gets its own binding. Gated to captures HIR marks read-only (captures minus mutable_captures), with a rollback verifier. * fix(transform): analyze labeled loops for per-iteration bindings (#6345) Stmt::Labeled wraps the loop, so a bare Stmt::For match skipped every labeled loop and each_child_stmt_list unwrapped straight to its body — the loop statement itself was never analyzed. * test(gap): per-iteration loop binding regression fixture (#6345) Covers const/loop-var capture, await before/after the capture, while, do-while, for-of, for-in, for-await-of, nested loops, labeled loops, try/catch/finally, switch, array + destructured bindings, nested closures, sync generators, var (function-scoped, last value), closure-written and outer-written bindings, and a non-async control. --------- Co-authored-by: Ralph Küpper <ralph@skelpo.com>
1 parent 7310e56 commit a1732ba

6 files changed

Lines changed: 1108 additions & 6 deletions

File tree

‎crates/perry-transform/src/generator/lower.rs‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,17 @@ pub fn transform_generator_function_with_extra_captures(
139139
await_async_generator_yield_operands(&mut func.body, next_local_id);
140140
}
141141

142+
// #6345: decide which loop bindings must NOT be hoisted into the
143+
// activation-wide box frame, and snapshot the ones that outlive a suspend
144+
// into per-state locals. Both run BEFORE `linearize_body` so the inserted
145+
// `Let`s land in the same state as the closure that reads them, and before
146+
// `local_id_before` below so the new ids are not swept into
147+
// `extra_local_ids` (which is force-preallocated).
148+
let per_iteration_ids = collect_per_iteration_ids(&func.body);
149+
let mut no_hoist_ids =
150+
snapshot_suspended_loop_captures(&mut func.body, next_local_id, &per_iteration_ids);
151+
no_hoist_ids.extend(per_iteration_ids);
152+
142153
let state_id = alloc_local(next_local_id);
143154
let done_id = alloc_local(next_local_id);
144155
let sent_id = alloc_local(next_local_id); // value passed by caller via next(val)
@@ -223,8 +234,23 @@ pub fn transform_generator_function_with_extra_captures(
223234
}
224235
}
225236

226-
// Collect hoisted var IDs first so we know which Lets to rewrite
227-
let hoisted_for_rewrite = collect_hoisted_vars(&func.body);
237+
// Collect hoisted var IDs first so we know which Lets to rewrite.
238+
//
239+
// #6345: NOT every body `Let` may be hoisted. A `let`/`const` declared in a
240+
// loop gets a FRESH binding per iteration, and a closure made in iteration
241+
// k must capture iteration k's binding. Hoisting moves the declaration into
242+
// the activation-wide `PreallocateBoxes` frame (one box per call), which
243+
// collapses every iteration onto a single cell — so all closures read the
244+
// last value (`for (let i…) { const j = i; fns.push(() => j) }` printed the
245+
// final `j` N times). `no_hoist_ids` (computed above) holds the bindings
246+
// that keep their in-loop declaration — where codegen re-executes and
247+
// re-boxes them every iteration, exactly as the non-async path does — plus
248+
// the per-state snapshot locals. `var` and anything else live across an
249+
// `await` is excluded there and keeps today's hoisting.
250+
let hoisted_for_rewrite: Vec<(LocalId, String, Type)> = collect_hoisted_vars(&func.body)
251+
.into_iter()
252+
.filter(|(id, _, _)| !no_hoist_ids.contains(id))
253+
.collect();
228254
let mut hoisted_ids: std::collections::HashSet<LocalId> =
229255
hoisted_for_rewrite.iter().map(|(id, _, _)| *id).collect();
230256
// The lifted param prologue defines locals (destructured targets + temps)

‎crates/perry-transform/src/generator/mod.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ mod id_scan;
1818
mod iter_result_rewrite;
1919
mod linearize;
2020
mod lower;
21+
mod per_iteration;
2122
mod rewrite_returns;
2223

2324
// Explicit named re-exports so siblings can reach each other via
@@ -41,6 +42,7 @@ pub(crate) use linearize::{
4142
pub(crate) use lower::{
4243
transform_generator_function, transform_generator_function_with_extra_captures,
4344
};
45+
pub(crate) use per_iteration::{collect_per_iteration_ids, snapshot_suspended_loop_captures};
4446
pub(crate) use rewrite_returns::{
4547
body_contains_return, prepend_done_before_returns, rewrite_catch_returns_to_iter_result,
4648
rewrite_iter_results_in_stmts, rewrite_returns_as_done, rewrite_returns_to_labeled_break,

0 commit comments

Comments
 (0)