Skip to content

Commit 800c506

Browse files
committed
Slightly more readable NLL/constraint graph dumps
1 parent b14d8b2 commit 800c506

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

compiler/rustc_borrowck/src/region_infer/graphviz.rs

+36-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,38 @@ use std::borrow::Cow;
66
use std::io::{self, Write};
77

88
use super::*;
9+
use itertools::Itertools;
910
use rustc_graphviz as dot;
11+
use rustc_middle::ty::UniverseIndex;
12+
13+
fn render_outlives_constraint(constraint: &OutlivesConstraint<'_>) -> String {
14+
match constraint.locations {
15+
Locations::All(_) => "All(...)".to_string(),
16+
Locations::Single(loc) => format!("{loc:?}"),
17+
}
18+
}
19+
20+
fn render_universe(u: UniverseIndex) -> String {
21+
if u.is_root() {
22+
return "".to_string();
23+
}
24+
25+
format!("/{:?}", u)
26+
}
27+
28+
fn render_region_vid(rvid: RegionVid, regioncx: &RegionInferenceContext<'_>) -> String {
29+
let universe_str = render_universe(regioncx.region_definition(rvid).universe);
30+
31+
let external_name_str = if let Some(external_name) =
32+
regioncx.region_definition(rvid).external_name.and_then(|e| e.get_name())
33+
{
34+
format!(" ({external_name})")
35+
} else {
36+
"".to_string()
37+
};
38+
39+
format!("{:?}{universe_str}{external_name_str}", rvid)
40+
}
1041

1142
impl<'tcx> RegionInferenceContext<'tcx> {
1243
/// Write out the region constraint graph.
@@ -46,10 +77,10 @@ impl<'a, 'this, 'tcx> dot::Labeller<'this> for RawConstraints<'a, 'tcx> {
4677
Some(dot::LabelText::LabelStr(Cow::Borrowed("box")))
4778
}
4879
fn node_label(&'this self, n: &RegionVid) -> dot::LabelText<'this> {
49-
dot::LabelText::LabelStr(format!("{n:?}").into())
80+
dot::LabelText::LabelStr(render_region_vid(*n, self.regioncx).into())
5081
}
5182
fn edge_label(&'this self, e: &OutlivesConstraint<'tcx>) -> dot::LabelText<'this> {
52-
dot::LabelText::LabelStr(format!("{:?}", e.locations).into())
83+
dot::LabelText::LabelStr(render_outlives_constraint(e).into())
5384
}
5485
}
5586

@@ -96,8 +127,9 @@ impl<'a, 'this, 'tcx> dot::Labeller<'this> for SccConstraints<'a, 'tcx> {
96127
Some(dot::LabelText::LabelStr(Cow::Borrowed("box")))
97128
}
98129
fn node_label(&'this self, n: &ConstraintSccIndex) -> dot::LabelText<'this> {
99-
let nodes = &self.nodes_per_scc[*n];
100-
dot::LabelText::LabelStr(format!("{n:?} = {nodes:?}").into())
130+
let nodes_str =
131+
self.nodes_per_scc[*n].iter().map(|n| render_region_vid(*n, self.regioncx)).join(", ");
132+
dot::LabelText::LabelStr(format!("SCC({n}) = {{{nodes_str}}}", n = n.as_usize()).into())
101133
}
102134
}
103135

compiler/rustc_borrowck/src/region_infer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1562,7 +1562,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
15621562

15631563
// Because this free region must be in the ROOT universe, we
15641564
// know it cannot contain any bound universes.
1565-
assert!(self.scc_universes[longer_fr_scc] == ty::UniverseIndex::ROOT);
1565+
assert!(self.scc_universes[longer_fr_scc].is_root());
15661566
debug_assert!(self.scc_values.placeholders_contained_in(longer_fr_scc).next().is_none());
15671567

15681568
// Only check all of the relations for the main representative of each

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
213213
let scc = self.constraint_sccs.scc(vid);
214214

215215
// Special handling of higher-ranked regions.
216-
if self.scc_universes[scc] != ty::UniverseIndex::ROOT {
216+
if !self.scc_universes[scc].is_root() {
217217
match self.scc_values.placeholders_contained_in(scc).enumerate().last() {
218218
// If the region contains a single placeholder then they're equal.
219219
Some((0, placeholder)) => {

compiler/rustc_type_ir/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,11 @@ impl UniverseIndex {
346346
pub fn cannot_name(self, other: UniverseIndex) -> bool {
347347
self < other
348348
}
349+
350+
/// Returns `true` if `self` is the root universe, otherwise false.
351+
pub fn is_root(self) -> bool {
352+
self == Self::ROOT
353+
}
349354
}
350355

351356
impl Default for UniverseIndex {

0 commit comments

Comments
 (0)