diff --git a/src/liballoc/lib.rs b/src/liballoc/lib.rs index c78ebdf4340e..899e7de4ed59 100644 --- a/src/liballoc/lib.rs +++ b/src/liballoc/lib.rs @@ -103,6 +103,8 @@ #![feature(unsize)] #![feature(core_slice_ext)] #![feature(core_str_ext)] +#![feature(drop_in_place)] + #![cfg_attr(stage0, feature(alloc_system))] #![cfg_attr(not(stage0), feature(needs_allocator))] diff --git a/src/libcore/intrinsics.rs b/src/libcore/intrinsics.rs index 45b1c8a3599c..c14a07424a43 100644 --- a/src/libcore/intrinsics.rs +++ b/src/libcore/intrinsics.rs @@ -195,7 +195,26 @@ extern "rust-intrinsic" { pub fn size_of_val(_: &T) -> usize; pub fn min_align_of_val(_: &T) -> usize; - pub fn drop_in_place(_: *mut T); + + /// Executes the destructor (if any) of the pointed-to value. + /// + /// This has two usecases: + /// + /// * It is *required* to use `drop_in_place` to drop unsized types like + /// trait objects, because they can't be read out onto the stack and + /// dropped normally. + /// + /// * It is friendlier to the optimizer to do this over `ptr::read` when + /// dropping manually allocated memory (e.g. when writing Box/Rc/Vec), + /// as the compiler doesn't need to prove that it's sound to elide the + /// copy. + /// + /// # Undefined Behaviour + /// + /// This has all the same safety problems as `ptr::read` with respect to + /// invalid pointers, types, and double drops. + #[unstable(feature = "drop_in_place", reason = "just exposed, needs FCP", issue = "27908")] + pub fn drop_in_place(to_drop: *mut T); /// Gets a static string slice containing the name of a type. pub fn type_name() -> &'static str; diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 52a888d797ba..54cd3d0c8670 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -40,6 +40,8 @@ pub use intrinsics::copy; #[stable(feature = "rust1", since = "1.0.0")] pub use intrinsics::write_bytes; +pub use intrinsics::drop_in_place; + /// Creates a null raw pointer. /// /// # Examples