Files
rust/compiler/rustc_const_eval/src/util/collect_writes.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

37 lines
1.1 KiB
Rust
Raw Normal View History

2020-03-29 16:41:09 +02:00
use rustc_middle::mir::visit::PlaceContext;
use rustc_middle::mir::visit::Visitor;
use rustc_middle::mir::{Body, Local, Location};
2018-03-03 01:49:55 +05:30
pub trait FindAssignments {
2018-03-14 23:51:54 +05:30
// Finds all statements that assign directly to local (i.e., X = ...)
// and returns their locations.
fn find_assignments(&self, local: Local) -> Vec<Location>;
}
impl<'tcx> FindAssignments for Body<'tcx> {
2018-03-14 23:51:54 +05:30
fn find_assignments(&self, local: Local) -> Vec<Location> {
let mut visitor = FindLocalAssignmentVisitor { needle: local, locations: vec![] };
visitor.visit_body(self);
2018-03-14 23:51:54 +05:30
visitor.locations
}
}
// The Visitor walks the MIR to return the assignment statements corresponding
// to a Local.
2018-03-14 23:51:54 +05:30
struct FindLocalAssignmentVisitor {
2018-03-03 01:49:55 +05:30
needle: Local,
locations: Vec<Location>,
}
impl<'tcx> Visitor<'tcx> for FindLocalAssignmentVisitor {
fn visit_local(&mut self, local: Local, place_context: PlaceContext, location: Location) {
if self.needle != local {
2018-03-03 01:49:55 +05:30
return;
}
2018-03-03 01:49:55 +05:30
if place_context.is_place_assignment() {
self.locations.push(location);
2018-03-03 01:49:55 +05:30
}
}
}