Skip to content

Commit a41db50

Browse files
committed
Refactored used_mut_nodes
1 parent a7e0018 commit a41db50

File tree

7 files changed

+31
-14
lines changed

7 files changed

+31
-14
lines changed

src/librustc/ty/context.rs

-6
Original file line numberDiff line numberDiff line change
@@ -811,11 +811,6 @@ pub struct GlobalCtxt<'tcx> {
811811
/// present in this set can be warned about.
812812
pub used_unsafe: RefCell<NodeSet>,
813813

814-
/// Set of nodes which mark locals as mutable which end up getting used at
815-
/// some point. Local variable definitions not in this set can be warned
816-
/// about.
817-
pub used_mut_nodes: RefCell<NodeSet>,
818-
819814
/// Maps any item's def-id to its stability index.
820815
pub stability: RefCell<stability::Index<'tcx>>,
821816

@@ -1043,7 +1038,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
10431038
inhabitedness_cache: RefCell::new(FxHashMap()),
10441039
lang_items,
10451040
used_unsafe: RefCell::new(NodeSet()),
1046-
used_mut_nodes: RefCell::new(NodeSet()),
10471041
stability: RefCell::new(stability),
10481042
selection_cache: traits::SelectionCache::new(),
10491043
evaluation_cache: traits::EvaluationCache::new(),

src/librustc/ty/maps.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use mir;
2121
use mir::transform::{MirSuite, MirPassIndex};
2222
use session::CompileResult;
2323
use traits::specialization_graph;
24-
use ty::{self, CrateInherentImpls, Ty, TyCtxt};
24+
use ty::{self, BorrowCheckResult, CrateInherentImpls, Ty, TyCtxt};
2525
use ty::layout::{Layout, LayoutError};
2626
use ty::item_path;
2727
use ty::steal::Steal;
@@ -922,7 +922,7 @@ define_maps! { <'tcx>
922922

923923
[] coherent_trait: coherent_trait_dep_node((CrateNum, DefId)) -> (),
924924

925-
[] borrowck: BorrowCheck(DefId) -> (),
925+
[] borrowck: BorrowCheck(DefId) -> Rc<BorrowCheckResult>,
926926

927927
/// Gets a complete map from all types to their inherent impls.
928928
/// Not meant to be used directly outside of coherence.

src/librustc/ty/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -2599,3 +2599,10 @@ impl fmt::Display for SymbolName {
25992599
fmt::Display::fmt(&self.name, fmt)
26002600
}
26012601
}
2602+
2603+
pub struct BorrowCheckResult {
2604+
/// contains the node-ids for variables within this function where the `mut`
2605+
/// declaration was used in some way (e.g., by modifying the variable's value,
2606+
/// or taking an `&mut` borrow of it).
2607+
used_mut_nodes: NodeSet
2608+
}

src/librustc_borrowck/borrowck/check_loans.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ pub fn check_loans<'a, 'b, 'c, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
189189
dfcx_loans: &LoanDataFlow<'b, 'tcx>,
190190
move_data: &move_data::FlowedMoveData<'c, 'tcx>,
191191
all_loans: &[Loan<'tcx>],
192-
body: &hir::Body) {
192+
body: &hir::Body) -> BorrowCheckResult {
193193
debug!("check_loans(body id={})", body.value.id);
194194

195195
let def_id = bccx.tcx.hir.body_owner_def_id(body.id());
@@ -203,6 +203,7 @@ pub fn check_loans<'a, 'b, 'c, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
203203
};
204204
euv::ExprUseVisitor::new(&mut clcx, bccx.tcx, param_env, &bccx.region_maps, bccx.tables)
205205
.consume_body(body);
206+
clcx.bccx.borrowck_result.borrow()
206207
}
207208

