rustc_borrowck/region_infer/
dump_mir.rs1use 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
14const REGION_WIDTH: usize = 8;
18
19impl<'tcx> RegionInferenceContext<'tcx> {
20 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, 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 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}