diff options
| author | ouz-a <oguz.agcayazi@gmail.com> | 2022-04-09 20:38:06 +0300 |
|---|---|---|
| committer | ouz-a <oguz.agcayazi@gmail.com> | 2022-04-09 20:38:06 +0300 |
| commit | cc57656969554bd0dcbf95641bebadc076fac61f (patch) | |
| tree | 8b7b4aa7993475d3208f5801863c754e5ca27071 /compiler/rustc_mir_transform/src | |
| parent | 1cf6d6940c6e1368c76d2565ce13aeaa7e70a4b7 (diff) | |
| download | rust-cc57656969554bd0dcbf95641bebadc076fac61f.tar.gz rust-cc57656969554bd0dcbf95641bebadc076fac61f.zip | |
support multiple derefs
Diffstat (limited to 'compiler/rustc_mir_transform/src')
| -rw-r--r-- | compiler/rustc_mir_transform/src/deref_separator.rs | 39 |
1 files changed, 26 insertions, 13 deletions
diff --git a/compiler/rustc_mir_transform/src/deref_separator.rs b/compiler/rustc_mir_transform/src/deref_separator.rs index 79aac163550..d8660d4f2fd 100644 --- a/compiler/rustc_mir_transform/src/deref_separator.rs +++ b/compiler/rustc_mir_transform/src/deref_separator.rs @@ -11,6 +11,8 @@ pub fn deref_finder<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { for (i, stmt) in data.statements.iter_mut().enumerate() { match stmt.kind { StatementKind::Assign(box (og_place, Rvalue::Ref(region, borrow_knd, place))) => { + let mut place_local = place.local; + let mut last_len = 0; for (idx, (p_ref, p_elem)) in place.iter_projections().enumerate() { if p_elem == ProjectionElem::Deref && !p_ref.projection.is_empty() { // The type that we are derefing. @@ -23,14 +25,30 @@ pub fn deref_finder<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { patch.add_statement(loc, StatementKind::StorageLive(temp)); // We are adding current p_ref's projections to our - // temp value. - let deref_place = - Place::from(p_ref.local).project_deeper(p_ref.projection, tcx); - patch.add_assign( - loc, - Place::from(temp), - Rvalue::Use(Operand::Move(deref_place)), - ); + // temp value, excluding projections we already covered. + if idx == 1 { + let deref_place = Place::from(place_local) + .project_deeper(&p_ref.projection[last_len..], tcx); + patch.add_assign( + loc, + Place::from(temp), + Rvalue::Use(Operand::Move(deref_place)), + ); + + place_local = temp; + last_len = p_ref.projection.len(); + } else { + let deref_place = Place::from(place_local) + .project_deeper(&p_ref.projection[last_len..], tcx); + patch.add_assign( + loc, + Place::from(temp), + Rvalue::Use(Operand::Move(deref_place)), + ); + + place_local = temp; + last_len = p_ref.projection.len(); + } // We are creating a place by using our temp value's location // and copying derefed values which we need to create new statement. @@ -50,11 +68,6 @@ pub fn deref_finder<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { // Since our job with the temp is done it should be gone let loc = Location { block: block, statement_index: i + 1 }; patch.add_statement(loc, StatementKind::StorageDead(temp)); - - // As all projections are off the base projection, if there are - // multiple derefs in the middle of projection, it might cause - // unsoundness, to not let that happen we break the loop. - break; } } } |
