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.
6 use super::{OutlivesConstraint, RegionInferenceContext};
7 use rustc_infer::infer::NLLRegionVariableOrigin;
8 use std::io::{self, Write};
10 // Room for "'_#NNNNr" before things get misaligned.
11 // Easy enough to fix if this ever doesn't seem like
13 const REGION_WIDTH: usize = 8;
15 impl<'tcx> RegionInferenceContext<'tcx> {
16 /// Write out our state into the `.mir` files.
17 pub(crate) fn dump_mir(&self, out: &mut dyn Write) -> io::Result<()> {
18 writeln!(out, "| Free Region Mapping")?;
20 for region in self.regions() {
21 if let NLLRegionVariableOrigin::FreeRegion = self.definitions[region].origin {
22 let classification = self.universal_regions.region_classification(region).unwrap();
23 let outlived_by = self.universal_region_relations.regions_outlived_by(region);
26 "| {r:rw$?} | {c:cw$?} | {ob:?}",
30 cw = 8, // "External" at most
37 writeln!(out, "| Inferred Region Values")?;
38 for region in self.regions() {
41 "| {r:rw$?} | {ui:4?} | {v}",
44 ui = self.region_universe(region),
45 v = self.region_value_str(region),
50 writeln!(out, "| Inference Constraints")?;
51 self.for_each_constraint(&mut |msg| writeln!(out, "| {}", msg))?;
56 /// Debugging aid: Invokes the `with_msg` callback repeatedly with
57 /// our internal region constraints. These are dumped into the
58 /// -Zdump-mir file so that we can figure out why the region
59 /// inference resulted in the values that it did when debugging.
60 fn for_each_constraint(
62 with_msg: &mut dyn FnMut(&str) -> io::Result<()>,
64 for region in self.definitions.indices() {
65 let value = self.liveness_constraints.region_value_str(region);
67 with_msg(&format!("{:?} live at {}", region, value))?;
71 let mut constraints: Vec<_> = self.constraints.outlives().iter().collect();
73 for constraint in &constraints {
74 let OutlivesConstraint { sup, sub, locations, category } = constraint;
75 with_msg(&format!("{:?}: {:?} due to {:?} at {:?}", sup, sub, category, locations,))?;