diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index ff29d15f1b5..66003cada4a 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -830,8 +830,10 @@ fn lifetime_res_to_generic_param( ), }; let hir_id = self.lower_node_id(node_id); + let def_id = self.local_def_id(node_id); Some(hir::GenericParam { hir_id, + def_id, name, span: self.lower_span(ident.span), pure_wrt_drop: false, @@ -1521,6 +1523,7 @@ fn lower_opaque_impl_trait( hir::GenericParam { hir_id, + def_id: lctx.local_def_id(new_node_id), name, span: lifetime.ident.span, pure_wrt_drop: false, @@ -1978,6 +1981,7 @@ fn lower_async_fn_ret_ty( hir::GenericParam { hir_id, + def_id: this.local_def_id(new_node_id), name, span: lifetime.ident.span, pure_wrt_drop: false, @@ -2176,6 +2180,7 @@ fn lower_generic_param(&mut self, param: &GenericParam) -> hir::GenericParam<'hi self.lower_attrs(hir_id, ¶m.attrs); hir::GenericParam { hir_id, + def_id: self.local_def_id(param.id), name, span: self.lower_span(param.span()), pure_wrt_drop: self.tcx.sess.contains_name(¶m.attrs, sym::may_dangle), @@ -2280,6 +2285,7 @@ fn lower_generic_and_bounds( // Set the name to `impl Bound1 + Bound2`. let param = hir::GenericParam { hir_id: self.lower_node_id(node_id), + def_id, name: ParamName::Plain(self.lower_ident(ident)), pure_wrt_drop: false, span: self.lower_span(span), diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 1be03552c88..4493bd3b48e 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -487,6 +487,7 @@ pub enum GenericParamKind<'hir> { #[derive(Debug, HashStable_Generic)] pub struct GenericParam<'hir> { pub hir_id: HirId, + pub def_id: LocalDefId, pub name: ParamName, pub span: Span, pub pure_wrt_drop: bool, @@ -1628,7 +1629,7 @@ pub fn hir_id(&self) -> HirId { /// explicit discriminant values for enum variants. /// /// You can check if this anon const is a default in a const param -/// `const N: usize = { ... }` with `tcx.hir().opt_const_param_default_param_hir_id(..)` +/// `const N: usize = { ... }` with `tcx.hir().opt_const_param_default_param_def_id(..)` #[derive(Copy, Clone, PartialEq, Eq, Encodable, Debug, HashStable_Generic)] pub struct AnonConst { pub hir_id: HirId, diff --git a/compiler/rustc_hir_analysis/src/astconv/mod.rs b/compiler/rustc_hir_analysis/src/astconv/mod.rs index 4518cf30acd..a0ac43aa9f0 100644 --- a/compiler/rustc_hir_analysis/src/astconv/mod.rs +++ b/compiler/rustc_hir_analysis/src/astconv/mod.rs @@ -856,7 +856,7 @@ pub(crate) fn add_implicitly_sized<'hir>( &self, bounds: &mut Bounds<'hir>, ast_bounds: &'hir [hir::GenericBound<'hir>], - self_ty_where_predicates: Option<(hir::HirId, &'hir [hir::WherePredicate<'hir>])>, + self_ty_where_predicates: Option<(LocalDefId, &'hir [hir::WherePredicate<'hir>])>, span: Span, ) { let tcx = self.tcx(); @@ -876,10 +876,9 @@ pub(crate) fn add_implicitly_sized<'hir>( }; search_bounds(ast_bounds); if let Some((self_ty, where_clause)) = self_ty_where_predicates { - let self_ty_def_id = tcx.hir().local_def_id(self_ty).to_def_id(); for clause in where_clause { if let hir::WherePredicate::BoundPredicate(pred) = clause { - if pred.is_param_bound(self_ty_def_id) { + if pred.is_param_bound(self_ty.to_def_id()) { search_bounds(pred.bounds); } } diff --git a/compiler/rustc_hir_analysis/src/check/wfcheck.rs b/compiler/rustc_hir_analysis/src/check/wfcheck.rs index 837ff0bdf3e..efb34e4ff65 100644 --- a/compiler/rustc_hir_analysis/src/check/wfcheck.rs +++ b/compiler/rustc_hir_analysis/src/check/wfcheck.rs @@ -853,7 +853,7 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) { // Const parameters are well formed if their type is structural match. hir::GenericParamKind::Const { ty: hir_ty, default: _ } => { - let ty = tcx.type_of(tcx.hir().local_def_id(param.hir_id)); + let ty = tcx.type_of(param.def_id); if tcx.features().adt_const_params { if let Some(non_structural_match_ty) = diff --git a/compiler/rustc_hir_analysis/src/collect.rs b/compiler/rustc_hir_analysis/src/collect.rs index f122e99946f..c269855af08 100644 --- a/compiler/rustc_hir_analysis/src/collect.rs +++ b/compiler/rustc_hir_analysis/src/collect.rs @@ -291,18 +291,16 @@ fn visit_generics(&mut self, generics: &'tcx hir::Generics<'tcx>) { match param.kind { hir::GenericParamKind::Lifetime { .. } => {} hir::GenericParamKind::Type { default: Some(_), .. } => { - let def_id = self.tcx.hir().local_def_id(param.hir_id); - self.tcx.ensure().type_of(def_id); + self.tcx.ensure().type_of(param.def_id); } hir::GenericParamKind::Type { .. } => {} hir::GenericParamKind::Const { default, .. } => { - let def_id = self.tcx.hir().local_def_id(param.hir_id); - self.tcx.ensure().type_of(def_id); + self.tcx.ensure().type_of(param.def_id); if let Some(default) = default { let default_def_id = self.tcx.hir().local_def_id(default.hir_id); // need to store default and type of default self.tcx.ensure().type_of(default_def_id); - self.tcx.ensure().const_param_default(def_id); + self.tcx.ensure().const_param_default(param.def_id); } } } diff --git a/compiler/rustc_hir_analysis/src/collect/generics_of.rs b/compiler/rustc_hir_analysis/src/collect/generics_of.rs index c7777a94689..b369a1eb109 100644 --- a/compiler/rustc_hir_analysis/src/collect/generics_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/generics_of.rs @@ -51,7 +51,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics { // of a const parameter type, e.g. `struct Foo` is not allowed. None } else if tcx.lazy_normalization() { - if let Some(param_id) = tcx.hir().opt_const_param_default_param_hir_id(hir_id) { + if let Some(param_id) = tcx.hir().opt_const_param_default_param_def_id(hir_id) { // If the def_id we are calling generics_of on is an anon ct default i.e: // // struct Foo; @@ -77,8 +77,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics { // This has some implications for how we get the predicates available to the anon const // see `explicit_predicates_of` for more information on this let generics = tcx.generics_of(parent_def_id.to_def_id()); - let param_def = tcx.hir().local_def_id(param_id).to_def_id(); - let param_def_idx = generics.param_def_id_to_index[¶m_def]; + let param_def_idx = generics.param_def_id_to_index[¶m_id.to_def_id()]; // In the above example this would be .params[..N#0] let params = generics.params[..param_def_idx as usize].to_owned(); let param_def_id_to_index = @@ -241,7 +240,7 @@ enum Defaults { params.extend(early_lifetimes.enumerate().map(|(i, param)| ty::GenericParamDef { name: param.name.ident().name, index: own_start + i as u32, - def_id: tcx.hir().local_def_id(param.hir_id).to_def_id(), + def_id: param.def_id.to_def_id(), pure_wrt_drop: param.pure_wrt_drop, kind: ty::GenericParamDefKind::Lifetime, })); @@ -286,7 +285,7 @@ enum Defaults { Some(ty::GenericParamDef { index: next_index(), name: param.name.ident().name, - def_id: tcx.hir().local_def_id(param.hir_id).to_def_id(), + def_id: param.def_id.to_def_id(), pure_wrt_drop: param.pure_wrt_drop, kind, }) @@ -303,7 +302,7 @@ enum Defaults { Some(ty::GenericParamDef { index: next_index(), name: param.name.ident().name, - def_id: tcx.hir().local_def_id(param.hir_id).to_def_id(), + def_id: param.def_id.to_def_id(), pure_wrt_drop: param.pure_wrt_drop, kind: ty::GenericParamDefKind::Const { has_default: default.is_some() }, }) diff --git a/compiler/rustc_hir_analysis/src/collect/lifetimes.rs b/compiler/rustc_hir_analysis/src/collect/lifetimes.rs index 6ee7aa9cdac..ce5cde5b883 100644 --- a/compiler/rustc_hir_analysis/src/collect/lifetimes.rs +++ b/compiler/rustc_hir_analysis/src/collect/lifetimes.rs @@ -15,7 +15,6 @@ use rustc_hir::intravisit::{self, Visitor}; use rustc_hir::{GenericArg, GenericParam, GenericParamKind, HirIdMap, LifetimeName, Node}; use rustc_middle::bug; -use rustc_middle::hir::map::Map; use rustc_middle::hir::nested_filter; use rustc_middle::middle::resolve_lifetime::*; use rustc_middle::ty::{self, DefIdTree, TyCtxt, TypeSuperVisitable, TypeVisitor}; @@ -25,9 +24,9 @@ use std::fmt; trait RegionExt { - fn early(hir_map: Map<'_>, param: &GenericParam<'_>) -> (LocalDefId, Region); + fn early(param: &GenericParam<'_>) -> (LocalDefId, Region); - fn late(index: u32, hir_map: Map<'_>, param: &GenericParam<'_>) -> (LocalDefId, Region); + fn late(index: u32, param: &GenericParam<'_>) -> (LocalDefId, Region); fn id(&self) -> Option; @@ -35,20 +34,18 @@ trait RegionExt { } impl RegionExt for Region { - fn early(hir_map: Map<'_>, param: &GenericParam<'_>) -> (LocalDefId, Region) { - let def_id = hir_map.local_def_id(param.hir_id); - debug!("Region::early: def_id={:?}", def_id); - (def_id, Region::EarlyBound(def_id.to_def_id())) + fn early(param: &GenericParam<'_>) -> (LocalDefId, Region) { + debug!("Region::early: def_id={:?}", param.def_id); + (param.def_id, Region::EarlyBound(param.def_id.to_def_id())) } - fn late(idx: u32, hir_map: Map<'_>, param: &GenericParam<'_>) -> (LocalDefId, Region) { + fn late(idx: u32, param: &GenericParam<'_>) -> (LocalDefId, Region) { let depth = ty::INNERMOST; - let def_id = hir_map.local_def_id(param.hir_id); debug!( "Region::late: idx={:?}, param={:?} depth={:?} def_id={:?}", - idx, param, depth, def_id, + idx, param, depth, param.def_id, ); - (def_id, Region::LateBound(depth, idx, def_id.to_def_id())) + (param.def_id, Region::LateBound(depth, idx, param.def_id.to_def_id())) } fn id(&self) -> Option { @@ -395,7 +392,7 @@ fn visit_ty(&mut self, t: &'v hir::Ty<'v>) { .filter(|param| matches!(param.kind, GenericParamKind::Lifetime { .. })) .enumerate() .map(|(late_bound_idx, param)| { - let pair = Region::late(late_bound_idx as u32, self.tcx.hir(), param); + let pair = Region::late(late_bound_idx as u32, param); let r = late_region_as_bound_region(self.tcx, &pair.1); (pair, r) }) @@ -492,7 +489,7 @@ fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) { for param in generics.params { match param.kind { GenericParamKind::Lifetime { .. } => { - let (def_id, reg) = Region::early(self.tcx.hir(), ¶m); + let (def_id, reg) = Region::early(¶m); lifetimes.insert(def_id, reg); } GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => {} @@ -523,9 +520,7 @@ fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) { .params .iter() .filter_map(|param| match param.kind { - GenericParamKind::Lifetime { .. } => { - Some(Region::early(self.tcx.hir(), param)) - } + GenericParamKind::Lifetime { .. } => Some(Region::early(param)), GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => None, }) .collect(); @@ -573,7 +568,7 @@ fn visit_ty(&mut self, ty: &'tcx hir::Ty<'tcx>) { .filter(|param| matches!(param.kind, GenericParamKind::Lifetime { .. })) .enumerate() .map(|(late_bound_idx, param)| { - let pair = Region::late(late_bound_idx as u32, self.tcx.hir(), param); + let pair = Region::late(late_bound_idx as u32, param); let r = late_region_as_bound_region(self.tcx, &pair.1); (pair, r) }) @@ -731,9 +726,7 @@ fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) { .params .iter() .filter_map(|param| match param.kind { - GenericParamKind::Lifetime { .. } => { - Some(Region::early(self.tcx.hir(), param)) - } + GenericParamKind::Lifetime { .. } => Some(Region::early(param)), GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => None, }) .collect(); @@ -779,9 +772,7 @@ fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) { .params .iter() .filter_map(|param| match param.kind { - GenericParamKind::Lifetime { .. } => { - Some(Region::early(self.tcx.hir(), param)) - } + GenericParamKind::Lifetime { .. } => Some(Region::early(param)), GenericParamKind::Const { .. } | GenericParamKind::Type { .. } => None, }) .collect(); @@ -886,7 +877,7 @@ fn visit_generics(&mut self, generics: &'tcx hir::Generics<'tcx>) { }) .enumerate() .map(|(late_bound_idx, param)| { - Region::late(late_bound_idx as u32, this.tcx.hir(), param) + Region::late(late_bound_idx as u32, param) }) .collect(); let binders: Vec<_> = @@ -999,8 +990,7 @@ fn visit_poly_trait_ref(&mut self, trait_ref: &'tcx hir::PolyTraitRef<'tcx>) { .filter(|param| matches!(param.kind, GenericParamKind::Lifetime { .. })) .enumerate() .map(|(late_bound_idx, param)| { - let pair = - Region::late(initial_bound_vars + late_bound_idx as u32, self.tcx.hir(), param); + let pair = Region::late(initial_bound_vars + late_bound_idx as u32, param); let r = late_region_as_bound_region(self.tcx, &pair.1); lifetimes.insert(pair.0, pair.1); r @@ -1131,9 +1121,9 @@ fn visit_early_late( if self.tcx.is_late_bound(param.hir_id) { let late_bound_idx = named_late_bound_vars; named_late_bound_vars += 1; - Some(Region::late(late_bound_idx, self.tcx.hir(), param)) + Some(Region::late(late_bound_idx, param)) } else { - Some(Region::early(self.tcx.hir(), param)) + Some(Region::early(param)) } } GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => None, @@ -1149,7 +1139,7 @@ fn visit_early_late( }) .enumerate() .map(|(late_bound_idx, param)| { - let pair = Region::late(late_bound_idx as u32, self.tcx.hir(), param); + let pair = Region::late(late_bound_idx as u32, param); late_region_as_bound_region(self.tcx, &pair.1) }) .collect(); diff --git a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs index 5d1ca1cbd23..5ed3ccdbf1e 100644 --- a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs @@ -199,7 +199,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericP &icx, &mut bounds, &[], - Some((param.hir_id, ast_generics.predicates)), + Some((param.def_id, ast_generics.predicates)), param.span, ); trace!(?bounds); @@ -429,7 +429,7 @@ pub(super) fn explicit_predicates_of<'tcx>( let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); let parent_def_id = tcx.hir().get_parent_item(hir_id); - if tcx.hir().opt_const_param_default_param_hir_id(hir_id).is_some() { + if tcx.hir().opt_const_param_default_param_def_id(hir_id).is_some() { // In `generics_of` we set the generics' parent to be our parent's parent which means that // we lose out on the predicates of our actual parent if we dont return those predicates here. // (See comment in `generics_of` for more information on why the parent shenanigans is necessary) @@ -531,7 +531,7 @@ pub(super) fn super_predicates_that_define_assoc_type( let is_trait_alias = tcx.is_trait_alias(trait_def_id); let superbounds2 = icx.type_parameter_bounds_in_generics( generics, - item.hir_id(), + item.owner_id.def_id, self_param_ty, OnlySelfBounds(!is_trait_alias), assoc_name, @@ -641,7 +641,7 @@ pub(super) fn type_param_predicates( let extra_predicates = extend.into_iter().chain( icx.type_parameter_bounds_in_generics( ast_generics, - param_id, + def_id, ty, OnlySelfBounds(true), Some(assoc_name), @@ -666,13 +666,11 @@ impl<'tcx> ItemCtxt<'tcx> { fn type_parameter_bounds_in_generics( &self, ast_generics: &'tcx hir::Generics<'tcx>, - param_id: hir::HirId, + param_def_id: LocalDefId, ty: Ty<'tcx>, only_self_bounds: OnlySelfBounds, assoc_name: Option, ) -> Vec<(ty::Predicate<'tcx>, Span)> { - let param_def_id = self.tcx.hir().local_def_id(param_id).to_def_id(); - trace!(?param_def_id); ast_generics .predicates .iter() @@ -681,7 +679,7 @@ fn type_parameter_bounds_in_generics( _ => None, }) .flat_map(|bp| { - let bt = if bp.is_param_bound(param_def_id) { + let bt = if bp.is_param_bound(param_def_id.to_def_id()) { Some(ty) } else if !only_self_bounds.0 { Some(self.to_ty(bp.bounded_ty)) diff --git a/compiler/rustc_hir_analysis/src/collect/type_of.rs b/compiler/rustc_hir_analysis/src/collect/type_of.rs index 0bd459a1762..9bd1715ce39 100644 --- a/compiler/rustc_hir_analysis/src/collect/type_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/type_of.rs @@ -514,10 +514,10 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { } Node::GenericParam(&GenericParam { - hir_id: param_hir_id, + def_id: param_def_id, kind: GenericParamKind::Const { default: Some(ct), .. }, .. - }) if ct.hir_id == hir_id => tcx.type_of(tcx.hir().local_def_id(param_hir_id)), + }) if ct.hir_id == hir_id => tcx.type_of(param_def_id), x => tcx.ty_error_with_message( DUMMY_SP, diff --git a/compiler/rustc_hir_analysis/src/outlives/mod.rs b/compiler/rustc_hir_analysis/src/outlives/mod.rs index e50c267659e..ea0c2a20de3 100644 --- a/compiler/rustc_hir_analysis/src/outlives/mod.rs +++ b/compiler/rustc_hir_analysis/src/outlives/mod.rs @@ -22,7 +22,7 @@ fn inferred_outlives_of(tcx: TyCtxt<'_>, item_def_id: DefId) -> &[(ty::Predicate if matches!(tcx.def_kind(item_def_id), hir::def::DefKind::AnonConst) && tcx.lazy_normalization() { - if tcx.hir().opt_const_param_default_param_hir_id(id).is_some() { + if tcx.hir().opt_const_param_default_param_def_id(id).is_some() { // In `generics_of` we set the generics' parent to be our parent's parent which means that // we lose out on the predicates of our actual parent if we dont return those predicates here. // (See comment in `generics_of` for more information on why the parent shenanigans is necessary) diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index ed4b53c4abd..df7731d6036 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -2101,11 +2101,10 @@ fn visit_generics(&mut self, generics: &'tcx hir::Generics<'tcx>) { impl<'a, 'tcx> EncodeContext<'a, 'tcx> { fn encode_info_for_generics(&mut self, generics: &hir::Generics<'tcx>) { for param in generics.params { - let def_id = self.tcx.hir().local_def_id(param.hir_id); match param.kind { hir::GenericParamKind::Lifetime { .. } | hir::GenericParamKind::Type { .. } => {} hir::GenericParamKind::Const { ref default, .. } => { - let def_id = def_id.to_def_id(); + let def_id = param.def_id.to_def_id(); if default.is_some() { record!(self.tables.const_param_default[def_id] <- self.tcx.const_param_default(def_id)) } diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index 7130a2a8809..2581b01193d 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -1086,10 +1086,10 @@ pub fn node_to_string(self, id: HirId) -> String { /// Returns the HirId of `N` in `struct Foo` when /// called with the HirId for the `{ ... }` anon const - pub fn opt_const_param_default_param_hir_id(self, anon_const: HirId) -> Option { + pub fn opt_const_param_default_param_def_id(self, anon_const: HirId) -> Option { match self.get(self.get_parent_node(anon_const)) { Node::GenericParam(GenericParam { - hir_id: param_id, + def_id: param_id, kind: GenericParamKind::Const { .. }, .. }) => Some(*param_id), diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index a4495d2934d..f49efd460f7 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -10,7 +10,7 @@ use crate::ty::fold::{FallibleTypeFolder, TypeFoldable}; use crate::ty::print::{FmtPrinter, Printer}; use crate::ty::visit::{TypeVisitable, TypeVisitor}; -use crate::ty::{self, List, Ty, TyCtxt}; +use crate::ty::{self, DefIdTree, List, Ty, TyCtxt}; use crate::ty::{AdtDef, InstanceDef, ScalarInt, UserTypeAnnotationIndex}; use crate::ty::{GenericArg, InternalSubsts, SubstsRef}; @@ -2470,12 +2470,10 @@ fn from_opt_const_arg_anon_const( ExprKind::Path(QPath::Resolved(_, &Path { res: Res::Def(ConstParam, def_id), .. })) => { // Find the name and index of the const parameter by indexing the generics of // the parent item and construct a `ParamConst`. - let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); - let item_id = tcx.hir().get_parent_node(hir_id); - let item_def_id = tcx.hir().local_def_id(item_id); - let generics = tcx.generics_of(item_def_id.to_def_id()); + let item_def_id = tcx.parent(def_id); + let generics = tcx.generics_of(item_def_id); let index = generics.param_def_id_to_index[&def_id]; - let name = tcx.hir().name(hir_id); + let name = tcx.item_name(def_id); let ty_const = tcx.mk_const(ty::ConstKind::Param(ty::ParamConst::new(index, name)), ty); debug!(?ty_const); diff --git a/compiler/rustc_middle/src/ty/consts.rs b/compiler/rustc_middle/src/ty/consts.rs index e2e2761501b..c93abdfaa68 100644 --- a/compiler/rustc_middle/src/ty/consts.rs +++ b/compiler/rustc_middle/src/ty/consts.rs @@ -1,6 +1,6 @@ use crate::mir::interpret::LitToConstInput; use crate::mir::ConstantKind; -use crate::ty::{self, InternalSubsts, ParamEnv, ParamEnvAnd, Ty, TyCtxt}; +use crate::ty::{self, DefIdTree, InternalSubsts, ParamEnv, ParamEnvAnd, Ty, TyCtxt}; use rustc_data_structures::intern::Interned; use rustc_hir as hir; use rustc_hir::def_id::{DefId, LocalDefId}; @@ -131,12 +131,10 @@ fn try_eval_lit_or_param( ExprKind::Path(QPath::Resolved(_, &Path { res: Res::Def(ConstParam, def_id), .. })) => { // Find the name and index of the const parameter by indexing the generics of // the parent item and construct a `ParamConst`. - let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); - let item_id = tcx.hir().get_parent_node(hir_id); - let item_def_id = tcx.hir().local_def_id(item_id); - let generics = tcx.generics_of(item_def_id.to_def_id()); + let item_def_id = tcx.parent(def_id); + let generics = tcx.generics_of(item_def_id); let index = generics.param_def_id_to_index[&def_id]; - let name = tcx.hir().name(hir_id); + let name = tcx.item_name(def_id); Some(tcx.mk_const(ty::ConstKind::Param(ty::ParamConst::new(index, name)), ty)) } _ => None, diff --git a/compiler/rustc_middle/src/ty/diagnostics.rs b/compiler/rustc_middle/src/ty/diagnostics.rs index b8fd01e6a77..029ee15d68d 100644 --- a/compiler/rustc_middle/src/ty/diagnostics.rs +++ b/compiler/rustc_middle/src/ty/diagnostics.rs @@ -151,7 +151,6 @@ enum SuggestChangingConstraintsMessage<'a> { } fn suggest_removing_unsized_bound( - tcx: TyCtxt<'_>, generics: &hir::Generics<'_>, suggestions: &mut Vec<(Span, String, SuggestChangingConstraintsMessage<'_>)>, param: &hir::GenericParam<'_>, @@ -160,17 +159,16 @@ fn suggest_removing_unsized_bound( // See if there's a `?Sized` bound that can be removed to suggest that. // First look at the `where` clause because we can have `where T: ?Sized`, // then look at params. - let param_def_id = tcx.hir().local_def_id(param.hir_id); for (where_pos, predicate) in generics.predicates.iter().enumerate() { let WherePredicate::BoundPredicate(predicate) = predicate else { continue; }; - if !predicate.is_param_bound(param_def_id.to_def_id()) { + if !predicate.is_param_bound(param.def_id.to_def_id()) { continue; }; for (pos, bound) in predicate.bounds.iter().enumerate() { - let hir::GenericBound::Trait(poly, hir::TraitBoundModifier::Maybe) = bound else { + let hir::GenericBound::Trait(poly, hir::TraitBoundModifier::Maybe) = bound else { continue; }; if poly.trait_ref.trait_def_id() != def_id { @@ -232,7 +230,7 @@ pub fn suggest_constraining_type_params<'a>( param.span, &format!("this type parameter needs to be `{}`", constraint), ); - suggest_removing_unsized_bound(tcx, generics, &mut suggestions, param, def_id); + suggest_removing_unsized_bound(generics, &mut suggestions, param, def_id); } } @@ -283,8 +281,7 @@ pub fn suggest_constraining_type_params<'a>( // -- // | // replace with: `T: Bar +` - let param_def_id = tcx.hir().local_def_id(param.hir_id); - if let Some(span) = generics.bounds_span_for_suggestions(param_def_id) { + if let Some(span) = generics.bounds_span_for_suggestions(param.def_id) { suggest_restrict(span, true); continue; } diff --git a/compiler/rustc_mir_build/src/thir/cx/expr.rs b/compiler/rustc_mir_build/src/thir/cx/expr.rs index c4639d3a513..a7b59e50ad1 100644 --- a/compiler/rustc_mir_build/src/thir/cx/expr.rs +++ b/compiler/rustc_mir_build/src/thir/cx/expr.rs @@ -859,9 +859,7 @@ fn convert_path_expr(&mut self, expr: &'tcx hir::Expr<'tcx>, res: Res) -> ExprKi Res::Def(DefKind::ConstParam, def_id) => { let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); - let item_id = self.tcx.hir().get_parent_node(hir_id); - let item_def_id = self.tcx.hir().local_def_id(item_id); - let generics = self.tcx.generics_of(item_def_id); + let generics = self.tcx.generics_of(hir_id.owner); let index = generics.param_def_id_to_index[&def_id]; let name = self.tcx.hir().name(hir_id); let param = ty::ParamConst::new(index, name); diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index f3fc26e4784..3afe609bd0f 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -411,8 +411,7 @@ fn check_object_lifetime_default(&self, hir_id: HirId) { if let Some(generics) = tcx.hir().get_generics(tcx.hir().local_def_id(hir_id)) { for p in generics.params { let hir::GenericParamKind::Type { .. } = p.kind else { continue }; - let param_id = tcx.hir().local_def_id(p.hir_id); - let default = tcx.object_lifetime_default(param_id); + let default = tcx.object_lifetime_default(p.def_id); let repr = match default { ObjectLifetimeDefault::Empty => "BaseDefault".to_owned(), ObjectLifetimeDefault::Static => "'static".to_owned(), diff --git a/compiler/rustc_passes/src/stability.rs b/compiler/rustc_passes/src/stability.rs index 78afa2f25f8..172a948b372 100644 --- a/compiler/rustc_passes/src/stability.rs +++ b/compiler/rustc_passes/src/stability.rs @@ -500,7 +500,7 @@ fn visit_generic_param(&mut self, p: &'tcx hir::GenericParam<'tcx>) { }; self.annotate( - self.tcx.hir().local_def_id(p.hir_id), + p.def_id, p.span, None, kind, diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs index 98c13ffdafb..7c93e2d66ea 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs @@ -2619,11 +2619,10 @@ fn maybe_suggest_unsized_generics(&self, err: &mut Diagnostic, span: Span, node: let Some(param) = generics.params.iter().find(|param| param.span == span) else { return; }; - let param_def_id = self.tcx.hir().local_def_id(param.hir_id); // Check that none of the explicit trait bounds is `Sized`. Assume that an explicit // `Sized` bound is there intentionally and we don't need to suggest relaxing it. let explicitly_sized = generics - .bounds_for_param(param_def_id) + .bounds_for_param(param.def_id) .flat_map(|bp| bp.bounds) .any(|bound| bound.trait_ref().and_then(|tr| tr.trait_def_id()) == sized_trait); if explicitly_sized { @@ -2646,7 +2645,7 @@ fn maybe_suggest_unsized_generics(&self, err: &mut Diagnostic, span: Span, node: _ => {} }; // Didn't add an indirection suggestion, so add a general suggestion to relax `Sized`. - let (span, separator) = if let Some(s) = generics.bounds_span_for_suggestions(param_def_id) + let (span, separator) = if let Some(s) = generics.bounds_span_for_suggestions(param.def_id) { (s, " +") } else {