Skip to content

Commit

Permalink
Refactored used_mut_nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilshagri committed Aug 17, 2017
1 parent a7e0018 commit a41db50
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 14 deletions.
6 changes: 0 additions & 6 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -811,11 +811,6 @@ pub struct GlobalCtxt<'tcx> {
/// present in this set can be warned about.
pub used_unsafe: RefCell<NodeSet>,

/// Set of nodes which mark locals as mutable which end up getting used at
/// some point. Local variable definitions not in this set can be warned
/// about.
pub used_mut_nodes: RefCell<NodeSet>,

/// Maps any item's def-id to its stability index.
pub stability: RefCell<stability::Index<'tcx>>,

Expand Down Expand Up @@ -1043,7 +1038,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
inhabitedness_cache: RefCell::new(FxHashMap()),
lang_items,
used_unsafe: RefCell::new(NodeSet()),
used_mut_nodes: RefCell::new(NodeSet()),
stability: RefCell::new(stability),
selection_cache: traits::SelectionCache::new(),
evaluation_cache: traits::EvaluationCache::new(),
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/ty/maps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use mir;
use mir::transform::{MirSuite, MirPassIndex};
use session::CompileResult;
use traits::specialization_graph;
use ty::{self, CrateInherentImpls, Ty, TyCtxt};
use ty::{self, BorrowCheckResult, CrateInherentImpls, Ty, TyCtxt};
use ty::layout::{Layout, LayoutError};
use ty::item_path;
use ty::steal::Steal;
Expand Down Expand Up @@ -922,7 +922,7 @@ define_maps! { <'tcx>

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

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

/// Gets a complete map from all types to their inherent impls.
/// Not meant to be used directly outside of coherence.
Expand Down
7 changes: 7 additions & 0 deletions src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2599,3 +2599,10 @@ impl fmt::Display for SymbolName {
fmt::Display::fmt(&self.name, fmt)
}
}

pub struct BorrowCheckResult {
/// contains the node-ids for variables within this function where the `mut`
/// declaration was used in some way (e.g., by modifying the variable's value,
/// or taking an `&mut` borrow of it).
used_mut_nodes: NodeSet
}
5 changes: 3 additions & 2 deletions src/librustc_borrowck/borrowck/check_loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ pub fn check_loans<'a, 'b, 'c, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
dfcx_loans: &LoanDataFlow<'b, 'tcx>,
move_data: &move_data::FlowedMoveData<'c, 'tcx>,
all_loans: &[Loan<'tcx>],
body: &hir::Body) {
body: &hir::Body) -> BorrowCheckResult {
debug!("check_loans(body id={})", body.value.id);

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

#[derive(PartialEq)]
Expand Down Expand Up @@ -843,7 +844,7 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
let lp = opt_loan_path(&assignee_cmt).unwrap();
self.move_data.each_assignment_of(assignment_id, &lp, |assign| {
if assignee_cmt.mutbl.is_mutable() {
self.tcx().used_mut_nodes.borrow_mut().insert(local_id);
self.bccx.borrowck_result.borrow_mut().used_mut_nodes.insert(local_id);
} else {
self.bccx.report_reassigned_immutable_variable(
assignment_span,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_borrowck/borrowck/gather_loans/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ impl<'a, 'tcx> GatherLoanCtxt<'a, 'tcx> {
}
LpUpvar(ty::UpvarId{ var_id, closure_expr_id: _ }) => {
let local_id = self.tcx().hir.def_index_to_node_id(var_id);
self.tcx().used_mut_nodes.borrow_mut().insert(local_id);
self.bccx.borrowck_result.borrow_mut().used_mut_nodes.insert(local_id);
None
}
LpExtend(ref base, mc::McInherited, LpDeref(pointer_kind)) |
Expand Down
8 changes: 6 additions & 2 deletions src/librustc_borrowck/borrowck/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use rustc::middle::mem_categorization::Categorization;
use rustc::middle::mem_categorization::ImmutabilityBlame;
use rustc::middle::region::{self, RegionMaps};
use rustc::middle::free_region::RegionRelations;
use rustc::ty::{self, TyCtxt};
use rustc::ty::{self, BorrowCheckResult, TyCtxt};
use rustc::ty::maps::Providers;

use rustc_mir::util::borrowck_errors::{BorrowckErrors, Origin};
Expand Down Expand Up @@ -80,7 +80,7 @@ pub struct AnalysisData<'a, 'tcx: 'a> {
pub move_data: move_data::FlowedMoveData<'a, 'tcx>,
}

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

let owner_id = tcx.hir.as_local_node_id(owner_def_id).unwrap();
Expand Down Expand Up @@ -128,6 +128,8 @@ fn borrowck<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, owner_def_id: DefId) {
{
check_loans::check_loans(bccx, &loan_dfcx, &flowed_moves, &all_loans, body);
}

check_loans::check_loans(bccx, &loan_dfcx, &flowed_moves, &all_loans, body)
}

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

region_maps: Rc<RegionMaps>,

borrowck_result: Rc<BorrowCheckResult>,

owner_def_id: DefId,
}

Expand Down
13 changes: 12 additions & 1 deletion src/librustc_lint/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ declare_lint! {
}

#[derive(Copy, Clone)]
pub struct UnusedMut;
pub struct UnusedMut {
borrowck_results: Vec<Rc<BorrowCheckResult>>
}

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

impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedMut {
fn check_body(&mut self, cx: &LateContext, body: &'tcx hir::Body) {
let body_owner_def_id = cx.tcx().hir.body_owner_def_id(body.id());
self.borrowck_results.push(cx.tcx().borrowck(body_owner_def_id));
}

fn check_body_post(&mut self, cx: &LateContext, body: &'tcx hir::Body) {
self.borrowck_results.pop();
}

fn check_expr(&mut self, cx: &LateContext, e: &hir::Expr) {
if let hir::ExprMatch(_, ref arms, _) = e.node {
for a in arms {
Expand Down

0 comments on commit a41db50

Please sign in to comment.