Skip to content

Commit 202724c

Browse files
authored
Rollup merge of rust-lang#55785 - stjepang:unsized-drop-forget, r=alexcrichton
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
2 parents 6ca7bc0 + 56d3a82 commit 202724c

File tree

5 files changed

+20
-1
lines changed

5 files changed

+20
-1
lines changed

src/libcore/intrinsics.rs

+4
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,10 @@ extern "rust-intrinsic" {
717717
/// initialize memory previous set to the result of `uninit`.
718718
pub fn uninit<T>() -> T;
719719

720+
/// Moves a value out of scope without running drop glue.
721+
#[cfg(not(stage0))]
722+
pub fn forget<T: ?Sized>(_: T);
723+
720724
/// Reinterprets the bits of a value of one type as another type.
721725
///
722726
/// Both types must have the same size. Neither the original, nor the result,

src/libcore/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
#![feature(staged_api)]
107107
#![feature(stmt_expr_attributes)]
108108
#![feature(unboxed_closures)]
109+
#![feature(unsized_locals)]
109110
#![feature(untagged_unions)]
110111
#![feature(unwind_attributes)]
111112
#![feature(doc_alias)]

src/libcore/mem.rs

+13
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,19 @@ pub fn forget<T>(t: T) {
143143
ManuallyDrop::new(t);
144144
}
145145

146+
/// Like [`forget`], but also accepts unsized values.
147+
///
148+
/// This function is just a shim intended to be removed when the `unsized_locals` feature gets
149+
/// stabilized.
150+
///
151+
/// [`forget`]: fn.forget.html
152+
#[inline]
153+
#[cfg(not(stage0))]
154+
#[unstable(feature = "forget_unsized", issue = "0")]
155+
pub fn forget_unsized<T: ?Sized>(t: T) {
156+
unsafe { intrinsics::forget(t) }
157+
}
158+
146159
/// Returns the size of a type in bytes.
147160
///
148161
/// More specifically, this is the offset in bytes between successive elements

src/librustc_codegen_llvm/intrinsic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ pub fn codegen_intrinsic_call(
194194
return;
195195
}
196196
// Effectively no-ops
197-
"uninit" => {
197+
"uninit" | "forget" => {
198198
return;
199199
}
200200
"needs_drop" => {

src/librustc_typeck/check/intrinsic.rs

+1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ pub fn check_intrinsic_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
134134
"rustc_peek" => (1, vec![param(0)], param(0)),
135135
"init" => (1, Vec::new(), param(0)),
136136
"uninit" => (1, Vec::new(), param(0)),
137+
"forget" => (1, vec![param(0)], tcx.mk_unit()),
137138
"transmute" => (2, vec![ param(0) ], param(1)),
138139
"move_val_init" => {
139140
(1,

0 commit comments

Comments
 (0)