diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index d6d986905e6c1..778283c34bbfa 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -59,7 +59,7 @@ use core::cmp::Ordering; use core::convert::TryFrom; use core::fmt; use core::hash::{Hash, Hasher}; -use core::intrinsics::assume; +use core::intrinsics::{assume, needs_drop}; use core::iter; #[cfg(not(no_global_oom_handling))] use core::iter::FromIterator; @@ -2913,12 +2913,18 @@ impl Ord for Vec { #[stable(feature = "rust1", since = "1.0.0")] unsafe impl<#[may_dangle] T, A: Allocator> Drop for Vec { + #[inline] fn drop(&mut self) { - unsafe { - // use drop for [T] - // use a raw slice to refer to the elements of the vector as weakest necessary type; - // could avoid questions of validity in certain cases - ptr::drop_in_place(ptr::slice_from_raw_parts_mut(self.as_mut_ptr(), self.len)) + if needs_drop::() { + unsafe fn drop_via_slice(vec: &mut Vec) { + unsafe { + // use drop for [T] + // use a raw slice to refer to the elements of the vector as weakest + // necessary type; could avoid questions of validity in certain cases + ptr::drop_in_place(ptr::slice_from_raw_parts_mut(vec.as_mut_ptr(), vec.len)) + } + } + unsafe { drop_via_slice(self); } } // RawVec handles deallocation }