fixed error, made function leaner and tighter

This commit is contained in:
ouz-a 2022-04-04 21:54:01 +03:00
parent 4332b5f903
commit 105e90f836
8 changed files with 69 additions and 89 deletions

View file

@ -11,41 +11,46 @@ 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))) => {
if borrow_knd == (BorrowKind::Mut { allow_two_phase_borrow: false }) {
for (idx, (p_ref, p_elem)) in place.iter_projections().enumerate() {
if p_elem == ProjectionElem::Deref {
// The type that we are derefing
let ty = p_ref.ty(local_decl, tcx).ty;
let temp = patch.new_temp(ty, stmt.source_info.span);
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
let ty = p_ref.ty(local_decl, tcx).ty;
let temp = patch.new_temp(ty, stmt.source_info.span);
// Because we are assigning this right before original statement
// we are using index i of statement
let loc = Location { block: block, statement_index: i };
patch.add_statement(loc, StatementKind::StorageLive(temp));
// Because we are assigning this right before original statement
// we are using index i of statement
let loc = Location { block: block, statement_index: i };
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)),
);
// 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)),
);
// We are creating a place by using our temp value's location
// and copying derefed values we need to it
let temp_place =
Place::from(temp).project_deeper(&place.projection[idx..], tcx);
patch.add_assign(
loc,
// We are creating a place by using our temp value's location
// and copying derefed values which we need to create new statement
let temp_place =
Place::from(temp).project_deeper(&place.projection[idx..], tcx);
patch.add_assign(
loc,
og_place,
Rvalue::Ref(region, borrow_knd, temp_place),
);
let new_stmt = Statement {
source_info: stmt.source_info,
kind: StatementKind::Assign(Box::new((
og_place,
Rvalue::Ref(region, borrow_knd, temp_place),
);
// We have to delete the original statement since we just
// replaced it
stmt.make_nop();
}
))),
};
// Replace current statement with newly created one
*stmt = new_stmt;
}
}
}