Skip to content

Commit 4fdb4be

Browse files
committed
Auto merge of rust-lang#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 rust-lang#43108. `let hindsight = 20/20;`)
2 parents 80be2f8 + 224c6ca commit 4fdb4be

File tree

6 files changed

+30
-32
lines changed

6 files changed

+30
-32
lines changed

src/librustc/dep_graph/dep_node.rs

+2
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,8 @@ define_dep_nodes!( <'tcx>
411411

412412
[] BorrowCheckKrate,
413413
[] BorrowCheck(DefId),
414+
[] MirBorrowCheck(DefId),
415+
414416
[] RvalueCheck(DefId),
415417
[] Reachability,
416418
[] MirKeys,

src/librustc/ty/maps.rs

+2
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,8 @@ define_maps! { <'tcx>
923923
[] coherent_trait: coherent_trait_dep_node((CrateNum, DefId)) -> (),
924924

925925
[] borrowck: BorrowCheck(DefId) -> (),
926+
// FIXME: shouldn't this return a `Result<(), BorrowckErrors>` instead?
927+
[] mir_borrowck: MirBorrowCheck(DefId) -> (),
926928

927929
/// Gets a complete map from all types to their inherent impls.
928930
/// Not meant to be used directly outside of coherence.

src/librustc_driver/driver.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -983,10 +983,6 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
983983

984984
// borrowck runs between MIR_VALIDATED and MIR_OPTIMIZED.
985985

986-
// FIXME: niko says this should be a query (see rustc::ty::maps)
987-
// instead of a pass.
988-
passes.push_pass(MIR_VALIDATED, mir::transform::borrow_check::BorrowckMir);
989-
990986
// These next passes must be executed together
991987
passes.push_pass(MIR_OPTIMIZED, mir::transform::no_landing_pads::NoLandingPads);
992988
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,
10751071
"borrow checking",
10761072
|| borrowck::check_crate(tcx));
10771073

1074+
time(time_passes,
1075+
"MIR borrow checking",
1076+
|| for def_id in tcx.body_owners() { tcx.mir_borrowck(def_id) });
1077+
10781078
// Avoid overwhelming user with errors if type checking failed.
10791079
// I'm not sure how helpful this is, to be honest, but it avoids
10801080
// a

src/librustc_mir/transform/borrow_check.rs renamed to src/librustc_mir/borrow_check.rs

+18-26
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
//! This pass borrow-checks the MIR to (further) ensure it is not broken.
11+
//! This query borrow-checks the MIR to (further) ensure it is not broken.
1212
13+
use rustc::hir::def_id::{DefId};
1314
use rustc::infer::{InferCtxt};
1415
use rustc::ty::{self, TyCtxt, ParamEnv};
16+
use rustc::ty::maps::Providers;
1517
use rustc::mir::{AssertMessage, BasicBlock, BorrowKind, Location, Lvalue};
1618
use rustc::mir::{Mir, Mutability, Operand, Projection, ProjectionElem, Rvalue};
1719
use rustc::mir::{Statement, StatementKind, Terminator, TerminatorKind};
18-
use rustc::mir::transform::{MirPass, MirSource};
20+
use rustc::mir::transform::{MirSource};
1921

2022
use rustc_data_structures::indexed_set::{self, IdxSetBuf};
2123
use rustc_data_structures::indexed_vec::{Idx};
@@ -34,35 +36,25 @@ use util::borrowck_errors::{BorrowckErrors, Origin};
3436
use self::MutateMode::{JustWrite, WriteAndRead};
3537
use self::ConsumeKind::{Consume};
3638

37-
pub struct BorrowckMir;
3839

39-
impl MirPass for BorrowckMir {
40-
fn run_pass<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, src: MirSource, mir: &mut Mir<'tcx>) {
41-
42-
// let err_count = tcx.sess.err_count();
43-
// if err_count > 0 {
44-
// // compiling a broken program can obviously result in a
45-
// // broken MIR, so try not to report duplicate errors.
46-
// debug!("skipping BorrowckMir: {} due to {} previous errors",
47-
// tcx.node_path_str(src.item_id()), err_count);
48-
// return;
49-
// }
40+
pub fn provide(providers: &mut Providers) {
41+
*providers = Providers {
42+
mir_borrowck,
43+
..*providers
44+
};
45+
}
5046

51-
debug!("run_pass BorrowckMir: {}", tcx.node_path_str(src.item_id()));
47+
fn mir_borrowck<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) {
48+
let mir = tcx.mir_validated(def_id);
49+
let src = MirSource::from_local_def_id(tcx, def_id);
50+
debug!("run query mir_borrowck: {}", tcx.node_path_str(src.item_id()));
5251

53-
let def_id = tcx.hir.local_def_id(src.item_id());
54-
if tcx.has_attr(def_id, "rustc_mir_borrowck") || tcx.sess.opts.debugging_opts.borrowck_mir {
55-
borrowck_mir(tcx, src, mir);
56-
}
52+
let mir: &Mir<'tcx> = &mir.borrow();
53+
if !tcx.has_attr(def_id, "rustc_mir_borrowck") || !tcx.sess.opts.debugging_opts.borrowck_mir {
54+
return;
5755
}
58-
}
5956

60-
fn borrowck_mir<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, src: MirSource, mir: &Mir<'tcx>)
61-
{
6257
let id = src.item_id();
63-
let def_id = tcx.hir.local_def_id(id);
64-
debug!("borrowck_mir({}) UNIMPLEMENTED", tcx.item_path_str(def_id));
65-
6658
let attributes = tcx.get_attrs(def_id);
6759
let param_env = tcx.param_env(def_id);
6860
tcx.infer_ctxt().enter(|_infcx| {
@@ -96,7 +88,7 @@ fn borrowck_mir<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, src: MirSource, mir: &Mir
9688
mbcx.analyze_results(&mut state); // entry point for DataflowResultsConsumer
9789
});
9890

99-
debug!("borrowck_mir done");
91+
debug!("mir_borrowck done");
10092
}
10193

10294
#[allow(dead_code)]

src/librustc_mir/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ extern crate core; // for NonZero
4545

4646
mod diagnostics;
4747

48+
mod borrow_check;
4849
mod build;
4950
mod dataflow;
5051
mod hair;
@@ -55,6 +56,7 @@ pub mod util;
5556
use rustc::ty::maps::Providers;
5657

5758
pub fn provide(providers: &mut Providers) {
59+
borrow_check::provide(providers);
5860
shim::provide(providers);
5961
transform::provide(providers);
6062
}

src/librustc_mir/transform/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ pub mod simplify;
3131
pub mod erase_regions;
3232
pub mod no_landing_pads;
3333
pub mod type_check;
34-
pub mod borrow_check;
3534
pub mod rustc_peek;
3635
pub mod elaborate_drops;
3736
pub mod add_call_guards;
@@ -123,8 +122,9 @@ fn mir_validated<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx
123122
}
124123

125124
fn optimized_mir<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx Mir<'tcx> {
126-
// Borrowck uses `mir_validated`, so we have to force it to
125+
// (Mir-)Borrowck uses `mir_validated`, so we have to force it to
127126
// execute before we can steal.
127+
ty::queries::mir_borrowck::force(tcx, DUMMY_SP, def_id);
128128
ty::queries::borrowck::force(tcx, DUMMY_SP, def_id);
129129

130130
let mut mir = tcx.mir_validated(def_id).steal();

0 commit comments

Comments
 (0)