Skip to content

Commit 1af2c39

Browse files
authored
Merge pull request rust-lang#141 from oli-obk/bug1
fix a bug in drop code of structs with unsized fields
2 parents e0d8fd2 + 545f700 commit 1af2c39

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

src/terminator/drop.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
9494
ty::TyAdt(adt_def, substs) => {
9595
// FIXME: some structs are represented as ByValPair
9696
let mut lval = self.force_allocation(lval)?;
97-
let adt_ptr = match lval {
98-
Lvalue::Ptr { ptr, .. } => ptr,
99-
_ => bug!("force allocation can only yield Lvalue::Ptr"),
100-
};
97+
let (adt_ptr, extra) = lval.to_ptr_and_extra();
10198

10299
// run drop impl before the fields' drop impls
103100
if let Some(drop_def_id) = adt_def.destructor() {
@@ -109,7 +106,13 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
109106
traits::VtableImpl(data) => data,
110107
_ => bug!("dtor for {:?} is not an impl???", ty)
111108
};
112-
drop.push((drop_def_id, Value::ByVal(PrimVal::Ptr(adt_ptr)), vtable.substs));
109+
let val = match extra {
110+
LvalueExtra::None => Value::ByVal(PrimVal::Ptr(adt_ptr)),
111+
LvalueExtra::DowncastVariant(_) => bug!("downcast variant in drop"),
112+
LvalueExtra::Length(n) => Value::ByValPair(PrimVal::Ptr(adt_ptr), PrimVal::from_u128(n as u128)),
113+
LvalueExtra::Vtable(vtable) => Value::ByValPair(PrimVal::Ptr(adt_ptr), PrimVal::Ptr(vtable)),
114+
};
115+
drop.push((drop_def_id, val, vtable.substs));
113116
}
114117

115118
let layout = self.type_layout(ty)?;

tests/run-pass/issue-26709.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
struct Wrapper<'a, T: ?Sized>(&'a mut i32, T);
12+
13+
impl<'a, T: ?Sized> Drop for Wrapper<'a, T> {
14+
fn drop(&mut self) {
15+
*self.0 = 432;
16+
}
17+
}
18+
19+
fn main() {
20+
let mut x = 0;
21+
{
22+
let wrapper = Box::new(Wrapper(&mut x, 123));
23+
let _: Box<Wrapper<Send>> = wrapper;
24+
}
25+
assert_eq!(432, x)
26+
}

0 commit comments

Comments
 (0)