]> git.lizzy.rs Git - rust.git/commitdiff
make `borrowck_fn` and friends create `bccx`
authorNiko Matsakis <niko@alum.mit.edu>
Sat, 18 Feb 2017 10:59:48 +0000 (05:59 -0500)
committerNiko Matsakis <niko@alum.mit.edu>
Tue, 28 Feb 2017 13:43:47 +0000 (08:43 -0500)
src/librustc_borrowck/borrowck/gather_loans/mod.rs
src/librustc_borrowck/borrowck/mod.rs

index 1783ca74a25923fdf4fcad56f6b5a29c75e93788..b31b54a25415a2261fc547db509c435bad4bbdca 100644 (file)
@@ -547,9 +547,15 @@ fn visit_expr(&mut self, ex: &'tcx Expr) {
     }
 }
 
-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
index 2011cbe416f18f337adbe42c4ce887f2729da822..9c29cd375ff5152bfcade9bb671809e4530a6330 100644 (file)
@@ -70,31 +70,43 @@ fn visit_fn(&mut self, fk: FnKind<'tcx>, fd: &'tcx hir::FnDecl,
         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<Loan<'tcx>>,
@@ -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>,