1
Fork 0

Only store a LocalDefId in hir::TraitItem.

This commit is contained in:
Camille GILLOT 2021-01-30 20:46:50 +01:00
parent cebbba081e
commit a871a0f111
45 changed files with 139 additions and 125 deletions

View file

@ -1907,7 +1907,14 @@ pub struct FnSig<'hir> {
// so it can fetched later.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Encodable, Debug)]
pub struct TraitItemId {
pub hir_id: HirId,
pub def_id: LocalDefId,
}
impl TraitItemId {
pub fn hir_id(&self) -> HirId {
// Items are always HIR owners.
HirId::make_owner(self.def_id)
}
}
/// Represents an item declaration within a trait declaration,
@ -1917,13 +1924,24 @@ pub struct TraitItemId {
#[derive(Debug)]
pub struct TraitItem<'hir> {
pub ident: Ident,
pub hir_id: HirId,
pub def_id: LocalDefId,
pub attrs: &'hir [Attribute],
pub generics: Generics<'hir>,
pub kind: TraitItemKind<'hir>,
pub span: Span,
}
impl TraitItem<'_> {
pub fn hir_id(&self) -> HirId {
// Items are always HIR owners.
HirId::make_owner(self.def_id)
}
pub fn trait_item_id(&self) -> TraitItemId {
TraitItemId { def_id: self.def_id }
}
}
/// Represents a trait method's body (or just argument names).
#[derive(Encodable, Debug, HashStable_Generic)]
pub enum TraitFn<'hir> {
@ -2885,9 +2903,10 @@ impl<'hir> Node<'hir> {
pub fn hir_id(&self) -> Option<HirId> {
match self {
Node::Item(Item { def_id, .. }) => Some(HirId::make_owner(*def_id)),
Node::Item(Item { def_id, .. }) | Node::TraitItem(TraitItem { def_id, .. }) => {
Some(HirId::make_owner(*def_id))
}
Node::ForeignItem(ForeignItem { hir_id, .. })
| Node::TraitItem(TraitItem { hir_id, .. })
| Node::ImplItem(ImplItem { hir_id, .. })
| Node::Field(StructField { hir_id, .. })
| Node::AnonConst(AnonConst { hir_id, .. })
@ -2922,7 +2941,7 @@ mod size_asserts {
rustc_data_structures::static_assert_size!(super::Ty<'static>, 72);
rustc_data_structures::static_assert_size!(super::Item<'static>, 200);
rustc_data_structures::static_assert_size!(super::TraitItem<'static>, 152);
rustc_data_structures::static_assert_size!(super::TraitItem<'static>, 144);
rustc_data_structures::static_assert_size!(super::ImplItem<'static>, 168);
rustc_data_structures::static_assert_size!(super::ForeignItem<'static>, 160);
}

View file

@ -964,12 +964,12 @@ pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v Trai
visitor.visit_generics(&trait_item.generics);
match trait_item.kind {
TraitItemKind::Const(ref ty, default) => {
visitor.visit_id(trait_item.hir_id);
visitor.visit_id(trait_item.hir_id());
visitor.visit_ty(ty);
walk_list!(visitor, visit_nested_body, default);
}
TraitItemKind::Fn(ref sig, TraitFn::Required(param_names)) => {
visitor.visit_id(trait_item.hir_id);
visitor.visit_id(trait_item.hir_id());
visitor.visit_fn_decl(&sig.decl);
for &param_name in param_names {
visitor.visit_ident(param_name);
@ -981,11 +981,11 @@ pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v Trai
&sig.decl,
body_id,
trait_item.span,
trait_item.hir_id,
trait_item.hir_id(),
);
}
TraitItemKind::Type(bounds, ref default) => {
visitor.visit_id(trait_item.hir_id);
visitor.visit_id(trait_item.hir_id());
walk_list!(visitor, visit_param_bound, bounds);
walk_list!(visitor, visit_ty, default);
}

View file

@ -44,11 +44,11 @@ impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ItemId {
}
impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for TraitItemId {
type KeyType = (DefPathHash, ItemLocalId);
type KeyType = DefPathHash;
#[inline]
fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) {
self.hir_id.to_stable_hash_key(hcx)
fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
hcx.local_def_path_hash(self.def_id)
}
}
@ -109,7 +109,7 @@ impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ImplItemId {
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for TraitItemId {
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
hcx.hash_reference_to_item(self.hir_id, hasher)
hcx.hash_reference_to_item(self.hir_id(), hasher)
}
}
@ -139,7 +139,7 @@ impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for VisibilityKind<'_>
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for TraitItem<'_> {
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
let TraitItem { hir_id: _, ident, ref attrs, ref generics, ref kind, span } = *self;
let TraitItem { def_id: _, ident, ref attrs, ref generics, ref kind, span } = *self;
hcx.hash_hir_item_like(|hcx| {
ident.name.hash_stable(hcx, hasher);