Repository navigation
let-else: a move in the diverging else branch stays on that path — Sema's move state and MIR's pending resets (#1383) - #1551
Merged
Conversation
…ma's move state and MIR's pending resets (#1383) §2.2: a value is moved at a point "if moved on any reaching path that does not diverge"; §9.7: the else branch of `let PAT = e else` must diverge. So a value the else branch consumes is still owned on the continuing path. Two layers carried the else branch's move past the let-else. 1. Sema (the reported rejection). check_let_else checked the else body in the enclosing move state and never restored it, so `keep.len()` after `let Ok(v) = vec_or_err(k) else: return consume(keep) - 100` was "use of moved value". Fix: check the else body as a branch — save the matched path's bind states and moved-field set, push a move control-flow context, restore after (check_if_expr's shape for a diverging arm). 2. MIR, latent behind (1). With Sema fixed, the program printed `-96 1` (expected `-96 5`) and `run --debug-alloc` reported leak count=1. `--dump-mir` of `h`: the continuing block bb5 carries `_2 = const zst(ty16)` before `call len(copy _2)`, and `--trace-ownership h:_2` shows `bb5.stmt20 event=assign before=_2=Init after=_2=Init` — keep blanked while live, its buffer lost. Exact line: lower_let_else's fail path lowered the else body with bare `lower_expr(else_body)`; the reset queued by `consume(move _2)` stayed in pending_reset_locals and the let- else statement's own frame flushed it on the matched path. (The observed-subject path had the same call.) Fix: lower_let_else_branch lowers the else body like an `if` arm — its own statement-temp frame, field_move_in_branch, and flush_pending_resets_since its own start, so its resets are emitted on its path and forgotten; both paths use it. Matrix (base = origin/main ba2d5d1 stage1 -> fixed), 13 cells: - inline `else: return consume(keep)`: rejected -> `-96 5`, leak 0 - block else consuming then `return`: rejected -> `-96 5`, leak 0 - `move b.f` (var base) in else: rejected -> `-99 4`, leak 0 - two let-elses both consuming in else, consume on matched path: rejected -> `-96 6`, leak 0 - per-iteration owner consumed in else + `continue`: rejected -> 8, leak 0 - else consumes then `panic`: rejected -> 5, leak 0 - `if` spelling (control): `-96 4` both - still rejected (correct): owner outside a loop consumed in else + break/ continue then used after/next iteration; subject used in else (#1365: the let-else consumes its subject on both paths); moved before the let-else and used in else; matched path consumes then reads. - binding `v` used in the else branch: accepted by check, MIR lowering fails — filed as #1476 (not this change). Fixtures: da_let_else_fail_owned_payload.w gains the #1383 functions (as the issue asked; leak count=0), behav_let_else_else_moves.w (exact stdout), err_let_else_else_move_then_reuse.w (the matched path is still checked). Audits: tools/move_audit.w had no let-else cell: let_else_else_move_ {return,break,continue}/{drop,vec} (OK) and let_else_else_move_then_reuse (MOVE-ERR). tools/drop_audit.w: let_else_consume_{hit,miss}_{inline, block,field} — exactly-once sums 9/10 (base: COMPILE-FAIL). The ownership validator said ok over the blanked-while-Init store: filed as #1487.
ehartford
force-pushed
the
fix-1383-let-else-move
branch
from
September 23, 2026 21:39
6c864a2 to
afb0e45
Compare
ehartford
marked this pull request as ready for review
September 23, 2026 22:32
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.
Draft: layer 3 of 7 in the ownership/leaks stack (batch battery pending; iterate tier +
:drop-audit/:move-auditon the layer's stage1 done locally). Closes #1383. Filed on the way: #1476, #1487.Audits (tools/drop_audit.w + tools/move_audit.w, candidate = this layer's stage1, baseline = the pinned seed): drop-audit 190 cells, 0 non-PASS, 0 regressions; move-audit 81 cells, 0 vs-expected FAIL (6 new let-else cells).
let-else: a move in the diverging else branch stays on that path — Sema's move state and MIR's pending resets (#1383)
§2.2: a value is moved at a point "if moved on any reaching path that does
not diverge"; §9.7: the else branch of
let PAT = e elsemust diverge. So avalue the else branch consumes is still owned on the continuing path.
Two layers carried the else branch's move past the let-else.
Sema (the reported rejection). check_let_else checked the else body in
the enclosing move state and never restored it, so
keep.len()afterlet Ok(v) = vec_or_err(k) else: return consume(keep) - 100was"use of moved value". Fix: check the else body as a branch — save the
matched path's bind states and moved-field set, push a move control-flow
context, restore after (check_if_expr's shape for a diverging arm).
MIR, latent behind (1). With Sema fixed, the program printed
-96 1(expected
-96 5) andrun --debug-allocreported leak count=1.--dump-mirofh: the continuing block bb5 carries_2 = const zst(ty16)beforecall len(copy _2), and--trace-ownership h:_2shows
bb5.stmt20 event=assign before=_2=Init after=_2=Init— keepblanked while live, its buffer lost. Exact line: lower_let_else's fail
path lowered the else body with bare
lower_expr(else_body); the resetqueued by
consume(move _2)stayed in pending_reset_locals and the let-else statement's own frame flushed it on the matched path. (The
observed-subject path had the same call.) Fix: lower_let_else_branch
lowers the else body like an
ifarm — its own statement-temp frame,field_move_in_branch, and flush_pending_resets_since its own start, so
its resets are emitted on its path and forgotten; both paths use it.
Matrix (base = origin/main ba2d5d1 stage1 -> fixed), 13 cells:
else: return consume(keep): rejected ->-96 5, leak 0return: rejected ->-96 5, leak 0move b.f(var base) in else: rejected ->-99 4, leak 0rejected ->
-96 6, leak 0continue: rejected -> 8, leak 0panic: rejected -> 5, leak 0ifspelling (control):-96 4bothcontinue then used after/next iteration; subject used in else (let-else failure path: owned payload of the non-matching variant leaks (str Err) or faults on drop (Result[Vec[str], str]) #1365:
the let-else consumes its subject on both paths); moved before the
let-else and used in else; matched path consumes then reads.
vused in the else branch: accepted by check, MIR loweringfails — filed as let-else: the pattern's bindings are in scope inside the else branch; check accepts it and MIR lowering then fails #1476 (not this change).
Fixtures: da_let_else_fail_owned_payload.w gains the #1383 functions (as
the issue asked; leak count=0), behav_let_else_else_moves.w (exact
stdout), err_let_else_else_move_then_reuse.w (the matched path is still
checked).
Audits: tools/move_audit.w had no let-else cell: let_else_else_move_
{return,break,continue}/{drop,vec} (OK) and let_else_else_move_then_reuse
(MOVE-ERR). tools/drop_audit.w: let_else_consume_{hit,miss}_{inline,
block,field} — exactly-once sums 9/10 (base: COMPILE-FAIL). The ownership
validator said ok over the blanked-while-Init store: filed as #1487.