Skip to content

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
ehartford merged 1 commit into
fix-1392-error-display-leakfrom
fix-1383-let-else-move
Sep 24, 2026
Merged

ehartford merged 1 commit into
fix-1392-error-display-leakfrom
fix-1383-let-else-move

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Draft: layer 3 of 7 in the ownership/leaks stack (batch battery pending; iterate tier + :drop-audit/:move-audit on 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 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:

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
ehartford added this pull request to stack #1556 September 23, 2026 19:42
…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
ehartford force-pushed the fix-1383-let-else-move branch from 6c864a2 to afb0e45 Compare September 23, 2026 21:39
@ehartford
ehartford marked this pull request as ready for review September 23, 2026 22:32
@ehartford
ehartford merged commit b4c3819 into main Sep 24, 2026
@ehartford
ehartford deleted the fix-1383-let-else-move 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.

Sema: a move inside a let-else's diverging else body marks the value moved after the let-else

1 participant