Skip to content

collect into a HashMap, BTreeMap or HashSet leaks a key that was already present #2165

Closed

Description

@ehartford

Repro (main 3207aac; with run --debug-alloc):

use std.collections.HashMap

fn a: "A".to_lower()

fn main:
    let pairs: Vec[(str, i32)] = [(a(), 1), ("b".to_lower(), 2), (a(), 3)]
    let m: HashMap[str, i32] = pairs.iter() |> map(p => (p.0.clone(), p.1)) |> collect[HashMap[str, i32]]()
    print(m.len())      // 2
}

debug-alloc: leak count=1. The same with collect[BTreeMap[str, i32]] and, over the keys alone, collect[HashSet[str]]: one leak per duplicate. m.insert(a(), 1) twice by hand leaks nothing, so it is the collect path's insert that does not drop the key (or the displaced one) when the key is already there.

test/spec/spec_ss13_3_collect_targets.w has shown leak count=1 on main for this reason; the spec lanes do not run under the allocator.

Route: debug allocator, then tools/debug_drop.w for the alloc site, then the collect lowering's duplicate-key branch.

Activity

  1. ehartford commented on Oct 5, 2026

    @ehartford
    ContributorAuthor

    Fixed on main 0ade380 (stack #2175, battery green; the stack top also builds and passes its new tests on linux-x86_64 and windows-x86_64).

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