Skip to content

Commit

Permalink
Auto merge of #44009 - pnkfelix:mir-borrowck-as-query, r=arielb1
Browse files Browse the repository at this point in the history
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;`)
  • Loading branch information
bors committed Aug 21, 2017
2 parents 80be2f8 + 224c6ca commit 4fdb4be
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 32 deletions.
2 changes: 2 additions & 0 deletions src/librustc/dep_graph/dep_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,8 @@ define_dep_nodes!( <'tcx>

[] BorrowCheckKrate,
[] BorrowCheck(DefId),
[] MirBorrowCheck(DefId),

[] RvalueCheck(DefId),
[] Reachability,
[] MirKeys,
Expand Down
2 changes: 2 additions & 0 deletions src/librustc/ty/maps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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| {
Expand Down Expand Up @@ -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)]
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_mir/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ extern crate core; // for NonZero

mod diagnostics;

mod borrow_check;
mod build;
mod dataflow;
mod hair;
Expand All @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/transform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 4fdb4be

Please sign in to comment.