Skip to content

Commit 35f2fb9

Browse files
authored
Rollup merge of #144988 - amandasystems:more-detailed-region-graph, r=lcnr
Add annotations to the graphviz region graph on region origins This adds - `(ex<'e>)` for regions whose origin is existential, with name if one exists, - `(for<'p>)` for regions whose origin is a placeholder, with name if one exists For any region whose name we don't know, use `'_`. This has helped _my_ debugging and it doesn't create too bad clutter, I feel. The change ~~is~~was ridiculously small, but I turned it into a separate PR so we can bikeshed the format. The following snippet: ```rust struct Co<'a>(&'a ()); struct Contra<'a>(fn(&'a ())); // `exists<'e> forall<'p> 'p: 'e` -> ERROR fn p_outlives_e( x: for<'e> fn(for<'p> fn(fn(fn(Contra<'e>, Co<'p>)))), ) -> fn(fn(fn(for<'unify> fn(Contra<'unify>, Co<'unify>)))) { x ``` Gives this graph: ![new-naming](https://github.com/user-attachments/assets/f2c8f17c-d29b-4d42-9da7-4b8e520e76a6)
2 parents 7f77dfd + 648e3fc commit 35f2fb9

File tree

6 files changed

+22
-6
lines changed

6 files changed

+22
-6
lines changed

compiler/rustc_borrowck/src/handle_placeholders.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ fn region_definitions<'tcx>(
157157
for info in var_infos.iter() {
158158
let origin = match info.origin {
159159
RegionVariableOrigin::Nll(origin) => origin,
160-
_ => NllRegionVariableOrigin::Existential { from_forall: false },
160+
_ => NllRegionVariableOrigin::Existential { from_forall: false, name: None },
161161
};
162162

163163
let definition = RegionDefinition { origin, universe: info.universe, external_name: None };

compiler/rustc_borrowck/src/region_infer/graphviz.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,22 @@ fn render_region_vid<'tcx>(
4141
"".to_string()
4242
};
4343

44-
format!("{:?}{universe_str}{external_name_str}", rvid)
44+
let extra_info = match regioncx.region_definition(rvid).origin {
45+
NllRegionVariableOrigin::FreeRegion => "".to_string(),
46+
NllRegionVariableOrigin::Placeholder(p) => match p.bound.kind {
47+
ty::BoundRegionKind::Named(def_id) => {
48+
format!(" (for<{}>)", tcx.item_name(def_id))
49+
}
50+
ty::BoundRegionKind::ClosureEnv | ty::BoundRegionKind::Anon => " (for<'_>)".to_string(),
51+
ty::BoundRegionKind::NamedAnon(_) => {
52+
bug!("only used for pretty printing")
53+
}
54+
},
55+
NllRegionVariableOrigin::Existential { name: Some(name), .. } => format!(" (ex<{name}>)"),
56+
NllRegionVariableOrigin::Existential { .. } => format!(" (ex<'_>)"),
57+
};
58+
59+
format!("{:?}{universe_str}{external_name_str}{extra_info}", rvid)
4560
}
4661

4762
impl<'tcx> RegionInferenceContext<'tcx> {

compiler/rustc_borrowck/src/region_infer/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1940,9 +1940,9 @@ impl<'tcx> RegionInferenceContext<'tcx> {
19401940
// and here we prefer to blame the source (the y = x statement).
19411941
let blame_source = match from_region_origin {
19421942
NllRegionVariableOrigin::FreeRegion
1943-
| NllRegionVariableOrigin::Existential { from_forall: false } => true,
1943+
| NllRegionVariableOrigin::Existential { from_forall: false, name: _ } => true,
19441944
NllRegionVariableOrigin::Placeholder(_)
1945-
| NllRegionVariableOrigin::Existential { from_forall: true } => false,
1945+
| NllRegionVariableOrigin::Existential { from_forall: true, name: _ } => false,
19461946
};
19471947

19481948
// To pick a constraint to blame, we organize constraints by how interesting we expect them

compiler/rustc_borrowck/src/renumber.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl<'a, 'tcx> RegionRenumberer<'a, 'tcx> {
6666
T: TypeFoldable<TyCtxt<'tcx>>,
6767
F: Fn() -> RegionCtxt,
6868
{
69-
let origin = NllRegionVariableOrigin::Existential { from_forall: false };
69+
let origin = NllRegionVariableOrigin::Existential { from_forall: false, name: None };
7070
fold_regions(self.infcx.tcx, value, |_region, _depth| {
7171
self.infcx.next_nll_region_var(origin, || region_ctxt_fn())
7272
})

compiler/rustc_borrowck/src/type_check/relate_tys.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ impl<'a, 'b, 'tcx> NllTypeRelating<'a, 'b, 'tcx> {
249249
from_forall: bool,
250250
name: Option<Symbol>,
251251
) -> ty::Region<'tcx> {
252-
let origin = NllRegionVariableOrigin::Existential { from_forall };
252+
let origin = NllRegionVariableOrigin::Existential { name, from_forall };
253253

254254
let reg_var =
255255
self.type_checker.infcx.next_nll_region_var(origin, || RegionCtxt::Existential(name));

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ pub enum NllRegionVariableOrigin {
484484
Placeholder(ty::PlaceholderRegion),
485485

486486
Existential {
487+
name: Option<Symbol>,
487488
/// If this is true, then this variable was created to represent a lifetime
488489
/// bound in a `for` binder. For example, it might have been created to
489490
/// represent the lifetime `'a` in a type like `for<'a> fn(&'a u32)`.

0 commit comments

Comments
 (0)