diff --git a/src/librustc/dep_graph/dep_node.rs b/src/librustc/dep_graph/dep_node.rs index 5b609f192e1c2..9e5d40812311d 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 a640da31eec25..26b51630d93e0 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 c98f9c3d4660a..246fc7fc5241b 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 similarity index 97% rename from src/librustc_mir/transform/borrow_check.rs rename to src/librustc_mir/borrow_check.rs index 46e65d355a15b..5ae3bee326700 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 6b1fe0d2ca9f6..7aa4679992471 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 25a156ea3fd97..d8dffa036621a 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();