diff options
| author | bors <bors@rust-lang.org> | 2025-07-05 06:29:56 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2025-07-05 06:29:56 +0000 |
| commit | f0b67dd97d74610ee4185cf01c775a563c2017a2 (patch) | |
| tree | c34f0fd7ed315275affdd9051aca537205eada80 /compiler/rustc_hir_analysis/src | |
| parent | 733b47ea4b1b86216f14ef56e49440c33933f230 (diff) | |
| parent | dc8cac8e8d1e7e8f9535bd53c4aa7d70cce617fb (diff) | |
| download | rust-f0b67dd97d74610ee4185cf01c775a563c2017a2.tar.gz rust-f0b67dd97d74610ee4185cf01c775a563c2017a2.zip | |
Auto merge of #139598 - compiler-errors:no-bound-var-symbol, r=WaffleLapkin
Remove `Symbol` from `Named` variant of `BoundRegionKind`/`LateParamRegionKind` The `Symbol` is redundant, since we already store a `DefId` in the region variant. Instead, load the name via `item_name` when needed (which is almost always on the diagnostic path). This introduces a `BoundRegionKind::NamedAnon` which is used for giving anonymous bound regions names, but which should only be used during pretty printing and error reporting.
Diffstat (limited to 'compiler/rustc_hir_analysis/src')
7 files changed, 34 insertions, 52 deletions
diff --git a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs index abbe497858b..87db80f2423 100644 --- a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs +++ b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs @@ -1239,7 +1239,7 @@ fn check_region_late_boundedness<'tcx>( .unwrap_region_constraints() .opportunistic_resolve_var(tcx, vid) && let ty::ReLateParam(ty::LateParamRegion { - kind: ty::LateParamRegionKind::Named(trait_param_def_id, _), + kind: ty::LateParamRegionKind::Named(trait_param_def_id), .. }) = r.kind() && let ty::ReEarlyParam(ebr) = id_arg.expect_region().kind() @@ -1264,7 +1264,7 @@ fn check_region_late_boundedness<'tcx>( .unwrap_region_constraints() .opportunistic_resolve_var(tcx, vid) && let ty::ReLateParam(ty::LateParamRegion { - kind: ty::LateParamRegionKind::Named(impl_param_def_id, _), + kind: ty::LateParamRegionKind::Named(impl_param_def_id), .. }) = r.kind() && let ty::ReEarlyParam(ebr) = id_arg.expect_region().kind() @@ -2468,7 +2468,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( @@ -2479,7 +2479,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( diff --git a/compiler/rustc_hir_analysis/src/check/wfcheck.rs b/compiler/rustc_hir_analysis/src/check/wfcheck.rs index 4934136bc7a..0a3e018b79a 100644 --- a/compiler/rustc_hir_analysis/src/check/wfcheck.rs +++ b/compiler/rustc_hir_analysis/src/check/wfcheck.rs @@ -2338,7 +2338,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.is_named(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 diff --git a/compiler/rustc_hir_analysis/src/coherence/builtin.rs b/compiler/rustc_hir_analysis/src/coherence/builtin.rs index 65bc441a473..8356a0af63c 100644 --- a/compiler/rustc_hir_analysis/src/coherence/builtin.rs +++ b/compiler/rustc_hir_analysis/src/coherence/builtin.rs @@ -654,7 +654,7 @@ fn infringing_fields_error<'tcx>( .or_default() .push(origin.span()); if let ty::RegionKind::ReEarlyParam(ebr) = b.kind() - && ebr.has_name() + && ebr.is_named() { bounds.push((b.to_string(), a.to_string(), None)); } diff --git a/compiler/rustc_hir_analysis/src/collect.rs b/compiler/rustc_hir_analysis/src/collect.rs index 271104c20c6..25064c327d0 100644 --- a/compiler/rustc_hir_analysis/src/collect.rs +++ b/compiler/rustc_hir_analysis/src/collect.rs @@ -578,13 +578,7 @@ fn get_new_lifetime_name<'tcx>( let existing_lifetimes = 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 - } - }) + .filter_map(|lt| lt.get_name(tcx).map(|name| name.as_str().to_string())) .chain(generics.params.iter().filter_map(|param| { if let hir::GenericParamKind::Lifetime { .. } = ¶m.kind { Some(param.name.ident().as_str().to_string()) diff --git a/compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs b/compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs index 0e775d374ab..a0d1273eb85 100644 --- a/compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs +++ b/compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs @@ -279,19 +279,13 @@ fn resolve_bound_vars(tcx: TyCtxt<'_>, local_def_id: hir::OwnerId) -> ResolveBou rbv } -fn late_arg_as_bound_arg<'tcx>( - tcx: TyCtxt<'tcx>, - param: &GenericParam<'tcx>, -) -> ty::BoundVariableKind { +fn late_arg_as_bound_arg<'tcx>(param: &GenericParam<'tcx>) -> ty::BoundVariableKind { let def_id = param.def_id.to_def_id(); - let name = tcx.item_name(def_id); match param.kind { GenericParamKind::Lifetime { .. } => { - ty::BoundVariableKind::Region(ty::BoundRegionKind::Named(def_id, name)) - } - GenericParamKind::Type { .. } => { - ty::BoundVariableKind::Ty(ty::BoundTyKind::Param(def_id, name)) + ty::BoundVariableKind::Region(ty::BoundRegionKind::Named(def_id)) } + GenericParamKind::Type { .. } => ty::BoundVariableKind::Ty(ty::BoundTyKind::Param(def_id)), GenericParamKind::Const { .. } => ty::BoundVariableKind::Const, } } @@ -302,10 +296,10 @@ fn late_arg_as_bound_arg<'tcx>( fn generic_param_def_as_bound_arg(param: &ty::GenericParamDef) -> ty::BoundVariableKind { match param.kind { ty::GenericParamDefKind::Lifetime => { - ty::BoundVariableKind::Region(ty::BoundRegionKind::Named(param.def_id, param.name)) + ty::BoundVariableKind::Region(ty::BoundRegionKind::Named(param.def_id)) } ty::GenericParamDefKind::Type { .. } => { - ty::BoundVariableKind::Ty(ty::BoundTyKind::Param(param.def_id, param.name)) + ty::BoundVariableKind::Ty(ty::BoundTyKind::Param(param.def_id)) } ty::GenericParamDefKind::Const { .. } => ty::BoundVariableKind::Const, } @@ -386,7 +380,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> { trait_ref.bound_generic_params.iter().enumerate().map(|(late_bound_idx, param)| { let arg = ResolvedArg::late(initial_bound_vars + late_bound_idx as u32, param); bound_vars.insert(param.def_id, arg); - late_arg_as_bound_arg(self.tcx, param) + late_arg_as_bound_arg(param) }); binders.extend(binders_iter); @@ -485,7 +479,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> { .map(|(late_bound_idx, param)| { ( (param.def_id, ResolvedArg::late(late_bound_idx as u32, param)), - late_arg_as_bound_arg(self.tcx, param), + late_arg_as_bound_arg(param), ) }) .unzip(); @@ -718,7 +712,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> { .map(|(late_bound_idx, param)| { ( (param.def_id, ResolvedArg::late(late_bound_idx as u32, param)), - late_arg_as_bound_arg(self.tcx, param), + late_arg_as_bound_arg(param), ) }) .unzip(); @@ -748,7 +742,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> { .map(|(late_bound_idx, param)| { ( (param.def_id, ResolvedArg::late(late_bound_idx as u32, param)), - late_arg_as_bound_arg(self.tcx, param), + late_arg_as_bound_arg(param), ) }) .unzip(); @@ -957,7 +951,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> { .map(|(late_bound_idx, param)| { ( (param.def_id, ResolvedArg::late(late_bound_idx as u32, param)), - late_arg_as_bound_arg(self.tcx, param), + late_arg_as_bound_arg(param), ) }) .unzip(); @@ -1171,7 +1165,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> { matches!(param.kind, GenericParamKind::Lifetime { .. }) && self.tcx.is_late_bound(param.hir_id) }) - .map(|param| late_arg_as_bound_arg(self.tcx, param)) + .map(|param| late_arg_as_bound_arg(param)) .collect(); self.record_late_bound_vars(hir_id, binders); let scope = Scope::Binder { diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs index d17986d45d2..4784cfb5235 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs @@ -12,7 +12,7 @@ use rustc_middle::ty::{ self as ty, IsSuggestable, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor, Upcast, }; -use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol, kw, sym}; +use rustc_span::{ErrorGuaranteed, Ident, Span, kw, sym}; use rustc_trait_selection::traits; use smallvec::SmallVec; use tracing::{debug, instrument}; @@ -888,7 +888,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { ty::INNERMOST, ty::BoundRegion { var: ty::BoundVar::from_usize(num_bound_vars), - kind: ty::BoundRegionKind::Named(param.def_id, param.name), + kind: ty::BoundRegionKind::Named(param.def_id), }, ) .into(), @@ -1006,12 +1006,12 @@ fn check_assoc_const_binding_type<'tcx>( ty_note, })); } - for (var_def_id, var_name) in collector.vars { + for var_def_id in collector.vars { guar.get_or_insert(cx.dcx().emit_err( crate::errors::EscapingBoundVarInTyOfAssocConstBinding { span: assoc_const.span, assoc_const, - var_name, + var_name: cx.tcx().item_name(var_def_id), var_def_kind: tcx.def_descr(var_def_id), var_defined_here_label: tcx.def_ident_span(var_def_id).unwrap(), ty_note, @@ -1026,7 +1026,7 @@ fn check_assoc_const_binding_type<'tcx>( struct GenericParamAndBoundVarCollector<'a, 'tcx> { cx: &'a dyn HirTyLowerer<'tcx>, params: FxIndexSet<u32>, - vars: FxIndexSet<(DefId, Symbol)>, + vars: FxIndexSet<DefId>, depth: ty::DebruijnIndex, } @@ -1050,7 +1050,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for GenericParamAndBoundVarCollector<'_, 't } ty::Bound(db, bt) if *db >= self.depth => { self.vars.insert(match bt.kind { - ty::BoundTyKind::Param(def_id, name) => (def_id, name), + ty::BoundTyKind::Param(def_id) => def_id, ty::BoundTyKind::Anon => { let reported = self .cx @@ -1073,7 +1073,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for GenericParamAndBoundVarCollector<'_, 't } ty::ReBound(db, br) if db >= self.depth => { self.vars.insert(match br.kind { - ty::BoundRegionKind::Named(def_id, name) => (def_id, name), + ty::BoundRegionKind::Named(def_id) => def_id, ty::BoundRegionKind::Anon | ty::BoundRegionKind::ClosureEnv => { let guar = self .cx @@ -1081,6 +1081,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for GenericParamAndBoundVarCollector<'_, 't .delayed_bug(format!("unexpected bound region kind: {:?}", br.kind)); return ControlFlow::Break(guar); } + ty::BoundRegionKind::NamedAnon(_) => bug!("only used for pretty printing"), }); } _ => {} diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs index e13daabeb50..434375060df 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs @@ -392,16 +392,14 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { #[instrument(level = "debug", skip(self), ret)] pub fn lower_resolved_lifetime(&self, resolved: rbv::ResolvedArg) -> ty::Region<'tcx> { let tcx = self.tcx(); - let lifetime_name = |def_id| tcx.hir_name(tcx.local_def_id_to_hir_id(def_id)); match resolved { rbv::ResolvedArg::StaticLifetime => tcx.lifetimes.re_static, rbv::ResolvedArg::LateBound(debruijn, index, def_id) => { - let name = lifetime_name(def_id); let br = ty::BoundRegion { var: ty::BoundVar::from_u32(index), - kind: ty::BoundRegionKind::Named(def_id.to_def_id(), name), + kind: ty::BoundRegionKind::Named(def_id.to_def_id()), }; ty::Region::new_bound(tcx, debruijn, br) } @@ -415,11 +413,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { } rbv::ResolvedArg::Free(scope, id) => { - let name = lifetime_name(id); ty::Region::new_late_param( tcx, scope.to_def_id(), - ty::LateParamRegionKind::Named(id.to_def_id(), name), + ty::LateParamRegionKind::Named(id.to_def_id()), ) // (*) -- not late-bound, won't change @@ -2070,10 +2067,9 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { let tcx = self.tcx(); match tcx.named_bound_var(hir_id) { Some(rbv::ResolvedArg::LateBound(debruijn, index, def_id)) => { - let name = tcx.item_name(def_id.to_def_id()); let br = ty::BoundTy { var: ty::BoundVar::from_u32(index), - kind: ty::BoundTyKind::Param(def_id.to_def_id(), name), + kind: ty::BoundTyKind::Param(def_id.to_def_id()), }; Ty::new_bound(tcx, debruijn, br) } @@ -2749,18 +2745,15 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { generate_err: impl Fn(&str) -> Diag<'cx>, ) { for br in referenced_regions.difference(&constrained_regions) { - let br_name = match *br { - ty::BoundRegionKind::Named(_, kw::UnderscoreLifetime) - | ty::BoundRegionKind::Anon - | ty::BoundRegionKind::ClosureEnv => "an anonymous lifetime".to_string(), - ty::BoundRegionKind::Named(_, name) => format!("lifetime `{name}`"), + let br_name = if let Some(name) = br.get_name(self.tcx()) { + format!("lifetime `{name}`") + } else { + "an anonymous lifetime".to_string() }; let mut err = generate_err(&br_name); - if let ty::BoundRegionKind::Named(_, kw::UnderscoreLifetime) - | ty::BoundRegionKind::Anon = *br - { + if !br.is_named(self.tcx()) { // The only way for an anonymous lifetime to wind up // in the return type but **also** be unconstrained is // if it only appears in "associated types" in the |
