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