Repository navigation
let-else: the failing path drops the whole subject before it diverges (#1365) - #1385
Merged
Merged
Conversation
…; 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`).
Contributor
Author
ehartford
force-pushed
the
fix-1365-let-else-else-drop
branch
from
September 23, 2026 01:19
f1de719 to
160e603
Compare
ehartford
marked this pull request as ready for review
September 23, 2026 01:19
This was referenced Sep 23, 2026
Merged
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.
Fixes #1365
Route: debug allocator → MIR / drop-state dump → validator. Root cause is in
MirLower.lower_let_else:returnhad no owner. Seed fail block:bb3: _0 = -1; _2 = zst; return;with_3=Init. Result:LEAK size=16,leak count=1.lower_patternscheduled the binding's scope-exit drop before the else body was lowered, so the elsereturnemitteddrop(_5) @ drop#7 scope-exit _5for a binding it never initialized. Result: rc=138 inwith_str_free_drop_origin.--validate-allreports: 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):
-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 newda_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.