Skip to content

for with a refutable pattern over consuming iteration leaks every skipped element #1490

Closed

Description

@ehartford

Spec: §13.5 — "Refutable patterns are allowed; elements that do not match are skipped and iteration continues." Skipping an owned element (consuming iteration) must still drop it: §2.4 / the mission (a leak is a defect).

fn main:
    var ps: Vec[(i32, str)] = Vec.new()
    ps.push((0, "a".clone()))
    ps.push((1, "b".clone()))
    var n = 0
    for (0, s) in ps.into_iter():
        n = n + s.len() as i32
    print(n)

run --debug-alloc (stage1 of origin/main ba2d5d1): prints 1, then LEAK size=16, leak count=1 — the skipped (1, "b")'s str.

MIR of main: the pattern test's fail edge is switchInt(copy _19) -> [1: bb12, otherwise: bb7] — bb7 is the loop header; the item local _18 (the moved-out (i32, str)) is never dropped on that edge.

Exact line: MirLower.bind_for_element_or_skip, self.lower_pattern_match(item_place, pat_or_sym, matched_bb, continue_bb) with continue_bb = the header: the non-matching edge carries an owned item with no drop. A view iteration (for (0, s) in ps) is unaffected (the item is a reference).

Fix shape: route the fail edge through a block that drops the item (when its type needs drop) and then continues — what the #1403 PR does for comprehension clauses (lower_comprehension_skip_block).

Found while building the #1403 comprehension matrix.

Activity

  1. ehartford commented on Sep 26, 2026

    @ehartford
    ContributorAuthor

    Fixed by #1718 (drop cluster). Verified on main 3ccb998: the issue's repro under run --debug-alloc prints 1, leak count=0 (was LEAK size=16).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions