Skip to content

Add annotations to the graphviz region graph on region origins #144988

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/handle_placeholders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ fn region_definitions<'tcx>(
for info in var_infos.iter() {
let origin = match info.origin {
RegionVariableOrigin::Nll(origin) => origin,
_ => NllRegionVariableOrigin::Existential { from_forall: false },
_ => NllRegionVariableOrigin::Existential { from_forall: false, name: None },
};

let definition = RegionDefinition { origin, universe: info.universe, external_name: None };
Expand Down
17 changes: 16 additions & 1 deletion compiler/rustc_borrowck/src/region_infer/graphviz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,22 @@ fn render_region_vid<'tcx>(
"".to_string()
};

format!("{:?}{universe_str}{external_name_str}", rvid)
let extra_info = match regioncx.region_definition(rvid).origin {
NllRegionVariableOrigin::FreeRegion => "".to_string(),
NllRegionVariableOrigin::Placeholder(p) => match p.bound.kind {
ty::BoundRegionKind::Named(def_id) => {
format!(" (for<{}>)", tcx.item_name(def_id))
}
ty::BoundRegionKind::ClosureEnv | ty::BoundRegionKind::Anon => " (for<'_>)".to_string(),
ty::BoundRegionKind::NamedAnon(_) => {
bug!("only used for pretty printing")
}
},
NllRegionVariableOrigin::Existential { name: Some(name), .. } => format!(" (ex<{name}>)"),
NllRegionVariableOrigin::Existential { .. } => format!(" (ex<'_>)"),
};

format!("{:?}{universe_str}{external_name_str}{extra_info}", rvid)
}

impl<'tcx> RegionInferenceContext<'tcx> {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_borrowck/src/region_infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1940,9 +1940,9 @@ impl<'tcx> RegionInferenceContext<'tcx> {
// and here we prefer to blame the source (the y = x statement).
let blame_source = match from_region_origin {
NllRegionVariableOrigin::FreeRegion
| NllRegionVariableOrigin::Existential { from_forall: false } => true,
| NllRegionVariableOrigin::Existential { from_forall: false, name: _ } => true,
NllRegionVariableOrigin::Placeholder(_)
| NllRegionVariableOrigin::Existential { from_forall: true } => false,
| NllRegionVariableOrigin::Existential { from_forall: true, name: _ } => false,
};

// To pick a constraint to blame, we organize constraints by how interesting we expect them
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/renumber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl<'a, 'tcx> RegionRenumberer<'a, 'tcx> {
T: TypeFoldable<TyCtxt<'tcx>>,
F: Fn() -> RegionCtxt,
{
let origin = NllRegionVariableOrigin::Existential { from_forall: false };
let origin = NllRegionVariableOrigin::Existential { from_forall: false, name: None };
fold_regions(self.infcx.tcx, value, |_region, _depth| {
self.infcx.next_nll_region_var(origin, || region_ctxt_fn())
})
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/type_check/relate_tys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ impl<'a, 'b, 'tcx> NllTypeRelating<'a, 'b, 'tcx> {
from_forall: bool,
name: Option<Symbol>,
) -> ty::Region<'tcx> {
let origin = NllRegionVariableOrigin::Existential { from_forall };
let origin = NllRegionVariableOrigin::Existential { name, from_forall };

let reg_var =
self.type_checker.infcx.next_nll_region_var(origin, || RegionCtxt::Existential(name));
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_infer/src/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ pub enum NllRegionVariableOrigin {
Placeholder(ty::PlaceholderRegion),

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