]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_mir/util/def_use.rs
Rollup merge of #69213 - LeSeulArtichaut:improve-doc-iter, r=steveklabnik
[rust.git] / src / librustc_mir / util / def_use.rs
index 1611caddad1a6fe5344c91a40a6c4d44fbf54c33..aa9ddbdbda94ff36749c1fea313982d3209d5170 100644 (file)
@@ -1,7 +1,9 @@
 //! Def-use analysis.
 
-use rustc::mir::{Body, Local, Location, PlaceElem, VarDebugInfo};
-use rustc::mir::visit::{PlaceContext, MutVisitor, Visitor};
+use rustc::mir::visit::{MutVisitor, PlaceContext, Visitor};
+use rustc::mir::{
+    Body, BodyAndCache, Local, Location, PlaceElem, ReadOnlyBodyAndCache, VarDebugInfo,
+};
 use rustc::ty::TyCtxt;
 use rustc_index::vec::IndexVec;
 use std::mem;
@@ -25,12 +27,10 @@ pub struct Use {
 
 impl DefUseAnalysis {
     pub fn new(body: &Body<'_>) -> DefUseAnalysis {
-        DefUseAnalysis {
-            info: IndexVec::from_elem_n(Info::new(), body.local_decls.len()),
-        }
+        DefUseAnalysis { info: IndexVec::from_elem_n(Info::new(), body.local_decls.len()) }
     }
 
-    pub fn analyze(&mut self, body: &Body<'_>) {
+    pub fn analyze(&mut self, body: ReadOnlyBodyAndCache<'_, '_>) {
         self.clear();
 
         let mut finder = DefUseFinder {
@@ -55,11 +55,11 @@ pub fn local_info(&self, local: Local) -> &Info {
     fn mutate_defs_and_uses(
         &self,
         local: Local,
-        body: &mut Body<'tcx>,
+        body: &mut BodyAndCache<'tcx>,
         new_local: Local,
         tcx: TyCtxt<'tcx>,
     ) {
-        let mut visitor = MutateUseVisitor::new(local, new_local, body, tcx);
+        let mut visitor = MutateUseVisitor::new(local, new_local, tcx);
         let info = &self.info[local];
         for place_use in &info.defs_and_uses {
             visitor.visit_location(body, place_use.location)
@@ -71,11 +71,13 @@ fn mutate_defs_and_uses(
     }
 
     // FIXME(pcwalton): this should update the def-use chains.
-    pub fn replace_all_defs_and_uses_with(&self,
-                                          local: Local,
-                                          body: &mut Body<'tcx>,
-                                          new_local: Local,
-                                          tcx: TyCtxt<'tcx>) {
+    pub fn replace_all_defs_and_uses_with(
+        &self,
+        local: Local,
+        body: &mut BodyAndCache<'tcx>,
+        new_local: Local,
+        tcx: TyCtxt<'tcx>,
+    ) {
         self.mutate_defs_and_uses(local, body, new_local, tcx)
     }
 }
@@ -87,18 +89,12 @@ struct DefUseFinder {
 }
 
 impl Visitor<'_> for DefUseFinder {
-    fn visit_local(&mut self,
-                   &local: &Local,
-                   context: PlaceContext,
-                   location: Location) {
+    fn visit_local(&mut self, &local: &Local, context: PlaceContext, location: Location) {
         let info = &mut self.info[local];
         if self.in_var_debug_info {
             info.var_debug_info_indices.push(self.var_debug_info_index);
         } else {
-            info.defs_and_uses.push(Use {
-                context,
-                location,
-            });
+            info.defs_and_uses.push(Use { context, location });
         }
     }
     fn visit_var_debug_info(&mut self, var_debug_info: &VarDebugInfo<'tcx>) {
@@ -112,10 +108,7 @@ fn visit_var_debug_info(&mut self, var_debug_info: &VarDebugInfo<'tcx>) {
 
 impl Info {
     fn new() -> Info {
-        Info {
-            defs_and_uses: vec![],
-            var_debug_info_indices: vec![],
-        }
+        Info { defs_and_uses: vec![], var_debug_info_indices: vec![] }
     }
 
     fn clear(&mut self) {
@@ -131,18 +124,14 @@ pub fn def_count_not_including_drop(&self) -> usize {
         self.defs_not_including_drop().count()
     }
 
-    pub fn defs_not_including_drop(
-        &self,
-    ) -> impl Iterator<Item=&Use> {
-        self.defs_and_uses.iter().filter(|place_use| {
-            place_use.context.is_mutating_use() && !place_use.context.is_drop()
-        })
+    pub fn defs_not_including_drop(&self) -> impl Iterator<Item = &Use> {
+        self.defs_and_uses
+            .iter()
+            .filter(|place_use| place_use.context.is_mutating_use() && !place_use.context.is_drop())
     }
 
     pub fn use_count(&self) -> usize {
-        self.defs_and_uses.iter().filter(|place_use| {
-            place_use.context.is_nonmutating_use()
-        }).count()
+        self.defs_and_uses.iter().filter(|place_use| place_use.context.is_nonmutating_use()).count()
     }
 }
 
@@ -153,12 +142,7 @@ struct MutateUseVisitor<'tcx> {
 }
 
 impl MutateUseVisitor<'tcx> {
-    fn new(
-        query: Local,
-        new_local: Local,
-        _: &Body<'tcx>,
-        tcx: TyCtxt<'tcx>,
-    ) -> MutateUseVisitor<'tcx> {
+    fn new(query: Local, new_local: Local, tcx: TyCtxt<'tcx>) -> MutateUseVisitor<'tcx> {
         MutateUseVisitor { query, new_local, tcx }
     }
 }
@@ -168,19 +152,13 @@ fn tcx(&self) -> TyCtxt<'tcx> {
         self.tcx
     }
 
-    fn visit_local(&mut self,
-                    local: &mut Local,
-                    _context: PlaceContext,
-                    _location: Location) {
+    fn visit_local(&mut self, local: &mut Local, _context: PlaceContext, _location: Location) {
         if *local == self.query {
             *local = self.new_local;
         }
     }
 
-    fn process_projection_elem(
-        &mut self,
-        elem: &PlaceElem<'tcx>,
-    ) -> Option<PlaceElem<'tcx>> {
+    fn process_projection_elem(&mut self, elem: &PlaceElem<'tcx>) -> Option<PlaceElem<'tcx>> {
         match elem {
             PlaceElem::Index(local) if *local == self.query => {
                 Some(PlaceElem::Index(self.new_local))