Skip to content

Commit 527dce7

Browse files
committed
Auto merge of #62679 - Xanewok:after-expansion, r=Zoxc
Add an `after_expansion` callback in the driver To format a given file RLS needs to know the Rust edition associated with it. It's not enough to look at the `edition` key in Cargo.toml - each crate target can have a different edition associated with it so the sure way to fetch a correct edition is to scan the input files used to compile a given crate target. Right now this was done in the `after_analysis` callback of our shim, however this leads to other problems - if a crate cannot be successfully compiled (e.g. it has a type error) then a callback would not be invoked meaning we can't populate the files -> edition mapping. However, doing this only after parsing is not enough, since expansion can pull in additional source files (e.g. by invoking `macro_rules! include_my_mod { () => { mod some_mod; }; }`). Without copy-pasting the entire driver setup it's also not possible to expand the crate ourselves in the `after_parsing` callback - to expand crate we'd need to register plugins and initialize session ourselves. However, this is done normally after executing the callback itself, thus triggering the `Once::set` assertions in `Session::init_features`. r? @Zoxc cc @RalfJung @oli-obk this affects public driver interface used by Miri and Clippy
2 parents f9477a7 + ff63336 commit 527dce7

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

src/librustc_driver/lib.rs

+20-8
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,20 @@ pub fn abort_on_err<T>(result: Result<T, ErrorReported>, sess: &Session) -> T {
105105
pub trait Callbacks {
106106
/// Called before creating the compiler instance
107107
fn config(&mut self, _config: &mut interface::Config) {}
108-
/// Called after parsing and returns true to continue execution
109-
fn after_parsing(&mut self, _compiler: &interface::Compiler) -> bool {
110-
true
108+
/// Called after parsing. Return value instructs the compiler whether to
109+
/// continue the compilation afterwards (defaults to `Compilation::Continue`)
110+
fn after_parsing(&mut self, _compiler: &interface::Compiler) -> Compilation {
111+
Compilation::Continue
112+
}
113+
/// Called after expansion. Return value instructs the compiler whether to
114+
/// continue the compilation afterwards (defaults to `Compilation::Continue`)
115+
fn after_expansion(&mut self, _compiler: &interface::Compiler) -> Compilation {
116+
Compilation::Continue
111117
}
112-
/// Called after analysis and returns true to continue execution
113-
fn after_analysis(&mut self, _compiler: &interface::Compiler) -> bool {
114-
true
118+
/// Called after analysis. Return value instructs the compiler whether to
119+
/// continue the compilation afterwards (defaults to `Compilation::Continue`)
120+
fn after_analysis(&mut self, _compiler: &interface::Compiler) -> Compilation {
121+
Compilation::Continue
115122
}
116123
}
117124

@@ -294,7 +301,7 @@ pub fn run_compiler(
294301
}
295302
}
296303

297-
if !callbacks.after_parsing(compiler) {
304+
if callbacks.after_parsing(compiler) == Compilation::Stop {
298305
return sess.compile_status();
299306
}
300307

@@ -312,6 +319,11 @@ pub fn run_compiler(
312319
return sess.compile_status();
313320
}
314321

322+
compiler.expansion()?;
323+
if callbacks.after_expansion(compiler) == Compilation::Stop {
324+
return sess.compile_status();
325+
}
326+
315327
compiler.prepare_outputs()?;
316328

317329
if sess.opts.output_types.contains_key(&OutputType::DepInfo)
@@ -355,7 +367,7 @@ pub fn run_compiler(
355367

356368
compiler.global_ctxt()?.peek_mut().enter(|tcx| tcx.analysis(LOCAL_CRATE))?;
357369

358-
if !callbacks.after_analysis(compiler) {
370+
if callbacks.after_analysis(compiler) == Compilation::Stop {
359371
return sess.compile_status();
360372
}
361373

0 commit comments

Comments
 (0)