]> git.lizzy.rs Git - rust.git/blob - src/librustc_hir/hir_id.rs
Rollup merge of #68509 - GuillaumeGomez:clean-up-err-codes-e0223-e0225, r=Dylan-DPC
[rust.git] / src / librustc_hir / hir_id.rs
1 use crate::def_id::{DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX};
2 use rustc_serialize::{self, Decodable, Decoder, Encodable, Encoder};
3 use std::fmt;
4
5 /// Uniquely identifies a node in the HIR of the current crate. It is
6 /// composed of the `owner`, which is the `DefIndex` of the directly enclosing
7 /// `hir::Item`, `hir::TraitItem`, or `hir::ImplItem` (i.e., the closest "item-like"),
8 /// and the `local_id` which is unique within the given owner.
9 ///
10 /// This two-level structure makes for more stable values: One can move an item
11 /// around within the source code, or add or remove stuff before it, without
12 /// the `local_id` part of the `HirId` changing, which is a very useful property in
13 /// incremental compilation where we have to persist things through changes to
14 /// the code base.
15 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)]
16 pub struct HirId {
17     pub owner: DefIndex,
18     pub local_id: ItemLocalId,
19 }
20
21 impl HirId {
22     pub fn owner_def_id(self) -> DefId {
23         DefId::local(self.owner)
24     }
25
26     pub fn owner_local_def_id(self) -> LocalDefId {
27         LocalDefId::from_def_id(DefId::local(self.owner))
28     }
29 }
30
31 impl rustc_serialize::UseSpecializedEncodable for HirId {
32     fn default_encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
33         let HirId { owner, local_id } = *self;
34
35         owner.encode(s)?;
36         local_id.encode(s)?;
37         Ok(())
38     }
39 }
40
41 impl rustc_serialize::UseSpecializedDecodable for HirId {
42     fn default_decode<D: Decoder>(d: &mut D) -> Result<HirId, D::Error> {
43         let owner = DefIndex::decode(d)?;
44         let local_id = ItemLocalId::decode(d)?;
45
46         Ok(HirId { owner, local_id })
47     }
48 }
49
50 impl fmt::Display for HirId {
51     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52         write!(f, "{:?}", self)
53     }
54 }
55
56 rustc_data_structures::define_id_collections!(HirIdMap, HirIdSet, HirId);
57 rustc_data_structures::define_id_collections!(ItemLocalMap, ItemLocalSet, ItemLocalId);
58
59 rustc_index::newtype_index! {
60     /// An `ItemLocalId` uniquely identifies something within a given "item-like";
61     /// that is, within a `hir::Item`, `hir::TraitItem`, or `hir::ImplItem`. There is no
62     /// guarantee that the numerical value of a given `ItemLocalId` corresponds to
63     /// the node's position within the owning item in any way, but there is a
64     /// guarantee that the `LocalItemId`s within an owner occupy a dense range of
65     /// integers starting at zero, so a mapping that maps all or most nodes within
66     /// an "item-like" to something else can be implemented by a `Vec` instead of a
67     /// tree or hash map.
68     pub struct ItemLocalId { .. }
69 }
70 rustc_data_structures::impl_stable_hash_via_hash!(ItemLocalId);
71
72 /// The `HirId` corresponding to `CRATE_NODE_ID` and `CRATE_DEF_INDEX`.
73 pub const CRATE_HIR_ID: HirId =
74     HirId { owner: CRATE_DEF_INDEX, local_id: ItemLocalId::from_u32_const(0) };
75
76 pub const DUMMY_HIR_ID: HirId = HirId { owner: CRATE_DEF_INDEX, local_id: DUMMY_ITEM_LOCAL_ID };
77
78 pub const DUMMY_ITEM_LOCAL_ID: ItemLocalId = ItemLocalId::MAX;