File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -1152,3 +1152,24 @@ fn test_try_reserve_exact() {
11521152 }
11531153
11541154}
1155+
1156+ #[ test]
1157+ fn test_stable_push_pop ( ) {
1158+ // Test that, if we reserved enough space, adding and removing elements does not
1159+ // invalidate references into the vector (such as `v0`). This test also
1160+ // runs in Miri, which would detect such problems.
1161+ let mut v = Vec :: with_capacity ( 10 ) ;
1162+ v. push ( 13 ) ;
1163+
1164+ // laundering the lifetime -- we take care that `v` does not reallocate, so that's okay.
1165+ let v0 = unsafe { & * ( & v[ 0 ] as * const _ ) } ;
1166+
1167+ // Now do a bunch of things and occasionally use `v0` again to assert it is still valid.
1168+ v. push ( 1 ) ;
1169+ v. push ( 2 ) ;
1170+ v. insert ( 1 , 1 ) ;
1171+ assert_eq ! ( * v0, 13 ) ;
1172+ v. remove ( 1 ) ;
1173+ v. pop ( ) . unwrap ( ) ;
1174+ assert_eq ! ( * v0, 13 ) ;
1175+ }
You can’t perform that action at this time.
0 commit comments