fix aliasing in remove()

also add smoke test to detect relocation even in rustc runs
This commit is contained in:
Ralf Jung 2020-03-30 13:11:37 +02:00
parent 4eacf45c9c
commit 8f479e362f
2 changed files with 8 additions and 5 deletions

View File

@ -1366,7 +1366,8 @@ fn test_stable_pointers() {
v.push(13);
// Laundering the lifetime -- we take care that `v` does not reallocate, so that's okay.
let v0 = unsafe { &*(&v[0] as *const _) };
let v0 = &mut v[0];
let v0 = unsafe { &mut *(v0 as *mut _) };
// Now do a bunch of things and occasionally use `v0` again to assert it is still valid.
// Pushing/inserting and popping/removing
@ -1420,6 +1421,10 @@ fn test_stable_pointers() {
assert_eq!(*v0, 13);
next_then_drop(v.splice(5..6, vec![1; 10].into_iter().filter(|_| true))); // lower bound not exact
assert_eq!(*v0, 13);
// Smoke test that would fire even outside Miri if an actual relocation happened.
*v0 -= 13;
assert_eq!(v[0], 0);
}
// https://github.com/rust-lang/rust/pull/49496 introduced specialization based on:

View File

@ -1200,7 +1200,7 @@ impl<T> Vec<T> {
} else {
unsafe {
self.len -= 1;
Some(ptr::read(self.get_unchecked(self.len())))
Some(ptr::read(self.as_ptr().add(self.len())))
}
}
}
@ -2020,9 +2020,7 @@ where
let (lower, _) = iterator.size_hint();
let mut vector = Vec::with_capacity(lower.saturating_add(1));
unsafe {
// `vector` is new, cannot have aliases, so us getting exclusive references
// here is okay.
ptr::write(vector.get_unchecked_mut(0), element);
ptr::write(vector.as_mut_ptr(), element);
vector.set_len(1);
}
vector