Skip to content

MIR: a yield drops its statement temporaries before suspending; the next-body transform keeps a Drop's place (#1412) - #1555

Merged
ehartford merged 1 commit into
fix-1464-task-handle-typefrom
fix-1412-generator-yield-temps
Sep 24, 2026
Merged

ehartford merged 1 commit into
fix-1464-task-handle-typefrom
fix-1412-generator-yield-temps

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Draft: layer 7 of 7 in the ownership/leaks stack (batch battery pending; iterate tier + :drop-audit/:move-audit on the layer's stage1 done locally). Closes #1412. Filed on the way: #1534, #1539, #1548.

Audits (tools/drop_audit.w + tools/move_audit.w, candidate = this layer's stage1, baseline = the pinned seed): drop-audit/move-audit running on this layer's stage1 (results posted when done); generator_yield_{full,stop} verified by hand (base DOUBLE FREE, fixed 3/1).

MIR: a yield drops its statement temporaries before suspending; the next-body transform keeps a Drop's place (#1412)

§13.4: a gen fn suspends at each yield; lower_generator_next_body saves
the generator's named locals into its state and returns, and a later
next() restores them and resumes. Two lines put drops on the wrong path or
the wrong place.

  1. lower_generator_yield lowered the yielded expression and terminated with
    TK_YIELD, leaving the yield statement's temporaries to the statement's
    own flush — which runs after the yield, on the resume path of the next
    call, where nothing initialized them. --validate-ownership on the
    issue's nebula shape: "drop of _9 reaches a path that never initialized
    it (Uninit)"; --dump-mir: the resume target block bb11: drop(_9) @ drop#10 scope-exit _8 for fmt_to_str(i)'s part of f"dev-{i}".
    At runtime yield f"n{i}" in a while loop was a DOUBLE FREE under the
    debug allocator. Fix: the yield expression is lowered in its own
    statement-temp frame; the value moves into a local of its own, then the
    frame's temps drop and its reset-on-move blanks flush on the suspending
    path, before the state is saved (so a let w … yield w saves a blank,
    not a second owner of the str the caller received).

  2. lower_generator_next_body remapped a Drop statement's operand through
    the local map (sd0 = mir_gen_remap_local(&local_map, sd0) for
    StorageLive, StorageDead AND Drop). A Drop names a PLACE; places are
    copied one for one with their base locals remapped, so every Drop in a
    generator body dropped whichever place came next: _5 = move _10; drop(_10) @ drop#6 scope-exit _7 — the origin names the temp _7 the
    drop was for, the statement frees _10, the str the yield hands to the
    caller (let w = f"w{i}"; yield w: DOUBLE FREE; the temp leaked).
    Only StorageLive/StorageDead are remapped now.

Matrix (12 shapes, base = origin/main ba2d5d1 stage1 -> fixed, all
--validate-ownership: ok fixed): while + yield f"n{i}" DOUBLE FREE ->
6 clean; struct yield with an f-string field 15 (validator error ->
ok); yield label(i) ++ "-" ++ label(i+1) (validator error -> ok);
let w = f"…"; yield w DOUBLE FREE -> w0 w1 w2; two yields per iteration
"code generation failed" -> a0 b0! a1 b1!; yields in both arms of an if
DOUBLE FREE -> e0! o1 e2! o3; early break in the consumer DOUBLE FREE ->
clean; int yields unchanged (14, 5); the existing §13.4 fixtures pass.
Still wrong, filed: #1534 — a for loop in a generator loses its counter
at the yield (the nebula generator, for i in 0..count, still panics with
integer overflow on resume: no local but the named lets is carried);
#1548 — a named owned local that stays live across the yield is freed by
the body's scope exit and again by the state's drop; #1539 — the
ownership validator accepts a drop of a Moved place (it said ok over
line 2's double free).

Fixtures: behav_generator_yield_temps.w (exact stdout; base fails to
build), da_generator_yield_temps.w (base DOUBLE FREE). Drop audit gains
generator_yield_{full,stop} (an R yielded beside an f-string temp; base
DOUBLE FREE, fixed 3 and 1).

@ehartford
ehartford added this pull request to stack #1556 September 23, 2026 19:42
@ehartford

Copy link
Copy Markdown
Contributor Author

Audits on this layer's stage1 (candidate = stack top before the rebase onto 3cda6eb; baseline = pinned seed): drop-audit 201 cells, 0 non-PASS, 0 regressions vs baseline (generator_yield_{full,stop} FIXED: DOUBLE-FREE on the seed, PASS here; async_await_inferred FIXED); move-audit 105 cells, 0 vs-expected FAIL. All 20 new fixtures of the stack pass on the rebased top's stage1.

…ext-body transform keeps a Drop's place (#1412)

§13.4: a `gen fn` suspends at each `yield`; lower_generator_next_body saves
the generator's named locals into its state and returns, and a later
next() restores them and resumes. Two lines put drops on the wrong path or
the wrong place.

1. lower_generator_yield lowered the yielded expression and terminated with
   TK_YIELD, leaving the yield statement's temporaries to the statement's
   own flush — which runs after the yield, on the resume path of the next
   call, where nothing initialized them. `--validate-ownership` on the
   issue's nebula shape: "drop of _9 reaches a path that never initialized
   it (Uninit)"; `--dump-mir`: the resume target block `bb11: drop(_9) @
   drop#10 scope-exit _8` for `fmt_to_str(i)`'s part of `f"dev-{i}"`.
   At runtime `yield f"n{i}"` in a while loop was a DOUBLE FREE under the
   debug allocator. Fix: the yield expression is lowered in its own
   statement-temp frame; the value moves into a local of its own, then the
   frame's temps drop and its reset-on-move blanks flush on the suspending
   path, before the state is saved (so a `let w … yield w` saves a blank,
   not a second owner of the str the caller received).

2. lower_generator_next_body remapped a Drop statement's operand through
   the local map (`sd0 = mir_gen_remap_local(&local_map, sd0)` for
   StorageLive, StorageDead AND Drop). A Drop names a PLACE; places are
   copied one for one with their base locals remapped, so every Drop in a
   generator body dropped whichever place came next: `_5 = move _10;
   drop(_10) @ drop#6 scope-exit _7` — the origin names the temp `_7` the
   drop was for, the statement frees `_10`, the str the yield hands to the
   caller (`let w = f"w{i}"; yield w`: DOUBLE FREE; the temp leaked).
   Only StorageLive/StorageDead are remapped now.

Matrix (12 shapes, base = origin/main ba2d5d1 stage1 -> fixed, all
`--validate-ownership: ok` fixed): while + `yield f"n{i}"` DOUBLE FREE ->
6 clean; struct yield with an f-string field 15 (validator error ->
ok); `yield label(i) ++ "-" ++ label(i+1)` (validator error -> ok);
`let w = f"…"; yield w` DOUBLE FREE -> w0 w1 w2; two yields per iteration
"code generation failed" -> a0 b0! a1 b1!; yields in both arms of an if
DOUBLE FREE -> e0! o1 e2! o3; early `break` in the consumer DOUBLE FREE ->
clean; int yields unchanged (14, 5); the existing §13.4 fixtures pass.
Still wrong, filed: #1534 — a `for` loop in a generator loses its counter
at the yield (the nebula generator, `for i in 0..count`, still panics with
integer overflow on resume: no local but the named lets is carried);
#1548 — a named owned local that stays live across the yield is freed by
the body's scope exit and again by the state's drop; #1539 — the
ownership validator accepts a drop of a Moved place (it said ok over
line 2's double free).

Fixtures: behav_generator_yield_temps.w (exact stdout; base fails to
build), da_generator_yield_temps.w (base DOUBLE FREE). Drop audit gains
generator_yield_{full,stop} (an R yielded beside an f-string temp; base
DOUBLE FREE, fixed 3 and 1).
@ehartford
ehartford force-pushed the fix-1412-generator-yield-temps branch from f455efa to 77368cf Compare September 23, 2026 21:39
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery green — ownership stack #1549 → #1555 (top 77368cf, rebased onto main 02301f8, no diff change), driven by the pinned seed with a cold c_import cache:

build        rc=0
:fixpoint    rc=0
:drop-audit  rc=0
:move-audit  rc=0
:test        rc=0   (no red targets)
:test-green  rc=0
:last-green  rc=0

Ready to merge: gh stack merge 1555 --squash.

@ehartford
ehartford marked this pull request as ready for review September 23, 2026 22:32
@ehartford
ehartford merged commit bfa3d1d into main Sep 24, 2026
@ehartford
ehartford deleted the fix-1412-generator-yield-temps branch September 24, 2026 16:55
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.

validate-ownership: generator next body drops a local on a resume path that never initialized it

1 participant