1
Fork 0

Revert "Revert "Merge CrateDisambiguator into StableCrateId""

This reverts commit 8176ab8bc1.
This commit is contained in:
bjorn3 2021-06-08 18:36:30 +02:00
parent 9a27044f42
commit 489ad8b8b5
28 changed files with 116 additions and 193 deletions

View file

@ -1,35 +0,0 @@
// This is here because `rustc_session` wants to refer to it,
// and so does `rustc_hir`, but `rustc_hir` shouldn't refer to `rustc_session`.
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::{base_n, impl_stable_hash_via_hash};
use std::fmt;
/// Hash value constructed out of all the `-C metadata` arguments passed to the
/// compiler. Together with the crate-name forms a unique global identifier for
/// the crate.
#[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Clone, Copy, Encodable, Decodable)]
pub struct CrateDisambiguator(Fingerprint);
impl CrateDisambiguator {
pub fn to_fingerprint(self) -> Fingerprint {
self.0
}
}
impl fmt::Display for CrateDisambiguator {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
let (a, b) = self.0.as_value();
let as_u128 = a as u128 | ((b as u128) << 64);
f.write_str(&base_n::encode(as_u128, base_n::CASE_INSENSITIVE))
}
}
impl From<Fingerprint> for CrateDisambiguator {
fn from(fingerprint: Fingerprint) -> CrateDisambiguator {
CrateDisambiguator(fingerprint)
}
}
impl_stable_hash_via_hash!(CrateDisambiguator);

View file

@ -1,4 +1,3 @@
use crate::crate_disambiguator::CrateDisambiguator;
use crate::HashStableContext;
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
@ -127,26 +126,51 @@ impl Borrow<Fingerprint> for DefPathHash {
}
}
/// A [StableCrateId] is a 64 bit hash of `(crate-name, crate-disambiguator)`. It
/// is to [CrateNum] what [DefPathHash] is to [DefId]. It is stable across
/// compilation sessions.
/// A [StableCrateId] is a 64 bit hash of the crate name combined with all
/// `-Cmetadata` arguments. It is to [CrateNum] what [DefPathHash] is to
/// [DefId]. It is stable across compilation sessions.
///
/// Since the ID is a hash value there is a (very small) chance that two crates
/// end up with the same [StableCrateId]. The compiler will check for such
/// collisions when loading crates and abort compilation in order to avoid
/// further trouble.
#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug, Encodable, Decodable)]
#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug)]
#[derive(HashStable_Generic, Encodable, Decodable)]
pub struct StableCrateId(u64);
impl StableCrateId {
pub fn to_u64(self) -> u64 {
self.0
}
/// Computes the stable ID for a crate with the given name and
/// disambiguator.
pub fn new(crate_name: &str, crate_disambiguator: CrateDisambiguator) -> StableCrateId {
/// `-Cmetadata` arguments.
pub fn new(crate_name: &str, is_exe: bool, mut metadata: Vec<String>) -> StableCrateId {
use std::hash::Hash;
use std::hash::Hasher;
let mut hasher = StableHasher::new();
crate_name.hash(&mut hasher);
crate_disambiguator.hash(&mut hasher);
// We don't want the stable crate id to dependent on the order
// -C metadata arguments, so sort them:
metadata.sort();
// Every distinct -C metadata value is only incorporated once:
metadata.dedup();
hasher.write(b"metadata");
for s in &metadata {
// Also incorporate the length of a metadata string, so that we generate
// different values for `-Cmetadata=ab -Cmetadata=c` and
// `-Cmetadata=a -Cmetadata=bc`
hasher.write_usize(s.len());
hasher.write(s.as_bytes());
}
// Also incorporate crate type, so that we don't get symbol conflicts when
// linking against a library of the same name, if this is an executable.
hasher.write(if is_exe { b"exe" } else { b"lib" });
StableCrateId(hasher.finish())
}
}

View file

@ -45,8 +45,6 @@ pub mod lev_distance;
mod span_encoding;
pub use span_encoding::{Span, DUMMY_SP};
pub mod crate_disambiguator;
pub mod symbol;
pub use symbol::{sym, Symbol};