Skip to content

Commit 7d31ae7

Browse files
authored
Rollup merge of #135880 - bjorn3:misc_driver_refactors, r=oli-obk
Get rid of RunCompiler The various `set_*` methods that have been removed can be replaced by setting the respective fields in the `Callbacks::config` implementation. `set_using_internal_features` was often forgotten and it's equivalent is now done automatically.
2 parents dafc861 + 241f824 commit 7d31ae7

File tree

16 files changed

+68
-174
lines changed

16 files changed

+68
-174
lines changed

compiler/rustc_driver_impl/src/lib.rs

+11-100
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ use std::io::{self, IsTerminal, Read, Write};
2828
use std::panic::{self, PanicHookInfo, catch_unwind};
2929
use std::path::{Path, PathBuf};
3030
use std::process::{self, Command, Stdio};
31+
use std::sync::OnceLock;
3132
use std::sync::atomic::{AtomicBool, Ordering};
32-
use std::sync::{Arc, OnceLock};
3333
use std::time::{Instant, SystemTime};
3434
use std::{env, str};
3535

@@ -60,7 +60,6 @@ use rustc_session::lint::{Lint, LintId};
6060
use rustc_session::output::collect_crate_types;
6161
use rustc_session::{EarlyDiagCtxt, Session, config, filesearch};
6262
use rustc_span::FileName;
63-
use rustc_span::source_map::FileLoader;
6463
use rustc_target::json::ToJson;
6564
use rustc_target::spec::{Target, TargetTuple};
6665
use time::OffsetDateTime;
@@ -208,84 +207,7 @@ pub fn diagnostics_registry() -> Registry {
208207
}
209208

210209
/// This is the primary entry point for rustc.
211-
pub struct RunCompiler<'a> {
212-
at_args: &'a [String],
213-
callbacks: &'a mut (dyn Callbacks + Send),
214-
file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
215-
make_codegen_backend:
216-
Option<Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>>,
217-
using_internal_features: Arc<std::sync::atomic::AtomicBool>,
218-
}
219-
220-
impl<'a> RunCompiler<'a> {
221-
pub fn new(at_args: &'a [String], callbacks: &'a mut (dyn Callbacks + Send)) -> Self {
222-
Self {
223-
at_args,
224-
callbacks,
225-
file_loader: None,
226-
make_codegen_backend: None,
227-
using_internal_features: Arc::default(),
228-
}
229-
}
230-
231-
/// Set a custom codegen backend.
232-
///
233-
/// Has no uses within this repository, but is used by bjorn3 for "the
234-
/// hotswapping branch of cg_clif" for "setting the codegen backend from a
235-
/// custom driver where the custom codegen backend has arbitrary data."
236-
/// (See #102759.)
237-
pub fn set_make_codegen_backend(
238-
&mut self,
239-
make_codegen_backend: Option<
240-
Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>,
241-
>,
242-
) -> &mut Self {
243-
self.make_codegen_backend = make_codegen_backend;
244-
self
245-
}
246-
247-
/// Load files from sources other than the file system.
248-
///
249-
/// Has no uses within this repository, but may be used in the future by
250-
/// bjorn3 for "hooking rust-analyzer's VFS into rustc at some point for
251-
/// running rustc without having to save". (See #102759.)
252-
pub fn set_file_loader(
253-
&mut self,
254-
file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
255-
) -> &mut Self {
256-
self.file_loader = file_loader;
257-
self
258-
}
259-
260-
/// Set the session-global flag that checks whether internal features have been used,
261-
/// suppressing the message about submitting an issue in ICEs when enabled.
262-
#[must_use]
263-
pub fn set_using_internal_features(mut self, using_internal_features: Arc<AtomicBool>) -> Self {
264-
self.using_internal_features = using_internal_features;
265-
self
266-
}
267-
268-
/// Parse args and run the compiler.
269-
pub fn run(self) {
270-
run_compiler(
271-
self.at_args,
272-
self.callbacks,
273-
self.file_loader,
274-
self.make_codegen_backend,
275-
self.using_internal_features,
276-
);
277-
}
278-
}
279-
280-
fn run_compiler(
281-
at_args: &[String],
282-
callbacks: &mut (dyn Callbacks + Send),
283-
file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
284-
make_codegen_backend: Option<
285-
Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>,
286-
>,
287-
using_internal_features: Arc<std::sync::atomic::AtomicBool>,
288-
) {
210+
pub fn run_compiler(at_args: &[String], callbacks: &mut (dyn Callbacks + Send)) {
289211
let mut default_early_dcx = EarlyDiagCtxt::new(ErrorOutputType::default());
290212

291213
// Throw away the first argument, the name of the binary.
@@ -322,16 +244,16 @@ fn run_compiler(
322244
output_file: ofile,
323245
output_dir: odir,
324246
ice_file,
325-
file_loader,
247+
file_loader: None,
326248
locale_resources: DEFAULT_LOCALE_RESOURCES.to_vec(),
327249
lint_caps: Default::default(),
328250
psess_created: None,
329251
hash_untracked_state: None,
330252
register_lints: None,
331253
override_queries: None,
332-
make_codegen_backend,
254+
make_codegen_backend: None,
333255
registry: diagnostics_registry(),
334-
using_internal_features,
256+
using_internal_features: &USING_INTERNAL_FEATURES,
335257
expanded_args: args,
336258
};
337259

@@ -1350,6 +1272,8 @@ fn ice_path_with_config(config: Option<&UnstableOptions>) -> &'static Option<Pat
13501272
})
13511273
}
13521274

