From b347a23b32ec46c580ce5aa1bbeeccd5142ae9f2 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Sat, 18 Feb 2017 05:59:48 -0500 Subject: [PATCH] make `borrowck_fn` and friends create `bccx` --- .../borrowck/gather_loans/mod.rs | 8 +- src/librustc_borrowck/borrowck/mod.rs | 73 +++++++++---------- 2 files changed, 42 insertions(+), 39 deletions(-) diff --git a/src/librustc_borrowck/borrowck/gather_loans/mod.rs b/src/librustc_borrowck/borrowck/gather_loans/mod.rs index 1783ca74a2592..b31b54a25415a 100644 --- a/src/librustc_borrowck/borrowck/gather_loans/mod.rs +++ b/src/librustc_borrowck/borrowck/gather_loans/mod.rs @@ -547,9 +547,15 @@ impl<'a, 'tcx> Visitor<'tcx> for StaticInitializerCtxt<'a, 'tcx> { } } -pub fn gather_loans_in_static_initializer(bccx: &mut BorrowckCtxt, body: hir::BodyId) { +pub fn gather_loans_in_static_initializer<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, + body: hir::BodyId) { debug!("gather_loans_in_static_initializer(expr={:?})", body); + let bccx = &BorrowckCtxt { + tcx: tcx, + tables: None + }; + let mut sicx = StaticInitializerCtxt { bccx: bccx, body_id: body diff --git a/src/librustc_borrowck/borrowck/mod.rs b/src/librustc_borrowck/borrowck/mod.rs index 2011cbe416f18..9c29cd375ff51 100644 --- a/src/librustc_borrowck/borrowck/mod.rs +++ b/src/librustc_borrowck/borrowck/mod.rs @@ -70,31 +70,43 @@ impl<'a, 'tcx> Visitor<'tcx> for BorrowckCtxt<'a, 'tcx> { match fk { FnKind::ItemFn(..) | FnKind::Method(..) => { - borrowck_fn(self, b); + borrowck_fn(self.tcx, b); intravisit::walk_fn(self, fk, fd, b, s, id); } FnKind::Closure(..) => { - borrowck_fn(self, b); + borrowck_fn(self.tcx, b); intravisit::walk_fn(self, fk, fd, b, s, id); } } } fn visit_item(&mut self, item: &'tcx hir::Item) { - borrowck_item(self, item); + // Gather loans for items. Note that we don't need + // to check loans for single expressions. The check + // loan step is intended for things that have a data + // flow dependent conditions. + match item.node { + hir::ItemStatic(.., ex) | + hir::ItemConst(_, ex) => { + gather_loans::gather_loans_in_static_initializer(self.tcx, ex); + } + _ => { } + } + + intravisit::walk_item(self, item); } fn visit_trait_item(&mut self, ti: &'tcx hir::TraitItem) { if let hir::TraitItemKind::Const(_, Some(expr)) = ti.node { - gather_loans::gather_loans_in_static_initializer(self, expr); + gather_loans::gather_loans_in_static_initializer(self.tcx, expr); } intravisit::walk_trait_item(self, ti); } fn visit_impl_item(&mut self, ii: &'tcx hir::ImplItem) { if let hir::ImplItemKind::Const(_, expr) = ii.node { - gather_loans::gather_loans_in_static_initializer(self, expr); + gather_loans::gather_loans_in_static_initializer(self.tcx, expr); } intravisit::walk_impl_item(self, ii); } @@ -109,22 +121,6 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) { tcx.visit_all_item_likes_in_krate(DepNode::BorrowCheck, &mut bccx.as_deep_visitor()); } -fn borrowck_item<'a, 'tcx>(this: &mut BorrowckCtxt<'a, 'tcx>, item: &'tcx hir::Item) { - // Gather loans for items. Note that we don't need - // to check loans for single expressions. The check - // loan step is intended for things that have a data - // flow dependent conditions. - match item.node { - hir::ItemStatic(.., ex) | - hir::ItemConst(_, ex) => { - gather_loans::gather_loans_in_static_initializer(this, ex); - } - _ => { } - } - - intravisit::walk_item(this, item); -} - /// Collection of conclusions determined via borrow checker analyses. pub struct AnalysisData<'a, 'tcx: 'a> { pub all_loans: Vec>, @@ -132,38 +128,39 @@ pub struct AnalysisData<'a, 'tcx: 'a> { pub move_data: move_data::FlowedMoveData<'a, 'tcx>, } -fn borrowck_fn<'a, 'tcx>(this: &mut BorrowckCtxt<'a, 'tcx>, body_id: hir::BodyId) { +fn borrowck_fn<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, body_id: hir::BodyId) { debug!("borrowck_fn(body_id={:?})", body_id); - assert!(this.tables.is_none()); - let owner_id = this.tcx.hir.body_owner(body_id); - let owner_def_id = this.tcx.hir.local_def_id(owner_id); - let attributes = this.tcx.get_attrs(owner_def_id); - let tables = this.tcx.item_tables(owner_def_id); - this.tables = Some(tables); + let owner_id = tcx.hir.body_owner(body_id); + let owner_def_id = tcx.hir.local_def_id(owner_id); + let attributes = tcx.get_attrs(owner_def_id); + let tables = tcx.item_tables(owner_def_id); + + let mut bccx = &mut BorrowckCtxt { + tcx: tcx, + tables: Some(tables), + }; - let body = this.tcx.hir.body(body_id); + let body = bccx.tcx.hir.body(body_id); - if this.tcx.has_attr(owner_def_id, "rustc_mir_borrowck") { - mir::borrowck_mir(this, owner_id, &attributes); + if bccx.tcx.has_attr(owner_def_id, "rustc_mir_borrowck") { + mir::borrowck_mir(bccx, owner_id, &attributes); } - let cfg = cfg::CFG::new(this.tcx, &body.value); + let cfg = cfg::CFG::new(bccx.tcx, &body.value); let AnalysisData { all_loans, loans: loan_dfcx, move_data: flowed_moves } = - build_borrowck_dataflow_data(this, &cfg, body_id); + build_borrowck_dataflow_data(bccx, &cfg, body_id); move_data::fragments::instrument_move_fragments(&flowed_moves.move_data, - this.tcx, + bccx.tcx, owner_id); - move_data::fragments::build_unfragmented_map(this, + move_data::fragments::build_unfragmented_map(bccx, &flowed_moves.move_data, owner_id); - check_loans::check_loans(this, &loan_dfcx, &flowed_moves, &all_loans[..], body); - - this.tables = None; + check_loans::check_loans(bccx, &loan_dfcx, &flowed_moves, &all_loans[..], body); } fn build_borrowck_dataflow_data<'a, 'tcx>(this: &mut BorrowckCtxt<'a, 'tcx>,