Share code between gather_used_muts and find_assignments

This commit is contained in:
Rémy Rakic 2018-06-21 14:12:26 +02:00
parent 71543836ab
commit 63a4e721b3
2 changed files with 30 additions and 42 deletions

View File

@ -14,6 +14,7 @@ use rustc::mir::{Local, Location, Place};
use rustc_data_structures::fx::FxHashSet;
use borrow_check::MirBorrowckCtxt;
use util::collect_writes::is_place_assignment;
impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
/// Walks the MIR looking for assignments to a set of locals, as part of the unused mutable
@ -45,31 +46,19 @@ impl<'visit, 'cx, 'gcx, 'tcx> Visitor<'tcx> for GatherUsedMutsVisitor<'visit, 'c
return;
}
match place_context {
PlaceContext::Store | PlaceContext::Call => {
// Propagate the Local assigned at this Location as a used mutable local variable
for moi in &self.mbcx.move_data.loc_map[location] {
let mpi = &self.mbcx.move_data.moves[*moi].path;
let path = &self.mbcx.move_data.move_paths[*mpi];
debug!(
"assignment of {:?} to {:?}, adding {:?} to used mutable set",
path.place, local, path.place
);
if let Place::Local(user_local) = path.place {
self.mbcx.used_mut.insert(user_local);
}
if is_place_assignment(&place_context) {
// Propagate the Local assigned at this Location as a used mutable local variable
for moi in &self.mbcx.move_data.loc_map[location] {
let mpi = &self.mbcx.move_data.moves[*moi].path;
let path = &self.mbcx.move_data.move_paths[*mpi];
debug!(
"assignment of {:?} to {:?}, adding {:?} to used mutable set",
path.place, local, path.place
);
if let Place::Local(user_local) = path.place {
self.mbcx.used_mut.insert(user_local);
}
}
PlaceContext::AsmOutput
| PlaceContext::Drop
| PlaceContext::Inspect
| PlaceContext::Borrow { .. }
| PlaceContext::Projection(..)
| PlaceContext::Copy
| PlaceContext::Move
| PlaceContext::StorageLive
| PlaceContext::StorageDead
| PlaceContext::Validate => {}
}
}
}

View File

@ -43,25 +43,24 @@ impl<'tcx> Visitor<'tcx> for FindLocalAssignmentVisitor {
return;
}
match place_context {
PlaceContext::Store | PlaceContext::Call => {
self.locations.push(location);
}
PlaceContext::AsmOutput |
PlaceContext::Drop |
PlaceContext::Inspect |
PlaceContext::Borrow { .. } |
PlaceContext::Projection(..) |
PlaceContext::Copy |
PlaceContext::Move |
PlaceContext::StorageLive |
PlaceContext::StorageDead |
PlaceContext::Validate => {
// TO-DO
// self.super_local(local)
}
if is_place_assignment(&place_context) {
self.locations.push(location);
}
}
// TO-DO
// fn super_local()
}
/// Returns true if this place context represents an assignment statement
crate fn is_place_assignment(place_context: &PlaceContext) -> bool {
match *place_context {
PlaceContext::Store | PlaceContext::Call | PlaceContext::AsmOutput => true,
PlaceContext::Drop
| PlaceContext::Inspect
| PlaceContext::Borrow { .. }
| PlaceContext::Projection(..)
| PlaceContext::Copy
| PlaceContext::Move
| PlaceContext::StorageLive
| PlaceContext::StorageDead
| PlaceContext::Validate => false,
}
}