Skip to content

Commit 6da4df9

Browse files
committed
Make the semantics of Vec::truncate(N) consistent with slices.
This commit simplifies the implementation of `Vec::truncate(N)` and makes its semantics identical to dropping the `[vec.len() - N..]` sub-slice tail of the vector, which is the same behavior as dropping a vector containing the same sub-slice. This changes two unspecified aspects of `Vec::truncate` behavior: * the drop order, from back-to-front to front-to-back, * the behavior of `Vec::truncate` on panics: if dropping one element of the tail panics, currently, `Vec::truncate` panics, but with this PR all other elements are still dropped, and if dropping a second element of the tail panics, with this PR, the program aborts. Programs can trivially observe both changes. For example ([playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=7bef575b83b06e82b3e3529e4edbcac7)): ```rust fn main() { struct Bomb(usize); impl Drop for Bomb { fn drop(&mut self) { panic!(format!("{}", self.0)); } } let mut v = vec![Bomb(0), Bomb(1)]; std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { v.truncate(0); })); assert_eq!(v.len(), 1); std::mem::forget(v); } ``` panics printing `1` today and succeeds. With this change, it panics printing `0` first (due to the drop order change), and then aborts with a double-panic printing `1`, just like dropping the `[Bomb(0), Bomb(1)]` slice does, or dropping `vec![Bomb(0), Bomb(1)]` does.
1 parent 572d3d9 commit 6da4df9

File tree

1 file changed

+12
-22
lines changed

1 file changed

+12
-22
lines changed

src/liballoc/vec.rs

+12-22
Original file line numberDiff line numberDiff line change
@@ -685,25 +685,20 @@ impl<T> Vec<T> {
685685
/// [`drain`]: #method.drain
686686
#[stable(feature = "rust1", since = "1.0.0")]
687687
pub fn truncate(&mut self, len: usize) {
688-
if mem::needs_drop::<T>() {
689-
let current_len = self.len;
690-
unsafe {
691-
let mut ptr = self.as_mut_ptr().add(self.len);
692-
// Set the final length at the end, keeping in mind that
693-
// dropping an element might panic. Works around a missed
694-
// optimization, as seen in the following issue:
695-
// https://github.com/rust-lang/rust/issues/51802
696-
let mut local_len = SetLenOnDrop::new(&mut self.len);
697-
698-
// drop any extra elements
699-
for _ in len..current_len {
700-
local_len.decrement_len(1);
701-
ptr = ptr.offset(-1);
702-
ptr::drop_in_place(ptr);
703-
}
688+
// This is safe because:
689+
//
690+
// * the slice passed to `drop_in_place` is valid; the `len > self.len`
691+
// case avoids creating an invalid slice, and
692+
// * the `len` of the vector is shrunk before calling `drop_in_place`,
693+
// such that no value will be dropped twice in case `drop_in_place`
694+
// were to panic once (if it panics twice, the program aborts).
695+
unsafe {
696+
if len > self.len {
697+
return;
704698
}
705-
} else if len <= self.len {
699+
let s = self.get_unchecked_mut(len..) as *mut _;
706700
self.len = len;
701+
ptr::drop_in_place(s);
707702
}
708703
}
709704

@@ -1590,11 +1585,6 @@ impl<'a> SetLenOnDrop<'a> {
15901585
fn increment_len(&mut self, increment: usize) {
15911586
self.local_len += increment;
15921587
}
1593-
1594-
#[inline]
1595-
fn decrement_len(&mut self, decrement: usize) {
1596-
self.local_len -= decrement;
1597-
}
15981588
}
15991589

16001590
impl Drop for SetLenOnDrop<'_> {

0 commit comments

Comments
 (0)