Skip to content

Commit

Permalink
rustc_driver: expose a way to override query providers in CompileCont…
Browse files Browse the repository at this point in the history
…roller.
  • Loading branch information
eddyb committed Nov 12, 2017
1 parent 18b3a9a commit 0ac6c3a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 24 deletions.
57 changes: 39 additions & 18 deletions src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ pub fn compile_input(sess: &Session,
None
};

phase_3_run_analysis_passes(sess,
phase_3_run_analysis_passes(control,
sess,
cstore,
hir_map,
analysis,
Expand Down Expand Up @@ -348,6 +349,13 @@ pub struct CompileController<'a> {
pub keep_ast: bool,
// -Zcontinue-parse-after-error
pub continue_parse_after_error: bool,

/// Allows overriding default rustc query providers,
/// after `default_provide` has installed them.
pub provide: Box<Fn(&mut ty::maps::Providers) + 'a>,
/// Same as `provide`, but only for non-local crates,
/// applied after `default_provide_extern`.
pub provide_extern: Box<Fn(&mut ty::maps::Providers) + 'a>,
}

impl<'a> CompileController<'a> {
Expand All @@ -362,6 +370,8 @@ impl<'a> CompileController<'a> {
make_glob_map: MakeGlobMap::No,
keep_ast: false,
continue_parse_after_error: false,
provide: box |_| {},
provide_extern: box |_| {},
}
}
}
Expand Down Expand Up @@ -907,10 +917,33 @@ pub fn phase_2_configure_and_expand<F>(sess: &Session,
})
}

pub fn default_provide(providers: &mut ty::maps::Providers) {
borrowck::provide(providers);
mir::provide(providers);
reachable::provide(providers);
rustc_privacy::provide(providers);
DefaultTransCrate::provide(providers);
typeck::provide(providers);
ty::provide(providers);
traits::provide(providers);
reachable::provide(providers);
rustc_const_eval::provide(providers);
rustc_passes::provide(providers);
middle::region::provide(providers);
cstore::provide(providers);
lint::provide(providers);
}

pub fn default_provide_extern(providers: &mut ty::maps::Providers) {
cstore::provide_extern(providers);
DefaultTransCrate::provide_extern(providers);
}

/// Run the resolution, typechecking, region checking and other
/// miscellaneous analysis passes on the crate. Return various
/// structures carrying the results of the analysis.
pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
pub fn phase_3_run_analysis_passes<'tcx, F, R>(control: &CompileController,
sess: &'tcx Session,
cstore: &'tcx CrateStore,
hir_map: hir_map::Map<'tcx>,
mut analysis: ty::CrateAnalysis,
Expand Down Expand Up @@ -966,24 +999,12 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
|| static_recursion::check_crate(sess, &hir_map))?;

let mut local_providers = ty::maps::Providers::default();
borrowck::provide(&mut local_providers);
mir::provide(&mut local_providers);
reachable::provide(&mut local_providers);
rustc_privacy::provide(&mut local_providers);
DefaultTransCrate::provide(&mut local_providers);
typeck::provide(&mut local_providers);
ty::provide(&mut local_providers);
traits::provide(&mut local_providers);
reachable::provide(&mut local_providers);
rustc_const_eval::provide(&mut local_providers);
rustc_passes::provide(&mut local_providers);
middle::region::provide(&mut local_providers);
cstore::provide(&mut local_providers);
lint::provide(&mut local_providers);
default_provide(&mut local_providers);
(control.provide)(&mut local_providers);

let mut extern_providers = local_providers;
cstore::provide_extern(&mut extern_providers);
DefaultTransCrate::provide_extern(&mut extern_providers);
default_provide_extern(&mut extern_providers);
(control.provide_extern)(&mut extern_providers);

// Setup the MIR passes that we want to run.
let mut passes = Passes::new();
Expand Down
8 changes: 6 additions & 2 deletions src/librustc_driver/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,9 @@ impl PpSourceMode {
f(&annotation, hir_map.forest.krate())
}
PpmTyped => {
abort_on_err(driver::phase_3_run_analysis_passes(sess,
let control = &driver::CompileController::basic();
abort_on_err(driver::phase_3_run_analysis_passes(control,
sess,
cstore,
hir_map.clone(),
analysis.clone(),
Expand Down Expand Up @@ -1036,7 +1038,9 @@ fn print_with_analysis<'tcx, 'a: 'tcx>(sess: &'a Session,

let mut out = Vec::new();

abort_on_err(driver::phase_3_run_analysis_passes(sess,
let control = &driver::CompileController::basic();
abort_on_err(driver::phase_3_run_analysis_passes(control,
sess,
cstore,
hir_map.clone(),
analysis.clone(),
Expand Down
9 changes: 5 additions & 4 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ pub fn run_core(search_paths: SearchPaths,
target_features::add_configuration(&mut cfg, &sess);
sess.parse_sess.config = cfg;

let krate = panictry!(driver::phase_1_parse_input(&driver::CompileController::basic(),
&sess,
&input));
let control = &driver::CompileController::basic();

let krate = panictry!(driver::phase_1_parse_input(control, &sess, &input));
let krate = ReplaceBodyWithLoop::new().fold_crate(krate);

let name = link::find_crate_name(Some(&sess), &krate.attrs, &input);
Expand All @@ -182,7 +182,8 @@ pub fn run_core(search_paths: SearchPaths,
&[],
&sess);

abort_on_err(driver::phase_3_run_analysis_passes(&sess,
abort_on_err(driver::phase_3_run_analysis_passes(control,
&sess,
&*cstore,
hir_map,
analysis,
Expand Down

0 comments on commit 0ac6c3a

Please sign in to comment.