driver_hax_frontend_exporter/
callbacks_wrapper.rs

1use hax_types::cli_options::{Command, ENV_VAR_OPTIONS_FRONTEND, Options};
2
3use rustc_ast::Crate;
4use rustc_driver::{Callbacks, Compilation};
5use rustc_interface::interface;
6use rustc_middle::ty::TyCtxt;
7use rustc_span::symbol::Symbol;
8
9/// Wraps a [Callbacks] structure, and injects some cache-related
10/// configuration in the `config` phase of rustc
11pub struct CallbacksWrapper<'a> {
12    pub sub: &'a mut (dyn Callbacks + Send + 'a),
13    pub options: Options,
14}
15impl<'a> Callbacks for CallbacksWrapper<'a> {
16    fn config(&mut self, config: &mut interface::Config) {
17        let options = self.options.clone();
18        config.psess_created = Some(Box::new(move |parse_sess| {
19            // Silence the "unexpected cfg" lints.
20            parse_sess.check_config.exhaustive_names = false;
21            let depinfo = parse_sess.env_depinfo.get_mut();
22            depinfo.insert((
23                Symbol::intern(ENV_VAR_OPTIONS_FRONTEND),
24                Some(Symbol::intern(&serde_json::to_string(&options).unwrap())),
25            ));
26            depinfo.insert((
27                Symbol::intern("HAX_CARGO_CACHE_KEY"),
28                std::env::var("HAX_CARGO_CACHE_KEY")
29                    .ok()
30                    .as_deref()
31                    .map(Symbol::intern),
32            ));
33        }));
34        self.sub.config(config)
35    }
36    fn after_crate_root_parsing<'tcx>(
37        &mut self,
38        compiler: &interface::Compiler,
39        krate: &mut Crate,
40    ) -> Compilation {
41        self.sub.after_crate_root_parsing(compiler, krate)
42    }
43    fn after_expansion<'tcx>(
44        &mut self,
45        compiler: &interface::Compiler,
46        tcx: TyCtxt<'tcx>,
47    ) -> Compilation {
48        self.sub.after_expansion(compiler, tcx)
49    }
50    fn after_analysis<'tcx>(
51        &mut self,
52        compiler: &interface::Compiler,
53        tcx: TyCtxt<'tcx>,
54    ) -> Compilation {
55        self.sub.after_analysis(compiler, tcx)
56    }
57}