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;`)
This commit is contained in:
bors 2017-08-21 20:16:21 +00:00
commit 4fdb4bedfd
6 changed files with 32 additions and 34 deletions

View file

@ -411,6 +411,8 @@ define_dep_nodes!( <'tcx>
[] BorrowCheckKrate, [] BorrowCheckKrate,
[] BorrowCheck(DefId), [] BorrowCheck(DefId),
[] MirBorrowCheck(DefId),
[] RvalueCheck(DefId), [] RvalueCheck(DefId),
[] Reachability, [] Reachability,
[] MirKeys, [] MirKeys,

View file

@ -923,6 +923,8 @@ define_maps! { <'tcx>
[] coherent_trait: coherent_trait_dep_node((CrateNum, DefId)) -> (), [] coherent_trait: coherent_trait_dep_node((CrateNum, DefId)) -> (),
[] borrowck: BorrowCheck(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. /// Gets a complete map from all types to their inherent impls.
/// Not meant to be used directly outside of coherence. /// Not meant to be used directly outside of coherence.

View file

@ -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. // 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 // 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::no_landing_pads::NoLandingPads);
passes.push_pass(MIR_OPTIMIZED, mir::transform::add_call_guards::CriticalCallEdges); 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", "borrow checking",
|| borrowck::check_crate(tcx)); || 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. // Avoid overwhelming user with errors if type checking failed.
// I'm not sure how helpful this is, to be honest, but it avoids // I'm not sure how helpful this is, to be honest, but it avoids
// a // a

View file

@ -8,14 +8,16 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // 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::infer::{InferCtxt};
use rustc::ty::{self, TyCtxt, ParamEnv}; use rustc::ty::{self, TyCtxt, ParamEnv};
use rustc::ty::maps::Providers;
use rustc::mir::{AssertMessage, BasicBlock, BorrowKind, Location, Lvalue}; use rustc::mir::{AssertMessage, BasicBlock, BorrowKind, Location, Lvalue};
use rustc::mir::{Mir, Mutability, Operand, Projection, ProjectionElem, Rvalue}; use rustc::mir::{Mir, Mutability, Operand, Projection, ProjectionElem, Rvalue};
use rustc::mir::{Statement, StatementKind, Terminator, TerminatorKind}; 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_set::{self, IdxSetBuf};
use rustc_data_structures::indexed_vec::{Idx}; use rustc_data_structures::indexed_vec::{Idx};
@ -34,35 +36,25 @@ use util::borrowck_errors::{BorrowckErrors, Origin};
use self::MutateMode::{JustWrite, WriteAndRead}; use self::MutateMode::{JustWrite, WriteAndRead};
use self::ConsumeKind::{Consume}; use self::ConsumeKind::{Consume};
pub struct BorrowckMir;
impl MirPass for BorrowckMir { pub fn provide(providers: &mut Providers) {
fn run_pass<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, src: MirSource, mir: &mut Mir<'tcx>) { *providers = Providers {
mir_borrowck,
// let err_count = tcx.sess.err_count(); ..*providers
// 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;
// }
debug!("run_pass BorrowckMir: {}", 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);
}
}
} }
fn borrowck_mir<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, src: MirSource, mir: &Mir<'tcx>) fn mir_borrowck<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) {
{ let mir = tcx.mir_validated(def_id);
let id = src.item_id(); let src = MirSource::from_local_def_id(tcx, def_id);
let def_id = tcx.hir.local_def_id(id); debug!("run query mir_borrowck: {}", tcx.node_path_str(src.item_id()));
debug!("borrowck_mir({}) UNIMPLEMENTED", tcx.item_path_str(def_id));
let mir: &Mir<'tcx> = &mir.borrow();
if !tcx.has_attr(def_id, "rustc_mir_borrowck") || !tcx.sess.opts.debugging_opts.borrowck_mir {
return;
}
let id = src.item_id();
let attributes = tcx.get_attrs(def_id); let attributes = tcx.get_attrs(def_id);
let param_env = tcx.param_env(def_id); let param_env = tcx.param_env(def_id);
tcx.infer_ctxt().enter(|_infcx| { 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 mbcx.analyze_results(&mut state); // entry point for DataflowResultsConsumer
}); });
debug!("borrowck_mir done"); debug!("mir_borrowck done");
} }
#[allow(dead_code)] #[allow(dead_code)]

View file

@ -45,6 +45,7 @@ extern crate core; // for NonZero
mod diagnostics; mod diagnostics;
mod borrow_check;
mod build; mod build;
mod dataflow; mod dataflow;
mod hair; mod hair;
@ -55,6 +56,7 @@ pub mod util;
use rustc::ty::maps::Providers; use rustc::ty::maps::Providers;
pub fn provide(providers: &mut Providers) { pub fn provide(providers: &mut Providers) {
borrow_check::provide(providers);
shim::provide(providers); shim::provide(providers);
transform::provide(providers); transform::provide(providers);
} }

View file

@ -31,7 +31,6 @@ pub mod simplify;
pub mod erase_regions; pub mod erase_regions;
pub mod no_landing_pads; pub mod no_landing_pads;
pub mod type_check; pub mod type_check;
pub mod borrow_check;
pub mod rustc_peek; pub mod rustc_peek;
pub mod elaborate_drops; pub mod elaborate_drops;
pub mod add_call_guards; 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> { 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. // execute before we can steal.
ty::queries::mir_borrowck::force(tcx, DUMMY_SP, def_id);
ty::queries::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(); let mut mir = tcx.mir_validated(def_id).steal();