Skip to content

Commit

Permalink
Rollup merge of rust-lang#66297 - vakaras:edit-queries, r=oli-obk
Browse files Browse the repository at this point in the history
Add a callback that allows compiler consumers to override queries.

This pull request adds an additional callback that allows compiler consumers such as Prusti and MIRAI to override queries. My hope is that in this way it will be possible to get access to the internal compiler information (e.g. borrow checker) without major changes to the compiler.

This pull request is work in progress because I am still testing if I can get the information which I need.

cc @nikomatsakis

r? @oli-obk
  • Loading branch information
JohnTitor committed Nov 13, 2019
2 parents 64e8cfa + f9f5a88 commit 57e0220
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/librustc_driver/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ pub fn run_compiler(
crate_name: None,
lint_caps: Default::default(),
register_lints: None,
override_queries: None,
};
callbacks.config(&mut config);
config
Expand Down Expand Up @@ -259,6 +260,7 @@ pub fn run_compiler(
crate_name: None,
lint_caps: Default::default(),
register_lints: None,
override_queries: None,
};

callbacks.config(&mut config);
Expand Down
11 changes: 11 additions & 0 deletions src/librustc_interface/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use rustc_data_structures::OnDrop;
use rustc_data_structures::sync::Lrc;
use rustc_data_structures::fx::{FxHashSet, FxHashMap};
use rustc_parse::new_parser_from_source_str;
use rustc::ty;
use std::path::PathBuf;
use std::result;
use std::sync::{Arc, Mutex};
Expand All @@ -38,6 +39,8 @@ pub struct Compiler {
pub(crate) queries: Queries,
pub(crate) crate_name: Option<String>,
pub(crate) register_lints: Option<Box<dyn Fn(&Session, &mut lint::LintStore) + Send + Sync>>,
pub(crate) override_queries:
Option<fn(&Session, &mut ty::query::Providers<'_>, &mut ty::query::Providers<'_>)>,
}

impl Compiler {
Expand Down Expand Up @@ -131,6 +134,13 @@ pub struct Config {
/// Note that if you find a Some here you probably want to call that function in the new
/// function being registered.
pub register_lints: Option<Box<dyn Fn(&Session, &mut lint::LintStore) + Send + Sync>>,

/// This is a callback from the driver that is called just after we have populated
/// the list of queries.
///
/// The second parameter is local providers and the third parameter is external providers.
pub override_queries:
Option<fn(&Session, &mut ty::query::Providers<'_>, &mut ty::query::Providers<'_>)>,
}

pub fn run_compiler_in_existing_thread_pool<F, R>(config: Config, f: F) -> R
Expand All @@ -157,6 +167,7 @@ where
queries: Default::default(),
crate_name: config.crate_name,
register_lints: config.register_lints,
override_queries: config.override_queries,
};

let _sess_abort_error = OnDrop(|| {
Expand Down
5 changes: 5 additions & 0 deletions src/librustc_interface/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,7 @@ pub fn create_global_ctxt(
let codegen_backend = compiler.codegen_backend().clone();
let crate_name = crate_name.to_string();
let defs = mem::take(&mut resolver_outputs.definitions);
let override_queries = compiler.override_queries;

let ((), result) = BoxedGlobalCtxt::new(static move || {
let sess = &*sess;
Expand All @@ -810,6 +811,10 @@ pub fn create_global_ctxt(
default_provide_extern(&mut extern_providers);
codegen_backend.provide_extern(&mut extern_providers);

if let Some(callback) = override_queries {
callback(sess, &mut local_providers, &mut extern_providers);
}

let gcx = TyCtxt::create_global_ctxt(
sess,
lint_store,
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt
crate_name,
lint_caps,
register_lints: None,
override_queries: None,
};

interface::run_compiler_in_existing_thread_pool(config, |compiler| {
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ pub fn run(options: Options) -> i32 {
crate_name: options.crate_name.clone(),
lint_caps: Default::default(),
register_lints: None,
override_queries: None,
};

let mut test_args = options.test_args.clone();
Expand Down
1 change: 1 addition & 0 deletions src/test/run-make-fulldeps/issue-19371/foo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ fn compile(code: String, output: PathBuf, sysroot: PathBuf) {
crate_name: None,
lint_caps: Default::default(),
register_lints: None,
override_queries: None,
};

interface::run_compiler(config, |compiler| {
Expand Down

0 comments on commit 57e0220

Please sign in to comment.