Skip to content
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

hash_map::OccupiedEntry::take leaves map in an inconsistent state #19292

Closed
spernsteiner opened this issue Nov 25, 2014 · 3 comments
Closed

hash_map::OccupiedEntry::take leaves map in an inconsistent state #19292

spernsteiner opened this issue Nov 25, 2014 · 3 comments

Comments

@spernsteiner
Copy link
Contributor

This code manipulates a HashMap using the new Entry API, checking for consistency after each step.

use std::collections::HashMap;
use std::collections::hash_map::Entry::{Vacant, Occupied};
use std::rand::Rng;

fn check(m: &HashMap<int, ()>) {
    for k in m.keys() {
        assert!(m.contains_key(k),
                "{} is in keys() but not in the map?", k);
    }
}

fn main() {
    let mut m = HashMap::new();
    let mut rng = std::rand::weak_rng();

    // Populate the map with some items.
    for _ in range(0u, 50) {
        let x = rng.gen_range(-10, 10);
        m.insert(x, ());
    }

    for i in range(0u, 1000) {
        let x = rng.gen_range(-10, 10);
        match m.entry(x) {
            Vacant(_) => {},
            Occupied(e) => {
                println!("{}: remove {}", i, x);
                e.take();
            },
        }

        check(&m);
    }
}

The assertion in check should never fail, but in my tests it does so reliably, almost always within the first 10 iterations of the second loop. When it does fail, the value of k is not one of the values previously removed during the loop (that is, keys is right to return this key, and contains_key is wrong to report it as absent).

I tested this with rustc 0.13.0-dev (377d7524a 2014-11-24 13:56:36 +0000) on 64-bit Linux.

@sfackler
Copy link
Member

cc @gankro

@Gankra
Copy link
Contributor

Gankra commented Nov 25, 2014

Fffffff--- looking into it now.

cc @pczarn

@Gankra
Copy link
Contributor

Gankra commented Nov 25, 2014

Oh thank god it was a simple stupid mistake, and not something complicated.

Gankra added a commit to Gankra/rust that referenced this issue Nov 25, 2014
@Gankra Gankra closed this as completed in b1e720f Nov 27, 2014
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

3 participants