Skip to content

Commit

Permalink
Rollup merge of #83144 - hyd-dev:parse-sess-created, r=oli-obk
Browse files Browse the repository at this point in the history
Introduce `rustc_interface::interface::Config::parse_sess_created` callback

Resolves #82900.

cc `@oli-obk`
  • Loading branch information
Dylan-DPC authored Mar 15, 2021
2 parents 13eac5b + 0bbfd54 commit 2816c11
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 13 deletions.
2 changes: 2 additions & 0 deletions compiler/rustc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ fn run_compiler(
diagnostic_output,
stderr: None,
lint_caps: Default::default(),
parse_sess_created: None,
register_lints: None,
override_queries: None,
make_codegen_backend: make_codegen_backend.take().unwrap(),
Expand Down Expand Up @@ -298,6 +299,7 @@ fn run_compiler(
diagnostic_output,
stderr: None,
lint_caps: Default::default(),
parse_sess_created: None,
register_lints: None,
override_queries: None,
make_codegen_backend: make_codegen_backend.unwrap(),
Expand Down
13 changes: 12 additions & 1 deletion compiler/rustc_interface/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ pub struct Config {

pub lint_caps: FxHashMap<lint::LintId, lint::Level>,

/// This is a callback from the driver that is called when [`ParseSess`] is created.
pub parse_sess_created: Option<Box<dyn FnOnce(&mut ParseSess) + Send>>,

/// This is a callback from the driver that is called when we're registering lints;
/// it is called during plugin registration when we have the LintStore in a non-shared state.
///
Expand All @@ -166,7 +169,7 @@ pub struct Config {

pub fn create_compiler_and_run<R>(config: Config, f: impl FnOnce(&Compiler) -> R) -> R {
let registry = &config.registry;
let (sess, codegen_backend) = util::create_session(
let (mut sess, codegen_backend) = util::create_session(
config.opts,
config.crate_cfg,
config.diagnostic_output,
Expand All @@ -177,6 +180,14 @@ pub fn create_compiler_and_run<R>(config: Config, f: impl FnOnce(&Compiler) -> R
registry.clone(),
);

if let Some(parse_sess_created) = config.parse_sess_created {
parse_sess_created(
&mut Lrc::get_mut(&mut sess)
.expect("create_session() should never share the returned session")
.parse_sess,
);
}

let compiler = Compiler {
sess,
codegen_backend,
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ crate fn create_config(
diagnostic_output: DiagnosticOutput::Default,
stderr: None,
lint_caps,
parse_sess_created: None,
register_lints: Some(box crate::lint::register_lints),
override_queries: Some(|_sess, providers, _external_providers| {
// Most lints will require typechecking, so just don't run them.
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/doctest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ crate fn run(options: Options) -> Result<(), ErrorReported> {
diagnostic_output: DiagnosticOutput::Default,
stderr: None,
lint_caps,
parse_sess_created: None,
register_lints: Some(box crate::lint::register_lints),
override_queries: None,
make_codegen_backend: None,
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 @@ -57,6 +57,7 @@ fn compile(code: String, output: PathBuf, sysroot: PathBuf) {
diagnostic_output: DiagnosticOutput::Default,
stderr: None,
lint_caps: Default::default(),
parse_sess_created: None,
register_lints: None,
override_queries: None,
make_codegen_backend: None,
Expand Down
20 changes: 8 additions & 12 deletions src/tools/clippy/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extern crate rustc_session;
extern crate rustc_span;

use rustc_interface::interface;
use rustc_session::Session;
use rustc_session::parse::ParseSess;
use rustc_span::symbol::Symbol;
use rustc_tools_util::VersionInfo;

Expand Down Expand Up @@ -63,8 +63,8 @@ fn test_arg_value() {
assert_eq!(arg_value(args, "--foo", |_| true), None);
}

fn track_clippy_args(sess: &Session, args_env_var: &Option<String>) {
sess.parse_sess.env_depinfo.borrow_mut().insert((
fn track_clippy_args(parse_sess: &mut ParseSess, args_env_var: &Option<String>) {
parse_sess.env_depinfo.get_mut().insert((
Symbol::intern("CLIPPY_ARGS"),
args_env_var.as_deref().map(Symbol::intern),
));
Expand All @@ -81,14 +81,9 @@ struct RustcCallbacks {

impl rustc_driver::Callbacks for RustcCallbacks {
fn config(&mut self, config: &mut interface::Config) {
let previous = config.register_lints.take();
let clippy_args_var = self.clippy_args_var.take();
config.register_lints = Some(Box::new(move |sess, lint_store| {
if let Some(ref previous) = previous {
(previous)(sess, lint_store);
}

track_clippy_args(sess, &clippy_args_var);
config.parse_sess_created = Some(Box::new(move |parse_sess| {
track_clippy_args(parse_sess, &clippy_args_var);
}));
}
}
Expand All @@ -101,15 +96,16 @@ impl rustc_driver::Callbacks for ClippyCallbacks {
fn config(&mut self, config: &mut interface::Config) {
let previous = config.register_lints.take();
let clippy_args_var = self.clippy_args_var.take();
config.parse_sess_created = Some(Box::new(move |parse_sess| {
track_clippy_args(parse_sess, &clippy_args_var);
}));
config.register_lints = Some(Box::new(move |sess, mut lint_store| {
// technically we're ~guaranteed that this is none but might as well call anything that
// is there already. Certainly it can't hurt.
if let Some(previous) = &previous {
(previous)(sess, lint_store);
}

track_clippy_args(sess, &clippy_args_var);

let conf = clippy_lints::read_conf(&[], &sess);
clippy_lints::register_plugins(&mut lint_store, &sess, &conf);
clippy_lints::register_pre_expansion_lints(&mut lint_store);
Expand Down

0 comments on commit 2816c11

Please sign in to comment.