Skip to content

Commit 4f45b06

Browse files
committed
Add AdHocCalls and pass self to build_controller as Box<Self>
1 parent 90f34b5 commit 4f45b06

File tree

2 files changed

+65
-34
lines changed

2 files changed

+65
-34
lines changed

src/librustc_driver/lib.rs

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ fn get_codegen_sysroot(backend_name: &str) -> fn() -> Box<CodegenBackend> {
454454
// See comments on CompilerCalls below for details about the callbacks argument.
455455
// The FileLoader provides a way to load files from sources other than the file system.
456456
pub fn run_compiler<'a>(args: &[String],
457-
callbacks: &mut (CompilerCalls<'a> + sync::Send),
457+
callbacks: Box<CompilerCalls<'a> + sync::Send + 'a>,
458458
file_loader: Option<Box<FileLoader + Send + Sync + 'static>>,
459459
emitter_dest: Option<Box<Write + Send>>)
460460
-> (CompileResult, Option<Session>)
@@ -478,7 +478,7 @@ fn run_compiler_with_pool<'a>(
478478
matches: getopts::Matches,
479479
sopts: config::Options,
480480
cfg: ast::CrateConfig,
481-
callbacks: &mut (CompilerCalls<'a> + sync::Send),
481+
mut callbacks: Box<CompilerCalls<'a> + sync::Send + 'a>,
482482
file_loader: Option<Box<FileLoader + Send + Sync + 'static>>,
483483
emitter_dest: Option<Box<Write + Send>>
484484
) -> (CompileResult, Option<Session>) {
@@ -642,12 +642,12 @@ impl Compilation {
642642
}
643643
}
644644

645-
// A trait for customising the compilation process. Offers a number of hooks for
646-
// executing custom code or customising input.
645+
/// A trait for customising the compilation process. Offers a number of hooks for
646+
/// executing custom code or customising input.
647647
pub trait CompilerCalls<'a> {
648-
// Hook for a callback early in the process of handling arguments. This will
649-
// be called straight after options have been parsed but before anything
650-
// else (e.g., selecting input and output).
648+
/// Hook for a callback early in the process of handling arguments. This will
649+
/// be called straight after options have been parsed but before anything
650+
/// else (e.g., selecting input and output).
651651
fn early_callback(&mut self,
652652
_: &getopts::Matches,
653653
_: &config::Options,
@@ -658,9 +658,9 @@ pub trait CompilerCalls<'a> {
658658
Compilation::Continue
659659
}
660660

661-
// Hook for a callback late in the process of handling arguments. This will
662-
// be called just before actual compilation starts (and before build_controller
663-
// is called), after all arguments etc. have been completely handled.
661+
/// Hook for a callback late in the process of handling arguments. This will
662+
/// be called just before actual compilation starts (and before build_controller
663+
/// is called), after all arguments etc. have been completely handled.
664664
fn late_callback(&mut self,
665665
_: &CodegenBackend,
666666
_: &getopts::Matches,
@@ -673,21 +673,21 @@ pub trait CompilerCalls<'a> {
673673
Compilation::Continue
674674
}
675675

676-
// Called after we extract the input from the arguments. Gives the implementer
677-
// an opportunity to change the inputs or to add some custom input handling.
678-
// The default behaviour is to simply pass through the inputs.
676+
/// Called after we extract the input from the arguments. Gives the implementer
677+
/// an opportunity to change the inputs or to add some custom input handling.
678+
/// The default behaviour is to simply pass through the inputs.
679679
fn some_input(&mut self,
680680
input: Input,
681681
input_path: Option<PathBuf>)
682682
-> (Input, Option<PathBuf>) {
683683
(input, input_path)
684684
}
685685

686-
// Called after we extract the input from the arguments if there is no valid
687-
// input. Gives the implementer an opportunity to supply alternate input (by
688-
// returning a Some value) or to add custom behaviour for this error such as
689-
// emitting error messages. Returning None will cause compilation to stop
690-
// at this point.
686+
/// Called after we extract the input from the arguments if there is no valid
687+
/// input. Gives the implementer an opportunity to supply alternate input (by
688+
/// returning a Some value) or to add custom behaviour for this error such as
689+
/// emitting error messages. Returning None will cause compilation to stop
690+
/// at this point.
691691
fn no_input(&mut self,
692692
_: &getopts::Matches,
693693
_: &config::Options,
@@ -701,13 +701,41 @@ pub trait CompilerCalls<'a> {
701701

702702
// Create a CompilController struct for controlling the behaviour of
703703
// compilation.
704-
fn build_controller(&mut self, _: &Session, _: &getopts::Matches) -> CompileController<'a>;
704+
fn build_controller(
705+
self: Box<Self>,
706+
_: &Session,
707+
_: &getopts::Matches
708+
) -> CompileController<'a>;
705709
}
706710

707-
// CompilerCalls instance for a regular rustc build.
711+
/// CompilerCalls instance for a regular rustc build.
708712
#[derive(Copy, Clone)]
709713
pub struct RustcDefaultCalls;
710714

715+
/// CompilerCalls instance for quick access to the result of one compile phase.
716+
pub enum AdHocCalls<'a> {
717+
AfterAnalysis(Compilation, Box<Fn(&mut ::driver::CompileState) + 'a>)
718+
}
719+
720+
impl<'a> CompilerCalls<'a> for AdHocCalls<'a> {
721+
fn build_controller(
722+
self: Box<Self>,
723+
_: &Session,
724+
_: &getopts::Matches
725+
) -> CompileController<'a> {
726+
let mut control = CompileController::basic();
727+
728+
match *self {
729+
AdHocCalls::AfterAnalysis(c, f) => {
730+
control.after_analysis.stop = c;
731+
control.after_analysis.callback = f;
732+
}
733+
}
734+
735+
control
736+
}
737+
}
738+
711739
// FIXME remove these and use winapi 0.3 instead
712740
// Duplicates: bootstrap/compile.rs, librustc_errors/emitter.rs
713741
#[cfg(unix)]
@@ -878,7 +906,7 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
878906
.and_then(|| RustcDefaultCalls::list_metadata(sess, cstore, matches, input))
879907
}
880908

