Skip to content

D115: match move xs on a List parameter whose pattern takes nothing out leaks the List ([..] => 1) #2351

Closed

Description

@ehartford

Spec §9.7 (D115), §2.5. The D115 entry notes "match move v: … the leak it has today is a defect of this ruling's implementation"; here is the minimal shape, on main 7a3737b66 with the installed compiler:

fn take(xs: List[i32]) -> i32:
    match move xs:
        [..] => 1
fn main:
    let ys = [1, 2, 3]
    print(take(ys))

with run --debug-alloc: LEAK … size=32 origin=List, leak count=1. The same match on a local (let ys = [1, 2, 3]; match move ys: [..] => 1) does not leak, and a pattern that binds the remainder ([h, ..t]) does not leak either: the parameter moved into the match is dropped by nobody when the pattern takes nothing out of it.

Consequence: the canonical D115 recursion fn sum(xs: List[i32]) -> i32: match move xs: [h, ..t] => h + sum(t); [] => 0 leaks the subject's buffer once, at the [] arm of the deepest call (one 400-byte buffer for a hundred elements). The allocation-count fixture for Amendment 1 (test/debug_alloc/da_d115_offset_rest_allocs.w, expecting leak count=0) waits on this fix; the behavior fixture behav_d115_amendment1_offset_rest.w covers the O(1) take without the allocator check.

Activity

  1. ehartford commented on Oct 12, 2026

    @ehartford
    ContributorAuthor

    A second shape of the same defect, on the installed compiler (main 7a3737b66): a head taken with a bare .. over a moved parameter leaks the remainder.

    type Tok { id: i32 }
    impl Drop for Tok:
        move fn drop(): print(f"drop {self.id}")
    fn first_tok(toks: List[Tok]) -> i32:
        match move toks:
            [t, ..] => t.id
            [] => 0
    fn main:
        print(first_tok([Tok { id: 9 }, Tok { id: 1 }]))
    

    Prints drop 9 and 9, never drop 1; --debug-alloc reports one leaked List buffer. With [t, ..rest] (the remainder bound) every element drops. So the missing drop is the moved parameter's own: a bare .. leaves the remainder "with the subject", and the subject that match move consumed is dropped by nobody. The #2289 fixture (behav_2289_guard_after_owned_list_take.w, branch d115-step2) binds ..rest to stay clear of this.

  2. ehartford commented on Oct 12, 2026

    @ehartford
    ContributorAuthor

    Fixed on branch gaps-2 (from a31a7df; gate running): in lower_slice_pattern_take_list (and its array twin) a pattern with no remainder binding moves what it leaves in the subject into a statement temporary (pending_move_temp_locals), so it dies at the statement's end as a temporary subject always did (§2.4: after a match arm's body, right after a let … else), and a consumed parameter or local is no longer dropped by nobody. The move is logged, so a failed guard restores it. Both repro shapes and the canonical recursion are leak-free under the debug allocator: test/debug_alloc/da_2351_match_move_subject_dropped.w. The D115 Amendment 1 recursion that waited on this is in the same fixture.

  3. added 3 commits that reference this issue on Oct 12, 2026
  4. ehartford commented on Oct 12, 2026

    @ehartford
    ContributorAuthor

    Fixed in c5a0209 (#2357): a pattern that takes nothing out, or leaves a bare .. remainder, moves what it leaves into a statement temporary that dies at the statement's end; fixture da_2351_match_move_subject_dropped.w.

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