diff options
Diffstat (limited to 'compiler/rustc_span/src/def_id.rs')
-rw-r--r-- | compiler/rustc_span/src/def_id.rs | 50 |
1 files changed, 27 insertions, 23 deletions
diff --git a/compiler/rustc_span/src/def_id.rs b/compiler/rustc_span/src/def_id.rs index b2c58caff..f65a6aa4f 100644 --- a/compiler/rustc_span/src/def_id.rs +++ b/compiler/rustc_span/src/def_id.rs @@ -1,12 +1,11 @@ use crate::{HashStableContext, Symbol}; use rustc_data_structures::fingerprint::Fingerprint; -use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey}; +use rustc_data_structures::stable_hasher::{Hash64, HashStable, StableHasher, ToStableHashKey}; use rustc_data_structures::unhash::Unhasher; use rustc_data_structures::AtomicRef; -use rustc_index::vec::Idx; +use rustc_index::Idx; use rustc_macros::HashStable_Generic; use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; -use std::borrow::Borrow; use std::fmt; use std::hash::{BuildHasherDefault, Hash, Hasher}; @@ -105,20 +104,20 @@ impl DefPathHash { /// originates from. #[inline] pub fn stable_crate_id(&self) -> StableCrateId { - StableCrateId(self.0.as_value().0) + StableCrateId(self.0.split().0) } /// Returns the crate-local part of the [DefPathHash]. /// /// Used for tests. #[inline] - pub fn local_hash(&self) -> u64 { - self.0.as_value().1 + pub fn local_hash(&self) -> Hash64 { + self.0.split().1 } /// Builds a new [DefPathHash] with the given [StableCrateId] and /// `local_hash`, where `local_hash` must be unique within its crate. - pub fn new(stable_crate_id: StableCrateId, local_hash: u64) -> DefPathHash { + pub fn new(stable_crate_id: StableCrateId, local_hash: Hash64) -> DefPathHash { DefPathHash(Fingerprint::new(stable_crate_id.0, local_hash)) } } @@ -129,13 +128,6 @@ impl Default for DefPathHash { } } -impl Borrow<Fingerprint> for DefPathHash { - #[inline] - fn borrow(&self) -> &Fingerprint { - &self.0 - } -} - /// A [`StableCrateId`] is a 64-bit hash of a crate name, together with all /// `-Cmetadata` arguments, and some other data. It is to [`CrateNum`] what [`DefPathHash`] is to /// [`DefId`]. It is stable across compilation sessions. @@ -147,18 +139,19 @@ impl Borrow<Fingerprint> for DefPathHash { /// /// For more information on the possibility of hash collisions in rustc, /// see the discussion in [`DefId`]. -#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug)] -#[derive(HashStable_Generic, Encodable, Decodable)] -pub struct StableCrateId(pub(crate) u64); +#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)] +#[derive(Hash, HashStable_Generic, Encodable, Decodable)] +pub struct StableCrateId(pub(crate) Hash64); impl StableCrateId { - pub fn to_u64(self) -> u64 { - self.0 - } - /// Computes the stable ID for a crate with the given name and /// `-Cmetadata` arguments. - pub fn new(crate_name: Symbol, is_exe: bool, mut metadata: Vec<String>) -> StableCrateId { + pub fn new( + crate_name: Symbol, + is_exe: bool, + mut metadata: Vec<String>, + cfg_version: &'static str, + ) -> StableCrateId { let mut hasher = StableHasher::new(); // We must hash the string text of the crate name, not the id, as the id is not stable // across builds. @@ -192,11 +185,22 @@ impl StableCrateId { if let Some(val) = std::env::var_os("RUSTC_FORCE_RUSTC_VERSION") { hasher.write(val.to_string_lossy().into_owned().as_bytes()) } else { - hasher.write(option_env!("CFG_VERSION").unwrap_or("unknown version").as_bytes()); + hasher.write(cfg_version.as_bytes()) } StableCrateId(hasher.finish()) } + + #[inline] + pub fn as_u64(self) -> u64 { + self.0.as_u64() + } +} + +impl fmt::LowerHex for StableCrateId { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::LowerHex::fmt(&self.0, f) + } } rustc_index::newtype_index! { |