Skip to content

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 into
fix-1383-let-else-movefrom
fix-1403-comprehension-refutable
Sep 24, 2026
Merged

ehartford merged 1 commit into
fix-1383-let-else-movefrom
fix-1403-comprehension-refutable

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Draft: layer 4 of 7 in the ownership/leaks stack (batch battery pending; iterate tier + :drop-audit/:move-audit on 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, "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
ehartford added this pull request to stack #1556 September 23, 2026 19:42
…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
ehartford force-pushed the fix-1403-comprehension-refutable branch from 6c99742 to 2bea77f 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 ac55033 into main Sep 24, 2026
@ehartford
ehartford deleted the fix-1403-comprehension-refutable 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.

Comprehension clause ignores refutable patterns: [v for Some(v) in opts] binds every element

1 participant