Repository navigation
MIR: a comprehension clause's refutable pattern skips what it does not match; a skipped owned element drops once (#1403) - #1552
Merged
ehartford merged 1 commit intoSep 24, 2026
Conversation
…t match; a skipped owned element drops once (#1403) §13.6: a comprehension clause is `for PATTERN in EXPR`, "always iteration"; §13.5: "Refutable patterns are allowed; elements that do not match are skipped and iteration continues." Exact line: MirLower.bind_comprehension_element's pattern branch called only `lower_pattern(pat, item_place)` — the binding lowering — never `lower_pattern_match`, so there was no fail edge and every element bound. `[v for Some(v) in opts]` bound None's absent payload (`3`, `vs[1]` = 0) and `[b for (0, b) in ts]` bound `(2, 3)`'s `b`. Over consuming iteration of Option[W] the None arm's garbage payload was dropped (the base run of the drop-audit cell dies with no output). Fix: the pattern branch tests the pattern first; the fail edge leaves through a skip block that continues at the clause's next element. Its callers pass that continue block (the counter increment for range/slice/vec clauses; the loop header for the generic-iterator clause) and whether the clause owns the element: only the generic-iterator clause does (the element moved out of next()'s Option) — there the skip block drops it, and it returns to the header without the per-iteration scope pop, which would drop pattern bindings the skipped path never initialized. Views and Copy elements (range, slice, vec clauses) need no drop. (A first version emitted the skip block's drop before the match terminated the current block; a block's statements are appended contiguously, so the drop landed in both blocks — `--validate-all` caught it: "drop of _24 reaches a path that never initialized it (Uninit)".) Matrix (base -> fixed; leak count 0 in every fixed cell): issue repro `3`/`2` -> `2 1 4`/`1 1`; `(0, s)` over `&(i32, str)` views 3 -> 2 (source intact); `Some(s)` over `Option[str]` views; consuming Option[str], (i32, str), user enum `.A(s)` -> right count and values, skipped element dropped; pattern + filter; two clauses; map and set comprehensions; enum variant; literal pattern over a range; irrefutable tuple (unchanged); `_` part (unchanged). `--validate-all: ok` on the consuming cells. Fixtures: behav_comprehension_refutable_pattern.w (exact stdout; base fails at `3`), da_comprehension_refutable_skip.w (base: assertion failed; fixed: leak count=0). Drop audit had no comprehension-pattern cell: comprehension_skip_{tuple,option,view} (sum 7 each; option dies on the base). Found, not fixed (filed): #1490 — the `for` statement's refutable pattern skips an owned element without dropping it; #1501 — a consuming comprehension's filter leaks the elements it filters out when the element would have moved (pre-existing; the filter-with-pattern cell stays out of the audit until it is fixed).
ehartford
force-pushed
the
fix-1403-comprehension-refutable
branch
from
September 23, 2026 21:39
6c99742 to
2bea77f
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 4 of 7 in the ownership/leaks stack (batch battery pending; iterate tier +
:drop-audit/:move-auditon the layer's stage1 done locally). Closes #1403. Filed on the way: #1490, #1501.Audits (tools/drop_audit.w + tools/move_audit.w, candidate = this layer's stage1, baseline = the pinned seed): drop-audit: 0 regressions on its own cells (comprehension_skip_{tuple,option,view} PASS; the only non-PASS cells are layer 5's, which pass there); move-audit likewise.
MIR: a comprehension clause's refutable pattern skips what it does not match; a skipped owned element drops once (#1403)
§13.6: a comprehension clause is
for PATTERN in EXPR, "alwaysiteration"; §13.5: "Refutable patterns are allowed; elements that do not
match are skipped and iteration continues."
Exact line: MirLower.bind_comprehension_element's pattern branch called only
lower_pattern(pat, item_place)— the binding lowering — neverlower_pattern_match, so there was no fail edge and every element bound.[v for Some(v) in opts]bound None's absent payload (3,vs[1]= 0) and[b for (0, b) in ts]bound(2, 3)'sb. Over consuming iteration ofOption[W] the None arm's garbage payload was dropped (the base run of the
drop-audit cell dies with no output).
Fix: the pattern branch tests the pattern first; the fail edge leaves through
a skip block that continues at the clause's next element. Its callers pass
that continue block (the counter increment for range/slice/vec clauses; the
loop header for the generic-iterator clause) and whether the clause owns the
element: only the generic-iterator clause does (the element moved out of
next()'s Option) — there the skip block drops it, and it returns to the
header without the per-iteration scope pop, which would drop pattern
bindings the skipped path never initialized. Views and Copy elements
(range, slice, vec clauses) need no drop.
(A first version emitted the skip block's drop before the match terminated
the current block; a block's statements are appended contiguously, so the
drop landed in both blocks —
--validate-allcaught it: "drop of _24reaches a path that never initialized it (Uninit)".)
Matrix (base -> fixed; leak count 0 in every fixed cell): issue repro
3/2->2 1 4/1 1;(0, s)over&(i32, str)views 3 -> 2 (sourceintact);
Some(s)overOption[str]views; consuming Option[str],(i32, str), user enum
.A(s)-> right count and values, skipped elementdropped; pattern + filter; two clauses; map and set comprehensions; enum
variant; literal pattern over a range; irrefutable tuple (unchanged);
_part (unchanged).
--validate-all: okon the consuming cells.Fixtures: behav_comprehension_refutable_pattern.w (exact stdout; base fails
at
3), da_comprehension_refutable_skip.w (base: assertion failed; fixed:leak count=0). Drop audit had no comprehension-pattern cell:
comprehension_skip_{tuple,option,view} (sum 7 each; option dies on the
base).
Found, not fixed (filed): #1490 — the
forstatement's refutable patternskips an owned element without dropping it; #1501 — a consuming
comprehension's filter leaks the elements it filters out when the element
would have moved (pre-existing; the filter-with-pattern cell stays out of
the audit until it is fixed).