]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/util/collect_writes.rs
Rollup merge of #65763 - ObsidianMinor:diag/65642, r=varkor
[rust.git] / src / librustc_mir / util / collect_writes.rs
1 use rustc::mir::{Local, Location};
2 use rustc::mir::Body;
3 use rustc::mir::visit::PlaceContext;
4 use rustc::mir::visit::Visitor;
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<'tcx> FindAssignments for Body<'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,
29                    local: &Local,
30                    place_context: PlaceContext,
31                    location: Location) {
32         if self.needle != *local {
33             return;
34         }
35
36         if place_context.is_place_assignment() {
37             self.locations.push(location);
38         }
39     }
40 }