Skip to content

Commit 6059879

Browse files
committed
replace box_free with deallocate_box
1 parent 22e491a commit 6059879

File tree

6 files changed

+28
-86
lines changed

6 files changed

+28
-86
lines changed

compiler/rustc_hir/src/lang_items.rs

-1
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,6 @@ language_item_table! {
287287
BeginPanic, sym::begin_panic, begin_panic_fn, Target::Fn, GenericRequirement::None;
288288

289289
ExchangeMalloc, sym::exchange_malloc, exchange_malloc_fn, Target::Fn, GenericRequirement::None;
290-
BoxFree, sym::box_free, box_free_fn, Target::Fn, GenericRequirement::Minimum(1);
291290
DropInPlace, sym::drop_in_place, drop_in_place_fn, Target::Fn, GenericRequirement::Minimum(1);
292291
Oom, sym::oom, oom, Target::Fn, GenericRequirement::None;
293292
AllocLayout, sym::alloc_layout, alloc_layout, Target::Struct, GenericRequirement::None;

compiler/rustc_mir_dataflow/src/elaborate_drops.rs

+1-71
Original file line numberDiff line numberDiff line change
@@ -408,19 +408,6 @@ where
408408
self.drop_ladder(fields, succ, unwind).0
409409
}
410410

411-
fn open_drop_for_box(&mut self, adt: &'tcx ty::AdtDef, substs: SubstsRef<'tcx>) -> BasicBlock {
412-
debug!("open_drop_for_box({:?}, {:?}, {:?})", self, adt, substs);
413-
414-
let interior = self.tcx().mk_place_deref(self.place);
415-
let interior_path = self.elaborator.deref_subpath(self.path);
416-
417-
let succ = self.box_free_block(adt, substs, self.succ, self.unwind);
418-
let unwind_succ =
419-
self.unwind.map(|unwind| self.box_free_block(adt, substs, unwind, Unwind::InCleanup));
420-
421-
self.drop_subpath(interior, interior_path, succ, unwind_succ)
422-
}
423-
424411
fn open_drop_for_adt(&mut self, adt: &'tcx ty::AdtDef, substs: SubstsRef<'tcx>) -> BasicBlock {
425412
debug!("open_drop_for_adt({:?}, {:?}, {:?})", self, adt, substs);
426413
if adt.variants.is_empty() {
@@ -872,11 +859,7 @@ where
872859
self.open_drop_for_tuple(&tys)
873860
}
874861
ty::Adt(def, substs) => {
875-
if def.is_box() {
876-
self.open_drop_for_box(def, substs)
877-
} else {
878-
self.open_drop_for_adt(def, substs)
879-
}
862+
self.open_drop_for_adt(def, substs)
880863
}
881864
ty::Dynamic(..) => self.complete_drop(self.succ, self.unwind),
882865
ty::Array(ety, size) => {
@@ -925,59 +908,6 @@ where
925908
blk
926909
}
927910

928-
/// Creates a block that frees the backing memory of a `Box` if its drop is required (either
929-
/// statically or by checking its drop flag).
930-
///
931-
/// The contained value will not be dropped.
932-
fn box_free_block(
933-
&mut self,
934-
adt: &'tcx ty::AdtDef,
935-
substs: SubstsRef<'tcx>,
936-
target: BasicBlock,
937-
unwind: Unwind,
938-
) -> BasicBlock {
939-
let block = self.unelaborated_free_block(adt, substs, target, unwind);
940-
self.drop_flag_test_block(block, target, unwind)
941-
}
942-
943-
/// Creates a block that frees the backing memory of a `Box` (without dropping the contained
944-
/// value).
945-
fn unelaborated_free_block(
946-
&mut self,
947-
adt: &'tcx ty::AdtDef,
948-
substs: SubstsRef<'tcx>,
949-
target: BasicBlock,
950-
unwind: Unwind,
951-
) -> BasicBlock {
952-
let tcx = self.tcx();
953-
let unit_temp = Place::from(self.new_temp(tcx.mk_unit()));
954-
let free_func = tcx.require_lang_item(LangItem::BoxFree, Some(self.source_info.span));
955-
let args = adt.variants[VariantIdx::new(0)]
956-
.fields
957-
.iter()
958-
.enumerate()
959-
.map(|(i, f)| {
960-
let field = Field::new(i);
961-
let field_ty = f.ty(tcx, substs);
962-
Operand::Move(tcx.mk_place_field(self.place, field, field_ty))
963-
})
964-
.collect();
965-
966-
let call = TerminatorKind::Call {
967-
func: Operand::function_handle(tcx, free_func, substs, self.source_info.span),
968-
args,
969-
destination: Some((unit_temp, target)),
970-
cleanup: None,
971-
from_hir_call: false,
972-
fn_span: self.source_info.span,
973-
}; // FIXME(#43234)
974-
let free_block = self.new_block(unwind, call);
975-
976-
let block_start = Location { block: free_block, statement_index: 0 };
977-
self.elaborator.clear_drop_flag(block_start, self.path, DropFlagMode::Shallow);
978-
free_block
979-
}
980-
981911
fn drop_block(&mut self, target: BasicBlock, unwind: Unwind) -> BasicBlock {
982912
let block =
983913
TerminatorKind::Drop { place: self.place, target, unwind: unwind.into_option() };

library/alloc/src/alloc.rs

+14-7
Original file line numberDiff line numberDiff line change
@@ -321,17 +321,24 @@ unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
321321
}
322322
}
323323

