Skip to content

Commit 72241ad

Browse files
committed
move HirId to librustc_hir::hir_id
1 parent 7a14073 commit 72241ad

File tree

3 files changed

+83
-85
lines changed

3 files changed

+83
-85
lines changed

src/librustc/hir/mod.rs

Lines changed: 2 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ pub use self::UnOp::*;
99
pub use self::UnsafeSource::*;
1010

1111
use crate::hir::def::{DefKind, Res};
12-
use crate::hir::def_id::{DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX};
12+
use crate::hir::def_id::{DefId, DefIndex};
1313
use crate::ty::query::Providers;
1414

1515
use errors::FatalError;
1616
use rustc_data_structures::fx::FxHashSet;
1717
use rustc_data_structures::sync::{par_for_each_in, Send, Sync};
1818
use rustc_macros::HashStable;
19-
use rustc_serialize::{self, Decodable, Decoder, Encodable, Encoder};
2019
use rustc_session::node_id::NodeMap;
2120
use rustc_span::source_map::{SourceMap, Spanned};
2221
use rustc_span::symbol::{kw, sym, Symbol};
@@ -35,96 +34,14 @@ use syntax::util::parser::ExprPrecedence;
3534
pub mod check_attr;
3635
pub mod def;
3736
pub use rustc_hir::def_id;
37+
pub use rustc_hir::hir_id::*;
3838
pub mod intravisit;
3939
pub mod itemlikevisit;
4040
pub mod map;
4141
pub mod pat_util;
4242
pub mod print;
4343
pub mod upvars;
4444

45-
/// Uniquely identifies a node in the HIR of the current crate. It is
46-
/// composed of the `owner`, which is the `DefIndex` of the directly enclosing
47-
/// `hir::Item`, `hir::TraitItem`, or `hir::ImplItem` (i.e., the closest "item-like"),
48-
/// and the `local_id` which is unique within the given owner.
49-
///
50-
/// This two-level structure makes for more stable values: One can move an item
51-
/// around within the source code, or add or remove stuff before it, without
52-
/// the `local_id` part of the `HirId` changing, which is a very useful property in
53-
/// incremental compilation where we have to persist things through changes to
54-
/// the code base.
55-
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)]
56-
pub struct HirId {
57-
pub owner: DefIndex,
58-
pub local_id: ItemLocalId,
59-
}
60-
61-
impl HirId {
62-
pub fn owner_def_id(self) -> DefId {
63-
DefId::local(self.owner)
64-
}
65-
66-
pub fn owner_local_def_id(self) -> LocalDefId {
67-
LocalDefId::from_def_id(DefId::local(self.owner))
68-
}
69-
}
70-
71-
impl rustc_serialize::UseSpecializedEncodable for HirId {
72-
fn default_encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
73-
let HirId { owner, local_id } = *self;
74-
75-
owner.encode(s)?;
76-
local_id.encode(s)?;
77-
Ok(())
78-
}
79-
}
80-
81-
impl rustc_serialize::UseSpecializedDecodable for HirId {
82-
fn default_decode<D: Decoder>(d: &mut D) -> Result<HirId, D::Error> {
83-
let owner = DefIndex::decode(d)?;
84-
let local_id = ItemLocalId::decode(d)?;
85-
86-
Ok(HirId { owner, local_id })
87-
}
88-
}
89-
90-
impl fmt::Display for HirId {
91-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
92-
write!(f, "{:?}", self)
93-
}
94-
}
95-
96-
rustc_data_structures::define_id_collections!(HirIdMap, HirIdSet, HirId);
97-
rustc_data_structures::define_id_collections!(ItemLocalMap, ItemLocalSet, ItemLocalId);
98-
99-
// Hack to ensure that we don't try to access the private parts of `ItemLocalId` in this module.
100-
mod item_local_id_inner {
101-
use rustc_index::vec::Idx;
102-
use rustc_macros::HashStable;
103-
rustc_index::newtype_index! {
104-
/// An `ItemLocalId` uniquely identifies something within a given "item-like";
105-
/// that is, within a `hir::Item`, `hir::TraitItem`, or `hir::ImplItem`. There is no
106-
/// guarantee that the numerical value of a given `ItemLocalId` corresponds to
107-
/// the node's position within the owning item in any way, but there is a
108-
/// guarantee that the `LocalItemId`s within an owner occupy a dense range of
109-
/// integers starting at zero, so a mapping that maps all or most nodes within
110-
/// an "item-like" to something else can be implemented by a `Vec` instead of a
111-
/// tree or hash map.
112-
pub struct ItemLocalId {
113-
derive [HashStable]
114-
}
115-
}
116-
}
117-
118-
pub use self::item_local_id_inner::ItemLocalId;
119-
120-
/// The `HirId` corresponding to `CRATE_NODE_ID` and `CRATE_DEF_INDEX`.
121-
pub const CRATE_HIR_ID: HirId =
122-
HirId { owner: CRATE_DEF_INDEX, local_id: ItemLocalId::from_u32_const(0) };
123-
124-
pub const DUMMY_HIR_ID: HirId = HirId { owner: CRATE_DEF_INDEX, local_id: DUMMY_ITEM_LOCAL_ID };
125-
126-
pub const DUMMY_ITEM_LOCAL_ID: ItemLocalId = ItemLocalId::MAX;
127-
12845
#[derive(Copy, Clone, RustcEncodable, RustcDecodable, HashStable)]
12946
pub struct Lifetime {
13047
pub hir_id: HirId,

src/librustc_hir/hir_id.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
use rustc_index::vec::Idx;
60+
rustc_index::newtype_index! {
61+
/// An `ItemLocalId` uniquely identifies something within a given "item-like";
62+
/// that is, within a `hir::Item`, `hir::TraitItem`, or `hir::ImplItem`. There is no
63+
/// guarantee that the numerical value of a given `ItemLocalId` corresponds to
64+
/// the node's position within the owning item in any way, but there is a
65+
/// guarantee that the `LocalItemId`s within an owner occupy a dense range of
66+
/// integers starting at zero, so a mapping that maps all or most nodes within
67+
/// an "item-like" to something else can be implemented by a `Vec` instead of a
68+
/// tree or hash map.
69+
pub struct ItemLocalId { .. }
70+
}
71+
rustc_data_structures::impl_stable_hash_via_hash!(ItemLocalId);
72+
73+
/// The `HirId` corresponding to `CRATE_NODE_ID` and `CRATE_DEF_INDEX`.
74+
pub const CRATE_HIR_ID: HirId =
75+
HirId { owner: CRATE_DEF_INDEX, local_id: ItemLocalId::from_u32_const(0) };
76+
77+
pub const DUMMY_HIR_ID: HirId = HirId { owner: CRATE_DEF_INDEX, local_id: DUMMY_ITEM_LOCAL_ID };
78+
79+
pub const DUMMY_ITEM_LOCAL_ID: ItemLocalId = ItemLocalId::MAX;

src/librustc_hir/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
#![feature(specialization)]
22

33
pub mod def_id;
4+
pub mod hir_id;
5+
pub use hir_id::*;

0 commit comments

Comments
 (0)