@@ -1376,7 +1376,7 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
1376
1376
1377
1377
/// Takes the value out of the entry, and returns it
1378
1378
pub fn take ( self ) -> V {
1379
- let ( _, _ , v) = self . elem . take ( ) ;
1379
+ let ( _, v) = pop_internal ( self . elem ) ;
1380
1380
v
1381
1381
}
1382
1382
}
@@ -1433,6 +1433,7 @@ mod test_map {
1433
1433
use hash;
1434
1434
use iter:: { Iterator , range_inclusive, range_step_inclusive} ;
1435
1435
use cell:: RefCell ;
1436
+ use rand:: { weak_rng, Rng } ;
1436
1437
1437
1438
struct KindaIntLike ( int ) ;
1438
1439
@@ -2073,4 +2074,37 @@ mod test_map {
2073
2074
assert_eq ! ( map. get( & 10 ) . unwrap( ) , & 1000 ) ;
2074
2075
assert_eq ! ( map. len( ) , 6 ) ;
2075
2076
}
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 ( 0 u, 50 ) {
2093
+ let x = rng. gen_range ( -10 , 10 ) ;
2094
+ m. insert ( x, ( ) ) ;
2095
+ }
2096
+
2097
+ for i in range ( 0 u, 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
+ }
2076
2110
}
0 commit comments