From d92085fd0e113d4e4cb7f6c204008541760e39cc Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Thu, 9 Feb 2017 19:08:08 +0100 Subject: [PATCH] properly extract the inner type in a drop impl --- src/terminator/drop.rs | 6 ++++-- tests/run-pass/box_box_trait.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 tests/run-pass/box_box_trait.rs diff --git a/src/terminator/drop.rs b/src/terminator/drop.rs index 32c5073421b..3b0ca371bba 100644 --- a/src/terminator/drop.rs +++ b/src/terminator/drop.rs @@ -165,9 +165,11 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> { // some values don't need to call a drop impl, so the value is null if drop_fn != Pointer::from_int(0) { let FunctionDefinition {def_id, substs, sig, ..} = self.memory.get_fn(drop_fn.alloc_id)?.expect_drop_glue()?; - let real_ty = sig.inputs()[0]; + let real_ty = match sig.inputs()[0].sty { + ty::TyRef(_, mt) => self.monomorphize(mt.ty, substs), + _ => bug!("first argument of Drop::drop must be &mut T"), + }; self.drop(Lvalue::from_ptr(ptr), real_ty, drop)?; - drop.push((def_id, Value::ByVal(PrimVal::Ptr(ptr)), substs)); } else { // just a sanity check assert_eq!(drop_fn.offset, 0); diff --git a/tests/run-pass/box_box_trait.rs b/tests/run-pass/box_box_trait.rs new file mode 100644 index 00000000000..57eef52d573 --- /dev/null +++ b/tests/run-pass/box_box_trait.rs @@ -0,0 +1,29 @@ +#![feature(box_syntax)] + +struct DroppableStruct; + +static mut DROPPED: bool = false; + +impl Drop for DroppableStruct { + fn drop(&mut self) { + unsafe { DROPPED = true; } + } +} + +trait MyTrait { fn dummy(&self) { } } +impl MyTrait for Box {} + +struct Whatever { w: Box } +impl Whatever { + fn new(w: Box) -> Whatever { + Whatever { w: w } + } +} + +fn main() { + { + let f: Box<_> = box DroppableStruct; + let _a = Whatever::new(box f as Box); + } + assert!(unsafe { DROPPED }); +}