driver_hax_frontend_exporter/
callbacks_wrapper.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use hax_types::cli_options::{Command, Options, ENV_VAR_OPTIONS_FRONTEND};

use rustc_driver::{Callbacks, Compilation};
use rustc_interface::{interface, Queries};
use rustc_span::symbol::Symbol;

/// Wraps a [Callbacks] structure, and injects some cache-related
/// configuration in the `config` phase of rustc
pub struct CallbacksWrapper<'a> {
    pub sub: &'a mut (dyn Callbacks + Send + 'a),
    pub options: Options,
}
impl<'a> Callbacks for CallbacksWrapper<'a> {
    fn config(&mut self, config: &mut interface::Config) {
        let options = self.options.clone();
        config.psess_created = Some(Box::new(move |parse_sess| {
            let depinfo = parse_sess.env_depinfo.get_mut();
            depinfo.insert((
                Symbol::intern(ENV_VAR_OPTIONS_FRONTEND),
                Some(Symbol::intern(&serde_json::to_string(&options).unwrap())),
            ));
            depinfo.insert((
                Symbol::intern("HAX_CARGO_CACHE_KEY"),
                std::env::var("HAX_CARGO_CACHE_KEY")
                    .ok()
                    .as_deref()
                    .map(Symbol::intern),
            ));
        }));
        self.sub.config(config)
    }
    fn after_crate_root_parsing<'tcx>(
        &mut self,
        compiler: &interface::Compiler,
        queries: &'tcx Queries<'tcx>,
    ) -> Compilation {
        self.sub.after_crate_root_parsing(compiler, queries)
    }
    fn after_expansion<'tcx>(
        &mut self,
        compiler: &interface::Compiler,
        queries: &'tcx Queries<'tcx>,
    ) -> Compilation {
        self.sub.after_expansion(compiler, queries)
    }
    fn after_analysis<'tcx>(
        &mut self,
        compiler: &interface::Compiler,
        queries: &'tcx Queries<'tcx>,
    ) -> Compilation {
        self.sub.after_analysis(compiler, queries)
    }
}