1275+
pub static USING_INTERNAL_FEATURES: AtomicBool = AtomicBool::new(false);
1276+
13531277
/// Installs a panic hook that will print the ICE message on unexpected panics.
13541278
///
13551279
/// The hook is intended to be useable even by external tools. You can pass a custom
@@ -1360,15 +1284,8 @@ fn ice_path_with_config(config: Option<&UnstableOptions>) -> &'static Option<Pat
13601284
/// If you have no extra info to report, pass the empty closure `|_| ()` as the argument to
13611285
/// extra_info.
13621286
///
1363-
/// Returns a flag that can be set to disable the note for submitting a bug. This can be passed to
1364-
/// [`RunCompiler::set_using_internal_features`] to let macro expansion set it when encountering
1365-
/// internal features.
1366-
///
13671287
/// A custom rustc driver can skip calling this to set up a custom ICE hook.
1368-
pub fn install_ice_hook(
1369-
bug_report_url: &'static str,
1370-
extra_info: fn(&DiagCtxt),
1371-
) -> Arc<AtomicBool> {
1288+
pub fn install_ice_hook(bug_report_url: &'static str, extra_info: fn(&DiagCtxt)) {
13721289
// If the user has not explicitly overridden "RUST_BACKTRACE", then produce
13731290
// full backtraces. When a compiler ICE happens, we want to gather
13741291
// as much information as possible to present in the issue opened
@@ -1385,8 +1302,6 @@ pub fn install_ice_hook(
13851302
}
13861303
}
13871304

1388-
let using_internal_features = Arc::new(std::sync::atomic::AtomicBool::default());
1389-
let using_internal_features_hook = Arc::clone(&using_internal_features);
13901305
panic::update_hook(Box::new(
13911306
move |default_hook: &(dyn Fn(&PanicHookInfo<'_>) + Send + Sync + 'static),
13921307
info: &PanicHookInfo<'_>| {
@@ -1438,11 +1353,9 @@ pub fn install_ice_hook(
14381353
}
14391354

14401355
// Print the ICE message
1441-
report_ice(info, bug_report_url, extra_info, &using_internal_features_hook);
1356+
report_ice(info, bug_report_url, extra_info, &USING_INTERNAL_FEATURES);
14421357
},
14431358
));
1444-
1445-
using_internal_features
14461359
}
14471360

