Skip to content

Commit 4a4f8ff

Browse files
committed
Implement Drop for Box
1 parent ffba0ce commit 4a4f8ff

File tree

8 files changed

+18
-33
lines changed

8 files changed

+18
-33
lines changed

src/liballoc/boxed.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,14 @@ impl<T: ?Sized> Box<T> {
293293
}
294294
}
295295

296+
#[cfg(not(stage0))]
297+
#[stable(feature = "rust1", since = "1.0.0")]
298+
unsafe impl<#[may_dangle] T: ?Sized> Drop for Box<T> {
299+
fn drop(&mut self) {
300+
// FIXME: Do nothing, drop is currently performed by compiler.
301+
}
302+
}
303+
296304
#[stable(feature = "rust1", since = "1.0.0")]
297305
impl<T: Default> Default for Box<T> {
298306
/// Creates a `Box<T>`, with the `Default` value for T.

src/librustc/ty/contents.rs

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,8 @@ def_type_content_sets! {
5656
// InteriorAll = 0b00000000__00000000__1111,
5757

5858
// Things that are owned by the value (second and third nibbles):
59-
OwnsOwned = 0b0000_0000__0000_0001__0000,
6059
OwnsDtor = 0b0000_0000__0000_0010__0000,
61-
OwnsAll = 0b0000_0000__1111_1111__0000,
62-
63-
// Things that mean drop glue is necessary
64-
NeedsDrop = 0b0000_0000__0000_0111__0000,
60+
// OwnsAll = 0b0000_0000__1111_1111__0000,
6561

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

80-
pub fn owns_owned(&self) -> bool {
81-
self.intersects(TC::OwnsOwned)
82-
}
83-
8476
pub fn interior_param(&self) -> bool {
8577
self.intersects(TC::InteriorParam)
8678
}
@@ -90,12 +82,7 @@ impl TypeContents {
9082
}
9183

9284
pub fn needs_drop(&self, _: TyCtxt) -> bool {
93-
self.intersects(TC::NeedsDrop)
94-
}
95-
96-
/// Includes only those bits that still apply when indirected through a `Box` pointer
97-
pub fn owned_pointer(&self) -> TypeContents {
98-
TC::OwnsOwned | (*self & TC::OwnsAll)
85+
self.intersects(TC::OwnsDtor)
9986
}
10087

10188
pub fn union<I, T, F>(v: I, mut f: F) -> TypeContents where
@@ -104,10 +91,6 @@ impl TypeContents {
10491
{
10592
v.into_iter().fold(TC::None, |tc, ty| tc | f(ty))
10693
}
107-
108-
pub fn has_dtor(&self) -> bool {
109-
self.intersects(TC::OwnsDtor)
110-
}
11194
}
11295

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

226-
ty::TyAdt(def, _) if def.is_box() => {
227-
tc_ty(tcx, ty.boxed_ty(), cache).owned_pointer()
228-
}
229-
230209
ty::TyAdt(def, substs) => {
231210
let mut res =
232211
TypeContents::union(&def.variants, |v| {
@@ -237,7 +216,7 @@ impl<'a, 'tcx> ty::TyS<'tcx> {
237216

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

243222
if def.has_dtor() {

src/librustc/ty/sty.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,8 +1142,7 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
11421142

11431143
pub fn boxed_ty(&self) -> Ty<'tcx> {
11441144
match self.sty {
1145-
TyAdt(def, substs) if def.is_box() =>
1146-
substs.types().next().expect("Box<T> doesn't have type parameters"),
1145+
TyAdt(def, substs) if def.is_box() => substs.type_at(0),
11471146
_ => bug!("`boxed_ty` is called on non-box type {:?}", self),
11481147
}
11491148
}

src/librustc_borrowck/borrowck/mir/elaborate_drops.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
895895

896896
match ty.sty {
897897
ty::TyAdt(def, _) => {
898-
if def.has_dtor() {
898+
if def.has_dtor() && !def.is_box() {
899899
self.tcx.sess.span_warn(
900900
c.source_info.span,
901901
&format!("dataflow bug??? moving out of type with dtor {:?}",

src/librustc_borrowck/borrowck/mir/gather_moves.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> {
289289
// error: can't move out of borrowed content
290290
ty::TyRef(..) | ty::TyRawPtr(..) => return Err(MovePathError::IllegalMove),
291291
// error: can't move out of struct with destructor
292-
ty::TyAdt(adt, _) if adt.has_dtor() =>
292+
ty::TyAdt(adt, _) if adt.has_dtor() && !adt.is_box() =>
293293
return Err(MovePathError::IllegalMove),
294294
// move out of union - always move the entire union
295295
ty::TyAdt(adt, _) if adt.is_union() =>

src/librustc_borrowck/borrowck/mir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ fn lvalue_contents_drop_state_cannot_differ<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx
248248
lv, ty);
249249
true
250250
}
251-
ty::TyAdt(def, _) if def.has_dtor() || def.is_union() => {
251+
ty::TyAdt(def, _) if (def.has_dtor() && !def.is_box()) || def.is_union() => {
252252
debug!("lvalue_contents_drop_state_cannot_differ lv: {:?} ty: {:?} Drop => true",
253253
lv, ty);
254254
true

src/librustc_trans/collector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ fn find_drop_glue_neighbors<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>,
740740
_ => None
741741
};
742742

743-
if let Some(destructor_did) = destructor_did {
743+
if let (Some(destructor_did), false) = (destructor_did, ty.is_box()) {
744744
use rustc::ty::ToPolyTraitRef;
745745

746746
let drop_trait_def_id = scx.tcx()

src/librustc_trans/glue.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,8 @@ pub fn implement_drop_glue<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, g: DropGlueKi
211211

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

0 commit comments

Comments
 (0)