@@ -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
@@ -2062,4 +2063,37 @@ mod test_map {
2062
2063
assert_eq ! ( map. get( & 10 ) . unwrap( ) , & 1000 ) ;
2063
2064
assert_eq ! ( map. len( ) , 6 ) ;
2064
2065
}
2066
+
2067
+ #[ test]
2068
+ fn test_entry_take_doesnt_corrupt ( ) {
2069
+ // Test for #19292
2070
+ fn check ( m : & HashMap < int , ( ) > ) {
2071
+ for k in m. keys ( ) {
2072
+ assert ! ( m. contains_key( k) ,
2073
+ "{} is in keys() but not in the map?" , k) ;
2074
+ }
2075
+ }
2076
+
2077
+ let mut m = HashMap :: new ( ) ;
2078
+ let mut rng = weak_rng ( ) ;
2079
+
2080
+ // Populate the map with some items.
2081
+ for _ in range ( 0 u, 50 ) {
2082
+ let x = rng. gen_range ( -10 , 10 ) ;
2083
+ m. insert ( x, ( ) ) ;
2084
+ }
2085
+
2086
+ for i in range ( 0 u, 1000 ) {
2087
+ let x = rng. gen_range ( -10 , 10 ) ;
2088
+ match m. entry ( x) {
2089
+ Vacant ( _) => { } ,
2090
+ Occupied ( e) => {
2091
+ println ! ( "{}: remove {}" , i, x) ;
2092
+ e. take ( ) ;
2093
+ } ,
2094
+ }
2095
+
2096
+ check ( & m) ;
2097
+ }
2098
+ }
2065
2099
}
0 commit comments