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