rustc_mir_transform/
dump_mir.rs

1//! This pass just dumps MIR at a specified point.
2
3use std::fs::File;
4use std::io;
5
6use rustc_middle::mir::{Body, write_mir_pretty};
7use rustc_middle::ty::TyCtxt;
8use rustc_session::config::{OutFileName, OutputType};
9
10pub(super) struct Marker(pub &'static str);
11
12impl<'tcx> crate::MirPass<'tcx> for Marker {
13    fn name(&self) -> &'static str {
14        self.0
15    }
16
17    fn run_pass(&self, _tcx: TyCtxt<'tcx>, _body: &mut Body<'tcx>) {}
18
19    fn is_required(&self) -> bool {
20        false
21    }
22}
23
24pub fn emit_mir(tcx: TyCtxt<'_>) -> io::Result<()> {
25    match tcx.output_filenames(()).path(OutputType::Mir) {
26        OutFileName::Stdout => {
27            let mut f = io::stdout();
28            write_mir_pretty(tcx, None, &mut f)?;
29        }
30        OutFileName::Real(path) => {
31            let mut f = File::create_buffered(&path)?;
32            write_mir_pretty(tcx, None, &mut f)?;
33            if tcx.sess.opts.json_artifact_notifications {
34                tcx.dcx().emit_artifact_notification(&path, "mir");
35            }
36        }
37    }
38    Ok(())
39}