@@ -13,21 +13,18 @@ pub fn deref_finder<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
1313 match stmt. kind {
1414 StatementKind :: Assign ( box ( og_place, Rvalue :: Ref ( region, borrow_knd, place) ) ) => {
1515 for ( idx, ( p_ref, p_elem) ) in place. iter_projections ( ) . enumerate ( ) {
16- if p_elem == ProjectionElem :: Deref
17- && !p_ref. projection . is_empty ( )
18- && region. is_erased ( )
19- {
20- // The type that we are derefing
16+ if p_elem == ProjectionElem :: Deref && !p_ref. projection . is_empty ( ) {
17+ // The type that we are derefing.
2118 let ty = p_ref. ty ( local_decl, tcx) . ty ;
2219 let temp = patch. new_temp ( ty, stmt. source_info . span ) ;
2320
2421 // Because we are assigning this right before original statement
25- // we are using index i of statement
22+ // we are using index i of statement.
2623 let loc = Location { block : block, statement_index : i } ;
2724 patch. add_statement ( loc, StatementKind :: StorageLive ( temp) ) ;
2825
2926 // We are adding current p_ref's projections to our
30- // temp value
27+ // temp value.
3128 let deref_place =
3229 Place :: from ( p_ref. local ) . project_deeper ( p_ref. projection , tcx) ;
3330 patch. add_assign (
@@ -37,7 +34,7 @@ pub fn deref_finder<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
3734 ) ;
3835
3936 // We are creating a place by using our temp value's location
40- // and copying derefed values which we need to create new statement
37+ // and copying derefed values which we need to create new statement.
4138 let temp_place =
4239 Place :: from ( temp) . project_deeper ( & place. projection [ idx..] , tcx) ;
4340 let new_stmt = Statement {
@@ -48,12 +45,17 @@ pub fn deref_finder<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
4845 ) ) ) ,
4946 } ;
5047
51- // Replace current statement with newly created one
48+ // Replace current statement with newly created one.
5249 * stmt = new_stmt;
5350
5451 // Since our job with the temp is done it should be gone
5552 let loc = Location { block : block, statement_index : statement_len } ;
5653 patch. add_statement ( loc, StatementKind :: StorageDead ( temp) ) ;
54+
55+ // As all projections are off the base projection, if there are
56+ // multiple derefs in the middle of projection, it might cause
57+ // unsoundness, to not let that happen we break the loop.
58+ break ;
5759 }
5860 }
5961 }
0 commit comments