Skip to main content

psync_driver/
psync-driver.rs

1//! `psync-driver`: rustc with psync's effect analysis, run by `cargo psync check` as
2//! `RUSTC_WRAPPER`. For every crate it compiles, it writes effect summaries of the crate's
3//! functions; in crates with `#[psync::graph]` rewrites, it also checks them.
4#![feature(rustc_private)]
5
6extern crate rustc_driver;
7extern crate rustc_hir;
8extern crate rustc_interface;
9extern crate rustc_middle;
10extern crate rustc_session;
11extern crate rustc_span;
12
13// Override the C allocator in the same way that the `rustc` binary would do.
14rustc_driver::override_c_allocator_in_binary!();
15
16#[path = "../driver/analysis.rs"]
17mod analysis;
18
19use rustc_driver::Compilation;
20use rustc_middle::ty::TyCtxt;
21use rustc_session::EarlyDiagCtxt;
22use rustc_session::config::ErrorOutputType;
23use std::process::ExitCode;
24
25/// Plain rustc, for cargo's queries (`-vV`, `--print`).
26struct Plain;
27impl rustc_driver::Callbacks for Plain {}
28
29struct Psync;
30impl rustc_driver::Callbacks for Psync {
31    fn config(&mut self, config: &mut rustc_interface::interface::Config) {
32        // Recheck a crate when the linked resolution of `dyn` calls changes: cargo reruns a crate
33        // whose recorded environment dependencies changed.
34        config.track_state = Some(Box::new(|sess| {
35            // Only the user's crates use the linked resolution; crates.io and git crates do not
36            // need recompiling when it changes.
37            let source = sess
38                .local_crate_source_file()
39                .and_then(|f| f.local_path().map(|p| p.display().to_string()))
40                .unwrap_or_default();
41            if source.contains("/.cargo/registry/") || source.contains("/.cargo/git/") {
42                return;
43            }
44            let value = std::env::var("PSYNC_LINKED_STAMP").ok();
45            sess.env_depinfo.borrow_mut().insert((
46                rustc_span::Symbol::intern("PSYNC_LINKED_STAMP"),
47                value.as_deref().map(rustc_span::Symbol::intern),
48            ));
49        }));
50    }
51
52    fn after_analysis<'tcx>(
53        &mut self,
54        _compiler: &rustc_interface::interface::Compiler,
55        tcx: TyCtxt<'tcx>,
56    ) -> Compilation {
57        let mut analyzer = analysis::Analyzer::new(tcx);
58        let mut checker = analysis::Checker::new(&mut analyzer);
59        checker.run();
60        checker.write_findings();
61        analyzer.write_summaries();
62        Compilation::Continue
63    }
64}
65
66fn main() -> ExitCode {
67    let early_dcx = EarlyDiagCtxt::new(ErrorOutputType::default());
68    rustc_driver::init_rustc_env_logger(&early_dcx);
69    rustc_driver::install_ice_hook("https://github.com/sahastasai/psync/issues", |_| ());
70    rustc_driver::catch_with_exit_code(move || {
71        let mut args = rustc_driver::args::raw_args(&early_dcx);
72        // As `RUSTC_WRAPPER`, the second argument is the rustc cargo would have run.
73        if args.get(1).is_some_and(|a| {
74            std::path::Path::new(a)
75                .file_stem()
76                .is_some_and(|s| s == "rustc")
77        }) {
78            args.remove(1);
79        }
80        if !args
81            .iter()
82            .any(|a| a == "--sysroot" || a.starts_with("--sysroot="))
83        {
84            args.extend(["--sysroot".to_string(), env!("PSYNC_SYSROOT").to_string()]);
85        }
86        let query = args
87            .iter()
88            .any(|a| a == "-vV" || a == "-V" || a.starts_with("--print"));
89        if query {
90            rustc_driver::compiler_entrypoint(&args, &mut Plain);
91        } else {
92            rustc_driver::compiler_entrypoint(&args, &mut Psync);
93        }
94        ExitCode::SUCCESS
95    })
96}