Skip to content

Commit afdfe40

Browse files
committed
auto merge of #15226 : luqmana/rust/abol, r=pcwalton
This was causing a ton of leaks in macro expansion.
2 parents abdf71c + 04e64c0 commit afdfe40

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

src/librustc/middle/trans/expr.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -277,15 +277,19 @@ fn apply_adjustments<'a>(bcx: &'a Block<'a>,
277277
auto_ref(bcx, datum, expr)
278278
}
279279

280-
fn auto_borrow_obj<'a>(bcx: &'a Block<'a>,
280+
fn auto_borrow_obj<'a>(mut bcx: &'a Block<'a>,
281281
expr: &ast::Expr,
282282
source_datum: Datum<Expr>)
283283
-> DatumBlock<'a, Expr> {
284284
let tcx = bcx.tcx();
285285
let target_obj_ty = expr_ty_adjusted(bcx, expr);
286286
debug!("auto_borrow_obj(target={})", target_obj_ty.repr(tcx));
287287

288-
let mut datum = source_datum.to_expr_datum();
288+
// Arrange cleanup, if not already done. This is needed in
289+
// case we are auto-borrowing a Box<Trait> to &Trait
290+
let datum = unpack_datum!(
291+
bcx, source_datum.to_lvalue_datum(bcx, "autoborrowobj", expr.id));
292+
let mut datum = datum.to_expr_datum();
289293
datum.ty = target_obj_ty;
290294
DatumBlock::new(bcx, datum)
291295
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2014 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+
12+
// This would previously leak the Box<Trait> because we wouldn't
13+
// schedule cleanups when auto borrowing trait objects.
14+
// This program should be valgrind clean.
15+
16+
17+
static mut DROP_RAN: bool = false;
18+
19+
struct Foo;
20+
impl Drop for Foo {
21+
fn drop(&mut self) {
22+
unsafe { DROP_RAN = true; }
23+
}
24+
}
25+
26+
27+
trait Trait {}
28+
impl Trait for Foo {}
29+
30+
pub fn main() {
31+
{
32+
let _x: &Trait = box Foo as Box<Trait>;
33+
}
34+
unsafe {
35+
assert!(DROP_RAN);
36+
}
37+
}
38+

0 commit comments

Comments
 (0)