]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/borrow_check/used_muts.rs
Merge remote-tracking branch 'upstream/master' into asm-compile-tests
[rust.git] / src / librustc_mir / borrow_check / used_muts.rs
1 use rustc::mir::visit::{PlaceContext, Visitor};
2 use rustc::mir::{
3     BasicBlock, Local, Location, Place, PlaceBase, Statement, StatementKind, TerminatorKind
4 };
5
6 use rustc_data_structures::fx::FxHashSet;
7
8 use crate::borrow_check::MirBorrowckCtxt;
9
10 impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
11     /// Walks the MIR adding to the set of `used_mut` locals that will be ignored for the purposes
12     /// of the `unused_mut` lint.
13     ///
14     /// `temporary_used_locals` should contain locals that were found to be temporary, mutable and
15     ///  used from borrow checking. This function looks for assignments into these locals from
16     ///  user-declared locals and adds those user-defined locals to the `used_mut` set. This can
17     ///  occur due to a rare case involving upvars in closures.
18     ///
19     /// `never_initialized_mut_locals` should contain the set of user-declared mutable locals
20     ///  (not arguments) that have not already been marked as being used.
21     ///  This function then looks for assignments from statements or the terminator into the locals
22     ///  from this set and removes them from the set. This leaves only those locals that have not
23     ///  been assigned to - this set is used as a proxy for locals that were not initialized due to
24     ///  unreachable code. These locals are then considered "used" to silence the lint for them.
25     ///  See #55344 for context.
26     crate fn gather_used_muts(
27         &mut self,
28         temporary_used_locals: FxHashSet<Local>,
29         mut never_initialized_mut_locals: FxHashSet<Local>,
30     ) {
31         {
32             let mut visitor = GatherUsedMutsVisitor {
33                 temporary_used_locals,
34                 never_initialized_mut_locals: &mut never_initialized_mut_locals,
35                 mbcx: self,
36             };
37             visitor.visit_mir(visitor.mbcx.mir);
38         }
39
40         // Take the union of the existed `used_mut` set with those variables we've found were
41         // never initialized.
42         debug!("gather_used_muts: never_initialized_mut_locals={:?}", never_initialized_mut_locals);
43         self.used_mut = self.used_mut.union(&never_initialized_mut_locals).cloned().collect();
44     }
45 }
46
47 /// MIR visitor for collecting used mutable variables.
48 /// The 'visit lifetime represents the duration of the MIR walk.
49 struct GatherUsedMutsVisitor<'visit, 'cx: 'visit, 'gcx: 'tcx, 'tcx: 'cx> {
50     temporary_used_locals: FxHashSet<Local>,
51     never_initialized_mut_locals: &'visit mut FxHashSet<Local>,
52     mbcx: &'visit mut MirBorrowckCtxt<'cx, 'gcx, 'tcx>,
53 }
54
55 impl<'visit, 'cx, 'gcx, 'tcx> Visitor<'tcx> for GatherUsedMutsVisitor<'visit, 'cx, 'gcx, 'tcx> {
56     fn visit_terminator_kind(
57         &mut self,
58         _block: BasicBlock,
59         kind: &TerminatorKind<'tcx>,
60         _location: Location,
61     ) {
62         debug!("visit_terminator_kind: kind={:?}", kind);
63         match &kind {
64             TerminatorKind::Call { destination: Some((into, _)), .. } => {
65                 if let Some(local) = into.base_local() {
66                     debug!(
67                         "visit_terminator_kind: kind={:?} local={:?} \
68                          never_initialized_mut_locals={:?}",
69                         kind, local, self.never_initialized_mut_locals
70                     );
71                     let _ = self.never_initialized_mut_locals.remove(&local);
72                 }
73             },
74             _ => {},
75         }
76     }
77
78     fn visit_statement(
79         &mut self,
80         _block: BasicBlock,
81         statement: &Statement<'tcx>,
82         _location: Location,
83     ) {
84         match &statement.kind {
85             StatementKind::Assign(into, _) => {
86                 // Remove any locals that we found were initialized from the
87                 // `never_initialized_mut_locals` set. At the end, the only remaining locals will
88                 // be those that were never initialized - we will consider those as being used as
89                 // they will either have been removed by unreachable code optimizations; or linted
90                 // as unused variables.
91                 if let Some(local) = into.base_local() {
92                     debug!(
93                         "visit_statement: statement={:?} local={:?} \
94                          never_initialized_mut_locals={:?}",
95                         statement, local, self.never_initialized_mut_locals
96                     );
97                     let _ = self.never_initialized_mut_locals.remove(&local);
98                 }
99             },
100             _ => {},
101         }
102     }
103
104     fn visit_local(
105         &mut self,
106         local: &Local,
107         place_context: PlaceContext<'tcx>,
108         location: Location,
109     ) {
110         if place_context.is_place_assignment() && self.temporary_used_locals.contains(local) {
111             // Propagate the Local assigned at this Location as a used mutable local variable
112             for moi in &self.mbcx.move_data.loc_map[location] {
113                 let mpi = &self.mbcx.move_data.moves[*moi].path;
114                 let path = &self.mbcx.move_data.move_paths[*mpi];
115                 debug!(
116                     "assignment of {:?} to {:?}, adding {:?} to used mutable set",
117                     path.place, local, path.place
118                 );
119                 if let Place::Base(PlaceBase::Local(user_local)) = path.place {
120                     self.mbcx.used_mut.insert(user_local);
121                 }
122             }
123         }
124     }
125 }