Skip to content

Remove Symbol from Named variant of BoundRegionKind/LateParamRegionKind #139598

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
4 changes: 3 additions & 1 deletion compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4184,7 +4184,9 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
// anything.
let return_ty = sig.output();
match return_ty.skip_binder().kind() {
ty::Ref(return_region, _, _) if return_region.has_name() && !is_closure => {
ty::Ref(return_region, _, _)
if return_region.has_name(self.infcx.tcx) && !is_closure =>
{
// This is case 1 from above, return type is a named reference so we need to
// search for relevant arguments.
let mut arguments = Vec::new();
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_borrowck/src/diagnostics/region_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
return;
};

let lifetime = if f.has_name() { fr_name.name } else { kw::UnderscoreLifetime };
let lifetime =
if f.has_name(self.infcx.tcx) { fr_name.name } else { kw::UnderscoreLifetime };

let arg = match param.param.pat.simple_ident() {
Some(simple_ident) => format!("argument `{simple_ident}`"),
Expand Down
10 changes: 3 additions & 7 deletions compiler/rustc_borrowck/src/diagnostics/region_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,16 +300,11 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
}

ty::ReLateParam(late_param) => match late_param.kind {
ty::LateParamRegionKind::Named(region_def_id, name) => {
ty::LateParamRegionKind::Named(region_def_id) => {
// Get the span to point to, even if we don't use the name.
let span = tcx.hir_span_if_local(region_def_id).unwrap_or(DUMMY_SP);
debug!(
"bound region named: {:?}, is_named: {:?}",
name,
late_param.kind.is_named()
);

if late_param.kind.is_named() {
if let Some(name) = late_param.kind.get_name(tcx) {
// A named region that is actually named.
Some(RegionName {
name,
Expand Down Expand Up @@ -369,6 +364,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
}

ty::LateParamRegionKind::Anon(_) => None,
ty::LateParamRegionKind::NamedAnon(_, _) => bug!("only used for pretty printing"),
},

ty::ReBound(..)
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_borrowck/src/nll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,13 @@ pub(super) fn dump_nll_mir<'tcx>(
// Also dump the region constraint graph as a graphviz file.
let _: io::Result<()> = try {
let mut file = create_dump_file(tcx, "regioncx.all.dot", false, "nll", &0, body)?;
regioncx.dump_graphviz_raw_constraints(&mut file)?;
regioncx.dump_graphviz_raw_constraints(tcx, &mut file)?;
};

// Also dump the region constraint SCC graph as a graphviz file.
let _: io::Result<()> = try {
let mut file = create_dump_file(tcx, "regioncx.scc.dot", false, "nll", &0, body)?;
regioncx.dump_graphviz_scc_constraints(&mut file)?;
regioncx.dump_graphviz_scc_constraints(tcx, &mut file)?;
};
}

Expand Down
17 changes: 10 additions & 7 deletions compiler/rustc_borrowck/src/polonius/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ fn emit_polonius_dump<'tcx>(
writeln!(out, "<div>")?;
writeln!(out, "NLL regions")?;
writeln!(out, "<pre class='mermaid'>")?;
emit_mermaid_nll_regions(regioncx, out)?;
emit_mermaid_nll_regions(tcx, regioncx, out)?;
writeln!(out, "</pre>")?;
writeln!(out, "</div>")?;

// Section 5: mermaid visualization of the NLL SCC graph.
writeln!(out, "<div>")?;
writeln!(out, "NLL SCCs")?;
writeln!(out, "<pre class='mermaid'>")?;
emit_mermaid_nll_sccs(regioncx, out)?;
emit_mermaid_nll_sccs(tcx, regioncx, out)?;
writeln!(out, "</pre>")?;
writeln!(out, "</div>")?;

Expand Down Expand Up @@ -306,9 +306,10 @@ fn emit_mermaid_cfg(body: &Body<'_>, out: &mut dyn io::Write) -> io::Result<()>
}

/// Emits a region's label: index, universe, external name.
fn render_region(
fn render_region<'tcx>(
tcx: TyCtxt<'tcx>,
region: RegionVid,
regioncx: &RegionInferenceContext<'_>,
regioncx: &RegionInferenceContext<'tcx>,
out: &mut dyn io::Write,
) -> io::Result<()> {
let def = regioncx.region_definition(region);
Expand All @@ -318,7 +319,7 @@ fn render_region(
if !universe.is_root() {
write!(out, "/{universe:?}")?;
}
if let Some(name) = def.external_name.and_then(|e| e.get_name()) {
if let Some(name) = def.external_name.and_then(|e| e.get_name(tcx)) {
write!(out, " ({name})")?;
}
Ok(())
Expand All @@ -327,6 +328,7 @@ fn render_region(
/// Emits a mermaid flowchart of the NLL regions and the outlives constraints between them, similar
/// to the graphviz version.
fn emit_mermaid_nll_regions<'tcx>(
tcx: TyCtxt<'tcx>,
regioncx: &RegionInferenceContext<'tcx>,
out: &mut dyn io::Write,
) -> io::Result<()> {
Expand All @@ -336,7 +338,7 @@ fn emit_mermaid_nll_regions<'tcx>(
// Emit the region nodes.
for region in regioncx.definitions.indices() {
write!(out, "{}[\"", region.as_usize())?;
render_region(region, regioncx, out)?;
render_region(tcx, region, regioncx, out)?;
writeln!(out, "\"]")?;
}

Expand Down Expand Up @@ -378,6 +380,7 @@ fn emit_mermaid_nll_regions<'tcx>(
/// Emits a mermaid flowchart of the NLL SCCs and the outlives constraints between them, similar
/// to the graphviz version.
fn emit_mermaid_nll_sccs<'tcx>(
tcx: TyCtxt<'tcx>,
regioncx: &RegionInferenceContext<'tcx>,
out: &mut dyn io::Write,
) -> io::Result<()> {
Expand All @@ -395,7 +398,7 @@ fn emit_mermaid_nll_sccs<'tcx>(
// The node label: the regions contained in the SCC.
write!(out, "{scc}[\"SCC({scc}) = {{", scc = scc.as_usize())?;
for (idx, &region) in regions.iter().enumerate() {
render_region(region, regioncx, out)?;
render_region(tcx, region, regioncx, out)?;
if idx < regions.len() - 1 {
write!(out, ",")?;
}
Expand Down
34 changes: 25 additions & 9 deletions compiler/rustc_borrowck/src/region_infer/graphviz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ fn render_universe(u: UniverseIndex) -> String {
format!("/{:?}", u)
}

fn render_region_vid(rvid: RegionVid, regioncx: &RegionInferenceContext<'_>) -> String {
fn render_region_vid<'tcx>(
tcx: TyCtxt<'tcx>,
rvid: RegionVid,
regioncx: &RegionInferenceContext<'tcx>,
) -> String {
let universe_str = render_universe(regioncx.region_definition(rvid).universe);

let external_name_str = if let Some(external_name) =
regioncx.region_definition(rvid).external_name.and_then(|e| e.get_name())
regioncx.region_definition(rvid).external_name.and_then(|e| e.get_name(tcx))
{
format!(" ({external_name})")
} else {
Expand All @@ -42,12 +46,20 @@ fn render_region_vid(rvid: RegionVid, regioncx: &RegionInferenceContext<'_>) ->

impl<'tcx> RegionInferenceContext<'tcx> {
/// Write out the region constraint graph.
pub(crate) fn dump_graphviz_raw_constraints(&self, mut w: &mut dyn Write) -> io::Result<()> {
dot::render(&RawConstraints { regioncx: self }, &mut w)
pub(crate) fn dump_graphviz_raw_constraints(
&self,
tcx: TyCtxt<'tcx>,
mut w: &mut dyn Write,
) -> io::Result<()> {
dot::render(&RawConstraints { tcx, regioncx: self }, &mut w)
}

/// Write out the region constraint SCC graph.
pub(crate) fn dump_graphviz_scc_constraints(&self, mut w: &mut dyn Write) -> io::Result<()> {
pub(crate) fn dump_graphviz_scc_constraints(
&self,
tcx: TyCtxt<'tcx>,
mut w: &mut dyn Write,
) -> io::Result<()> {
let mut nodes_per_scc: IndexVec<ConstraintSccIndex, _> =
self.constraint_sccs.all_sccs().map(|_| Vec::new()).collect();

Expand All @@ -56,11 +68,12 @@ impl<'tcx> RegionInferenceContext<'tcx> {
nodes_per_scc[scc].push(region);
}

dot::render(&SccConstraints { regioncx: self, nodes_per_scc }, &mut w)
dot::render(&SccConstraints { tcx, regioncx: self, nodes_per_scc }, &mut w)
}
}

struct RawConstraints<'a, 'tcx> {
tcx: TyCtxt<'tcx>,
regioncx: &'a RegionInferenceContext<'tcx>,
}

Expand All @@ -78,7 +91,7 @@ impl<'a, 'this, 'tcx> dot::Labeller<'this> for RawConstraints<'a, 'tcx> {
Some(dot::LabelText::LabelStr(Cow::Borrowed("box")))
}
fn node_label(&'this self, n: &RegionVid) -> dot::LabelText<'this> {
dot::LabelText::LabelStr(render_region_vid(*n, self.regioncx).into())
dot::LabelText::LabelStr(render_region_vid(self.tcx, *n, self.regioncx).into())
}
fn edge_label(&'this self, e: &OutlivesConstraint<'tcx>) -> dot::LabelText<'this> {
dot::LabelText::LabelStr(render_outlives_constraint(e).into())
Expand Down Expand Up @@ -110,6 +123,7 @@ impl<'a, 'this, 'tcx> dot::GraphWalk<'this> for RawConstraints<'a, 'tcx> {
}

struct SccConstraints<'a, 'tcx> {
tcx: TyCtxt<'tcx>,
regioncx: &'a RegionInferenceContext<'tcx>,
nodes_per_scc: IndexVec<ConstraintSccIndex, Vec<RegionVid>>,
}
Expand All @@ -128,8 +142,10 @@ impl<'a, 'this, 'tcx> dot::Labeller<'this> for SccConstraints<'a, 'tcx> {
Some(dot::LabelText::LabelStr(Cow::Borrowed("box")))
}
fn node_label(&'this self, n: &ConstraintSccIndex) -> dot::LabelText<'this> {
let nodes_str =
self.nodes_per_scc[*n].iter().map(|n| render_region_vid(*n, self.regioncx)).join(", ");
let nodes_str = self.nodes_per_scc[*n]
.iter()
.map(|n| render_region_vid(self.tcx, *n, self.regioncx))
.join(", ");
dot::LabelText::LabelStr(format!("SCC({n}) = {{{nodes_str}}}", n = n.as_usize()).into())
}
}
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_borrowck/src/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,8 +800,11 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
let region_ctxt_fn = || {
let reg_info = match br.kind {
ty::BoundRegionKind::Anon => sym::anon,
ty::BoundRegionKind::Named(_, name) => name,
ty::BoundRegionKind::Named(def_id) => tcx.item_name(def_id),
ty::BoundRegionKind::ClosureEnv => sym::env,
ty::BoundRegionKind::NamedAnon(_) => {
bug!("only used for pretty printing")
}
};

RegionCtxt::LateBound(reg_info)
Expand Down
8 changes: 5 additions & 3 deletions compiler/rustc_borrowck/src/type_check/relate_tys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin};
use rustc_infer::traits::Obligation;
use rustc_infer::traits::solve::Goal;
use rustc_middle::mir::ConstraintCategory;
use rustc_middle::span_bug;
use rustc_middle::traits::ObligationCause;
use rustc_middle::traits::query::NoSolution;
use rustc_middle::ty::relate::combine::{super_combine_consts, super_combine_tys};
use rustc_middle::ty::{self, FnMutDelegate, Ty, TyCtxt, TypeVisitableExt};
use rustc_middle::{bug, span_bug};
use rustc_span::{Span, Symbol, sym};
use tracing::{debug, instrument};

Expand Down Expand Up @@ -215,7 +215,8 @@ impl<'a, 'b, 'tcx> NllTypeRelating<'a, 'b, 'tcx> {
if let Some(ex_reg_var) = reg_map.get(&br) {
*ex_reg_var
} else {
let ex_reg_var = self.next_existential_region_var(true, br.kind.get_name());
let ex_reg_var =
self.next_existential_region_var(true, br.kind.get_name(infcx.infcx.tcx));
debug!(?ex_reg_var);
reg_map.insert(br, ex_reg_var);

Expand Down Expand Up @@ -263,8 +264,9 @@ impl<'a, 'b, 'tcx> NllTypeRelating<'a, 'b, 'tcx> {

let reg_info = match placeholder.bound.kind {
ty::BoundRegionKind::Anon => sym::anon,
ty::BoundRegionKind::Named(_, name) => name,
ty::BoundRegionKind::Named(def_id) => self.type_checker.tcx().item_name(def_id),
ty::BoundRegionKind::ClosureEnv => sym::env,
ty::BoundRegionKind::NamedAnon(_) => bug!("only used for pretty printing"),
};

if cfg!(debug_assertions) {
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_borrowck/src/universal_regions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
|r| {
debug!(?r);
let region_vid = {
let name = r.get_name_or_anon();
let name = r.get_name_or_anon(self.infcx.tcx);
self.infcx.next_nll_region_var(FR, || RegionCtxt::LateBound(name))
};

Expand All @@ -519,7 +519,7 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
let kind = ty::LateParamRegionKind::from_bound(ty::BoundVar::from_usize(idx), kind);
let r = ty::Region::new_late_param(self.infcx.tcx, self.mir_def.to_def_id(), kind);
let region_vid = {
let name = r.get_name_or_anon();
let name = r.get_name_or_anon(self.infcx.tcx);
self.infcx.next_nll_region_var(FR, || RegionCtxt::LateBound(name))
};

Expand Down Expand Up @@ -857,7 +857,7 @@ impl<'tcx> BorrowckInferCtxt<'tcx> {
T: TypeFoldable<TyCtxt<'tcx>>,
{
fold_regions(self.infcx.tcx, value, |region, _depth| {
let name = region.get_name_or_anon();
let name = region.get_name_or_anon(self.infcx.tcx);
debug!(?region, ?name);

self.next_nll_region_var(origin, || RegionCtxt::Free(name))
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir_analysis/src/check/compare_impl_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2259,7 +2259,7 @@ fn param_env_with_gat_bounds<'tcx>(
let normalize_impl_ty_args = ty::GenericArgs::identity_for_item(tcx, container_id)
.extend_to(tcx, impl_ty.def_id, |param, _| match param.kind {
GenericParamDefKind::Type { .. } => {
let kind = ty::BoundTyKind::Param(param.def_id, param.name);
let kind = ty::BoundTyKind::Param(param.def_id);
let bound_var = ty::BoundVariableKind::Ty(kind);
bound_vars.push(bound_var);
Ty::new_bound(
Expand All @@ -2270,7 +2270,7 @@ fn param_env_with_gat_bounds<'tcx>(
.into()
}
GenericParamDefKind::Lifetime => {
let kind = ty::BoundRegionKind::Named(param.def_id, param.name);
let kind = ty::BoundRegionKind::Named(param.def_id);
let bound_var = ty::BoundVariableKind::Region(kind);
bound_vars.push(bound_var);
ty::Region::new_bound(
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2408,7 +2408,7 @@ fn lint_redundant_lifetimes<'tcx>(
lifetimes.push(ty::Region::new_late_param(tcx, owner_id.to_def_id(), kind));
}
}
lifetimes.retain(|candidate| candidate.has_name());
lifetimes.retain(|candidate| candidate.has_name(tcx));

// Keep track of lifetimes which have already been replaced with other lifetimes.
// This makes sure that if `'a = 'b = 'c`, we don't say `'c` should be replaced by
Expand Down
6 changes: 1 addition & 5 deletions compiler/rustc_hir_analysis/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,11 +638,7 @@ fn get_new_lifetime_name<'tcx>(
.collect_referenced_late_bound_regions(poly_trait_ref)
.into_iter()
.filter_map(|lt| {
if let ty::BoundRegionKind::Named(_, name) = lt {
Some(name.as_str().to_string())
} else {
None
}
if let Some(name) = lt.get_name(tcx) { Some(name.as_str().to_string()) } else { None }
})
.chain(generics.params.iter().filter_map(|param| {
if let hir::GenericParamKind::Lifetime { .. } = &param.kind {
Expand Down
Loading
Loading