1use std::fmt;
2use std::hash::{BuildHasherDefault, Hash, Hasher};
34use rustc_data_structures::AtomicRef;
5use rustc_data_structures::fingerprint::Fingerprint;
6use rustc_data_structures::stable_hasher::{
7RawDefId, RawDefPathHash, StableHash, StableHashCtxt, StableHasher, StableOrd, ToStableHashKey,
8};
9use rustc_data_structures::unhash::Unhasher;
10use rustc_hashes::Hash64;
11use rustc_index::Idx;
12use rustc_macros::{BlobDecodable, Decodable, Encodable, StableHash};
13use rustc_serialize::{Decodable, Encodable};
1415use crate::{SpanDecoder, SpanEncoder, Symbol};
1617pub type StableCrateIdMap =
18 indexmap::IndexMap<StableCrateId, CrateNum, BuildHasherDefault<Unhasher>>;
1920impl ::std::fmt::Debug for CrateNum {
fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
fmt.write_fmt(format_args!("crate{0}", self.as_u32()))
}
}rustc_index::newtype_index! {
21#[orderable]
22 #[debug_format = "crate{}"]
23pub struct CrateNum {}
24}2526/// Item definitions in the currently-compiled crate would have the `CrateNum`
27/// `LOCAL_CRATE` in their `DefId`.
28pub const LOCAL_CRATE: CrateNum = CrateNum::ZERO;
2930impl CrateNum {
31#[inline]
32pub fn new(x: usize) -> CrateNum {
33CrateNum::from_usize(x)
34 }
3536// FIXME(typed_def_id): Replace this with `as_mod_def_id`.
37#[inline]
38pub fn as_def_id(self) -> DefId {
39DefId { krate: self, index: CRATE_DEF_INDEX }
40 }
4142#[inline]
43pub fn as_mod_def_id(self) -> ModDefId {
44ModDefId::new_unchecked(DefId { krate: self, index: CRATE_DEF_INDEX })
45 }
46}
4748impl fmt::Displayfor CrateNum {
49fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50 fmt::Display::fmt(&self.as_u32(), f)
51 }
52}
5354/// A `DefPathHash` is a fixed-size representation of a `DefPath` that is
55/// stable across crate and compilation session boundaries. It consists of two
56/// separate 64-bit hashes. The first uniquely identifies the crate this
57/// `DefPathHash` originates from (see [StableCrateId]), and the second
58/// uniquely identifies the corresponding `DefPath` within that crate. Together
59/// they form a unique identifier within an entire crate graph.
60///
61/// There is a very small chance of hash collisions, which would mean that two
62/// different `DefPath`s map to the same `DefPathHash`. Proceeding compilation
63/// with such a hash collision would very probably lead to an ICE, and in the
64/// worst case lead to a silent mis-compilation. The compiler therefore actively
65/// and exhaustively checks for such hash collisions and aborts compilation if
66/// it finds one.
67///
68/// `DefPathHash` uses 64-bit hashes for both the crate-id part and the
69/// crate-internal part, even though it is likely that there are many more
70/// `LocalDefId`s in a single crate than there are individual crates in a crate
71/// graph. Since we use the same number of bits in both cases, the collision
72/// probability for the crate-local part will be quite a bit higher (though
73/// still very small).
74///
75/// This imbalance is not by accident: A hash collision in the
76/// crate-local part of a `DefPathHash` will be detected and reported while
77/// compiling the crate in question. Such a collision does not depend on
78/// outside factors and can be easily fixed by the crate maintainer (e.g. by
79/// renaming the item in question or by bumping the crate version in a harmless
80/// way).
81///
82/// A collision between crate-id hashes on the other hand is harder to fix
83/// because it depends on the set of crates in the entire crate graph of a
84/// compilation session. Again, using the same crate with a different version
85/// number would fix the issue with a high probability -- but that might be
86/// easier said than done if the crates in questions are dependencies of
87/// third-party crates.
88///
89/// That being said, given a high quality hash function, the collision
90/// probabilities in question are very small. For example, for a big crate like
91/// `rustc_middle` (with ~50000 `LocalDefId`s as of the time of writing) there
92/// is a probability of roughly 1 in 14,750,000,000 of a crate-internal
93/// collision occurring. For a big crate graph with 1000 crates in it, there is
94/// a probability of 1 in 36,890,000,000,000 of a `StableCrateId` collision.
95#[derive(#[automatically_derived]
impl ::core::marker::Copy for DefPathHash { }Copy, #[automatically_derived]
impl ::core::clone::Clone for DefPathHash {
#[inline]
fn clone(&self) -> DefPathHash {
let _: ::core::clone::AssertParamIsClone<Fingerprint>;
*self
}
}Clone, #[automatically_derived]
impl ::core::hash::Hash for DefPathHash {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.0, state)
}
}Hash, #[automatically_derived]
impl ::core::cmp::PartialEq for DefPathHash {
#[inline]
fn eq(&self, other: &DefPathHash) -> bool { self.0 == other.0 }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for DefPathHash {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<Fingerprint>;
}
}Eq, #[automatically_derived]
impl ::core::cmp::PartialOrd for DefPathHash {
#[inline]
fn partial_cmp(&self, other: &DefPathHash)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::cmp::PartialOrd::partial_cmp(&self.0, &other.0)
}
}PartialOrd, #[automatically_derived]
impl ::core::cmp::Ord for DefPathHash {
#[inline]
fn cmp(&self, other: &DefPathHash) -> ::core::cmp::Ordering {
::core::cmp::Ord::cmp(&self.0, &other.0)
}
}Ord, #[automatically_derived]
impl ::core::fmt::Debug for DefPathHash {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f, "DefPathHash",
&&self.0)
}
}Debug)]
96#[derive(const _: () =
{
impl ::rustc_data_structures::stable_hasher::StableHash for
DefPathHash {
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hasher::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hasher::StableHasher) {
match *self {
DefPathHash(ref __binding_0) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
}
}
}
}
};StableHash, const _: () =
{
impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
for DefPathHash {
fn encode(&self, __encoder: &mut __E) {
match *self {
DefPathHash(ref __binding_0) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
}
}
}
};Encodable, const _: () =
{
impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
for DefPathHash {
fn decode(__decoder: &mut __D) -> Self {
DefPathHash(::rustc_serialize::Decodable::decode(__decoder))
}
}
};Decodable)]
97pub struct DefPathHash(pub Fingerprint);
9899impl DefPathHash {
100/// Returns the [StableCrateId] identifying the crate this [DefPathHash]
101 /// originates from.
102#[inline]
103pub fn stable_crate_id(&self) -> StableCrateId {
104StableCrateId(self.0.split().0)
105 }
106107/// Returns the crate-local part of the [DefPathHash].
108#[inline]
109pub fn local_hash(&self) -> Hash64 {
110self.0.split().1
111}
112113/// Builds a new [DefPathHash] with the given [StableCrateId] and
114 /// `local_hash`, where `local_hash` must be unique within its crate.
115#[inline]
116pub fn new(stable_crate_id: StableCrateId, local_hash: Hash64) -> DefPathHash {
117DefPathHash(Fingerprint::new(stable_crate_id.0, local_hash))
118 }
119120#[inline]
121pub fn to_raw_def_path_hash(self) -> RawDefPathHash {
122RawDefPathHash(self.0.to_le_bytes())
123 }
124125#[inline]
126pub fn from_raw_def_path_hash(RawDefPathHash(a): RawDefPathHash) -> DefPathHash {
127DefPathHash(Fingerprint::from_le_bytes(a))
128 }
129}
130131impl Defaultfor DefPathHash {
132fn default() -> Self {
133DefPathHash(Fingerprint::ZERO)
134 }
135}
136137impl StableOrdfor DefPathHash {
138const CAN_USE_UNSTABLE_SORT: bool = true;
139140// `DefPathHash` sort order is not affected by (de)serialization.
141const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
142}
143144/// A [`StableCrateId`] is a 64-bit hash of a crate name, together with all
145/// `-Cmetadata` arguments, and some other data. It is to [`CrateNum`] what [`DefPathHash`] is to
146/// [`DefId`]. It is stable across compilation sessions.
147///
148/// Since the ID is a hash value, there is a small chance that two crates
149/// end up with the same [`StableCrateId`]. The compiler will check for such
150/// collisions when loading crates and abort compilation in order to avoid
151/// further trouble.
152///
153/// For more information on the possibility of hash collisions in rustc,
154/// see the discussion in [`DefId`].
155#[derive(#[automatically_derived]
impl ::core::marker::Copy for StableCrateId { }Copy, #[automatically_derived]
impl ::core::clone::Clone for StableCrateId {
#[inline]
fn clone(&self) -> StableCrateId {
let _: ::core::clone::AssertParamIsClone<Hash64>;
*self
}
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for StableCrateId {
#[inline]
fn eq(&self, other: &StableCrateId) -> bool { self.0 == other.0 }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for StableCrateId {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<Hash64>;
}
}Eq, #[automatically_derived]
impl ::core::cmp::PartialOrd for StableCrateId {
#[inline]
fn partial_cmp(&self, other: &StableCrateId)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::cmp::PartialOrd::partial_cmp(&self.0, &other.0)
}
}PartialOrd, #[automatically_derived]
impl ::core::cmp::Ord for StableCrateId {
#[inline]
fn cmp(&self, other: &StableCrateId) -> ::core::cmp::Ordering {
::core::cmp::Ord::cmp(&self.0, &other.0)
}
}Ord, #[automatically_derived]
impl ::core::fmt::Debug for StableCrateId {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f, "StableCrateId",
&&self.0)
}
}Debug)]
156#[derive(#[automatically_derived]
impl ::core::hash::Hash for StableCrateId {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.0, state)
}
}Hash, const _: () =
{
impl ::rustc_data_structures::stable_hasher::StableHash for
StableCrateId {
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hasher::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hasher::StableHasher) {
match *self {
StableCrateId(ref __binding_0) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
}
}
}
}
};StableHash, const _: () =
{
impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
for StableCrateId {
fn encode(&self, __encoder: &mut __E) {
match *self {
StableCrateId(ref __binding_0) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
}
}
}
};Encodable, const _: () =
{
impl<__D: ::rustc_span::BlobDecoder> ::rustc_serialize::Decodable<__D>
for StableCrateId {
fn decode(__decoder: &mut __D) -> Self {
StableCrateId(::rustc_serialize::Decodable::decode(__decoder))
}
}
};BlobDecodable)]
157pub struct StableCrateId(pub(crate) Hash64);
158159impl StableCrateId {
160/// Computes the stable ID for a crate with the given name and
161 /// `-Cmetadata` arguments.
162pub fn new(
163 crate_name: Symbol,
164 is_exe: bool,
165mut metadata: Vec<String>,
166 cfg_version: &'static str,
167 ) -> StableCrateId {
168let mut hasher = StableHasher::new();
169// We must hash the string text of the crate name, not the id, as the id is not stable
170 // across builds.
171crate_name.as_str().hash(&mut hasher);
172173// We don't want the stable crate ID to depend on the order of
174 // -C metadata arguments, so sort them:
175metadata.sort();
176// Every distinct -C metadata value is only incorporated once:
177metadata.dedup();
178179hasher.write(b"metadata");
180for s in &metadata {
181// Also incorporate the length of a metadata string, so that we generate
182 // different values for `-Cmetadata=ab -Cmetadata=c` and
183 // `-Cmetadata=a -Cmetadata=bc`
184hasher.write_usize(s.len());
185 hasher.write(s.as_bytes());
186 }
187188// Also incorporate crate type, so that we don't get symbol conflicts when
189 // linking against a library of the same name, if this is an executable.
190hasher.write(if is_exe { b"exe" } else { b"lib" });
191192// Also incorporate the rustc version. Otherwise, with -Zsymbol-mangling-version=v0
193 // and no -Cmetadata, symbols from the same crate compiled with different versions of
194 // rustc are named the same.
195 //
196 // RUSTC_FORCE_RUSTC_VERSION is used to inject rustc version information
197 // during testing.
198if let Some(val) = std::env::var_os("RUSTC_FORCE_RUSTC_VERSION") {
199hasher.write(val.to_string_lossy().into_owned().as_bytes())
200 } else {
201hasher.write(cfg_version.as_bytes())
202 }
203204StableCrateId(hasher.finish())
205 }
206207#[inline]
208pub fn as_u64(self) -> u64 {
209self.0.as_u64()
210 }
211}
212213impl fmt::LowerHexfor StableCrateId {
214fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
215 fmt::LowerHex::fmt(&self.0, f)
216 }
217}
218219impl ::std::fmt::Debug for DefIndex {
fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
fmt.write_fmt(format_args!("DefIndex({0})", self.as_u32()))
}
}rustc_index::newtype_index! {
220/// A DefIndex is an index into the hir-map for a crate, identifying a
221 /// particular definition. It should really be considered an interned
222 /// shorthand for a particular DefPath.
223#[orderable]
224 #[debug_format = "DefIndex({})"]
225pub struct DefIndex {
226/// The crate root is always assigned index 0 by the AST Map code,
227 /// thanks to `NodeCollector::new`.
228const CRATE_DEF_INDEX = 0;
229 }
230}231232/// A `DefId` identifies a particular *definition*, by combining a crate
233/// index and a def index.
234///
235/// You can create a `DefId` from a `LocalDefId` using `local_def_id.to_def_id()`.
236#[derive(#[automatically_derived]
impl ::core::clone::Clone for DefId {
#[inline]
fn clone(&self) -> DefId {
let _: ::core::clone::AssertParamIsClone<DefIndex>;
let _: ::core::clone::AssertParamIsClone<CrateNum>;
*self
}
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for DefId {
#[inline]
fn eq(&self, other: &DefId) -> bool {
self.index == other.index && self.krate == other.krate
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for DefId {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<DefIndex>;
let _: ::core::cmp::AssertParamIsEq<CrateNum>;
}
}Eq, #[automatically_derived]
impl ::core::marker::Copy for DefId { }Copy)]
237// On below-64 bit systems we can simply use the derived `Hash` impl
238#[cfg_attr(not(target_pointer_width = "64"), derive(Hash))]
239#[repr(C)]
240#[rustc_pass_by_value]
241// We guarantee field order. Note that the order is essential here, see below why.
242pub struct DefId {
243// cfg-ing the order of fields so that the `DefIndex` which is high entropy always ends up in
244 // the lower bits no matter the endianness. This allows the compiler to turn that `Hash` impl
245 // into a direct call to `u64::hash(_)`.
246#[cfg(not(all(target_pointer_width = "64", target_endian = "big")))]
247pub index: DefIndex,
248pub krate: CrateNum,
249#[cfg(all(target_pointer_width = "64", target_endian = "big"))]
250pub index: DefIndex,
251}
252253// To ensure correctness of incremental compilation,
254// `DefId` must not implement `Ord` or `PartialOrd`.
255// See https://github.com/rust-lang/rust/issues/90317.
256impl !Ordfor DefId {}
257impl !PartialOrdfor DefId {}
258259// On 64-bit systems, we can hash the whole `DefId` as one `u64` instead of two `u32`s. This
260// improves performance without impairing `FxHash` quality. So the below code gets compiled to a
261// noop on little endian systems because the memory layout of `DefId` is as follows:
262//
263// ```
264// +-1--------------31-+-32-------------63-+
265// ! index ! krate !
266// +-------------------+-------------------+
267// ```
268//
269// The order here has direct impact on `FxHash` quality because we have far more `DefIndex` per
270// crate than we have `Crate`s within one compilation. Or in other words, this arrangement puts
271// more entropy in the low bits than the high bits. The reason this matters is that `FxHash`, which
272// is used throughout rustc, has problems distributing the entropy from the high bits, so reversing
273// the order would lead to a large number of collisions and thus far worse performance.
274//
275// On 64-bit big-endian systems, this compiles to a 64-bit rotation by 32 bits, which is still
276// faster than another `FxHash` round.
277#[cfg(target_pointer_width = "64")]
278impl Hashfor DefId {
279fn hash<H: Hasher>(&self, h: &mut H) {
280 (((self.krate.as_u32() as u64) << 32) | (self.index.as_u32() as u64)).hash(h)
281 }
282}
283284impl DefId {
285/// Makes a local `DefId` from the given `DefIndex`.
286#[inline]
287pub fn local(index: DefIndex) -> DefId {
288DefId { krate: LOCAL_CRATE, index }
289 }
290291/// Returns whether the item is defined in the crate currently being compiled.
292#[inline]
293pub fn is_local(self) -> bool {
294self.krate == LOCAL_CRATE295 }
296297#[inline]
298pub fn as_local(self) -> Option<LocalDefId> {
299self.is_local().then(|| LocalDefId { local_def_index: self.index })
300 }
301302#[inline]
303 #[track_caller]
304pub fn expect_local(self) -> LocalDefId {
305// NOTE: `match` below is required to apply `#[track_caller]`,
306 // i.e. don't use closures.
307match self.as_local() {
308Some(local_def_id) => local_def_id,
309None => {
::core::panicking::panic_fmt(format_args!("DefId::expect_local: `{0:?}` isn\'t local",
self));
}panic!("DefId::expect_local: `{self:?}` isn't local"),
310 }
311 }
312313#[inline]
314pub fn is_crate_root(self) -> bool {
315self.index == CRATE_DEF_INDEX316 }
317318#[inline]
319pub fn as_crate_root(self) -> Option<CrateNum> {
320self.is_crate_root().then_some(self.krate)
321 }
322323#[inline]
324pub fn is_top_level_module(self) -> bool {
325self.is_local() && self.is_crate_root()
326 }
327328#[inline]
329pub fn to_raw_def_id(self) -> RawDefId {
330// Field order must match `from_raw_def_id`.
331RawDefId(self.krate.as_u32(), self.index.as_u32())
332 }
333334#[inline]
335pub fn from_raw_def_id(RawDefId(a, b): RawDefId) -> DefId {
336// Field order must match `to_raw_def_id`.
337DefId { krate: a.into(), index: b.into() }
338 }
339}
340341impl From<LocalDefId> for DefId {
342fn from(local: LocalDefId) -> DefId {
343local.to_def_id()
344 }
345}
346347pub fn default_def_id_debug(def_id: DefId, f: &mut fmt::Formatter<'_>) -> fmt::Result {
348f.debug_struct("DefId").field("krate", &def_id.krate).field("index", &def_id.index).finish()
349}
350351pub static DEF_ID_DEBUG: AtomicRef<fn(DefId, &mut fmt::Formatter<'_>) -> fmt::Result> =
352AtomicRef::new(&(default_def_id_debugas fn(_, &mut fmt::Formatter<'_>) -> _));
353354impl fmt::Debugfor DefId {
355fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
356 (*DEF_ID_DEBUG)(*self, f)
357 }
358}
359360pub type DefIdMap<T> = ::rustc_data_structures::unord::UnordMap<DefId, T>;
pub type DefIdSet = ::rustc_data_structures::unord::UnordSet<DefId>;
pub type DefIdMapEntry<'a, T> =
::rustc_data_structures::fx::StdEntry<'a, DefId, T>;rustc_data_structures::define_id_collections!(DefIdMap, DefIdSet, DefIdMapEntry, DefId);
361362/// A `LocalDefId` is equivalent to a `DefId` with `krate == LOCAL_CRATE`. Since
363/// we encode this information in the type, we can ensure at compile time that
364/// no `DefId`s from upstream crates get thrown into the mix. There are quite a
365/// few cases where we know that only `DefId`s from the local crate are expected;
366/// a `DefId` from a different crate would signify a bug somewhere. This
367/// is when `LocalDefId` comes in handy.
368#[derive(#[automatically_derived]
impl ::core::clone::Clone for LocalDefId {
#[inline]
fn clone(&self) -> LocalDefId {
let _: ::core::clone::AssertParamIsClone<DefIndex>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for LocalDefId { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for LocalDefId {
#[inline]
fn eq(&self, other: &LocalDefId) -> bool {
self.local_def_index == other.local_def_index
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for LocalDefId {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<DefIndex>;
}
}Eq, #[automatically_derived]
impl ::core::hash::Hash for LocalDefId {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.local_def_index, state)
}
}Hash)]
369pub struct LocalDefId {
370pub local_def_index: DefIndex,
371}
372373// To ensure correctness of incremental compilation,
374// `LocalDefId` must not implement `Ord` or `PartialOrd`.
375// See https://github.com/rust-lang/rust/issues/90317.
376impl !Ordfor LocalDefId {}
377impl !PartialOrdfor LocalDefId {}
378379pub const CRATE_DEF_ID: LocalDefId = LocalDefId { local_def_index: CRATE_DEF_INDEX };
380381impl Idxfor LocalDefId {
382#[inline]
383fn new(idx: usize) -> Self {
384LocalDefId { local_def_index: Idx::new(idx) }
385 }
386#[inline]
387fn index(self) -> usize {
388self.local_def_index.index()
389 }
390}
391392impl LocalDefId {
393#[inline]
394pub fn to_def_id(self) -> DefId {
395DefId { krate: LOCAL_CRATE, index: self.local_def_index }
396 }
397398#[inline]
399pub fn is_top_level_module(self) -> bool {
400self == CRATE_DEF_ID401 }
402}
403404impl fmt::Debugfor LocalDefId {
405fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
406self.to_def_id().fmt(f)
407 }
408}
409410impl<E: SpanEncoder> Encodable<E> for LocalDefId {
411fn encode(&self, s: &mut E) {
412self.to_def_id().encode(s);
413 }
414}
415416impl<D: SpanDecoder> Decodable<D> for LocalDefId {
417fn decode(d: &mut D) -> LocalDefId {
418DefId::decode(d).expect_local()
419 }
420}
421422pub type LocalDefIdMap<T> =
::rustc_data_structures::unord::UnordMap<LocalDefId, T>;
pub type LocalDefIdSet = ::rustc_data_structures::unord::UnordSet<LocalDefId>;
pub type LocalDefIdMapEntry<'a, T> =
::rustc_data_structures::fx::StdEntry<'a, LocalDefId, T>;rustc_data_structures::define_id_collections!(
423LocalDefIdMap,
424LocalDefIdSet,
425LocalDefIdMapEntry,
426 LocalDefId
427);
428429impl StableHashfor DefId {
430#[inline]
431fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
432self.to_stable_hash_key(hcx).stable_hash(hcx, hasher);
433 }
434}
435436impl StableHashfor LocalDefId {
437#[inline]
438fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
439self.to_stable_hash_key(hcx).local_hash().stable_hash(hcx, hasher);
440 }
441}
442443impl StableHashfor CrateNum {
444#[inline]
445fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
446self.as_def_id().to_stable_hash_key(hcx).stable_crate_id().stable_hash(hcx, hasher);
447 }
448}
449450impl ToStableHashKeyfor DefId {
451type KeyType = DefPathHash;
452453#[inline]
454fn to_stable_hash_key<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx) -> DefPathHash {
455DefPathHash::from_raw_def_path_hash(hcx.def_path_hash(self.to_raw_def_id()))
456 }
457}
458459impl ToStableHashKeyfor LocalDefId {
460type KeyType = DefPathHash;
461462#[inline]
463fn to_stable_hash_key<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx) -> DefPathHash {
464self.to_def_id().to_stable_hash_key(hcx)
465 }
466}
467468impl ToStableHashKeyfor CrateNum {
469type KeyType = DefPathHash;
470471#[inline]
472fn to_stable_hash_key<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx) -> DefPathHash {
473self.as_def_id().to_stable_hash_key(hcx)
474 }
475}
476477impl ToStableHashKeyfor DefPathHash {
478type KeyType = DefPathHash;
479480#[inline]
481fn to_stable_hash_key<Hcx>(&self, _: &mut Hcx) -> DefPathHash {
482*self483 }
484}
485486macro_rules!typed_def_id {
487 ($Name:ident, $LocalName:ident) => {
488#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Encodable, Decodable, StableHash)]
489pub struct $Name(DefId);
490491impl $Name {
492#[inline]
493pub const fn new_unchecked(def_id: DefId) -> Self {
494Self(def_id)
495 }
496497#[inline]
498pub fn to_def_id(self) -> DefId {
499self.into()
500 }
501502#[inline]
503pub fn is_local(self) -> bool {
504self.0.is_local()
505 }
506507#[inline]
508pub fn as_local(self) -> Option<$LocalName> {
509self.0.as_local().map($LocalName::new_unchecked)
510 }
511 }
512513impl From<$LocalName> for $Name {
514#[inline]
515fn from(local: $LocalName) -> Self {
516Self(local.0.to_def_id())
517 }
518 }
519520impl From<$Name> for DefId {
521#[inline]
522fn from(typed: $Name) -> Self {
523 typed.0
524}
525 }
526527#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Encodable, Decodable, StableHash)]
528pub struct $LocalName(LocalDefId);
529530impl !Ord for $LocalName {}
531impl !PartialOrd for $LocalName {}
532533impl $LocalName {
534#[inline]
535pub const fn new_unchecked(def_id: LocalDefId) -> Self {
536Self(def_id)
537 }
538539#[inline]
540pub fn to_def_id(self) -> DefId {
541self.0.into()
542 }
543544#[inline]
545pub fn to_local_def_id(self) -> LocalDefId {
546self.0
547}
548 }
549550impl From<$LocalName> for LocalDefId {
551#[inline]
552fn from(typed: $LocalName) -> Self {
553 typed.0
554}
555 }
556557impl From<$LocalName> for DefId {
558#[inline]
559fn from(typed: $LocalName) -> Self {
560 typed.0.into()
561 }
562 }
563 };
564}
565566// N.B.: when adding new typed `DefId`s update the corresponding trait impls in
567// `rustc_middle::dep_graph::dep_node_key` for `DepNodeKey`.
568pub struct ModDefId(DefId);
#[automatically_derived]
impl ::core::fmt::Debug for ModDefId {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f, "ModDefId",
&&self.0)
}
}
#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for ModDefId { }
#[automatically_derived]
impl ::core::clone::Clone for ModDefId {
#[inline]
fn clone(&self) -> ModDefId {
let _: ::core::clone::AssertParamIsClone<DefId>;
*self
}
}
#[automatically_derived]
impl ::core::marker::Copy for ModDefId { }
#[automatically_derived]
impl ::core::marker::StructuralPartialEq for ModDefId { }
#[automatically_derived]
impl ::core::cmp::PartialEq for ModDefId {
#[inline]
fn eq(&self, other: &ModDefId) -> bool { self.0 == other.0 }
}
#[automatically_derived]
impl ::core::cmp::Eq for ModDefId {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<DefId>;
}
}
#[automatically_derived]
impl ::core::hash::Hash for ModDefId {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.0, state)
}
}
const _: () =
{
impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
for ModDefId {
fn encode(&self, __encoder: &mut __E) {
match *self {
ModDefId(ref __binding_0) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
}
}
}
};
const _: () =
{
impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
for ModDefId {
fn decode(__decoder: &mut __D) -> Self {
ModDefId(::rustc_serialize::Decodable::decode(__decoder))
}
}
};
const _: () =
{
impl ::rustc_data_structures::stable_hasher::StableHash for ModDefId {
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hasher::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hasher::StableHasher) {
match *self {
ModDefId(ref __binding_0) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
}
}
}
}
};
impl ModDefId {
#[inline]
pub const fn new_unchecked(def_id: DefId) -> Self { Self(def_id) }
#[inline]
pub fn to_def_id(self) -> DefId { self.into() }
#[inline]
pub fn is_local(self) -> bool { self.0.is_local() }
#[inline]
pub fn as_local(self) -> Option<LocalModDefId> {
self.0.as_local().map(LocalModDefId::new_unchecked)
}
}
impl From<LocalModDefId> for ModDefId {
#[inline]
fn from(local: LocalModDefId) -> Self { Self(local.0.to_def_id()) }
}
impl From<ModDefId> for DefId {
#[inline]
fn from(typed: ModDefId) -> Self { typed.0 }
}
pub struct LocalModDefId(LocalDefId);
#[automatically_derived]
impl ::core::fmt::Debug for LocalModDefId {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f, "LocalModDefId",
&&self.0)
}
}
#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for LocalModDefId { }
#[automatically_derived]
impl ::core::clone::Clone for LocalModDefId {
#[inline]
fn clone(&self) -> LocalModDefId {
let _: ::core::clone::AssertParamIsClone<LocalDefId>;
*self
}
}
#[automatically_derived]
impl ::core::marker::Copy for LocalModDefId { }
#[automatically_derived]
impl ::core::marker::StructuralPartialEq for LocalModDefId { }
#[automatically_derived]
impl ::core::cmp::PartialEq for LocalModDefId {
#[inline]
fn eq(&self, other: &LocalModDefId) -> bool { self.0 == other.0 }
}
#[automatically_derived]
impl ::core::cmp::Eq for LocalModDefId {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<LocalDefId>;
}
}
#[automatically_derived]
impl ::core::hash::Hash for LocalModDefId {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.0, state)
}
}
const _: () =
{
impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
for LocalModDefId {
fn encode(&self, __encoder: &mut __E) {
match *self {
LocalModDefId(ref __binding_0) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
}
}
}
};
const _: () =
{
impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
for LocalModDefId {
fn decode(__decoder: &mut __D) -> Self {
LocalModDefId(::rustc_serialize::Decodable::decode(__decoder))
}
}
};
const _: () =
{
impl ::rustc_data_structures::stable_hasher::StableHash for
LocalModDefId {
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hasher::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hasher::StableHasher) {
match *self {
LocalModDefId(ref __binding_0) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
}
}
}
}
};
impl !Ord for LocalModDefId {}
impl !PartialOrd for LocalModDefId {}
impl LocalModDefId {
#[inline]
pub const fn new_unchecked(def_id: LocalDefId) -> Self { Self(def_id) }
#[inline]
pub fn to_def_id(self) -> DefId { self.0.into() }
#[inline]
pub fn to_local_def_id(self) -> LocalDefId { self.0 }
}
impl From<LocalModDefId> for LocalDefId {
#[inline]
fn from(typed: LocalModDefId) -> Self { typed.0 }
}
impl From<LocalModDefId> for DefId {
#[inline]
fn from(typed: LocalModDefId) -> Self { typed.0.into() }
}typed_def_id! { ModDefId, LocalModDefId }569570impl LocalModDefId {
571pub const CRATE_DEF_ID: Self = Self::new_unchecked(CRATE_DEF_ID);
572}
573574impl ModDefId {
575pub fn is_top_level_module(self) -> bool {
576self.0.is_top_level_module()
577 }
578}
579580impl LocalModDefId {
581pub fn is_top_level_module(self) -> bool {
582self.0.is_top_level_module()
583 }
584}