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 rustc::infer::NLLRegionVariableOrigin;
7 use std::io::{self, Write};
8 use super::{OutlivesConstraint, RegionInferenceContext};
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
24 .region_classification(region)
26 let outlived_by = self.universal_region_relations.regions_outlived_by(region);
29 "| {r:rw$?} | {c:cw$?} | {ob:?}",
33 cw = 8, // "External" at most
40 writeln!(out, "| Inferred Region Values")?;
41 for region in self.regions() {
44 "| {r:rw$?} | {ui:4?} | {v}",
47 ui = self.region_universe(region),
48 v = self.region_value_str(region),
53 writeln!(out, "| Inference Constraints")?;
54 self.for_each_constraint(&mut |msg| writeln!(out, "| {}", msg))?;
59 /// Debugging aid: Invokes the `with_msg` callback repeatedly with
60 /// our internal region constraints. These are dumped into the
61 /// -Zdump-mir file so that we can figure out why the region
62 /// inference resulted in the values that it did when debugging.
63 fn for_each_constraint(
65 with_msg: &mut dyn FnMut(&str) -> io::Result<()>,
67 for region in self.definitions.indices() {
68 let value = self.liveness_constraints.region_value_str(region);
70 with_msg(&format!("{:?} live at {}", region, value))?;
74 let mut constraints: Vec<_> = self.constraints.iter().collect();
76 for constraint in &constraints {
77 let OutlivesConstraint {
84 "{:?}: {:?} due to {:?} at {:?}",