diff --git a/src/librustc/middle/trans/expr.rs b/src/librustc/middle/trans/expr.rs index 0a557869758..01702e749a3 100644 --- a/src/librustc/middle/trans/expr.rs +++ b/src/librustc/middle/trans/expr.rs @@ -313,6 +313,36 @@ pub fn trans_to_datum(bcx: @mut Block, expr: @ast::Expr) -> DatumBlock { let target_obj_ty = expr_ty_adjusted(bcx, expr); debug!("auto_borrow_obj(target=%s)", target_obj_ty.repr(tcx)); + + // Extract source store information + let (source_store, source_mutbl) = match ty::get(source_datum.ty).sty { + ty::ty_trait(_, _, s, m, _) => (s, m), + _ => { + bcx.sess().span_bug( + expr.span, + fmt!("auto_borrow_trait_obj expected a trait, found %s", + source_datum.ty.repr(bcx.tcx()))); + } + }; + + // check if any borrowing is really needed or we could reuse the source_datum instead + match ty::get(target_obj_ty).sty { + ty::ty_trait(_, _, ty::RegionTraitStore(target_scope), target_mutbl, _) => { + if target_mutbl == ast::MutImmutable && target_mutbl == source_mutbl { + match source_store { + ty::RegionTraitStore(source_scope) => { + if tcx.region_maps.is_subregion_of(target_scope, source_scope) { + return DatumBlock { bcx: bcx, datum: source_datum }; + } + }, + _ => {} + + }; + } + }, + _ => {} + } + let scratch = scratch_datum(bcx, target_obj_ty, "__auto_borrow_obj", false); @@ -331,15 +361,6 @@ pub fn trans_to_datum(bcx: @mut Block, expr: @ast::Expr) -> DatumBlock { // ~T, or &T, depending on source_obj_ty. let source_data_ptr = GEPi(bcx, source_llval, [0u, abi::trt_field_box]); let source_data = Load(bcx, source_data_ptr); // always a ptr - let (source_store, source_mutbl) = match ty::get(source_datum.ty).sty { - ty::ty_trait(_, _, s, m, _) => (s, m), - _ => { - bcx.sess().span_bug( - expr.span, - fmt!("auto_borrow_trait_obj expected a trait, found %s", - source_datum.ty.repr(bcx.tcx()))); - } - }; let target_data = match source_store { ty::BoxTraitStore(*) => { // For deref of @T or @mut T, create a dummy datum and diff --git a/src/librustc/middle/trans/meth.rs b/src/librustc/middle/trans/meth.rs index 06d88f66323..934dfabbb4d 100644 --- a/src/librustc/middle/trans/meth.rs +++ b/src/librustc/middle/trans/meth.rs @@ -434,13 +434,22 @@ pub fn trans_trait_callee(bcx: @mut Block, let _icx = push_ctxt("impl::trans_trait_callee"); let mut bcx = bcx; + // make a local copy for trait if needed let self_ty = expr_ty_adjusted(bcx, self_expr); - let self_scratch = scratch_datum(bcx, self_ty, "__trait_callee", false); - bcx = expr::trans_into(bcx, self_expr, expr::SaveIn(self_scratch.val)); + let self_scratch = match ty::get(self_ty).sty { + ty::ty_trait(_, _, ty::RegionTraitStore(*), _, _) => { + unpack_datum!(bcx, expr::trans_to_datum(bcx, self_expr)) + } + _ => { + let d = scratch_datum(bcx, self_ty, "__trait_callee", false); + bcx = expr::trans_into(bcx, self_expr, expr::SaveIn(d.val)); + // Arrange a temporary cleanup for the object in case something + // should go wrong before the method is actually *invoked*. + d.add_clean(bcx); + d + } + }; - // Arrange a temporary cleanup for the object in case something - // should go wrong before the method is actually *invoked*. - self_scratch.add_clean(bcx); let callee_ty = node_id_type(bcx, callee_id); trans_trait_callee_from_llval(bcx, diff --git a/src/librustc/middle/trans/type_.rs b/src/librustc/middle/trans/type_.rs index 8b221a89c36..0954302ba81 100644 --- a/src/librustc/middle/trans/type_.rs +++ b/src/librustc/middle/trans/type_.rs @@ -278,23 +278,12 @@ impl Type { pub fn opaque_trait(ctx: &CrateContext, store: ty::TraitStore) -> Type { let tydesc_ptr = ctx.tydesc_type.ptr_to(); - match store { - ty::BoxTraitStore => { - Type::struct_( - [ tydesc_ptr, Type::opaque_box(ctx).ptr_to() ], - false) - } - ty::UniqTraitStore => { - Type::struct_( - [ tydesc_ptr, Type::unique(ctx, &Type::i8()).ptr_to()], - false) - } - ty::RegionTraitStore(*) => { - Type::struct_( - [ tydesc_ptr, Type::i8().ptr_to() ], - false) - } - } + let box_ty = match store { + ty::BoxTraitStore => Type::opaque_box(ctx), + ty::UniqTraitStore => Type::unique(ctx, &Type::i8()), + ty::RegionTraitStore(*) => Type::i8() + }; + Type::struct_([tydesc_ptr, box_ty.ptr_to()], false) } pub fn kind(&self) -> TypeKind { diff --git a/src/test/run-pass/core-run-destroy.rs b/src/test/run-pass/core-run-destroy.rs index 1c3e83f3cab..789295edaaa 100644 --- a/src/test/run-pass/core-run-destroy.rs +++ b/src/test/run-pass/core-run-destroy.rs @@ -55,6 +55,7 @@ fn test_destroy_actually_kills(force: bool) { #[cfg(windows)] fn process_exists(pid: libc::pid_t) -> bool { + #[fixed_stack_segment]; use std::libc::types::os::arch::extra::DWORD; use std::libc::funcs::extra::kernel32::{CloseHandle, GetExitCodeProcess, OpenProcess};