Skip to content

let-else: the failing path drops the whole subject before it diverges (#1365) - #1385

Merged
ehartford merged 1 commit into
mainfrom
fix-1365-let-else-else-drop
Sep 23, 2026
Merged

ehartford merged 1 commit into
mainfrom
fix-1365-let-else-else-drop

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Fixes #1365

Route: debug allocator → MIR / drop-state dump → validator. Root cause is in MirLower.lower_let_else:

  • Leak: the subject's cleanup was retired after the success path was lowered. That state is not path-sensitive, so the else body's return had no owner. Seed fail block: bb3: _0 = -1; _2 = zst; return; with _3=Init. Result: LEAK size=16, leak count=1.
  • Crash: lower_pattern scheduled the binding's scope-exit drop before the else body was lowered, so the else return emitted drop(_5) @ drop#7 scope-exit _5 for a binding it never initialized. Result: rc=138 in with_str_free_drop_origin. --validate-all reports: drop of _5 reaches a path that never initialized it.

Fix: retire the subject's cleanup once, before either path is lowered. Lower the fail block first: blank the moved source, emit drop(_3) @ let-else-fail, then lower the else body under a saved move state and restore that state for the continuation. Bind on the success path after that.

Evidence (release binary built from this branch):

  • Both repros: -1, debug-alloc: leak count=0.
  • :drop-audit: 176 cells, 0 non-PASS, 0 regressions. It has 40 new let-else cells. The seed fails all 30 miss cells.
  • :move-audit: 15 cells, 0 FAIL.
  • :debug-alloc-tests: ok, including the new da_let_else_fail_owned_payload.w. The seed crashes that fixture (rc=139).

Drop scheduling is alone in this batch. It does not touch MirCore drop-state storage (#1353). The full battery (build/fixpoint/test) has not run yet, so this stays a draft.

Filed from the matrix: #1373, #1382, #1383, #1384, and a comment on #750.

…; bindings drop only where bound (#1365)

§9.7/§2.4: `let PAT = subject else: <diverge>` consumes the subject on both
paths. MirLower.lower_let_else lowered the success path first and then the
else body, which broke both paths' ownership:

1. Leak (repro 1, `Result[i32, str]` Err). The subject's cleanup was retired
   (cancel_stmt_temp_for_local / cancel_scheduled_value_drop_for_local /
   mark_local_value_moved) after lowering the success path, and that state is
   not path-sensitive, so the else body's `return` found no owner. Seed MIR,
   fail block: `bb3: _0 = -1; _2 = zst; return;` with `_3 = Init` in
   --dump-drop-state. `run --debug-alloc`: `LEAK size=16`, `leak count=1`.

2. Garbage drop (repro 2, `Result[Vec[str], str]`). lower_pattern scheduled
   the binding `v`'s scope-exit drop before the else body was lowered, so its
   `return` dropped the never-initialized binding: seed MIR
   `bb3: ... drop(_5) @ drop#7 scope-exit _5; return;` (StorageLive(_5) is
   only in bb2). rc=138 in with_str_free_drop_origin; --validate-all:
   "drop of _5 reaches a path that never initialized it (Uninit)".

Fix: retire the subject's cleanup once, before either path; lower the fail
block first — blank the moved source (reset-on-move), drop the whole
materialized subject (`drop(_3) @ let-else-fail`), then the else body under
a saved move state that is restored for the continuation — and only then
bind on the success path. After: both repros print -1 with
`debug-alloc: leak count=0`; repro 2 --validate-all: ok.

Coverage: tools/drop_audit.w had no let-else cells (the audit stayed
silent). It gains 40: Result/Option/user enum/nested Option[enum]/struct
payload x subject temp|local x hit, and miss x return|break|continue. The
seed fails all 30 miss cells (RUN-FAIL); the candidate passes all 176 cells,
0 regressions. test/debug_alloc/da_let_else_fail_owned_payload.w pins the
str / Vec[str] / enum / nested / struct / tuple-with-`_` matrix at leak
count=0 (the seed crashes it, rc=139). :move-audit 15 cells, 0 FAIL.

Filed from the matrix: #1373 (qualified variant pattern in let), #1382
(multi-statement else block), #1383 (Sema else-body moves), #1384
(validate-ownership blind to leaks at return), #750 comment (user `fn src`).
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery (seed-driven, alone per the isolation rule, rebased on main dd786a4 → 160e603, force-pushed so the PR is the tested tree): build ok, :fixpoint ok, :drop-audit ok (incl. the 40 new let-else cells), :move-audit ok, :test ok (no reds), :test-green ok, :last-green ok.

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.

let-else failure path: owned payload of the non-matching variant leaks (str Err) or faults on drop (Result[Vec[str], str])

1 participant