Skip to content

Commit 69e7554

Browse files
committed
rollup merge of #19301: Gankro/take-fix
Was taking the value out correctly, but then not doing anything to actually fix the table. derp.
2 parents b095eb1 + b1e720f commit 69e7554

File tree

1 file changed

+35
-1
lines changed
  • src/libstd/collections/hash

1 file changed

+35
-1
lines changed

src/libstd/collections/hash/map.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1376,7 +1376,7 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
13761376

13771377
/// Takes the value out of the entry, and returns it
13781378
pub fn take(self) -> V {
1379-
let (_, _, v) = self.elem.take();
1379+
let (_, v) = pop_internal(self.elem);
13801380
v
13811381
}
13821382
}
@@ -1433,6 +1433,7 @@ mod test_map {
14331433
use hash;
14341434
use iter::{Iterator,range_inclusive,range_step_inclusive};
14351435
use cell::RefCell;
1436+
use rand::{weak_rng, Rng};
14361437

14371438
struct KindaIntLike(int);
14381439

@@ -2073,4 +2074,37 @@ mod test_map {
20732074
assert_eq!(map.get(&10).unwrap(), &1000);
20742075
assert_eq!(map.len(), 6);
20752076
}
2077+
2078+
#[test]
2079+
fn test_entry_take_doesnt_corrupt() {
2080+
// Test for #19292
2081+
fn check(m: &HashMap<int, ()>) {
2082+
for k in m.keys() {
2083+
assert!(m.contains_key(k),
2084+
"{} is in keys() but not in the map?", k);
2085+
}
2086+
}
2087+
2088+
let mut m = HashMap::new();
2089+
let mut rng = weak_rng();
2090+
2091+
// Populate the map with some items.
2092+
for _ in range(0u, 50) {
2093+
let x = rng.gen_range(-10, 10);
2094+
m.insert(x, ());
2095+
}
2096+
2097+
for i in range(0u, 1000) {
2098+
let x = rng.gen_range(-10, 10);
2099+
match m.entry(x) {
2100+
Vacant(_) => {},
2101+
Occupied(e) => {
2102+
println!("{}: remove {}", i, x);
2103+
e.take();
2104+
},
2105+
}
2106+
2107+
check(&m);
2108+
}
2109+
}
20762110
}

0 commit comments

Comments
 (0)