Skip to content

Commit

Permalink
Implement Drop for Box
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Jan 30, 2017
1 parent ffba0ce commit 4a4f8ff
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 33 deletions.
8 changes: 8 additions & 0 deletions src/liballoc/boxed.rs
Expand Up @@ -293,6 +293,14 @@ impl<T: ?Sized> Box<T> {
}
}

#[cfg(not(stage0))]
#[stable(feature = "rust1", since = "1.0.0")]
unsafe impl<#[may_dangle] T: ?Sized> Drop for Box<T> {
fn drop(&mut self) {
// FIXME: Do nothing, drop is currently performed by compiler.
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: Default> Default for Box<T> {
/// Creates a `Box<T>`, with the `Default` value for T.
Expand Down
27 changes: 3 additions & 24 deletions src/librustc/ty/contents.rs
Expand Up @@ -56,12 +56,8 @@ def_type_content_sets! {
// InteriorAll = 0b00000000__00000000__1111,

// Things that are owned by the value (second and third nibbles):
OwnsOwned = 0b0000_0000__0000_0001__0000,
OwnsDtor = 0b0000_0000__0000_0010__0000,
OwnsAll = 0b0000_0000__1111_1111__0000,

// Things that mean drop glue is necessary
NeedsDrop = 0b0000_0000__0000_0111__0000,
// OwnsAll = 0b0000_0000__1111_1111__0000,

// All bits
All = 0b1111_1111__1111_1111__1111
Expand All @@ -77,10 +73,6 @@ impl TypeContents {
(self.bits & tc.bits) != 0
}

pub fn owns_owned(&self) -> bool {
self.intersects(TC::OwnsOwned)
}

pub fn interior_param(&self) -> bool {
self.intersects(TC::InteriorParam)
}
Expand All @@ -90,12 +82,7 @@ impl TypeContents {
}

pub fn needs_drop(&self, _: TyCtxt) -> bool {
self.intersects(TC::NeedsDrop)
}

/// Includes only those bits that still apply when indirected through a `Box` pointer
pub fn owned_pointer(&self) -> TypeContents {
TC::OwnsOwned | (*self & TC::OwnsAll)
self.intersects(TC::OwnsDtor)
}

pub fn union<I, T, F>(v: I, mut f: F) -> TypeContents where
Expand All @@ -104,10 +91,6 @@ impl TypeContents {
{
v.into_iter().fold(TC::None, |tc, ty| tc | f(ty))
}

pub fn has_dtor(&self) -> bool {
self.intersects(TC::OwnsDtor)
}
}

impl ops::BitOr for TypeContents {
Expand Down Expand Up @@ -223,10 +206,6 @@ impl<'a, 'tcx> ty::TyS<'tcx> {
|ty| tc_ty(tcx, *ty, cache))
}

ty::TyAdt(def, _) if def.is_box() => {
tc_ty(tcx, ty.boxed_ty(), cache).owned_pointer()
}

ty::TyAdt(def, substs) => {
let mut res =
TypeContents::union(&def.variants, |v| {
Expand All @@ -237,7 +216,7 @@ impl<'a, 'tcx> ty::TyS<'tcx> {

if def.is_union() {
// unions don't have destructors regardless of the child types
res = res - TC::NeedsDrop;
res = res - TC::OwnsDtor;
}

if def.has_dtor() {
Expand Down
3 changes: 1 addition & 2 deletions src/librustc/ty/sty.rs
Expand Up @@ -1142,8 +1142,7 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {

pub fn boxed_ty(&self) -> Ty<'tcx> {
match self.sty {
TyAdt(def, substs) if def.is_box() =>
substs.types().next().expect("Box<T> doesn't have type parameters"),
TyAdt(def, substs) if def.is_box() => substs.type_at(0),
_ => bug!("`boxed_ty` is called on non-box type {:?}", self),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_borrowck/borrowck/mir/elaborate_drops.rs
Expand Up @@ -895,7 +895,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {

match ty.sty {
ty::TyAdt(def, _) => {
if def.has_dtor() {
if def.has_dtor() && !def.is_box() {
self.tcx.sess.span_warn(
c.source_info.span,
&format!("dataflow bug??? moving out of type with dtor {:?}",
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_borrowck/borrowck/mir/gather_moves.rs
Expand Up @@ -289,7 +289,7 @@ impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> {
// error: can't move out of borrowed content
ty::TyRef(..) | ty::TyRawPtr(..) => return Err(MovePathError::IllegalMove),
// error: can't move out of struct with destructor
ty::TyAdt(adt, _) if adt.has_dtor() =>
ty::TyAdt(adt, _) if adt.has_dtor() && !adt.is_box() =>
return Err(MovePathError::IllegalMove),
// move out of union - always move the entire union
ty::TyAdt(adt, _) if adt.is_union() =>
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_borrowck/borrowck/mir/mod.rs
Expand Up @@ -248,7 +248,7 @@ fn lvalue_contents_drop_state_cannot_differ<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx
lv, ty);
true
}
ty::TyAdt(def, _) if def.has_dtor() || def.is_union() => {
ty::TyAdt(def, _) if (def.has_dtor() && !def.is_box()) || def.is_union() => {
debug!("lvalue_contents_drop_state_cannot_differ lv: {:?} ty: {:?} Drop => true",
lv, ty);
true
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/collector.rs
Expand Up @@ -740,7 +740,7 @@ fn find_drop_glue_neighbors<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>,
_ => None
};

if let Some(destructor_did) = destructor_did {
if let (Some(destructor_did), false) = (destructor_did, ty.is_box()) {
use rustc::ty::ToPolyTraitRef;

let drop_trait_def_id = scx.tcx()
Expand Down
5 changes: 2 additions & 3 deletions src/librustc_trans/glue.rs
Expand Up @@ -211,9 +211,8 @@ pub fn implement_drop_glue<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, g: DropGlueKi

let bcx = match t.sty {
ty::TyAdt(def, _) if def.is_box() => {
// Support for Box is built-in and its drop glue is
// special. It may move to library and have Drop impl. As
// a safe-guard, assert Box not used with TyContents.
// Support for Box is built-in as yet and its drop glue is special
// despite having a dummy Drop impl in the library.
assert!(!skip_dtor);
let content_ty = t.boxed_ty();
let ptr = if !bcx.ccx.shared().type_is_sized(content_ty) {
Expand Down

0 comments on commit 4a4f8ff

Please sign in to comment.