Make derefer work everwhere

Co-Authored-By: Oli Scherer <332036+oli-obk@users.noreply.github.com>
This commit is contained in:
ouz-a 2022-04-16 16:03:14 +03:00
parent c8422403f7
commit aada74b28f
12 changed files with 610 additions and 273 deletions

View file

@ -2,6 +2,7 @@ use rustc_index::vec::{Idx, IndexVec};
use rustc_middle::mir::*;
use rustc_middle::ty::Ty;
use rustc_span::Span;
use std::collections::VecDeque;
/// This struct represents a patch to MIR, which can add
/// new statements and basic blocks and patch over block
@ -141,6 +142,9 @@ impl<'tcx> MirPatch<'tcx> {
let mut delta = 0;
let mut last_bb = START_BLOCK;
let mut terminator_targets = Vec::new();
let mut inf_and_stmt: VecDeque<(SourceInfo, StatementKind<'_>)> = VecDeque::new();
for (mut loc, stmt) in new_statements {
if loc.block != last_bb {
delta = 0;
@ -149,11 +153,33 @@ impl<'tcx> MirPatch<'tcx> {
debug!("MirPatch: adding statement {:?} at loc {:?}+{}", stmt, loc, delta);
loc.statement_index += delta;
let source_info = Self::source_info_for_index(&body[loc.block], loc);
// For mir-opt `Derefer` to work in all cases we need to
// get terminator's targets and apply the statement to all of them.
if loc.statement_index > body[loc.block].statements.len() {
let term = body[loc.block].terminator();
let successors = term.successors().clone();
for i in successors {
inf_and_stmt.push_back((source_info, stmt.clone()));
terminator_targets.push(i.clone());
}
delta += 1;
continue;
}
body[loc.block]
.statements
.insert(loc.statement_index, Statement { source_info, kind: stmt });
delta += 1;
}
for target in terminator_targets.iter() {
let inf_and_stmt = inf_and_stmt.pop_front().unwrap();
body[*target]
.statements
.insert(0, Statement { source_info: inf_and_stmt.0, kind: inf_and_stmt.1 });
}
}
pub fn source_info_for_index(data: &BasicBlockData<'_>, loc: Location) -> SourceInfo {