From 63a4e721b30964022cbe7abb98dabc74f3a7a676 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Re=CC=81my=20Rakic?= Date: Thu, 21 Jun 2018 14:12:26 +0200 Subject: [PATCH] Share code between gather_used_muts and find_assignments --- src/librustc_mir/borrow_check/used_muts.rs | 35 +++++++------------- src/librustc_mir/util/collect_writes.rs | 37 +++++++++++----------- 2 files changed, 30 insertions(+), 42 deletions(-) diff --git a/src/librustc_mir/borrow_check/used_muts.rs b/src/librustc_mir/borrow_check/used_muts.rs index 0a6258c00110..dbe19bc47859 100644 --- a/src/librustc_mir/borrow_check/used_muts.rs +++ b/src/librustc_mir/borrow_check/used_muts.rs @@ -14,6 +14,7 @@ use rustc::mir::{Local, Location, Place}; 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 @@ impl<'visit, 'cx, 'gcx, 'tcx> Visitor<'tcx> for GatherUsedMutsVisitor<'visit, 'c 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 => {} } } } diff --git a/src/librustc_mir/util/collect_writes.rs b/src/librustc_mir/util/collect_writes.rs index f04f9233447c..23f753f8569b 100644 --- a/src/librustc_mir/util/collect_writes.rs +++ b/src/librustc_mir/util/collect_writes.rs @@ -43,25 +43,24 @@ impl<'tcx> Visitor<'tcx> for FindLocalAssignmentVisitor { 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, + } }