@@ -313,6 +313,37 @@ pub fn trans_to_datum(bcx: @mut Block, expr: @ast::Expr) -> DatumBlock {
313313 let target_obj_ty = expr_ty_adjusted ( bcx, expr) ;
314314 debug ! ( "auto_borrow_obj(target=%s)" ,
315315 target_obj_ty. repr( tcx) ) ;
316+
317+ // Extract source store information
318+ let ( source_store, source_mutbl) = match ty:: get ( source_datum. ty ) . sty {
319+ ty:: ty_trait( _, _, s, m, _) => ( s, m) ,
320+ _ => {
321+ bcx. sess ( ) . span_bug (
322+ expr. span ,
323+ fmt ! ( "auto_borrow_trait_obj expected a trait, found %s" ,
324+ source_datum. ty. repr( bcx. tcx( ) ) ) ) ;
325+ }
326+ } ;
327+
328+ // check if any borrowing is really needed or we could reuse the source_datum instead
329+ match ty:: get ( target_obj_ty) . sty {
330+ ty:: ty_trait( _, _, ty:: RegionTraitStore ( target_scope) , target_mutbl, _) => {
331+ if target_mutbl == ast:: MutImmutable && target_mutbl == source_mutbl {
332+ match source_store {
333+ ty:: RegionTraitStore ( source_scope) => {
334+ if tcx. region_maps . is_subregion_of ( target_scope, source_scope) {
335+ return DatumBlock { bcx : bcx, datum : source_datum } ;
336+ }
337+ } ,
338+ _ => { }
339+
340+ } ;
341+ }
342+ } ,
343+ _ => { }
344+ }
345+
346+
316347 let scratch = scratch_datum ( bcx, target_obj_ty,
317348 "__auto_borrow_obj" , false ) ;
318349
@@ -331,15 +362,6 @@ pub fn trans_to_datum(bcx: @mut Block, expr: @ast::Expr) -> DatumBlock {
331362 // ~T, or &T, depending on source_obj_ty.
332363 let source_data_ptr = GEPi ( bcx, source_llval, [ 0 u, abi:: trt_field_box] ) ;
333364 let source_data = Load ( bcx, source_data_ptr) ; // always a ptr
334- let ( source_store, source_mutbl) = match ty:: get ( source_datum. ty ) . sty {
335- ty:: ty_trait( _, _, s, m, _) => ( s, m) ,
336- _ => {
337- bcx. sess ( ) . span_bug (
338- expr. span ,
339- fmt ! ( "auto_borrow_trait_obj expected a trait, found %s" ,
340- source_datum. ty. repr( bcx. tcx( ) ) ) ) ;
341- }
342- } ;
343365 let target_data = match source_store {
344366 ty:: BoxTraitStore ( * ) => {
345367 // For deref of @T or @mut T, create a dummy datum and
0 commit comments