From 04e64c0c91f1d0de04940eab76ddcfe126d635b1 Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Fri, 27 Jun 2014 16:54:18 -0400 Subject: [PATCH] librustc: Schedule cleanups properly when coercing to a &Trait. --- src/librustc/middle/trans/expr.rs | 8 +++-- src/test/run-pass/cleanup-auto-borrow-obj.rs | 38 ++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 src/test/run-pass/cleanup-auto-borrow-obj.rs diff --git a/src/librustc/middle/trans/expr.rs b/src/librustc/middle/trans/expr.rs index 2a01e165f3c..b10190b23c7 100644 --- a/src/librustc/middle/trans/expr.rs +++ b/src/librustc/middle/trans/expr.rs @@ -277,7 +277,7 @@ fn auto_slice_and_ref<'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) -> DatumBlock<'a, Expr> { @@ -285,7 +285,11 @@ fn auto_borrow_obj<'a>(bcx: &'a Block<'a>, 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 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) } diff --git a/src/test/run-pass/cleanup-auto-borrow-obj.rs b/src/test/run-pass/cleanup-auto-borrow-obj.rs new file mode 100644 index 00000000000..23142082a8c --- /dev/null +++ b/src/test/run-pass/cleanup-auto-borrow-obj.rs @@ -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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + + +// This would previously leak the Box 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; + } + unsafe { + assert!(DROP_RAN); + } +} + -- 2.44.0