about summary refs log tree commit diff
path: root/compiler/rustc_hir/src/tests.rs
diff options
context:
space:
mode:
authorMichael Woerister <michaelwoerister@posteo>2021-02-04 11:04:29 +0100
committerMichael Woerister <michaelwoerister@posteo>2021-02-04 16:33:58 +0100
commit9e5054498b181fc3984e266d1aa05f076dfea22f (patch)
tree7914141ecdc8ee6b647b6f5be441c1f1b8c10008 /compiler/rustc_hir/src/tests.rs
parent97380e3b06555c0dd3a8fd1e3d2ca7c4bfeeb21f (diff)
downloadrust-9e5054498b181fc3984e266d1aa05f076dfea22f.tar.gz
rust-9e5054498b181fc3984e266d1aa05f076dfea22f.zip
Add unit test to ensure that both parts of a DefPathHash depend on the defining crate's ID.
Diffstat (limited to 'compiler/rustc_hir/src/tests.rs')
-rw-r--r--compiler/rustc_hir/src/tests.rs39
1 files changed, 39 insertions, 0 deletions
diff --git a/compiler/rustc_hir/src/tests.rs b/compiler/rustc_hir/src/tests.rs
new file mode 100644
index 00000000000..2aafc6afa23
--- /dev/null
+++ b/compiler/rustc_hir/src/tests.rs
@@ -0,0 +1,39 @@
+use crate::definitions::{DefKey, DefPathData, DisambiguatedDefPathData};
+use rustc_data_structures::fingerprint::Fingerprint;
+use rustc_span::crate_disambiguator::CrateDisambiguator;
+use rustc_span::def_id::{DefPathHash, StableCrateId};
+
+#[test]
+fn def_path_hash_depends_on_crate_id() {
+    // This test makes sure that *both* halves of a DefPathHash depend on
+    // the crate-id of the defining crate. This is a desirable property
+    // because the crate-id can be more easily changed than the DefPath
+    // of an item, so, in the case of a crate-local DefPathHash collision,
+    // the user can simply "role the dice again" for all DefPathHashes in
+    // the crate by changing the crate disambiguator (e.g. via bumping the
+    // crate's version number).
+
+    let d0 = CrateDisambiguator::from(Fingerprint::new(12, 34));
+    let d1 = CrateDisambiguator::from(Fingerprint::new(56, 78));
+
+    let h0 = mk_test_hash("foo", d0);
+    let h1 = mk_test_hash("foo", d1);
+
+    assert_ne!(h0.stable_crate_id(), h1.stable_crate_id());
+    assert_ne!(h0.local_hash(), h1.local_hash());
+
+    fn mk_test_hash(crate_name: &str, crate_disambiguator: CrateDisambiguator) -> DefPathHash {
+        let stable_crate_id = StableCrateId::new(crate_name, crate_disambiguator);
+        let parent_hash = DefPathHash::new(stable_crate_id, 0);
+
+        let key = DefKey {
+            parent: None,
+            disambiguated_data: DisambiguatedDefPathData {
+                data: DefPathData::CrateRoot,
+                disambiguator: 0,
+            },
+        };
+
+        key.compute_stable_hash(parent_hash)
+    }
+}