]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/borrow_check/region_infer/dump_mir.rs
Auto merge of #69590 - Dylan-DPC:rollup-i3z0sic, r=Dylan-DPC
[rust.git] / src / librustc_mir / borrow_check / 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
6 use super::{OutlivesConstraint, RegionInferenceContext};
7 use rustc_infer::infer::NLLRegionVariableOrigin;
8 use std::io::{self, Write};
9
10 // Room for "'_#NNNNr" before things get misaligned.
11 // Easy enough to fix if this ever doesn't seem like
12 // enough.
13 const REGION_WIDTH: usize = 8;
14
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")?;
19
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);
24                 writeln!(
25                     out,
26                     "| {r:rw$?} | {c:cw$?} | {ob:?}",
27                     r = region,
28                     rw = REGION_WIDTH,
29                     c = classification,
30                     cw = 8, // "External" at most
31                     ob = outlived_by
32                 )?;
33             }
34         }
35
36         writeln!(out, "|")?;
37         writeln!(out, "| Inferred Region Values")?;
38         for region in self.regions() {
39             writeln!(
40                 out,
41                 "| {r:rw$?} | {ui:4?} | {v}",
42                 r = region,
43                 rw = REGION_WIDTH,
44                 ui = self.region_universe(region),
45                 v = self.region_value_str(region),
46             )?;
47         }
48
49         writeln!(out, "|")?;
50         writeln!(out, "| Inference Constraints")?;
51         self.for_each_constraint(&mut |msg| writeln!(out, "| {}", msg))?;
52
53         Ok(())
54     }
55
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(
61         &self,
62         with_msg: &mut dyn FnMut(&str) -> io::Result<()>,
63     ) -> io::Result<()> {
64         for region in self.definitions.indices() {
65             let value = self.liveness_constraints.region_value_str(region);
66             if value != "{}" {
67                 with_msg(&format!("{:?} live at {}", region, value))?;
68             }
69         }
70
71         let mut constraints: Vec<_> = self.constraints.outlives().iter().collect();
72         constraints.sort();
73         for constraint in &constraints {
74             let OutlivesConstraint { sup, sub, locations, category } = constraint;
75             with_msg(&format!("{:?}: {:?} due to {:?} at {:?}", sup, sub, category, locations,))?;
76         }
77
78         Ok(())
79     }
80 }