Skip to content

Commit e8dca78

Browse files
committed
Replace run_compiler with RunCompiler builder pattern.
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`.
1 parent ccea570 commit e8dca78

File tree

3 files changed

+50
-12
lines changed

3 files changed

+50
-12
lines changed

Diff for: compiler/rustc_driver/src/lib.rs

+45-2
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,52 @@ pub fn diagnostics_registry() -> Registry {
134134
Registry::new(&rustc_error_codes::DIAGNOSTICS)
135135
}
136136

137+
pub struct RunCompiler<'a, 'b> {
138+
at_args: &'a [String],
139+
callbacks: &'b mut (dyn Callbacks + Send),
140+
file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
141+
emitter: Option<Box<dyn Write + Send>>,
142+
make_codegen_backend:
143+
Option<Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>>,
144+
}
145+
146+
impl<'a, 'b> RunCompiler<'a, 'b> {
147+
pub fn new(at_args: &'a [String], callbacks: &'b mut (dyn Callbacks + Send)) -> Self {
148+
Self { at_args, callbacks, file_loader: None, emitter: None, make_codegen_backend: None }
149+
}
150+
pub fn set_make_codegen_backend(
151+
&mut self,
152+
make_codegen_backend: Option<
153+
Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>,
154+
>,
155+
) -> &mut Self {
156+
self.make_codegen_backend = make_codegen_backend;
157+
self
158+
}
159+
pub fn set_emitter(&mut self, emitter: Option<Box<dyn Write + Send>>) -> &mut Self {
160+
self.emitter = emitter;
161+
self
162+
}
163+
pub fn set_file_loader(
164+
&mut self,
165+
file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
166+
) -> &mut Self {
167+
self.file_loader = file_loader;
168+
self
169+
}
170+
pub fn run(self) -> interface::Result<()> {
171+
run_compiler(
172+
self.at_args,
173+
self.callbacks,
174+
self.file_loader,
175+
self.emitter,
176+
self.make_codegen_backend,
177+
)
178+
}
179+
}
137180
// Parse args and run the compiler. This is the primary entry point for rustc.
138181
// The FileLoader provides a way to load files from sources other than the file system.
139-
pub fn run_compiler(
182+
fn run_compiler(
140183
at_args: &[String],
141184
callbacks: &mut (dyn Callbacks + Send),
142185
file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
@@ -1285,7 +1328,7 @@ pub fn main() -> ! {
12851328
})
12861329
})
12871330
.collect::<Vec<_>>();
1288-
run_compiler(&args, &mut callbacks, None, None, None)
1331+
RunCompiler::new(&args, &mut callbacks).run()
12891332
});
12901333
// The extra `\t` is necessary to align this label with the others.
12911334
print_time_passes_entry(callbacks.time_passes, "\ttotal", start.elapsed());

Diff for: src/test/ui-fulldeps/compiler-calls.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,8 @@ fn main() {
2626
let mut count = 1;
2727
let args = vec!["compiler-calls".to_string(), "foo.rs".to_string()];
2828
rustc_driver::catch_fatal_errors(|| {
29-
rustc_driver::run_compiler(
30-
&args,
31-
&mut TestCalls { count: &mut count },
32-
None,
33-
None,
34-
None,
35-
).ok();
36-
}).ok();
29+
rustc_driver::RunCompiler::new(&args, &mut TestCalls { count: &mut count }).run().ok();
30+
})
31+
.ok();
3732
assert_eq!(count, 2);
3833
}

Diff for: src/tools/clippy/src/driver.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ pub fn main() {
357357
args.extend(vec!["--sysroot".into(), sys_root]);
358358
};
359359

360-
return rustc_driver::run_compiler(&args, &mut DefaultCallbacks, None, None, None);
360+
return rustc_driver::RunCompiler::new(&args, &mut DefaultCallbacks).run();
361361
}
362362

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

0 commit comments

Comments
 (0)