diff options
| author | Takayuki Maeda <takoyaki0316@gmail.com> | 2022-09-20 14:11:23 +0900 |
|---|---|---|
| committer | Takayuki Maeda <takoyaki0316@gmail.com> | 2022-09-24 23:21:19 +0900 |
| commit | 8fe936099a3a2ea236d40212a340fc4a326eb506 (patch) | |
| tree | 26b0d90d8a211b35dd4a48d312e018d4fe6de4fa /compiler/rustc_hir/src | |
| parent | bb5a01617589b5e3ece5a36435fc285bfd13c9a4 (diff) | |
| download | rust-8fe936099a3a2ea236d40212a340fc4a326eb506.tar.gz rust-8fe936099a3a2ea236d40212a340fc4a326eb506.zip | |
separate definitions and `HIR` owners
fix a ui test use `into` fix clippy ui test fix a run-make-fulldeps test implement `IntoQueryParam<DefId>` for `OwnerId` use `OwnerId` for more queries change the type of `ParentOwnerIterator::Item` to `(OwnerId, OwnerNode)`
Diffstat (limited to 'compiler/rustc_hir/src')
| -rw-r--r-- | compiler/rustc_hir/src/hir.rs | 36 | ||||
| -rw-r--r-- | compiler/rustc_hir/src/hir_id.rs | 60 | ||||
| -rw-r--r-- | compiler/rustc_hir/src/stable_hash_impls.rs | 10 |
3 files changed, 75 insertions, 31 deletions
diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index a8436ea64f8..018b7cc5d9d 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -1,6 +1,6 @@ use crate::def::{CtorKind, DefKind, Res}; use crate::def_id::DefId; -pub(crate) use crate::hir_id::{HirId, ItemLocalId}; +pub(crate) use crate::hir_id::{HirId, ItemLocalId, OwnerId}; use crate::intravisit::FnKind; use crate::LangItem; @@ -2206,14 +2206,14 @@ pub struct FnSig<'hir> { // so it can fetched later. #[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Debug, HashStable_Generic)] pub struct TraitItemId { - pub def_id: LocalDefId, + pub def_id: OwnerId, } impl TraitItemId { #[inline] pub fn hir_id(&self) -> HirId { // Items are always HIR owners. - HirId::make_owner(self.def_id) + HirId::make_owner(self.def_id.def_id) } } @@ -2224,7 +2224,7 @@ impl TraitItemId { #[derive(Debug, HashStable_Generic)] pub struct TraitItem<'hir> { pub ident: Ident, - pub def_id: LocalDefId, + pub def_id: OwnerId, pub generics: &'hir Generics<'hir>, pub kind: TraitItemKind<'hir>, pub span: Span, @@ -2235,7 +2235,7 @@ impl TraitItem<'_> { #[inline] pub fn hir_id(&self) -> HirId { // Items are always HIR owners. - HirId::make_owner(self.def_id) + HirId::make_owner(self.def_id.def_id) } pub fn trait_item_id(&self) -> TraitItemId { @@ -2270,14 +2270,14 @@ pub enum TraitItemKind<'hir> { // so it can fetched later. #[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Debug, HashStable_Generic)] pub struct ImplItemId { - pub def_id: LocalDefId, + pub def_id: OwnerId, } impl ImplItemId { #[inline] pub fn hir_id(&self) -> HirId { // Items are always HIR owners. - HirId::make_owner(self.def_id) + HirId::make_owner(self.def_id.def_id) } } @@ -2285,7 +2285,7 @@ impl ImplItemId { #[derive(Debug, HashStable_Generic)] pub struct ImplItem<'hir> { pub ident: Ident, - pub def_id: LocalDefId, + pub def_id: OwnerId, pub generics: &'hir Generics<'hir>, pub kind: ImplItemKind<'hir>, pub defaultness: Defaultness, @@ -2297,7 +2297,7 @@ impl ImplItem<'_> { #[inline] pub fn hir_id(&self) -> HirId { // Items are always HIR owners. - HirId::make_owner(self.def_id) + HirId::make_owner(self.def_id.def_id) } pub fn impl_item_id(&self) -> ImplItemId { @@ -2888,14 +2888,14 @@ impl<'hir> VariantData<'hir> { // so it can fetched later. #[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Debug, Hash, HashStable_Generic)] pub struct ItemId { - pub def_id: LocalDefId, + pub def_id: OwnerId, } impl ItemId { #[inline] pub fn hir_id(&self) -> HirId { // Items are always HIR owners. - HirId::make_owner(self.def_id) + HirId::make_owner(self.def_id.def_id) } } @@ -2905,7 +2905,7 @@ impl ItemId { #[derive(Debug, HashStable_Generic)] pub struct Item<'hir> { pub ident: Ident, - pub def_id: LocalDefId, + pub def_id: OwnerId, pub kind: ItemKind<'hir>, pub span: Span, pub vis_span: Span, @@ -2915,7 +2915,7 @@ impl Item<'_> { #[inline] pub fn hir_id(&self) -> HirId { // Items are always HIR owners. - HirId::make_owner(self.def_id) + HirId::make_owner(self.def_id.def_id) } pub fn item_id(&self) -> ItemId { @@ -3132,14 +3132,14 @@ pub enum AssocItemKind { // so it can fetched later. #[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Debug, HashStable_Generic)] pub struct ForeignItemId { - pub def_id: LocalDefId, + pub def_id: OwnerId, } impl ForeignItemId { #[inline] pub fn hir_id(&self) -> HirId { // Items are always HIR owners. - HirId::make_owner(self.def_id) + HirId::make_owner(self.def_id.def_id) } } @@ -3160,7 +3160,7 @@ pub struct ForeignItemRef { pub struct ForeignItem<'hir> { pub ident: Ident, pub kind: ForeignItemKind<'hir>, - pub def_id: LocalDefId, + pub def_id: OwnerId, pub span: Span, pub vis_span: Span, } @@ -3169,7 +3169,7 @@ impl ForeignItem<'_> { #[inline] pub fn hir_id(&self) -> HirId { // Items are always HIR owners. - HirId::make_owner(self.def_id) + HirId::make_owner(self.def_id.def_id) } pub fn foreign_item_id(&self) -> ForeignItemId { @@ -3263,7 +3263,7 @@ impl<'hir> OwnerNode<'hir> { Node::generics(self.into()) } - pub fn def_id(self) -> LocalDefId { + pub fn def_id(self) -> OwnerId { match self { OwnerNode::Item(Item { def_id, .. }) | OwnerNode::TraitItem(TraitItem { def_id, .. }) diff --git a/compiler/rustc_hir/src/hir_id.rs b/compiler/rustc_hir/src/hir_id.rs index 84b0740c7b3..752f760ea97 100644 --- a/compiler/rustc_hir/src/hir_id.rs +++ b/compiler/rustc_hir/src/hir_id.rs @@ -1,6 +1,43 @@ -use crate::def_id::{LocalDefId, CRATE_DEF_ID}; +use crate::def_id::{DefId, LocalDefId, CRATE_DEF_ID}; +use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey}; +use rustc_span::{def_id::DefPathHash, HashStableContext}; use std::fmt; +#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] +#[derive(Encodable, Decodable)] +pub struct OwnerId { + pub def_id: LocalDefId, +} + +impl From<OwnerId> for HirId { + fn from(owner: OwnerId) -> HirId { + HirId { owner, local_id: ItemLocalId::from_u32(0) } + } +} + +impl OwnerId { + #[inline] + pub fn to_def_id(self) -> DefId { + self.def_id.to_def_id() + } +} + +impl<CTX: HashStableContext> HashStable<CTX> for OwnerId { + #[inline] + fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { + self.to_stable_hash_key(hcx).hash_stable(hcx, hasher); + } +} + +impl<CTX: HashStableContext> ToStableHashKey<CTX> for OwnerId { + type KeyType = DefPathHash; + + #[inline] + fn to_stable_hash_key(&self, hcx: &CTX) -> DefPathHash { + hcx.def_path_hash(self.to_def_id()) + } +} + /// Uniquely identifies a node in the HIR of the current crate. It is /// composed of the `owner`, which is the `LocalDefId` of the directly enclosing /// `hir::Item`, `hir::TraitItem`, or `hir::ImplItem` (i.e., the closest "item-like"), @@ -15,22 +52,23 @@ use std::fmt; #[derive(Encodable, Decodable, HashStable_Generic)] #[rustc_pass_by_value] pub struct HirId { - pub owner: LocalDefId, + pub owner: OwnerId, pub local_id: ItemLocalId, } impl HirId { /// Signal local id which should never be used. - pub const INVALID: HirId = HirId { owner: CRATE_DEF_ID, local_id: ItemLocalId::INVALID }; + pub const INVALID: HirId = + HirId { owner: OwnerId { def_id: CRATE_DEF_ID }, local_id: ItemLocalId::INVALID }; #[inline] - pub fn expect_owner(self) -> LocalDefId { + pub fn expect_owner(self) -> OwnerId { assert_eq!(self.local_id.index(), 0); self.owner } #[inline] - pub fn as_owner(self) -> Option<LocalDefId> { + pub fn as_owner(self) -> Option<OwnerId> { if self.local_id.index() == 0 { Some(self.owner) } else { None } } @@ -41,11 +79,14 @@ impl HirId { #[inline] pub fn make_owner(owner: LocalDefId) -> Self { - Self { owner, local_id: ItemLocalId::from_u32(0) } + Self { owner: OwnerId { def_id: owner }, local_id: ItemLocalId::from_u32(0) } } pub fn index(self) -> (usize, usize) { - (rustc_index::vec::Idx::index(self.owner), rustc_index::vec::Idx::index(self.local_id)) + ( + rustc_index::vec::Idx::index(self.owner.def_id), + rustc_index::vec::Idx::index(self.local_id), + ) } } @@ -94,4 +135,7 @@ impl ItemLocalId { } /// The `HirId` corresponding to `CRATE_NODE_ID` and `CRATE_DEF_ID`. -pub const CRATE_HIR_ID: HirId = HirId { owner: CRATE_DEF_ID, local_id: ItemLocalId::from_u32(0) }; +pub const CRATE_HIR_ID: HirId = + HirId { owner: OwnerId { def_id: CRATE_DEF_ID }, local_id: ItemLocalId::from_u32(0) }; + +pub const CRATE_OWNER_ID: OwnerId = OwnerId { def_id: CRATE_DEF_ID }; diff --git a/compiler/rustc_hir/src/stable_hash_impls.rs b/compiler/rustc_hir/src/stable_hash_impls.rs index 5b9c42686c3..06b7a65662e 100644 --- a/compiler/rustc_hir/src/stable_hash_impls.rs +++ b/compiler/rustc_hir/src/stable_hash_impls.rs @@ -20,7 +20,7 @@ impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for HirId { #[inline] fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) { - let def_path_hash = self.owner.to_stable_hash_key(hcx); + let def_path_hash = self.owner.def_id.to_stable_hash_key(hcx); (def_path_hash, self.local_id) } } @@ -49,7 +49,7 @@ impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ItemId { #[inline] fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash { - self.def_id.to_stable_hash_key(hcx) + self.def_id.def_id.to_stable_hash_key(hcx) } } @@ -58,7 +58,7 @@ impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for TraitItemId { #[inline] fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash { - self.def_id.to_stable_hash_key(hcx) + self.def_id.def_id.to_stable_hash_key(hcx) } } @@ -67,7 +67,7 @@ impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ImplItemId { #[inline] fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash { - self.def_id.to_stable_hash_key(hcx) + self.def_id.def_id.to_stable_hash_key(hcx) } } @@ -76,7 +76,7 @@ impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ForeignItemId #[inline] fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash { - self.def_id.to_stable_hash_key(hcx) + self.def_id.def_id.to_stable_hash_key(hcx) } } |
