]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_hir/src/tests.rs
Revert "Revert "Fix test""
[rust.git] / compiler / rustc_hir / src / tests.rs
1 use crate::definitions::{DefKey, DefPathData, DisambiguatedDefPathData};
2 use rustc_span::def_id::{DefPathHash, StableCrateId};
3
4 #[test]
5 fn def_path_hash_depends_on_crate_id() {
6     // This test makes sure that *both* halves of a DefPathHash depend on
7     // the crate-id of the defining crate. This is a desirable property
8     // because the crate-id can be more easily changed than the DefPath
9     // of an item, so, in the case of a crate-local DefPathHash collision,
10     // the user can simply "role the dice again" for all DefPathHashes in
11     // the crate by changing the crate disambiguator (e.g. via bumping the
12     // crate's version number).
13
14     let id0 = StableCrateId::new("foo", false, vec!["1".to_string()]);
15     let id1 = StableCrateId::new("foo", false, vec!["2".to_string()]);
16
17     let h0 = mk_test_hash(id0);
18     let h1 = mk_test_hash(id1);
19
20     assert_ne!(h0.stable_crate_id(), h1.stable_crate_id());
21     assert_ne!(h0.local_hash(), h1.local_hash());
22
23     fn mk_test_hash(stable_crate_id: StableCrateId) -> DefPathHash {
24         let parent_hash = DefPathHash::new(stable_crate_id, 0);
25
26         let key = DefKey {
27             parent: None,
28             disambiguated_data: DisambiguatedDefPathData {
29                 data: DefPathData::CrateRoot,
30                 disambiguator: 0,
31             },
32         };
33
34         key.compute_stable_hash(parent_hash)
35     }
36 }