about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2017-08-21 20:16:21 +0000
committerbors <bors@rust-lang.org>2017-08-21 20:16:21 +0000
commit4fdb4bedfd2395e1e1ebb923b10165b850e6064c (patch)
treeebd3119697c9434962c16f54ba5c12a1afe71a77
parent80be2f8697e869e75b0b341b0c1eecef694ee5b9 (diff)
parent224c6ca2ee31d717a412d6c1901fac04ab780edb (diff)
downloadrust-4fdb4bedfd2395e1e1ebb923b10165b850e6064c.tar.gz
rust-4fdb4bedfd2395e1e1ebb923b10165b850e6064c.zip
Auto merge of #44009 - pnkfelix:mir-borrowck-as-query, r=arielb1
Mir borrowck as query

Turn the `mir-borrowck` pass (aka "transform") into a query.

(If I had realized how relatively easy this was going to be, I would have made it part of #43108. `let hindsight = 20/20;`)
-rw-r--r--src/librustc/dep_graph/dep_node.rs2
-rw-r--r--src/librustc/ty/maps.rs2
-rw-r--r--src/librustc_driver/driver.rs8
-rw-r--r--src/librustc_mir/borrow_check.rs (renamed from src/librustc_mir/transform/borrow_check.rs)44
-rw-r--r--src/librustc_mir/lib.rs2
-rw-r--r--src/librustc_mir/transform/mod.rs4
6 files changed, 30 insertions, 32 deletions
diff --git a/src/librustc/dep_graph/dep_node.rs b/src/librustc/dep_graph/dep_node.rs
index 5b609f192e1..9e5d4081231 100644
--- a/src/librustc/dep_graph/dep_node.rs
+++ b/src/librustc/dep_graph/dep_node.rs
@@ -411,6 +411,8 @@ define_dep_nodes!( <'tcx>
 
     [] BorrowCheckKrate,
     [] BorrowCheck(DefId),
+    [] MirBorrowCheck(DefId),
+
     [] RvalueCheck(DefId),
     [] Reachability,
     [] MirKeys,
diff --git a/src/librustc/ty/maps.rs b/src/librustc/ty/maps.rs
index a640da31eec..26b51630d93 100644
--- a/src/librustc/ty/maps.rs
+++ b/src/librustc/ty/maps.rs
@@ -923,6 +923,8 @@ define_maps! { <'tcx>
     [] coherent_trait: coherent_trait_dep_node((CrateNum, DefId)) -> (),
 
     [] borrowck: BorrowCheck(DefId) -> (),
+    // FIXME: shouldn't this return a `Result<(), BorrowckErrors>` instead?
+    [] mir_borrowck: MirBorrowCheck(DefId) -> (),
 
     /// Gets a complete map from all types to their inherent impls.
     /// Not meant to be used directly outside of coherence.
diff --git a/src/librustc_driver/driver.rs b/src/librustc_driver/driver.rs
index c98f9c3d466..246fc7fc524 100644
--- a/src/librustc_driver/driver.rs
+++ b/src/librustc_driver/driver.rs
@@ -983,10 +983,6 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
 
     // borrowck runs between MIR_VALIDATED and MIR_OPTIMIZED.
 
-    // FIXME: niko says this should be a query (see rustc::ty::maps)
-    // instead of a pass.
-    passes.push_pass(MIR_VALIDATED, mir::transform::borrow_check::BorrowckMir);
-
     // These next passes must be executed together
     passes.push_pass(MIR_OPTIMIZED, mir::transform::no_landing_pads::NoLandingPads);
     passes.push_pass(MIR_OPTIMIZED, mir::transform::add_call_guards::CriticalCallEdges);
@@ -1075,6 +1071,10 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
              "borrow checking",
              || borrowck::check_crate(tcx));
 
+        time(time_passes,
+             "MIR borrow checking",
+             || for def_id in tcx.body_owners() { tcx.mir_borrowck(def_id) });
+
         // Avoid overwhelming user with errors if type checking failed.
         // I'm not sure how helpful this is, to be honest, but it avoids
         // a
diff --git a/src/librustc_mir/transform/borrow_check.rs b/src/librustc_mir/borrow_check.rs
index 46e65d355a1..5ae3bee3267 100644
--- a/src/librustc_mir/transform/borrow_check.rs
+++ b/src/librustc_mir/borrow_check.rs
@@ -8,14 +8,16 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-//! This pass borrow-checks the MIR to (further) ensure it is not broken.
+//! This query borrow-checks the MIR to (further) ensure it is not broken.
 
+use rustc::hir::def_id::{DefId};
 use rustc::infer::{InferCtxt};
 use rustc::ty::{self, TyCtxt, ParamEnv};
+use rustc::ty::maps::Providers;
 use rustc::mir::{AssertMessage, BasicBlock, BorrowKind, Location, Lvalue};
 use rustc::mir::{Mir, Mutability, Operand, Projection, ProjectionElem, Rvalue};
 use rustc::mir::{Statement, StatementKind, Terminator, TerminatorKind};
-use rustc::mir::transform::{MirPass, MirSource};
+use rustc::mir::transform::{MirSource};
 
 use rustc_data_structures::indexed_set::{self, IdxSetBuf};
 use rustc_data_structures::indexed_vec::{Idx};
@@ -34,35 +36,25 @@ use util::borrowck_errors::{BorrowckErrors, Origin};
 use self::MutateMode::{JustWrite, WriteAndRead};
 use self::ConsumeKind::{Consume};
 
-pub struct BorrowckMir;
 
-impl MirPass for BorrowckMir {
-    fn run_pass<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, src: MirSource, mir: &mut Mir<'tcx>) {
-
-        // let err_count = tcx.sess.err_count();
-        // if err_count > 0 {
-        //     // compiling a broken program can obviously result in a
-        //     // broken MIR, so try not to report duplicate errors.
-        //     debug!("skipping BorrowckMir: {} due to {} previous errors",
-        //            tcx.node_path_str(src.item_id()), err_count);
-        //     return;
-        // }
+pub fn provide(providers: &mut Providers) {
+    *providers = Providers {
+        mir_borrowck,
+        ..*providers
+    };
+}
 
-        debug!("run_pass BorrowckMir: {}", tcx.node_path_str(src.item_id()));
+fn mir_borrowck<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) {
+    let mir = tcx.mir_validated(def_id);
+    let src = MirSource::from_local_def_id(tcx, def_id);
+    debug!("run query mir_borrowck: {}", tcx.node_path_str(src.item_id()));
 
-        let def_id = tcx.hir.local_def_id(src.item_id());
-        if tcx.has_attr(def_id, "rustc_mir_borrowck") || tcx.sess.opts.debugging_opts.borrowck_mir {
-            borrowck_mir(tcx, src, mir);
-        }
+    let mir: &Mir<'tcx> = &mir.borrow();
+    if !tcx.has_attr(def_id, "rustc_mir_borrowck") || !tcx.sess.opts.debugging_opts.borrowck_mir {
+        return;
     }
-}
 
-fn borrowck_mir<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, src: MirSource, mir: &Mir<'tcx>)
-{
     let id = src.item_id();
-    let def_id = tcx.hir.local_def_id(id);
-    debug!("borrowck_mir({}) UNIMPLEMENTED", tcx.item_path_str(def_id));
-
     let attributes = tcx.get_attrs(def_id);
     let param_env = tcx.param_env(def_id);
     tcx.infer_ctxt().enter(|_infcx| {
@@ -96,7 +88,7 @@ fn borrowck_mir<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, src: MirSource, mir: &Mir
         mbcx.analyze_results(&mut state); // entry point for DataflowResultsConsumer
     });
 
-    debug!("borrowck_mir done");
+    debug!("mir_borrowck done");
 }
 
 #[allow(dead_code)]
diff --git a/src/librustc_mir/lib.rs b/src/librustc_mir/lib.rs
index 6b1fe0d2ca9..7aa46799924 100644
--- a/src/librustc_mir/lib.rs
+++ b/src/librustc_mir/lib.rs
@@ -45,6 +45,7 @@ extern crate core; // for NonZero
 
 mod diagnostics;
 
+mod borrow_check;
 mod build;
 mod dataflow;
 mod hair;
@@ -55,6 +56,7 @@ pub mod util;
 use rustc::ty::maps::Providers;
 
 pub fn provide(providers: &mut Providers) {
+    borrow_check::provide(providers);
     shim::provide(providers);
     transform::provide(providers);
 }
diff --git a/src/librustc_mir/transform/mod.rs b/src/librustc_mir/transform/mod.rs
index 25a156ea3fd..d8dffa03662 100644
--- a/src/librustc_mir/transform/mod.rs
+++ b/src/librustc_mir/transform/mod.rs
@@ -31,7 +31,6 @@ pub mod simplify;
 pub mod erase_regions;
 pub mod no_landing_pads;
 pub mod type_check;
-pub mod borrow_check;
 pub mod rustc_peek;
 pub mod elaborate_drops;
 pub mod add_call_guards;
@@ -123,8 +122,9 @@ fn mir_validated<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx
 }
 
 fn optimized_mir<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx Mir<'tcx> {
-    // Borrowck uses `mir_validated`, so we have to force it to
+    // (Mir-)Borrowck uses `mir_validated`, so we have to force it to
     // execute before we can steal.
+    ty::queries::mir_borrowck::force(tcx, DUMMY_SP, def_id);
     ty::queries::borrowck::force(tcx, DUMMY_SP, def_id);
 
     let mut mir = tcx.mir_validated(def_id).steal();