about summary refs log tree commit diff
path: root/compiler/rustc_span/src
diff options
context:
space:
mode:
authorbjorn3 <bjorn3@users.noreply.github.com>2021-06-01 16:32:13 +0200
committerbjorn3 <bjorn3@users.noreply.github.com>2021-06-07 10:37:45 +0200
commit8176ab8bc18fdd7d3c2cf7f720c51166364c33a3 (patch)
tree474b75784053dd164149e0aafc979b23bb45ba3c /compiler/rustc_span/src
parent6c5b6985fdce0921fe4ac0247fd026355953c1ea (diff)
downloadrust-8176ab8bc18fdd7d3c2cf7f720c51166364c33a3.tar.gz
rust-8176ab8bc18fdd7d3c2cf7f720c51166364c33a3.zip
Revert "Merge CrateDisambiguator into StableCrateId"
This reverts commit d0ec85d3fb6d322496cb8f4bc1c21e19f23284ad.
Diffstat (limited to 'compiler/rustc_span/src')
-rw-r--r--compiler/rustc_span/src/crate_disambiguator.rs35
-rw-r--r--compiler/rustc_span/src/def_id.rs40
-rw-r--r--compiler/rustc_span/src/lib.rs2
3 files changed, 45 insertions, 32 deletions
diff --git a/compiler/rustc_span/src/crate_disambiguator.rs b/compiler/rustc_span/src/crate_disambiguator.rs
new file mode 100644
index 00000000000..bd7d8516714
--- /dev/null
+++ b/compiler/rustc_span/src/crate_disambiguator.rs
@@ -0,0 +1,35 @@
+// 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);
diff --git a/compiler/rustc_span/src/def_id.rs b/compiler/rustc_span/src/def_id.rs
index b04a10d22a0..228d09b30d9 100644
--- a/compiler/rustc_span/src/def_id.rs
+++ b/compiler/rustc_span/src/def_id.rs
@@ -1,3 +1,4 @@
+use crate::crate_disambiguator::CrateDisambiguator;
 use crate::HashStableContext;
 use rustc_data_structures::fingerprint::Fingerprint;
 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
@@ -126,51 +127,26 @@ impl Borrow<Fingerprint> for DefPathHash {
     }
 }
 
-/// 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.
+/// 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.
 ///
 /// 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)]
-#[derive(HashStable_Generic, Encodable, Decodable)]
+#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug, 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
-    /// `-Cmetadata` arguments.
-    pub fn new(crate_name: &str, is_exe: bool, mut metadata: Vec<String>) -> StableCrateId {
+    /// disambiguator.
+    pub fn new(crate_name: &str, crate_disambiguator: CrateDisambiguator) -> StableCrateId {
         use std::hash::Hash;
-        use std::hash::Hasher;
 
         let mut hasher = StableHasher::new();
         crate_name.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" });
-
+        crate_disambiguator.hash(&mut hasher);
         StableCrateId(hasher.finish())
     }
 }
diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs
index 51a53918f07..de132616b0d 100644
--- a/compiler/rustc_span/src/lib.rs
+++ b/compiler/rustc_span/src/lib.rs
@@ -45,6 +45,8 @@ 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};