1#![allow(internal_features)]
6#![feature(negative_impls)]
7#![feature(register_tool)]
8#![feature(rustc_attrs)]
9#![register_tool(lint)]
10pub mod def_path_hash_map;
13pub mod definitions;
14
15#[cfg(test)]
16mod tests;
17
18use std::fmt::{self, Debug};
19
20use rustc_data_structures::stable_hash::{
21 StableHash, StableHashCtxt, StableHasher, StableOrd, ToStableHashKey,
22};
23use rustc_macros::{Decodable, Encodable, StableHash};
24use rustc_serialize::ContextOrd;
25use rustc_span::def_id::{CRATE_DEF_ID, DefId, DefIndex, DefPathHash, LocalDefId, LocalModId};
26
27#[derive(#[automatically_derived]impl ::core::marker::Copy for OwnerId { }Copy, #[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
for OwnerId {
}
#[automatically_derived]impl ::core::clone::Clone for OwnerId {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self {
let _: ::core::clone::AssertParamIsClone<LocalDefId>;
*self
}
}Clone, #[automatically_derived]impl ::core::marker::StructuralPartialEq for OwnerId {
}
#[automatically_derived]impl ::core::cmp::PartialEq for OwnerId {
#[inline]
fn eq(&self, other: &Self) -> bool { self.def_id == other.def_id }
}PartialEq, #[automatically_derived]impl ::core::cmp::Eq for OwnerId {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<LocalDefId>;
}
}Eq, #[automatically_derived]impl ::core::hash::Hash for OwnerId {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.def_id, state)
}
}Hash, const _: () =
{
impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
for OwnerId {
fn encode(&self, __encoder: &mut __E) {
let OwnerId { def_id: ref __binding_0 } = *self;
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
}
};Encodable, const _: () =
{
impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
for OwnerId {
fn decode(__decoder: &mut __D) -> Self {
OwnerId {
def_id: ::rustc_serialize::Decodable::decode(__decoder),
}
}
}
};Decodable)]
28pub struct OwnerId {
29 pub def_id: LocalDefId,
30}
31
32impl Debug for OwnerId {
33 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34 Debug::fmt(&self.def_id, f)
36 }
37}
38
39impl From<OwnerId> for HirId {
40 fn from(owner: OwnerId) -> HirId {
41 HirId { owner, local_id: ItemLocalId::ZERO }
42 }
43}
44
45impl From<OwnerId> for DefId {
46 fn from(value: OwnerId) -> Self {
47 value.to_def_id()
48 }
49}
50
51impl From<LocalModId> for OwnerId {
52 fn from(value: LocalModId) -> Self {
53 OwnerId { def_id: value.to_local_def_id() }
54 }
55}
56
57impl OwnerId {
58 #[inline]
59 pub fn to_def_id(self) -> DefId {
60 self.def_id.to_def_id()
61 }
62}
63
64impl rustc_index::Idx for OwnerId {
65 #[inline]
66 fn new(idx: usize) -> Self {
67 OwnerId { def_id: LocalDefId { local_def_index: DefIndex::from_usize(idx) } }
68 }
69
70 #[inline]
71 fn index(self) -> usize {
72 self.def_id.local_def_index.as_usize()
73 }
74}
75
76impl StableHash for OwnerId {
77 #[inline]
78 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
79 self.to_stable_hash_key(hcx).stable_hash(hcx, hasher);
80 }
81}
82
83impl ToStableHashKey for OwnerId {
84 type KeyType = DefPathHash;
85
86 #[inline]
87 fn to_stable_hash_key<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx) -> DefPathHash {
88 self.to_def_id().to_stable_hash_key(hcx)
89 }
90}
91
92#[derive(#[automatically_derived]impl ::core::marker::Copy for HirId { }Copy, #[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
for HirId {
}
#[automatically_derived]impl ::core::clone::Clone for HirId {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self {
let _: ::core::clone::AssertParamIsClone<OwnerId>;
let _: ::core::clone::AssertParamIsClone<ItemLocalId>;
*self
}
}Clone, #[automatically_derived]impl ::core::marker::StructuralPartialEq for HirId { }
#[automatically_derived]impl ::core::cmp::PartialEq for HirId {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.owner == other.owner && self.local_id == other.local_id
}
}PartialEq, #[automatically_derived]impl ::core::cmp::Eq for HirId {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<OwnerId>;
let _: ::core::cmp::AssertParamIsEq<ItemLocalId>;
}
}Eq, #[automatically_derived]impl ::core::hash::Hash for HirId {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.owner, state);
::core::hash::Hash::hash(&self.local_id, state)
}
}Hash, const _: () =
{
impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
for HirId {
fn encode(&self, __encoder: &mut __E) {
let HirId { owner: ref __binding_0, local_id: ref __binding_1
} = *self;
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
}
}
};Encodable, const _: () =
{
impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
for HirId {
fn decode(__decoder: &mut __D) -> Self {
HirId {
owner: ::rustc_serialize::Decodable::decode(__decoder),
local_id: ::rustc_serialize::Decodable::decode(__decoder),
}
}
}
};Decodable, const _: () =
{
impl ::rustc_data_structures::stable_hash::StableHash for HirId {
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hash::StableHasher) {
match *self {
HirId { owner: ref __binding_0, local_id: ref __binding_1 }
=> {
{ __binding_0.stable_hash(__hcx, __hasher); }
{ __binding_1.stable_hash(__hcx, __hasher); }
}
}
}
}
};StableHash)]
103#[cfg_attr(bootstrap, rustc_pass_by_value)]
104#[cfg_attr(not(bootstrap), lint::pass_by_value)]
105pub struct HirId {
106 pub owner: OwnerId,
107 pub local_id: ItemLocalId,
108}
109
110impl !Ord for HirId {}
114impl !PartialOrd for HirId {}
115
116impl Debug for HirId {
117 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
118 f.write_fmt(format_args!("HirId({0:?}.{1:?})", self.owner, self.local_id))write!(f, "HirId({:?}.{:?})", self.owner, self.local_id)
121 }
122}
123
124impl HirId {
125 pub const INVALID: HirId =
127 HirId { owner: OwnerId { def_id: CRATE_DEF_ID }, local_id: ItemLocalId::INVALID };
128
129 #[inline]
130 pub fn expect_owner(self) -> OwnerId {
131 {
match (&self.local_id.index(), &0) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
}
};assert_eq!(self.local_id.index(), 0);
132 self.owner
133 }
134
135 #[inline]
136 pub fn as_owner(self) -> Option<OwnerId> {
137 if self.local_id.index() == 0 { Some(self.owner) } else { None }
138 }
139
140 #[inline]
141 pub fn is_owner(self) -> bool {
142 self.local_id.index() == 0
143 }
144
145 #[inline]
146 pub fn make_owner(owner: LocalDefId) -> Self {
147 Self { owner: OwnerId { def_id: owner }, local_id: ItemLocalId::ZERO }
148 }
149}
150
151impl fmt::Display for HirId {
152 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
153 f.write_fmt(format_args!("{0:?}", self))write!(f, "{self:?}")
154 }
155}
156
157impl From<LocalModId> for HirId {
158 fn from(id: LocalModId) -> Self {
159 HirId::make_owner(id.to_local_def_id())
160 }
161}
162
163pub type HirIdMap<T> = ::rustc_data_structures::fx::FxIndexMap<HirId, T>;
pub type HirIdSet = ::rustc_data_structures::fx::FxIndexSet<HirId>;
pub type HirIdMapEntry<'a, T> =
::rustc_data_structures::fx::IndexEntry<'a, HirId, T>;rustc_data_structures::define_stable_id_collections!(HirIdMap, HirIdSet, HirIdMapEntry, HirId);
164pub type ItemLocalMap<T> =
::rustc_data_structures::unord::UnordMap<ItemLocalId, T>;
pub type ItemLocalSet = ::rustc_data_structures::unord::UnordSet<ItemLocalId>;
pub type ItemLocalMapEntry<'a, T> =
::rustc_data_structures::fx::StdEntry<'a, ItemLocalId, T>;rustc_data_structures::define_id_collections!(
165 ItemLocalMap,
166 ItemLocalSet,
167 ItemLocalMapEntry,
168 ItemLocalId
169);
170
171#[automatically_derived]impl ::core::marker::Copy for ItemLocalId { }
impl ItemLocalId {
#[doc = r" Maximum value the index can take, as a `u32`."]
pub const MAX_AS_U32: u32 = 0xFFFF_FF00;
#[doc = r" Maximum value the index can take."]
pub const MAX: Self = Self::from_u32(0xFFFF_FF00);
#[doc = r" Zero value of the index."]
pub const ZERO: Self = Self::from_u32(0);
#[doc = r" Creates a new index from a given `usize`."]
#[doc = r""]
#[doc = r" # Panics"]
#[doc = r""]
#[doc = r" Will panic if `value` exceeds `MAX`."]
#[inline]
pub const fn from_usize(value: usize) -> Self {
if !(value <= (0xFFFF_FF00 as usize)) {
::core::panicking::panic("assertion failed: value <= (0xFFFF_FF00 as usize)")
};
unsafe { Self::from_u32_unchecked(value as u32) }
}
#[doc = r" Creates a new index from a given `u32`."]
#[doc = r""]
#[doc = r" # Panics"]
#[doc = r""]
#[doc = r" Will panic if `value` exceeds `MAX`."]
#[inline]
pub const fn from_u32(value: u32) -> Self {
if !(value <= 0xFFFF_FF00) {
::core::panicking::panic("assertion failed: value <= 0xFFFF_FF00")
};
unsafe { Self::from_u32_unchecked(value) }
}
#[doc = r" Creates a new index from a given `u16`."]
#[doc = r""]
#[doc = r" # Panics"]
#[doc = r""]
#[doc = r" Will panic if `value` exceeds `MAX`."]
#[inline]
pub const fn from_u16(value: u16) -> Self {
let value = value as u32;
if !(value <= 0xFFFF_FF00) {
::core::panicking::panic("assertion failed: value <= 0xFFFF_FF00")
};
unsafe { Self::from_u32_unchecked(value) }
}
#[doc = r" Creates a new index from a given `u32`."]
#[doc = r""]
#[doc = r" # Safety"]
#[doc = r""]
#[doc =
r" The provided value must be less than or equal to the maximum value for the newtype."]
#[doc =
r" Providing a value outside this range is undefined due to layout restrictions."]
#[doc = r""]
#[doc = r" Prefer using `from_u32`."]
#[inline]
pub const unsafe fn from_u32_unchecked(value: u32) -> Self {
Self {
private_use_as_methods_instead: unsafe {
std::mem::transmute(value)
},
}
}
#[doc = r" Extracts the value of this index as a `usize`."]
#[inline]
pub const fn index(self) -> usize { self.as_usize() }
#[doc = r" Extracts the value of this index as a `u32`."]
#[inline]
pub const fn as_u32(self) -> u32 {
unsafe { std::mem::transmute(self.private_use_as_methods_instead) }
}
#[doc = r" Extracts the value of this index as a `usize`."]
#[inline]
pub const fn as_usize(self) -> usize { self.as_u32() as usize }
}
impl std::ops::Add<usize> for ItemLocalId {
type Output = Self;
#[inline]
fn add(self, other: usize) -> Self {
Self::from_usize(self.index() + other)
}
}
impl std::ops::AddAssign<usize> for ItemLocalId {
#[inline]
fn add_assign(&mut self, other: usize) { *self = *self + other; }
}
impl rustc_index::Idx for ItemLocalId {
#[inline]
fn new(value: usize) -> Self { Self::from_usize(value) }
#[inline]
fn index(self) -> usize { self.as_usize() }
}
impl ::std::iter::Step for ItemLocalId {
#[inline]
fn steps_between(start: &Self, end: &Self) -> (usize, Option<usize>) {
<usize as
::std::iter::Step>::steps_between(&Self::index(*start),
&Self::index(*end))
}
#[inline]
fn forward_checked(start: Self, u: usize) -> Option<Self> {
Self::index(start).checked_add(u).map(Self::from_usize)
}
#[inline]
fn backward_checked(start: Self, u: usize) -> Option<Self> {
Self::index(start).checked_sub(u).map(Self::from_usize)
}
#[inline]
fn forward_overflowing(start: Self, u: usize) -> (Self, bool) {
let (s, o) = Self::index(start).overflowing_add(u);
(Self::from_usize(s), o)
}
#[inline]
fn backward_overflowing(start: Self, u: usize) -> (Self, bool) {
let (s, o) = Self::index(start).overflowing_sub(u);
(Self::from_usize(s), o)
}
}
impl ::std::cmp::Ord for ItemLocalId {
#[inline]
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.as_u32().cmp(&other.as_u32())
}
}
impl ::std::cmp::PartialOrd for ItemLocalId {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl ::rustc_data_structures::stable_hash::StableHash for ItemLocalId {
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
hcx: &mut __Hcx,
hasher: &mut ::rustc_data_structures::stable_hash::StableHasher) {
self.as_u32().stable_hash(hcx, hasher)
}
}
impl From<ItemLocalId> for u32 {
#[inline]
fn from(v: ItemLocalId) -> u32 { v.as_u32() }
}
impl From<ItemLocalId> for usize {
#[inline]
fn from(v: ItemLocalId) -> usize { v.as_usize() }
}
impl From<usize> for ItemLocalId {
#[inline]
fn from(value: usize) -> Self { Self::from_usize(value) }
}
impl From<u32> for ItemLocalId {
#[inline]
fn from(value: u32) -> Self { Self::from_u32(value) }
}
impl ::std::cmp::Eq for ItemLocalId {}
impl ::std::cmp::PartialEq for ItemLocalId {
fn eq(&self, other: &Self) -> bool { self.as_u32().eq(&other.as_u32()) }
}
impl ::std::marker::StructuralPartialEq for ItemLocalId {}
impl ::std::hash::Hash for ItemLocalId {
fn hash<H: ::std::hash::Hasher>(&self, state: &mut H) {
self.as_u32().hash(state)
}
}
impl<D: ::rustc_serialize::Decoder> ::rustc_serialize::Decodable<D> for
ItemLocalId {
fn decode(d: &mut D) -> Self { Self::from_u32(d.read_u32()) }
}
impl<E: ::rustc_serialize::Encoder> ::rustc_serialize::Encodable<E> for
ItemLocalId {
fn encode(&self, e: &mut E) { e.emit_u32(self.as_u32()); }
}
impl ::std::fmt::Debug for ItemLocalId {
fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
fmt.write_fmt(format_args!("{0}", self.as_u32()))
}
}rustc_index::newtype_index! {
172 #[stable_hash]
181 #[encodable]
182 #[orderable]
183 pub struct ItemLocalId {}
184}
185
186impl<E> ContextOrd<E> for ItemLocalId {
187 fn stable_cmp_with_context(_context: E) -> impl FnMut(&Self, &Self) -> std::cmp::Ordering {
188 <Self as Ord>::cmp
189 }
190}
191
192impl ItemLocalId {
193 pub const INVALID: ItemLocalId = ItemLocalId::MAX;
195}
196
197impl StableOrd for ItemLocalId {
198 const CAN_USE_UNSTABLE_SORT: bool = true;
199
200 const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
203}
204
205pub const CRATE_HIR_ID: HirId =
207 HirId { owner: OwnerId { def_id: CRATE_DEF_ID }, local_id: ItemLocalId::ZERO };
208
209pub const CRATE_OWNER_ID: OwnerId = OwnerId { def_id: CRATE_DEF_ID };