324-
#[cfg_attr(not(test), lang = "box_free")]
324+
#[cfg(all(bootstrap, not(test)))]
325+
#[lang = "box_free"]
325326
#[inline]
326327
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
327-
// This signature has to be the same as `Box`, otherwise an ICE will happen.
328-
// When an additional parameter to `Box` is added (like `A: Allocator`), this has to be added here as
329-
// well.
330-
// For example if `Box` is changed to `struct Box<T: ?Sized, A: Allocator>(Unique<T>, A)`,
331-
// this function has to be changed to `fn box_free<T: ?Sized, A: Allocator>(Unique<T>, A)` as well.
332-
pub(crate) const unsafe fn box_free<T: ?Sized, A: ~const Allocator + ~const Drop>(
328+
const unsafe fn box_free<T: ?Sized, A: ~const Allocator + ~const Drop>(
333329
ptr: Unique<T>,
334330
alloc: A,
331+
) {
332+
unsafe {
333+
deallocate_box(ptr, &alloc);
334+
}
335+
}
336+
337+
#[inline]
338+
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
339+
pub(crate) const unsafe fn deallocate_box<T: ?Sized, A: ~const Allocator>(
340+
ptr: Unique<T>,
341+
alloc: &A,
335342
) {
336343
unsafe {
337344
let size = size_of_val(ptr.as_ref());

library/alloc/src/boxed.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ use core::task::{Context, Poll};
154154

155155
#[cfg(not(no_global_oom_handling))]
156156
use crate::alloc::{handle_alloc_error, WriteCloneIntoRaw};
157-
use crate::alloc::{AllocError, Allocator, Global, Layout};
157+
use crate::alloc::{deallocate_box, AllocError, Allocator, Global, Layout};
158158
#[cfg(not(no_global_oom_handling))]
159159
use crate::borrow::Cow;
160160
use crate::raw_vec::RawVec;
@@ -1171,9 +1171,15 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
11711171

11721172
#[stable(feature = "rust1", since = "1.0.0")]
11731173
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
1174-
unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> const Drop for Box<T, A> {
1174+
unsafe impl<
1175+
#[may_dangle] T: ?Sized,
1176+
A: ~const Allocator,
1177+
> const Drop for Box<T, A> {
1178+
#[inline]
11751179
fn drop(&mut self) {
1176-
// FIXME: Do nothing, drop is currently performed by compiler.
1180+
unsafe {
1181+
deallocate_box(self.0, &self.1)
1182+
}
11771183
}
11781184
}
11791185

library/alloc/src/rc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ use core::slice::from_raw_parts_mut;
272272
#[cfg(not(no_global_oom_handling))]
273273
use crate::alloc::handle_alloc_error;
274274
#[cfg(not(no_global_oom_handling))]
275-
use crate::alloc::{box_free, WriteCloneIntoRaw};
275+
use crate::alloc::{deallocate_box, WriteCloneIntoRaw};
276276
use crate::alloc::{AllocError, Allocator, Global, Layout};
277277
use crate::borrow::{Cow, ToOwned};
278278
#[cfg(not(no_global_oom_handling))]
@@ -1315,7 +1315,7 @@ impl<T: ?Sized> Rc<T> {
13151315
);
13161316

13171317
// Free the allocation without dropping its contents
1318-
box_free(box_unique, alloc);
1318+
deallocate_box(box_unique, &alloc);
13191319

13201320
Self::from_ptr(ptr)
13211321
}

library/alloc/src/sync.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use core::sync::atomic::Ordering::{Acquire, Relaxed, Release, SeqCst};
3030
#[cfg(not(no_global_oom_handling))]
3131
use crate::alloc::handle_alloc_error;
3232
#[cfg(not(no_global_oom_handling))]
33-
use crate::alloc::{box_free, WriteCloneIntoRaw};
33+
use crate::alloc::{deallocate_box, WriteCloneIntoRaw};
3434
use crate::alloc::{AllocError, Allocator, Global, Layout};
3535
use crate::borrow::{Cow, ToOwned};
3636
use crate::boxed::Box;
@@ -1191,7 +1191,7 @@ impl<T: ?Sized> Arc<T> {
11911191
);
11921192

11931193
// Free the allocation without dropping its contents
1194-
box_free(box_unique, alloc);
1194+
deallocate_box(box_unique, &alloc);
11951195

11961196
Self::from_ptr(ptr)
11971197
}

0 commit comments

Comments
 (0)