Skip to content

expose drop_in_place as ptr::drop_in_place #27204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions src/doc/nomicon/destructors.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ this is totally fine.
For instance, a custom implementation of `Box` might write `Drop` like this:

```rust
#![feature(heap_api, core_intrinsics, unique)]
#![feature(heap_api, drop_in_place, unique)]

use std::rt::heap;
use std::ptr::Unique;
use std::intrinsics::drop_in_place;
use std::ptr::{drop_in_place, Unique};
use std::mem;

struct Box<T>{ ptr: Unique<T> }
Expand All @@ -54,11 +53,10 @@ use-after-free the `ptr` because when drop exits, it becomes inacessible.
However this wouldn't work:

```rust
#![feature(heap_api, core_intrinsics, unique)]
#![feature(heap_api, drop_in_place, unique)]

use std::rt::heap;
use std::ptr::Unique;
use std::intrinsics::drop_in_place;
use std::ptr::{drop_in_place, Unique};
use std::mem;

struct Box<T>{ ptr: Unique<T> }
Expand Down Expand Up @@ -129,11 +127,10 @@ The classic safe solution to overriding recursive drop and allowing moving out
of Self during `drop` is to use an Option:

```rust
#![feature(heap_api, core_intrinsics, unique)]
#![feature(heap_api, drop_in_place, unique)]

use std::rt::heap;
use std::ptr::Unique;
use std::intrinsics::drop_in_place;
use std::ptr::{drop_in_place, Unique};
use std::mem;

struct Box<T>{ ptr: Unique<T> }
Expand Down
4 changes: 2 additions & 2 deletions src/liballoc/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ use core::atomic::Ordering::{Relaxed, Release, Acquire, SeqCst};
use core::fmt;
use core::cmp::Ordering;
use core::mem::{align_of_val, size_of_val};
use core::intrinsics::{drop_in_place, abort};
use core::intrinsics::abort;
use core::mem;
use core::nonzero::NonZero;
use core::ops::{Deref, CoerceUnsized};
Expand Down Expand Up @@ -265,7 +265,7 @@ impl<T: ?Sized> Arc<T> {

// Destroy the data at this time, even though we may not free the box
// allocation itself (there may still be weak pointers lying around).
drop_in_place(&mut (*ptr).data);
ptr::drop_in_place(&mut (*ptr).data);

if self.inner().weak.fetch_sub(1, Release) == 1 {
atomic::fence(Acquire);
Expand Down
2 changes: 2 additions & 0 deletions src/liballoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,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))]

Expand Down
4 changes: 2 additions & 2 deletions src/liballoc/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ use core::cell::Cell;
use core::cmp::Ordering;
use core::fmt;
use core::hash::{Hasher, Hash};
use core::intrinsics::{assume, drop_in_place, abort};
use core::intrinsics::{assume, abort};
use core::marker::{self, Unsize};
use core::mem::{self, align_of, size_of, align_of_val, size_of_val, forget};
use core::nonzero::NonZero;
Expand Down Expand Up @@ -417,7 +417,7 @@ impl<T: ?Sized> Drop for Rc<T> {
self.dec_strong();
if self.strong() == 0 {
// destroy the contained object
drop_in_place(&mut (*ptr).value);
ptr::drop_in_place(&mut (*ptr).value);

// remove the implicit "strong weak" pointer now that we've
// destroyed the contents.
Expand Down
2 changes: 2 additions & 0 deletions src/libcollections/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
#![feature(unique)]
#![feature(unsafe_no_drop_flag, filling_drop)]
#![feature(utf8_error)]
#![feature(drop_in_place)]

#![cfg_attr(test, feature(rand, test))]

#![feature(no_std)]
Expand Down
4 changes: 2 additions & 2 deletions src/libcollections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ use alloc::heap::EMPTY;
use core::cmp::Ordering;
use core::fmt;
use core::hash::{self, Hash};
use core::intrinsics::{arith_offset, assume, drop_in_place};
use core::intrinsics::{arith_offset, assume};
use core::iter::FromIterator;
use core::mem;
use core::ops::{Index, IndexMut, Deref};
Expand Down Expand Up @@ -1329,7 +1329,7 @@ impl<T> Drop for Vec<T> {
// don't need unsafe_no_drop_flag shenanigans anymore.
if self.buf.unsafe_no_drop_flag_needs_drop() {
for x in self.iter_mut() {
unsafe { drop_in_place(x); }
unsafe { ptr::drop_in_place(x); }
}
}
// RawVec handles deallocation
Expand Down
21 changes: 20 additions & 1 deletion src/libcore/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,26 @@ extern "rust-intrinsic" {

pub fn size_of_val<T: ?Sized>(_: &T) -> usize;
pub fn min_align_of_val<T: ?Sized>(_: &T) -> usize;
pub fn drop_in_place<T: ?Sized>(_: *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<T: ?Sized>(to_drop: *mut T);

/// Gets a static string slice containing the name of a type.
pub fn type_name<T: ?Sized>() -> &'static str;
Expand Down
2 changes: 2 additions & 0 deletions src/libcore/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,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
Expand Down
2 changes: 2 additions & 0 deletions src/libstd/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@
#![feature(vec_resize)]
#![feature(wrapping)]
#![feature(zero_one)]
#![feature(drop_in_place)]

#![cfg_attr(windows, feature(str_utf16))]
#![cfg_attr(test, feature(float_from_str_radix, range_inclusive, float_extras, hash_default))]
#![cfg_attr(test, feature(test, rustc_private, float_consts))]
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/thread/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ mod imp {
if cfg!(target_os = "macos") {
ptr::read((*ptr).inner.get());
} else {
intrinsics::drop_in_place((*ptr).inner.get());
ptr::drop_in_place((*ptr).inner.get());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-pass/extern_fat_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

// aux-build:fat_drop.rs

#![feature(core_intrinsics)]
#![feature(core_intrinsics, drop_in_place)]

extern crate fat_drop;

Expand Down