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.
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};
14 // Room for "'_#NNNNr" before things get misaligned.
15 // Easy enough to fix if this ever doesn't seem like
17 const REGION_WIDTH: usize = 8;
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")?;
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);
30 "| {r:rw$?} | {c:cw$?} | {ob:?}",
34 cw = 8, // "External" at most
41 writeln!(out, "| Inferred Region Values")?;
42 for region in self.regions() {
45 "| {r:rw$?} | {ui:4?} | {v}",
48 ui = self.region_universe(region),
49 v = self.region_value_str(region),
54 writeln!(out, "| Inference Constraints")?;
55 self.for_each_constraint(tcx, &mut |msg| writeln!(out, "| {}", msg))?;
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(
67 with_msg: &mut dyn FnMut(&str) -> io::Result<()>,
69 for region in self.definitions.indices() {
70 let value = self.liveness_constraints.region_value_str(region);
72 with_msg(&format!("{:?} live at {}", region, value))?;
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))
84 Locations::Single(loc) => ("Single", format!("{:?}", loc)),
87 "{:?}: {:?} due to {:?} at {}({}) ({:?}",
88 sup, sub, category, name, arg, span