881-
fn build_controller(&mut self,
909+
fn build_controller(self: Box<Self>,
882910
sess: &Session,
883911
matches: &getopts::Matches)
884912
-> CompileController<'a> {
@@ -1693,7 +1721,7 @@ pub fn main() {
16931721
}))
16941722
.collect::<Vec<_>>();
16951723
run_compiler(&args,
1696-
&mut RustcDefaultCalls,
1724+
Box::new(RustcDefaultCalls),
16971725
None,
16981726
None)
16991727
});

src/test/run-pass-fulldeps/compiler-calls.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,19 @@ use syntax::ast;
3131

3232
use std::path::PathBuf;
3333

34-
struct TestCalls {
35-
count: u32
34+
struct TestCalls<'a> {
35+
count: &'a mut u32
3636
}
3737

38-
impl<'a> CompilerCalls<'a> for TestCalls {
38+
impl<'a> CompilerCalls<'a> for TestCalls<'a> {
3939
fn early_callback(&mut self,
4040
_: &getopts::Matches,
4141
_: &config::Options,
4242
_: &ast::CrateConfig,
4343
_: &errors::registry::Registry,
4444
_: config::ErrorOutputType)
4545
-> Compilation {
46-
self.count *= 2;
46+
*self.count *= 2;
4747
Compilation::Continue
4848
}
4949

@@ -56,13 +56,13 @@ impl<'a> CompilerCalls<'a> for TestCalls {
5656
_: &Option<PathBuf>,
5757
_: &Option<PathBuf>)
5858
-> Compilation {
59-
self.count *= 3;
59+
*self.count *= 3;
6060
Compilation::Stop
6161
}
6262

6363
fn some_input(&mut self, input: Input, input_path: Option<PathBuf>)
6464
-> (Input, Option<PathBuf>) {
65-
self.count *= 5;
65+
*self.count *= 5;
6666
(input, input_path)
6767
}
6868

@@ -77,7 +77,7 @@ impl<'a> CompilerCalls<'a> for TestCalls {
7777
panic!("This shouldn't happen");
7878
}
7979

80-
fn build_controller(&mut self,
80+
fn build_controller(self: Box<Self>,
8181
_: &Session,
8282
_: &getopts::Matches)
8383
-> driver::CompileController<'a> {
@@ -87,9 +87,12 @@ impl<'a> CompilerCalls<'a> for TestCalls {
8787

8888

8989
fn main() {
90-
let mut tc = TestCalls { count: 1 };
91-
// we should never get use this filename, but lets make sure they are valid args.
92-
let args = vec!["compiler-calls".to_string(), "foo.rs".to_string()];
93-
rustc_driver::run_compiler(&args, &mut tc, None, None);
94-
assert_eq!(tc.count, 30);
90+
let mut count = 1;
91+
{
92+
let tc = TestCalls { count: &mut count };
93+
// we should never get use this filename, but lets make sure they are valid args.
94+
let args = vec!["compiler-calls".to_string(), "foo.rs".to_string()];
95+
rustc_driver::run_compiler(&args, Box::new(tc), None, None);
96+
}
97+
assert_eq!(count, 30);
9598
}

0 commit comments

Comments
 (0)