Rollup merge of #66297 - vakaras:edit-queries, r=oli-obk

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
This commit is contained in:
Yuki Okushi 2019-11-13 22:09:22 +09:00 committed by GitHub
commit c75a48a924
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 21 additions and 0 deletions

View File

@ -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
@ -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);

View File

@ -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};
@ -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 {
@ -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
@ -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(|| {

View File

@ -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;
@ -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,

View File

@ -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| {

View File

@ -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();

View File

@ -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| {