14481361
/// Prints the ICE message, including query stack, but without backtrace.
@@ -1583,13 +1496,11 @@ pub fn main() -> ! {
15831496
init_rustc_env_logger(&early_dcx);
15841497
signal_handler::install();
15851498
let mut callbacks = TimePassesCallbacks::default();
1586-
let using_internal_features = install_ice_hook(DEFAULT_BUG_REPORT_URL, |_| ());
1499+
install_ice_hook(DEFAULT_BUG_REPORT_URL, |_| ());
15871500
install_ctrlc_handler();
15881501

15891502
let exit_code = catch_with_exit_code(|| {
1590-
RunCompiler::new(&args::raw_args(&early_dcx)?, &mut callbacks)
1591-
.set_using_internal_features(using_internal_features)
1592-
.run();
1503+
run_compiler(&args::raw_args(&early_dcx)?, &mut callbacks);
15931504
Ok(())
15941505
});
15951506

compiler/rustc_interface/src/interface.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::path::PathBuf;
22
use std::result;
3-
use std::sync::Arc;
43

54
use rustc_ast::{LitKind, MetaItemKind, token};
65
use rustc_codegen_ssa::traits::CodegenBackend;
@@ -309,6 +308,11 @@ pub struct Config {
309308
pub output_dir: Option<PathBuf>,
310309
pub output_file: Option<OutFileName>,
311310
pub ice_file: Option<PathBuf>,
311+
/// Load files from sources other than the file system.
312+
///
313+
/// Has no uses within this repository, but may be used in the future by
314+
/// bjorn3 for "hooking rust-analyzer's VFS into rustc at some point for
315+
/// running rustc without having to save". (See #102759.)
312316
pub file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
313317
/// The list of fluent resources, used for lints declared with
314318
/// [`Diagnostic`](rustc_errors::Diagnostic) and [`LintDiagnostic`](rustc_errors::LintDiagnostic).
@@ -337,6 +341,11 @@ pub struct Config {
337341
pub override_queries: Option<fn(&Session, &mut Providers)>,
338342

339343
/// This is a callback from the driver that is called to create a codegen backend.
344+
///
345+
/// Has no uses within this repository, but is used by bjorn3 for "the
346+
/// hotswapping branch of cg_clif" for "setting the codegen backend from a
347+
/// custom driver where the custom codegen backend has arbitrary data."
348+
/// (See #102759.)
340349
pub make_codegen_backend:
341350
Option<Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>>,
342351

@@ -346,8 +355,7 @@ pub struct Config {
346355
/// The inner atomic value is set to true when a feature marked as `internal` is
347356
/// enabled. Makes it so that "please report a bug" is hidden, as ICEs with
348357
/// internal features are wontfix, and they are usually the cause of the ICEs.
349-
/// None signifies that this is not tracked.
350-
pub using_internal_features: Arc<std::sync::atomic::AtomicBool>,
358+
pub using_internal_features: &'static std::sync::atomic::AtomicBool,
351359

352360
/// All commandline args used to invoke the compiler, with @file args fully expanded.
353361
/// This will only be used within debug info, e.g. in the pdb file on windows

compiler/rustc_interface/src/tests.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use std::collections::{BTreeMap, BTreeSet};
33
use std::num::NonZero;
44
use std::path::{Path, PathBuf};
5-
use std::sync::Arc;
5+
use std::sync::atomic::AtomicBool;
66

77
use rustc_data_structures::profiling::TimePassesFormat;
88
use rustc_errors::emitter::HumanReadableErrorType;
@@ -62,6 +62,8 @@ where
6262
temps_dir,
6363
};
6464

65+
static USING_INTERNAL_FEATURES: AtomicBool = AtomicBool::new(false);
66+
6567
let sess = build_session(
6668
early_dcx,
6769
sessopts,
@@ -74,7 +76,7 @@ where
7476
sysroot,
7577
"",
7678
None,
77-
Arc::default(),
79+
&USING_INTERNAL_FEATURES,
7880
Default::default(),
7981
);
8082
let cfg = parse_cfg(sess.dcx(), matches.opt_strs("cfg"));

compiler/rustc_session/src/session.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ pub struct Session {
189189
/// enabled. Makes it so that "please report a bug" is hidden, as ICEs with
190190
/// internal features are wontfix, and they are usually the cause of the ICEs.
191191
/// None signifies that this is not tracked.
192-
pub using_internal_features: Arc<AtomicBool>,
192+
pub using_internal_features: &'static AtomicBool,
193193

194194
/// All commandline args used to invoke the compiler, with @file args fully expanded.
195195
/// This will only be used within debug info, e.g. in the pdb file on windows
@@ -966,7 +966,7 @@ pub fn build_session(
966966
sysroot: PathBuf,
967967
cfg_version: &'static str,
968968
ice_file: Option<PathBuf>,
969-
using_internal_features: Arc<AtomicBool>,
969+
using_internal_features: &'static AtomicBool,
970970
expanded_args: Vec<String>,
971971
) -> Session {
972972
// FIXME: This is not general enough to make the warning lint completely override

compiler/rustc_smir/src/rustc_internal/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ macro_rules! optional {
316316
#[doc(hidden)]
317317
macro_rules! run_driver {
318318
($args:expr, $callback:expr $(, $with_tcx:ident)?) => {{
319-
use rustc_driver::{Callbacks, Compilation, RunCompiler};
319+
use rustc_driver::{Callbacks, Compilation, run_compiler};
320320
use rustc_middle::ty::TyCtxt;
321321
use rustc_interface::interface;
322322
use stable_mir::CompilerError;
@@ -347,7 +347,7 @@ macro_rules! run_driver {
347347
/// Runs the compiler against given target and tests it with `test_function`
348348
pub fn run(&mut self) -> Result<C, CompilerError<B>> {
349349
let compiler_result = rustc_driver::catch_fatal_errors(|| -> interface::Result::<()> {
350-
RunCompiler::new(&self.args.clone(), self).run();
350+
run_compiler(&self.args.clone(), self);
351351
Ok(())
352352
});
353353
match (compiler_result, self.result.take()) {

src/doc/rustc-dev-guide/examples/rustc-driver-example.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use std::path::Path;
1818

1919
use rustc_ast_pretty::pprust::item_to_string;
2020
use rustc_data_structures::sync::Lrc;
21-
use rustc_driver::{Compilation, RunCompiler};
22-
use rustc_interface::interface::Compiler;
21+
use rustc_driver::{Compilation, run_compiler};
22+
use rustc_interface::interface::{Compiler, Config};
2323
use rustc_middle::ty::TyCtxt;
2424

2525
struct MyFileLoader;
@@ -51,6 +51,10 @@ fn main() {
5151
struct MyCallbacks;
5252

5353
impl rustc_driver::Callbacks for MyCallbacks {
54+
fn config(&mut self, config: &mut Config) {
55+
config.file_loader = Some(Box::new(MyFileLoader));
56+
}
57+
5458
fn after_crate_root_parsing(
5559
&mut self,
5660
_compiler: &Compiler,
@@ -83,10 +87,5 @@ impl rustc_driver::Callbacks for MyCallbacks {
8387
}
8488

8589
fn main() {
86-
match RunCompiler::new(&["main.rs".to_string()], &mut MyCallbacks) {
87-
mut compiler => {
88-
compiler.set_file_loader(Some(Box::new(MyFileLoader)));
89-
compiler.run();
90-
}
91-
}
90+
run_compiler(&["main.rs".to_string()], &mut MyCallbacks);
9291
}

src/doc/rustc-dev-guide/examples/rustc-driver-interacting-with-the-ast.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use std::path::Path;
1818

1919
use rustc_ast_pretty::pprust::item_to_string;
2020
use rustc_data_structures::sync::Lrc;
21-
use rustc_driver::{Compilation, RunCompiler};
22-
use rustc_interface::interface::Compiler;
21+
use rustc_driver::{Compilation, run_compiler};
22+
use rustc_interface::interface::{Compiler, Config};
2323
use rustc_middle::ty::TyCtxt;
2424

2525
struct MyFileLoader;
@@ -51,6 +51,10 @@ fn main() {
5151
struct MyCallbacks;
5252

5353
impl rustc_driver::Callbacks for MyCallbacks {
54+
fn config(&mut self, config: &mut Config) {
55+
config.file_loader = Some(Box::new(MyFileLoader));
56+
}
57+
5458
fn after_crate_root_parsing(
5559
&mut self,
5660
_compiler: &Compiler,
@@ -90,10 +94,5 @@ impl rustc_driver::Callbacks for MyCallbacks {
9094
}
9195

9296
fn main() {
93-
match RunCompiler::new(&["main.rs".to_string()], &mut MyCallbacks) {
94-
mut compiler => {
95-
compiler.set_file_loader(Some(Box::new(MyFileLoader)));
96-
compiler.run();
97-
}
98-
}
97+
run_compiler(&["main.rs".to_string()], &mut MyCallbacks);
9998
}

src/doc/rustc-dev-guide/src/rustc-driver/intro.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The [`rustc_driver`] is essentially `rustc`'s `main` function.
66
It acts as the glue for running the various phases of the compiler in the correct order,
77
using the interface defined in the [`rustc_interface`] crate. Where possible, using [`rustc_driver`] rather than [`rustc_interface`] is recommended.
88

9-
The main entry point of [`rustc_driver`] is [`rustc_driver::RunCompiler`][rd_rc].
9+
The main entry point of [`rustc_driver`] is [`rustc_driver::run_compiler`][rd_rc].
1010
This builder accepts the same command-line args as rustc as well as an implementation of [`Callbacks`][cb] and a couple of other optional options.
1111
[`Callbacks`][cb] is a `trait` that allows for custom compiler configuration,
1212
as well as allowing custom code to run after different phases of the compilation.
@@ -40,7 +40,7 @@ specifically [`rustc_driver_impl::run_compiler`][rdi_rc]
4040
[cb]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_driver/trait.Callbacks.html
4141
[example]: https://github.com/rust-lang/rustc-dev-guide/blob/master/examples/rustc-interface-example.rs
4242
[i_rc]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_interface/interface/fn.run_compiler.html
43-
[rd_rc]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_driver/struct.RunCompiler.html
43+
[rd_rc]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_driver/fn.run_compiler.html
4444
[rdi_rc]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_driver_impl/fn.run_compiler.html
4545
[stupid-stats]: https://github.com/nrc/stupid-stats
4646
[`nightly-rustc`]: https://doc.rust-lang.org/nightly/nightly-rustc/

0 commit comments

Comments
 (0)