]> git.lizzy.rs Git - rust.git/commitdiff
Share code between gather_used_muts and find_assignments
authorRémy Rakic <remy.rakic@gmail.com>
Thu, 21 Jun 2018 12:12:26 +0000 (14:12 +0200)
committerRémy Rakic <remy.rakic@gmail.com>
Thu, 21 Jun 2018 12:12:26 +0000 (14:12 +0200)
src/librustc_mir/borrow_check/used_muts.rs
src/librustc_mir/util/collect_writes.rs

index 0a6258c00110aa16ef28c732a60c96842dee18d3..dbe19bc47859f917801e66bbfc279a34d2235b75 100644 (file)
@@ -14,6 +14,7 @@
 use rustc_data_structures::fx::FxHashSet;
 
 use borrow_check::MirBorrowckCtxt;
+use util::collect_writes::is_place_assignment;
 
 impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
     /// Walks the MIR looking for assignments to a set of locals, as part of the unused mutable
@@ -45,31 +46,19 @@ fn visit_local(
             return;
         }
 
-        match place_context {
-            PlaceContext::Store | PlaceContext::Call => {
-                // Propagate the Local assigned at this Location as a used mutable local variable
-                for moi in &self.mbcx.move_data.loc_map[location] {
-                    let mpi = &self.mbcx.move_data.moves[*moi].path;
-                    let path = &self.mbcx.move_data.move_paths[*mpi];
-                    debug!(
-                        "assignment of {:?} to {:?}, adding {:?} to used mutable set",
-                        path.place, local, path.place
-                    );
-                    if let Place::Local(user_local) = path.place {
-                        self.mbcx.used_mut.insert(user_local);
-                    }
+        if is_place_assignment(&place_context) {
+            // Propagate the Local assigned at this Location as a used mutable local variable
+            for moi in &self.mbcx.move_data.loc_map[location] {
+                let mpi = &self.mbcx.move_data.moves[*moi].path;
+                let path = &self.mbcx.move_data.move_paths[*mpi];
+                debug!(
+                    "assignment of {:?} to {:?}, adding {:?} to used mutable set",
+                    path.place, local, path.place
+                );
+                if let Place::Local(user_local) = path.place {
+                    self.mbcx.used_mut.insert(user_local);
                 }
             }
-            PlaceContext::AsmOutput
-            | PlaceContext::Drop
-            | PlaceContext::Inspect
-            | PlaceContext::Borrow { .. }
-            | PlaceContext::Projection(..)
-            | PlaceContext::Copy
-            | PlaceContext::Move
-            | PlaceContext::StorageLive
-            | PlaceContext::StorageDead
-            | PlaceContext::Validate => {}
         }
     }
 }
index f04f9233447c3d83761a34836fa913182de3aa0c..23f753f8569bb780170cb0118e6c61a453106f48 100644 (file)
@@ -43,25 +43,24 @@ fn visit_local(&mut self,
             return;
         }
 
-        match place_context {
-            PlaceContext::Store | PlaceContext::Call => {
-                self.locations.push(location);
-            }
-            PlaceContext::AsmOutput |
-            PlaceContext::Drop |
-            PlaceContext::Inspect |
-            PlaceContext::Borrow { .. } |
-            PlaceContext::Projection(..) |
-            PlaceContext::Copy |
-            PlaceContext::Move |
-            PlaceContext::StorageLive |
-            PlaceContext::StorageDead |
-            PlaceContext::Validate => {
-                // TO-DO
-                // self.super_local(local)
-            }
+        if is_place_assignment(&place_context) {
+            self.locations.push(location);
         }
     }
-    // TO-DO
-    // fn super_local()
+}
+
+/// Returns true if this place context represents an assignment statement
+crate fn is_place_assignment(place_context: &PlaceContext) -> bool {
+    match *place_context {
+        PlaceContext::Store | PlaceContext::Call | PlaceContext::AsmOutput => true,
+        PlaceContext::Drop
+        | PlaceContext::Inspect
+        | PlaceContext::Borrow { .. }
+        | PlaceContext::Projection(..)
+        | PlaceContext::Copy
+        | PlaceContext::Move
+        | PlaceContext::StorageLive
+        | PlaceContext::StorageDead
+        | PlaceContext::Validate => false,
+    }
 }