Skip to content

HashMap.clear() leads to VacantEntry.set() asserting #19485

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
frankmcsherry opened this issue Dec 3, 2014 · 2 comments
Closed

HashMap.clear() leads to VacantEntry.set() asserting #19485

frankmcsherry opened this issue Dec 3, 2014 · 2 comments

Comments

@frankmcsherry
Copy link
Contributor

The following code causes a panic in set(i) ('assertion failed: probe.index() != idx_end'). The current belief is that clear() causes the underlying array to shrink below tested levels (being fixed independently), at which point a corner case may show up in insertion.

use std::collections::HashMap;
use std::collections::hash_map::Vacant;

fn main()
{
    let mut m = HashMap::with_capacity(4); 

    m.clear(); 

    for i in range(0u, 4) 
    { 
        match m.entry(i) 
        { 
            Vacant(x) => { x.set(i); }, 
            _ => {}, 
        } 
    }
}
@cgaebel
Copy link
Contributor

cgaebel commented Dec 3, 2014

I'm working on this right now.

cgaebel pushed a commit to cgaebel/rust that referenced this issue Dec 3, 2014
`HashMap::clear()` was failing to set the capacity to a power of 2,
and could even go under INITIAL_CAPACITY! This broke invariants,
and at times (see unit test) led to failure of sanity-checking
asserts.

This patch fixes that.

Fixes rust-lang#19485
@frankmcsherry
Copy link
Contributor Author

This example now seems to work great with recently landed changes. Thanks!

lnicola pushed a commit to lnicola/rust that referenced this issue Apr 28, 2025
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

No branches or pull requests

2 participants