Skip to content

Commit

Permalink
Rollup merge of rust-lang#55785 - stjepang:unsized-drop-forget, r=ale…
Browse files Browse the repository at this point in the history
…xcrichton

Add mem::forget_unsized() for forgetting unsized values

~~Allows passing values of `T: ?Sized` types to `mem::drop` and `mem::forget`.~~

Adds `mem::forget_unsized()` that accepts `T: ?Sized`.

I had to revert the PR that removed the `forget` intrinsic and replaced it with `ManuallyDrop`: rust-lang#40559
We can't use `ManuallyDrop::new()` here because it needs `T: Sized` and we don't have support for unsized return values yet (will we ever?).

r? @eddyb
  • Loading branch information
pietroalbini authored Nov 15, 2018
2 parents 6ca7bc0 + 56d3a82 commit 202724c
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/libcore/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,10 @@ extern "rust-intrinsic" {
/// initialize memory previous set to the result of `uninit`.
pub fn uninit<T>() -> T;

/// Moves a value out of scope without running drop glue.
#[cfg(not(stage0))]
pub fn forget<T: ?Sized>(_: T);

/// Reinterprets the bits of a value of one type as another type.
///
/// Both types must have the same size. Neither the original, nor the result,
Expand Down
1 change: 1 addition & 0 deletions src/libcore/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
#![feature(staged_api)]
#![feature(stmt_expr_attributes)]
#![feature(unboxed_closures)]
#![feature(unsized_locals)]
#![feature(untagged_unions)]
#![feature(unwind_attributes)]
#![feature(doc_alias)]
Expand Down
13 changes: 13 additions & 0 deletions src/libcore/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,19 @@ pub fn forget<T>(t: T) {
ManuallyDrop::new(t);
}

/// Like [`forget`], but also accepts unsized values.
///
/// This function is just a shim intended to be removed when the `unsized_locals` feature gets
/// stabilized.
///
/// [`forget`]: fn.forget.html
#[inline]
#[cfg(not(stage0))]
#[unstable(feature = "forget_unsized", issue = "0")]
pub fn forget_unsized<T: ?Sized>(t: T) {
unsafe { intrinsics::forget(t) }
}

/// Returns the size of a type in bytes.
///
/// More specifically, this is the offset in bytes between successive elements
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_llvm/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ pub fn codegen_intrinsic_call(
return;
}
// Effectively no-ops
"uninit" => {
"uninit" | "forget" => {
return;
}
"needs_drop" => {
Expand Down
1 change: 1 addition & 0 deletions src/librustc_typeck/check/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ pub fn check_intrinsic_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
"rustc_peek" => (1, vec![param(0)], param(0)),
"init" => (1, Vec::new(), param(0)),
"uninit" => (1, Vec::new(), param(0)),
"forget" => (1, vec![param(0)], tcx.mk_unit()),
"transmute" => (2, vec![ param(0) ], param(1)),
"move_val_init" => {
(1,
Expand Down

0 comments on commit 202724c

Please sign in to comment.