208209
#[derive(PartialEq)]
@@ -843,7 +844,7 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
843844
let lp = opt_loan_path(&assignee_cmt).unwrap();
844845
self.move_data.each_assignment_of(assignment_id, &lp, |assign| {
845846
if assignee_cmt.mutbl.is_mutable() {
846-
self.tcx().used_mut_nodes.borrow_mut().insert(local_id);
847+
self.bccx.borrowck_result.borrow_mut().used_mut_nodes.insert(local_id);
847848
} else {
848849
self.bccx.report_reassigned_immutable_variable(
849850
assignment_span,

src/librustc_borrowck/borrowck/gather_loans/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ impl<'a, 'tcx> GatherLoanCtxt<'a, 'tcx> {
451451
}
452452
LpUpvar(ty::UpvarId{ var_id, closure_expr_id: _ }) => {
453453
let local_id = self.tcx().hir.def_index_to_node_id(var_id);
454-
self.tcx().used_mut_nodes.borrow_mut().insert(local_id);
454+
self.bccx.borrowck_result.borrow_mut().used_mut_nodes.insert(local_id);
455455
None
456456
}
457457
LpExtend(ref base, mc::McInherited, LpDeref(pointer_kind)) |

src/librustc_borrowck/borrowck/mod.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use rustc::middle::mem_categorization::Categorization;
3434
use rustc::middle::mem_categorization::ImmutabilityBlame;
3535
use rustc::middle::region::{self, RegionMaps};
3636
use rustc::middle::free_region::RegionRelations;
37-
use rustc::ty::{self, TyCtxt};
37+
use rustc::ty::{self, BorrowCheckResult, TyCtxt};
3838
use rustc::ty::maps::Providers;
3939

4040
use rustc_mir::util::borrowck_errors::{BorrowckErrors, Origin};
@@ -80,7 +80,7 @@ pub struct AnalysisData<'a, 'tcx: 'a> {
8080
pub move_data: move_data::FlowedMoveData<'a, 'tcx>,
8181
}
8282

83-
fn borrowck<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, owner_def_id: DefId) {
83+
fn borrowck<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, owner_def_id: DefId) -> BorrowCheckResult {
8484
debug!("borrowck(body_owner_def_id={:?})", owner_def_id);
8585

8686
let owner_id = tcx.hir.as_local_node_id(owner_def_id).unwrap();
@@ -128,6 +128,8 @@ fn borrowck<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, owner_def_id: DefId) {
128128
{
129129
check_loans::check_loans(bccx, &loan_dfcx, &flowed_moves, &all_loans, body);
130130
}
131+
132+
check_loans::check_loans(bccx, &loan_dfcx, &flowed_moves, &all_loans, body)
131133
}
132134

133135
fn build_borrowck_dataflow_data<'a, 'c, 'tcx, F>(this: &mut BorrowckCtxt<'a, 'tcx>,
@@ -217,6 +219,8 @@ pub struct BorrowckCtxt<'a, 'tcx: 'a> {
217219

218220
region_maps: Rc<RegionMaps>,
219221

222+
borrowck_result: Rc<BorrowCheckResult>,
223+
220224
owner_def_id: DefId,
221225
}
222226

src/librustc_lint/unused.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ declare_lint! {
3535
}
3636

3737
#[derive(Copy, Clone)]
38-
pub struct UnusedMut;
38+
pub struct UnusedMut {
39+
borrowck_results: Vec<Rc<BorrowCheckResult>>
40+
}
3941

4042
impl UnusedMut {
4143
fn check_unused_mut_pat(&self, cx: &LateContext, pats: &[P<hir::Pat>]) {
@@ -84,6 +86,15 @@ impl LintPass for UnusedMut {
8486
}
8587

8688
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedMut {
89+
fn check_body(&mut self, cx: &LateContext, body: &'tcx hir::Body) {
90+
let body_owner_def_id = cx.tcx().hir.body_owner_def_id(body.id());
91+
self.borrowck_results.push(cx.tcx().borrowck(body_owner_def_id));
92+
}
93+
94+
fn check_body_post(&mut self, cx: &LateContext, body: &'tcx hir::Body) {
95+
self.borrowck_results.pop();
96+
}
97+
8798
fn check_expr(&mut self, cx: &LateContext, e: &hir::Expr) {
8899
if let hir::ExprMatch(_, ref arms, _) = e.node {
89100
for a in arms {

0 commit comments

Comments
 (0)