Skip to content

librustc: Schedule cleanups properly when coercing to a &Trait. #15226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 27, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/librustc/middle/trans/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,15 +277,19 @@ fn apply_adjustments<'a>(bcx: &'a Block<'a>,
auto_ref(bcx, datum, expr)
}

fn auto_borrow_obj<'a>(bcx: &'a Block<'a>,
fn auto_borrow_obj<'a>(mut bcx: &'a Block<'a>,
expr: &ast::Expr,
source_datum: Datum<Expr>)
-> DatumBlock<'a, Expr> {
let tcx = bcx.tcx();
let target_obj_ty = expr_ty_adjusted(bcx, expr);
debug!("auto_borrow_obj(target={})", target_obj_ty.repr(tcx));

let mut datum = source_datum.to_expr_datum();
// Arrange cleanup, if not already done. This is needed in
// case we are auto-borrowing a Box<Trait> to &Trait
let datum = unpack_datum!(
bcx, source_datum.to_lvalue_datum(bcx, "autoborrowobj", expr.id));
let mut datum = datum.to_expr_datum();
datum.ty = target_obj_ty;
DatumBlock::new(bcx, datum)
}
Expand Down
38 changes: 38 additions & 0 deletions src/test/run-pass/cleanup-auto-borrow-obj.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.


// This would previously leak the Box<Trait> because we wouldn't
// schedule cleanups when auto borrowing trait objects.
// This program should be valgrind clean.


static mut DROP_RAN: bool = false;

struct Foo;
impl Drop for Foo {
fn drop(&mut self) {
unsafe { DROP_RAN = true; }
}
}


trait Trait {}
impl Trait for Foo {}

pub fn main() {
{
let _x: &Trait = box Foo as Box<Trait>;
}
unsafe {
assert!(DROP_RAN);
}
}