@@ -8,11 +8,15 @@ pub fn deref_finder<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
88 let mut patch = MirPatch :: new ( body) ;
99 let ( basic_blocks, local_decl) = body. basic_blocks_and_local_decls_mut ( ) ;
1010 for ( block, data) in basic_blocks. iter_enumerated_mut ( ) {
11+ let statement_len = data. statements . len ( ) ;
1112 for ( i, stmt) in data. statements . iter_mut ( ) . enumerate ( ) {
1213 match stmt. kind {
1314 StatementKind :: Assign ( box ( og_place, Rvalue :: Ref ( region, borrow_knd, place) ) ) => {
1415 for ( idx, ( p_ref, p_elem) ) in place. iter_projections ( ) . enumerate ( ) {
15- if p_elem == ProjectionElem :: Deref && !p_ref. projection . is_empty ( ) {
16+ if p_elem == ProjectionElem :: Deref
17+ && !p_ref. projection . is_empty ( )
18+ && region. is_erased ( )
19+ {
1620 // The type that we are derefing
1721 let ty = p_ref. ty ( local_decl, tcx) . ty ;
1822 let temp = patch. new_temp ( ty, stmt. source_info . span ) ;
@@ -36,21 +40,20 @@ pub fn deref_finder<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
3640 // and copying derefed values which we need to create new statement
3741 let temp_place =
3842 Place :: from ( temp) . project_deeper ( & place. projection [ idx..] , tcx) ;
39- patch. add_assign (
40- loc,
41- og_place,
42- Rvalue :: Ref ( region, borrow_knd, temp_place) ,
43- ) ;
44-
4543 let new_stmt = Statement {
4644 source_info : stmt. source_info ,
4745 kind : StatementKind :: Assign ( Box :: new ( (
4846 og_place,
4947 Rvalue :: Ref ( region, borrow_knd, temp_place) ,
5048 ) ) ) ,
5149 } ;
50+
5251 // Replace current statement with newly created one
5352 * stmt = new_stmt;
53+
54+ // Since our job with the temp is done it should be gone
55+ let loc = Location { block : block, statement_index : statement_len } ;
56+ patch. add_statement ( loc, StatementKind :: StorageDead ( temp) ) ;
5457 }
5558 }
5659 }
0 commit comments