Skip to content

Commit

Permalink
Replace run_compiler with RunCompiler builder pattern.
Browse files Browse the repository at this point in the history
RunCompiler::new takes non-optional params, and optional
params can be set using set_*field_name* method.
finally `run` will forward all fields to `run_compiler`.
  • Loading branch information
lakshya-sky authored and Mark-Simulacrum committed Nov 15, 2020
1 parent e68b265 commit d9beaaf
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
47 changes: 45 additions & 2 deletions compiler/rustc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,52 @@ pub fn diagnostics_registry() -> Registry {
Registry::new(&rustc_error_codes::DIAGNOSTICS)
}

pub struct RunCompiler<'a, 'b> {
at_args: &'a [String],
callbacks: &'b mut (dyn Callbacks + Send),
file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
emitter: Option<Box<dyn Write + Send>>,
make_codegen_backend:
Option<Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>>,
}

impl<'a, 'b> RunCompiler<'a, 'b> {
pub fn new(at_args: &'a [String], callbacks: &'b mut (dyn Callbacks + Send)) -> Self {
Self { at_args, callbacks, file_loader: None, emitter: None, make_codegen_backend: None }
}
pub fn set_make_codegen_backend(
&mut self,
make_codegen_backend: Option<
Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>,
>,
) -> &mut Self {
self.make_codegen_backend = make_codegen_backend;
self
}
pub fn set_emitter(&mut self, emitter: Option<Box<dyn Write + Send>>) -> &mut Self {
self.emitter = emitter;
self
}
pub fn set_file_loader(
&mut self,
file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
) -> &mut Self {
self.file_loader = file_loader;
self
}
pub fn run(self) -> interface::Result<()> {
run_compiler(
self.at_args,
self.callbacks,
self.file_loader,
self.emitter,
self.make_codegen_backend,
)
}
}
// Parse args and run the compiler. This is the primary entry point for rustc.
// The FileLoader provides a way to load files from sources other than the file system.
pub fn run_compiler(
fn run_compiler(
at_args: &[String],
callbacks: &mut (dyn Callbacks + Send),
file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
Expand Down Expand Up @@ -1275,7 +1318,7 @@ pub fn main() -> ! {
})
})
.collect::<Vec<_>>();
run_compiler(&args, &mut callbacks, None, None, None)
RunCompiler::new(&args, &mut callbacks).run()
});
// The extra `\t` is necessary to align this label with the others.
print_time_passes_entry(callbacks.time_passes, "\ttotal", start.elapsed());
Expand Down
11 changes: 3 additions & 8 deletions src/test/ui-fulldeps/compiler-calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,8 @@ fn main() {
let mut count = 1;
let args = vec!["compiler-calls".to_string(), "foo.rs".to_string()];
rustc_driver::catch_fatal_errors(|| {
rustc_driver::run_compiler(
&args,
&mut TestCalls { count: &mut count },
None,
None,
None,
).ok();
}).ok();
rustc_driver::RunCompiler::new(&args, &mut TestCalls { count: &mut count }).run().ok();
})
.ok();
assert_eq!(count, 2);
}
4 changes: 2 additions & 2 deletions src/tools/clippy/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ pub fn main() {
args.extend(vec!["--sysroot".into(), sys_root]);
};

return rustc_driver::run_compiler(&args, &mut DefaultCallbacks, None, None, None);
return rustc_driver::RunCompiler::new(&args, &mut DefaultCallbacks).run();
}

if orig_args.iter().any(|a| a == "--version" || a == "-V") {
Expand Down Expand Up @@ -420,6 +420,6 @@ pub fn main() {
let mut default = DefaultCallbacks;
let callbacks: &mut (dyn rustc_driver::Callbacks + Send) =
if clippy_enabled { &mut clippy } else { &mut default };
rustc_driver::run_compiler(&args, callbacks, None, None, None)
rustc_driver::RunCompiler::new(&args, callbacks).run()
}))
}

0 comments on commit d9beaaf

Please sign in to comment.