rustc_borrowck/region_infer/
dump_mir.rs

1//! As part of generating the regions, if you enable `-Zdump-mir=nll`,
2//! we will generate an annotated copy of the MIR that includes the
3//! state of region inference. This code handles emitting the region
4//! context internal state.
5
6use std::io::{self, Write};
7
8use rustc_infer::infer::NllRegionVariableOrigin;
9use rustc_middle::ty::TyCtxt;
10
11use super::{OutlivesConstraint, RegionInferenceContext};
12use crate::type_check::Locations;
13
14// Room for "'_#NNNNr" before things get misaligned.
15// Easy enough to fix if this ever doesn't seem like
16// enough.
17const REGION_WIDTH: usize = 8;
18
19impl<'tcx> RegionInferenceContext<'tcx> {
20    /// Write out our state into the `.mir` files.
21    pub(crate) fn dump_mir(&self, tcx: TyCtxt<'tcx>, out: &mut dyn Write) -> io::Result<()> {
22        writeln!(out, "| Free Region Mapping")?;
23
24        for region in self.regions() {
25            if let NllRegionVariableOrigin::FreeRegion = self.definitions[region].origin {
26                let classification =
27                    self.universal_regions().region_classification(region).unwrap();
28                let outlived_by = self.universal_region_relations.regions_outlived_by(region);
29                writeln!(
30                    out,
31                    "| {r:rw$?} | {c:cw$?} | {ob:?}",
32                    r = region,
33                    rw = REGION_WIDTH,
34                    c = classification,
35                    cw = 8, // "External" at most
36                    ob = outlived_by
37                )?;
38            }
39        }
40
41        writeln!(out, "|")?;
42        writeln!(out, "| Inferred Region Values")?;
43        for region in self.regions() {
44            writeln!(
45                out,
46                "| {r:rw$?} | {ui:4?} | {v}",
47                r = region,
48                rw = REGION_WIDTH,
49                ui = self.region_universe(region),
50                v = self.region_value_str(region),
51            )?;
52        }
53
54        writeln!(out, "|")?;
55        writeln!(out, "| Inference Constraints")?;
56        self.for_each_constraint(tcx, &mut |msg| writeln!(out, "| {msg}"))?;
57
58        Ok(())
59    }
60
61    /// Debugging aid: Invokes the `with_msg` callback repeatedly with
62    /// our internal region constraints. These are dumped into the
63    /// -Zdump-mir file so that we can figure out why the region
64    /// inference resulted in the values that it did when debugging.
65    fn for_each_constraint(
66        &self,
67        tcx: TyCtxt<'tcx>,
68        with_msg: &mut dyn FnMut(&str) -> io::Result<()>,
69    ) -> io::Result<()> {
70        for region in self.definitions.indices() {
71            let value = self.liveness_constraints.pretty_print_live_points(region);
72            if value != "{}" {
73                with_msg(&format!("{region:?} live at {value}"))?;
74            }
75        }
76
77        let mut constraints: Vec<_> = self.constraints.outlives().iter().collect();
78        constraints.sort_by_key(|c| (c.sup, c.sub));
79        for constraint in &constraints {
80            let OutlivesConstraint { sup, sub, locations, category, span, .. } = constraint;
81            let (name, arg) = match locations {
82                Locations::All(span) => {
83                    ("All", tcx.sess.source_map().span_to_embeddable_string(*span))
84                }
85                Locations::Single(loc) => ("Single", format!("{loc:?}")),
86            };
87            with_msg(&format!("{sup:?}: {sub:?} due to {category:?} at {name}({arg}) ({span:?}"))?;
88        }
89
90        Ok(())
91    }
92}