1 use crate::def_id::{DefId, LocalDefId, CRATE_DEF_ID};
2 use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
3 use rustc_span::{def_id::DefPathHash, HashStableContext};
6 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
7 #[derive(Encodable, Decodable)]
9 pub def_id: LocalDefId,
12 impl From<OwnerId> for HirId {
13 fn from(owner: OwnerId) -> HirId {
14 HirId { owner, local_id: ItemLocalId::from_u32(0) }
20 pub fn to_def_id(self) -> DefId {
21 self.def_id.to_def_id()
25 impl<CTX: HashStableContext> HashStable<CTX> for OwnerId {
27 fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
28 self.to_stable_hash_key(hcx).hash_stable(hcx, hasher);
32 impl<CTX: HashStableContext> ToStableHashKey<CTX> for OwnerId {
33 type KeyType = DefPathHash;
36 fn to_stable_hash_key(&self, hcx: &CTX) -> DefPathHash {
37 hcx.def_path_hash(self.to_def_id())
41 /// Uniquely identifies a node in the HIR of the current crate. It is
42 /// composed of the `owner`, which is the `LocalDefId` of the directly enclosing
43 /// `hir::Item`, `hir::TraitItem`, or `hir::ImplItem` (i.e., the closest "item-like"),
44 /// and the `local_id` which is unique within the given owner.
46 /// This two-level structure makes for more stable values: One can move an item
47 /// around within the source code, or add or remove stuff before it, without
48 /// the `local_id` part of the `HirId` changing, which is a very useful property in
49 /// incremental compilation where we have to persist things through changes to
51 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
52 #[derive(Encodable, Decodable, HashStable_Generic)]
53 #[rustc_pass_by_value]
56 pub local_id: ItemLocalId,
60 /// Signal local id which should never be used.
61 pub const INVALID: HirId =
62 HirId { owner: OwnerId { def_id: CRATE_DEF_ID }, local_id: ItemLocalId::INVALID };
65 pub fn expect_owner(self) -> OwnerId {
66 assert_eq!(self.local_id.index(), 0);
71 pub fn as_owner(self) -> Option<OwnerId> {
72 if self.local_id.index() == 0 { Some(self.owner) } else { None }
76 pub fn is_owner(self) -> bool {
77 self.local_id.index() == 0
81 pub fn make_owner(owner: LocalDefId) -> Self {
82 Self { owner: OwnerId { def_id: owner }, local_id: ItemLocalId::from_u32(0) }
85 pub fn index(self) -> (usize, usize) {
87 rustc_index::vec::Idx::index(self.owner.def_id),
88 rustc_index::vec::Idx::index(self.local_id),
93 impl fmt::Display for HirId {
94 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95 write!(f, "{:?}", self)
100 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
101 (self.index()).cmp(&(other.index()))
105 impl PartialOrd for HirId {
106 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
107 Some(self.cmp(&other))
111 rustc_data_structures::define_stable_id_collections!(HirIdMap, HirIdSet, HirIdMapEntry, HirId);
112 rustc_data_structures::define_id_collections!(
119 rustc_index::newtype_index! {
120 /// An `ItemLocalId` uniquely identifies something within a given "item-like";
121 /// that is, within a `hir::Item`, `hir::TraitItem`, or `hir::ImplItem`. There is no
122 /// guarantee that the numerical value of a given `ItemLocalId` corresponds to
123 /// the node's position within the owning item in any way, but there is a
124 /// guarantee that the `LocalItemId`s within an owner occupy a dense range of
125 /// integers starting at zero, so a mapping that maps all or most nodes within
126 /// an "item-like" to something else can be implemented by a `Vec` instead of a
127 /// tree or hash map.
128 #[derive(HashStable_Generic)]
129 pub struct ItemLocalId { .. }
133 /// Signal local id which should never be used.
134 pub const INVALID: ItemLocalId = ItemLocalId::MAX;
137 /// The `HirId` corresponding to `CRATE_NODE_ID` and `CRATE_DEF_ID`.
138 pub const CRATE_HIR_ID: HirId =
139 HirId { owner: OwnerId { def_id: CRATE_DEF_ID }, local_id: ItemLocalId::from_u32(0) };
141 pub const CRATE_OWNER_ID: OwnerId = OwnerId { def_id: CRATE_DEF_ID };