Auto merge of #95435 - cjgillot:one-name, r=oli-obk
Make def names and HIR names consistent. The name in the `DefKey` is interned to create the `DefId`, so it does not require any query to access. This can be leveraged to avoid a few useless HIR accesses for names. ~In order to achieve that, generic parameters created from universal impl-trait are given the pretty-printed ast as a name, instead of `{{opaque}}`.~ ~Drive-by: the `TyCtxt::opt_item_name` used a dummy span for non-local definitions. We have access to `def_ident_span`, so we use it.~
This commit is contained in:
commit
559c01931b
12 changed files with 56 additions and 56 deletions
|
@ -1,5 +1,5 @@
|
|||
use crate::hir::{ModuleItems, Owner};
|
||||
use crate::ty::TyCtxt;
|
||||
use crate::ty::{DefIdTree, TyCtxt};
|
||||
use rustc_ast as ast;
|
||||
use rustc_data_structures::fingerprint::Fingerprint;
|
||||
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
||||
|
@ -545,23 +545,21 @@ impl<'hir> Map<'hir> {
|
|||
});
|
||||
}
|
||||
|
||||
pub fn ty_param_owner(self, id: HirId) -> LocalDefId {
|
||||
match self.get(id) {
|
||||
Node::Item(&Item { kind: ItemKind::Trait(..) | ItemKind::TraitAlias(..), .. }) => {
|
||||
id.expect_owner()
|
||||
}
|
||||
Node::GenericParam(_) => self.get_parent_item(id),
|
||||
_ => bug!("ty_param_owner: {} not a type parameter", self.node_to_string(id)),
|
||||
pub fn ty_param_owner(self, def_id: LocalDefId) -> LocalDefId {
|
||||
let def_kind = self.tcx.def_kind(def_id);
|
||||
match def_kind {
|
||||
DefKind::Trait | DefKind::TraitAlias => def_id,
|
||||
DefKind::TyParam | DefKind::ConstParam => self.tcx.local_parent(def_id).unwrap(),
|
||||
_ => bug!("ty_param_owner: {:?} is a {:?} not a type parameter", def_id, def_kind),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ty_param_name(self, id: HirId) -> Symbol {
|
||||
match self.get(id) {
|
||||
Node::Item(&Item { kind: ItemKind::Trait(..) | ItemKind::TraitAlias(..), .. }) => {
|
||||
kw::SelfUpper
|
||||
}
|
||||
Node::GenericParam(param) => param.name.ident().name,
|
||||
_ => bug!("ty_param_name: {} not a type parameter", self.node_to_string(id)),
|
||||
pub fn ty_param_name(self, def_id: LocalDefId) -> Symbol {
|
||||
let def_kind = self.tcx.def_kind(def_id);
|
||||
match def_kind {
|
||||
DefKind::Trait | DefKind::TraitAlias => kw::SelfUpper,
|
||||
DefKind::TyParam | DefKind::ConstParam => self.tcx.item_name(def_id.to_def_id()),
|
||||
_ => bug!("ty_param_name: {:?} is a {:?} not a type parameter", def_id, def_kind),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -517,10 +517,7 @@ rustc_queries! {
|
|||
/// To avoid cycles within the predicates of a single item we compute
|
||||
/// per-type-parameter predicates for resolving `T::AssocTy`.
|
||||
query type_param_predicates(key: (DefId, LocalDefId, rustc_span::symbol::Ident)) -> ty::GenericPredicates<'tcx> {
|
||||
desc { |tcx| "computing the bounds for type parameter `{}`", {
|
||||
let id = tcx.hir().local_def_id_to_hir_id(key.1);
|
||||
tcx.hir().ty_param_name(id)
|
||||
}}
|
||||
desc { |tcx| "computing the bounds for type parameter `{}`", tcx.hir().ty_param_name(key.1) }
|
||||
}
|
||||
|
||||
query trait_def(key: DefId) -> ty::TraitDef {
|
||||
|
|
|
@ -1991,27 +1991,25 @@ impl<'tcx> TyCtxt<'tcx> {
|
|||
.filter(|item| item.kind == AssocKind::Fn && item.defaultness.has_value())
|
||||
}
|
||||
|
||||
fn item_name_from_hir(self, def_id: DefId) -> Option<Ident> {
|
||||
self.hir().get_if_local(def_id).and_then(|node| node.ident())
|
||||
}
|
||||
|
||||
fn item_name_from_def_id(self, def_id: DefId) -> Option<Symbol> {
|
||||
fn opt_item_name(self, def_id: DefId) -> Option<Symbol> {
|
||||
if def_id.index == CRATE_DEF_INDEX {
|
||||
Some(self.crate_name(def_id.krate))
|
||||
} else {
|
||||
let def_key = self.def_key(def_id);
|
||||
match def_key.disambiguated_data.data {
|
||||
// The name of a constructor is that of its parent.
|
||||
rustc_hir::definitions::DefPathData::Ctor => self.item_name_from_def_id(DefId {
|
||||
krate: def_id.krate,
|
||||
index: def_key.parent.unwrap(),
|
||||
}),
|
||||
_ => def_key.disambiguated_data.data.get_opt_name(),
|
||||
rustc_hir::definitions::DefPathData::Ctor => self
|
||||
.opt_item_name(DefId { krate: def_id.krate, index: def_key.parent.unwrap() }),
|
||||
// The name of opaque types only exists in HIR.
|
||||
rustc_hir::definitions::DefPathData::ImplTrait
|
||||
if let Some(def_id) = def_id.as_local() =>
|
||||
self.hir().opt_name(self.hir().local_def_id_to_hir_id(def_id)),
|
||||
_ => def_key.get_opt_name(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Look up the name of an item across crates. This does not look at HIR.
|
||||
/// Look up the name of a definition across crates. This does not look at HIR.
|
||||
///
|
||||
/// When possible, this function should be used for cross-crate lookups over
|
||||
/// [`opt_item_name`] to avoid invalidating the incremental cache. If you
|
||||
|
@ -2023,18 +2021,21 @@ impl<'tcx> TyCtxt<'tcx> {
|
|||
pub fn item_name(self, id: DefId) -> Symbol {
|
||||
// Look at cross-crate items first to avoid invalidating the incremental cache
|
||||
// unless we have to.
|
||||
self.item_name_from_def_id(id).unwrap_or_else(|| {
|
||||
self.opt_item_name(id).unwrap_or_else(|| {
|
||||
bug!("item_name: no name for {:?}", self.def_path(id));
|
||||
})
|
||||
}
|
||||
|
||||
/// Look up the name and span of an item or [`Node`].
|
||||
/// Look up the name and span of a definition.
|
||||
///
|
||||
/// See [`item_name`][Self::item_name] for more information.
|
||||
pub fn opt_item_name(self, def_id: DefId) -> Option<Ident> {
|
||||
// Look at the HIR first so the span will be correct if this is a local item.
|
||||
self.item_name_from_hir(def_id)
|
||||
.or_else(|| self.item_name_from_def_id(def_id).map(Ident::with_dummy_span))
|
||||
pub fn opt_item_ident(self, def_id: DefId) -> Option<Ident> {
|
||||
let def = self.opt_item_name(def_id)?;
|
||||
let span = def_id
|
||||
.as_local()
|
||||
.and_then(|id| self.def_ident_span(id))
|
||||
.unwrap_or(rustc_span::DUMMY_SP);
|
||||
Some(Ident::new(def, span))
|
||||
}
|
||||
|
||||
pub fn opt_associated_item(self, def_id: DefId) -> Option<&'tcx AssocItem> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue