]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/util/collect_writes.rs
Rollup merge of #69734 - tmiasko:di-enumerator, r=ecstatic-morse
[rust.git] / src / librustc_mir / util / collect_writes.rs
1 use rustc::mir::visit::PlaceContext;
2 use rustc::mir::visit::Visitor;
3 use rustc::mir::ReadOnlyBodyAndCache;
4 use rustc::mir::{Local, Location};
5
6 crate trait FindAssignments {
7     // Finds all statements that assign directly to local (i.e., X = ...)
8     // and returns their locations.
9     fn find_assignments(&self, local: Local) -> Vec<Location>;
10 }
11
12 impl<'a, 'tcx> FindAssignments for ReadOnlyBodyAndCache<'a, 'tcx> {
13     fn find_assignments(&self, local: Local) -> Vec<Location> {
14         let mut visitor = FindLocalAssignmentVisitor { needle: local, locations: vec![] };
15         visitor.visit_body(*self);
16         visitor.locations
17     }
18 }
19
20 // The Visitor walks the MIR to return the assignment statements corresponding
21 // to a Local.
22 struct FindLocalAssignmentVisitor {
23     needle: Local,
24     locations: Vec<Location>,
25 }
26
27 impl<'tcx> Visitor<'tcx> for FindLocalAssignmentVisitor {
28     fn visit_local(&mut self, local: &Local, place_context: PlaceContext, location: Location) {
29         if self.needle != *local {
30             return;
31         }
32
33         if place_context.is_place_assignment() {
34             self.locations.push(location);
35         }
36     }
37 }