1use std::fmt;
23use rustc_data_structures::stable_hasher::{StableHash, StableHashCtxt, StableHasher};
4use rustc_span::LocalExpnId;
56impl ::std::fmt::Debug for NodeId {
fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
fmt.write_fmt(format_args!("NodeId({0})", self.as_u32()))
}
}rustc_index::newtype_index! {
7/// Identifies an AST node.
8 ///
9 /// This identifies top-level definitions, expressions, and everything in between.
10 /// This is later turned into [`DefId`] and `HirId` for the HIR.
11 ///
12 /// [`DefId`]: rustc_span::def_id::DefId
13#[encodable]
14 #[orderable]
15 #[debug_format = "NodeId({})"]
16pub struct NodeId {
17/// The [`NodeId`] used to represent the root of the crate.
18const CRATE_NODE_ID = 0;
19 }
20}2122impl StableHashfor NodeId {
23#[inline]
24fn stable_hash<Hcx: StableHashCtxt>(&self, _: &mut Hcx, _: &mut StableHasher) {
25// This impl is never called but is necessary for types implementing `StableHash` such as
26 // `MainDefinition` and `DocLinkResMap` (both of which occur in `ResolverGlobalCtxt`).
27{
::core::panicking::panic_fmt(format_args!("Node IDs should not appear in incremental state"));
};panic!("Node IDs should not appear in incremental state");
28 }
29}
3031pub type NodeMap<T> = ::rustc_data_structures::unord::UnordMap<NodeId, T>;
pub type NodeSet = ::rustc_data_structures::unord::UnordSet<NodeId>;
pub type NodeMapEntry<'a, T> =
::rustc_data_structures::fx::StdEntry<'a, NodeId, T>;rustc_data_structures::define_id_collections!(NodeMap, NodeSet, NodeMapEntry, NodeId);
3233/// When parsing and at the beginning of doing expansions, we initially give all AST nodes
34/// this dummy AST [`NodeId`]. Then, during a later phase of expansion, we renumber them
35/// to have small, positive IDs.
36pub const DUMMY_NODE_ID: NodeId = NodeId::MAX;
3738impl NodeId {
39pub fn placeholder_from_expn_id(expn_id: LocalExpnId) -> Self {
40NodeId::from_u32(expn_id.as_u32())
41 }
4243pub fn placeholder_to_expn_id(self) -> LocalExpnId {
44LocalExpnId::from_u32(self.as_u32())
45 }
46}
4748impl fmt::Displayfor NodeId {
49fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50 fmt::Display::fmt(&self.as_u32(), f)
51 }
52}