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