Skip to content

Commit 0f6ba39

Browse files
committed
Auto merge of rust-lang#86180 - cjgillot:defmv, r=petrochenkov
Hash DefId in rustc_span. This is mostly just moving code around. Changes are simplifications of unneeded callbacks from rustc_span to rustc_middle. r? `@petrochenkov`
2 parents b84ffce + 72e9589 commit 0f6ba39

File tree

6 files changed

+56
-71
lines changed

6 files changed

+56
-71
lines changed

compiler/rustc_hir/src/stable_hash_impls.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::hir::{
55
TraitItem, TraitItemId, Ty, VisibilityKind,
66
};
77
use crate::hir_id::{HirId, ItemLocalId};
8-
use rustc_span::def_id::{DefPathHash, LocalDefId};
8+
use rustc_span::def_id::DefPathHash;
99

1010
/// Requirements for a `StableHashingContext` to be used in this crate.
1111
/// This is a hack to allow using the `HashStable_Generic` derive macro
@@ -21,15 +21,14 @@ pub trait HashStableContext:
2121
fn hash_hir_ty(&mut self, _: &Ty<'_>, hasher: &mut StableHasher);
2222
fn hash_hir_visibility_kind(&mut self, _: &VisibilityKind<'_>, hasher: &mut StableHasher);
2323
fn hash_hir_item_like<F: FnOnce(&mut Self)>(&mut self, f: F);
24-
fn local_def_path_hash(&self, def_id: LocalDefId) -> DefPathHash;
2524
}
2625

2726
impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for HirId {
2827
type KeyType = (DefPathHash, ItemLocalId);
2928

3029
#[inline]
3130
fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) {
32-
let def_path_hash = hcx.local_def_path_hash(self.owner);
31+
let def_path_hash = self.owner.to_stable_hash_key(hcx);
3332
(def_path_hash, self.local_id)
3433
}
3534
}
@@ -39,7 +38,7 @@ impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ItemId {
3938

4039
#[inline]
4140
fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
42-
hcx.local_def_path_hash(self.def_id)
41+
self.def_id.to_stable_hash_key(hcx)
4342
}
4443
}
4544

@@ -48,7 +47,7 @@ impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for TraitItemId {
4847

4948
#[inline]
5049
fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
51-
hcx.local_def_path_hash(self.def_id)
50+
self.def_id.to_stable_hash_key(hcx)
5251
}
5352
}
5453

@@ -57,7 +56,7 @@ impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ImplItemId {
5756

5857
#[inline]
5958
fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
60-
hcx.local_def_path_hash(self.def_id)
59+
self.def_id.to_stable_hash_key(hcx)
6160
}
6261
}
6362

@@ -66,7 +65,7 @@ impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ForeignItemId
6665

6766
#[inline]
6867
fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
69-
hcx.local_def_path_hash(self.def_id)
68+
self.def_id.to_stable_hash_key(hcx)
7069
}
7170
}
7271

compiler/rustc_middle/src/ich/hcx.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use rustc_span::source_map::SourceMap;
1414
use rustc_span::symbol::Symbol;
1515
use rustc_span::{BytePos, CachingSourceMapView, SourceFile, SpanData};
1616

17-
use rustc_span::def_id::{CrateNum, CRATE_DEF_INDEX};
1817
use smallvec::SmallVec;
1918
use std::cmp::Ord;
2019
use std::thread::LocalKey;
@@ -227,15 +226,8 @@ impl<'a> rustc_span::HashStableContext for StableHashingContext<'a> {
227226
}
228227

229228
#[inline]
230-
fn hash_crate_num(&mut self, cnum: CrateNum, hasher: &mut StableHasher) {
231-
let hcx = self;
232-
hcx.def_path_hash(DefId { krate: cnum, index: CRATE_DEF_INDEX }).hash_stable(hcx, hasher);
233-
}
234-
235-
#[inline]
236-
fn hash_def_id(&mut self, def_id: DefId, hasher: &mut StableHasher) {
237-
let hcx = self;
238-
hcx.def_path_hash(def_id).hash_stable(hcx, hasher);
229+
fn def_path_hash(&self, def_id: DefId) -> DefPathHash {
230+
self.def_path_hash(def_id)
239231
}
240232

241233
fn expn_id_cache() -> &'static LocalKey<rustc_span::ExpnIdCache> {

compiler/rustc_middle/src/ich/impls_hir.rs

-41
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use rustc_attr as attr;
66
use rustc_data_structures::fingerprint::Fingerprint;
77
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
88
use rustc_hir as hir;
9-
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX};
109
use rustc_hir::definitions::DefPathHash;
1110
use smallvec::SmallVec;
1211
use std::mem;
@@ -113,46 +112,6 @@ impl<'ctx> rustc_hir::HashStableContext for StableHashingContext<'ctx> {
113112

114113
self.node_id_hashing_mode = prev_hash_node_ids;
115114
}
116-
117-
#[inline]
118-
fn local_def_path_hash(&self, def_id: LocalDefId) -> DefPathHash {
119-
self.local_def_path_hash(def_id)
120-
}
121-
}
122-
123-
impl<'a> ToStableHashKey<StableHashingContext<'a>> for DefId {
124-
type KeyType = DefPathHash;
125-
126-
#[inline]
127-
fn to_stable_hash_key(&self, hcx: &StableHashingContext<'a>) -> DefPathHash {
128-
hcx.def_path_hash(*self)
129-
}
130-
}
131-
132-
impl<'a> HashStable<StableHashingContext<'a>> for LocalDefId {
133-
#[inline]
134-
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
135-
hcx.def_path_hash(self.to_def_id()).hash_stable(hcx, hasher);
136-
}
137-
}
138-
139-
impl<'a> ToStableHashKey<StableHashingContext<'a>> for LocalDefId {
140-
type KeyType = DefPathHash;
141-
142-
#[inline]
143-
fn to_stable_hash_key(&self, hcx: &StableHashingContext<'a>) -> DefPathHash {
144-
hcx.def_path_hash(self.to_def_id())
145-
}
146-
}
147-
148-
impl<'a> ToStableHashKey<StableHashingContext<'a>> for CrateNum {
149-
type KeyType = DefPathHash;
150-
151-
#[inline]
152-
fn to_stable_hash_key(&self, hcx: &StableHashingContext<'a>) -> DefPathHash {
153-
let def_id = DefId { krate: *self, index: CRATE_DEF_INDEX };
154-
def_id.to_stable_hash_key(hcx)
155-
}
156115
}
157116

158117
impl<'a> ToStableHashKey<StableHashingContext<'a>> for hir::ItemLocalId {

compiler/rustc_span/src/def_id.rs

+39-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::crate_disambiguator::CrateDisambiguator;
22
use crate::HashStableContext;
33
use rustc_data_structures::fingerprint::Fingerprint;
4-
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
4+
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
55
use rustc_data_structures::AtomicRef;
66
use rustc_index::vec::Idx;
77
use rustc_macros::HashStable_Generic;
@@ -308,13 +308,49 @@ impl<D: Decoder> Decodable<D> for LocalDefId {
308308
rustc_data_structures::define_id_collections!(LocalDefIdMap, LocalDefIdSet, LocalDefId);
309309

310310
impl<CTX: HashStableContext> HashStable<CTX> for DefId {
311+
#[inline]
312+
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
313+
self.to_stable_hash_key(hcx).hash_stable(hcx, hasher);
314+
}
315+
}
316+
317+
impl<CTX: HashStableContext> HashStable<CTX> for LocalDefId {
318+
#[inline]
311319
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
312-
hcx.hash_def_id(*self, hasher)
320+
self.to_stable_hash_key(hcx).hash_stable(hcx, hasher);
313321
}
314322
}
315323

316324
impl<CTX: HashStableContext> HashStable<CTX> for CrateNum {
325+
#[inline]
317326
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
318-
hcx.hash_crate_num(*self, hasher)
327+
self.to_stable_hash_key(hcx).hash_stable(hcx, hasher);
328+
}
329+
}
330+
331+
impl<CTX: HashStableContext> ToStableHashKey<CTX> for DefId {
332+
type KeyType = DefPathHash;
333+
334+
#[inline]
335+
fn to_stable_hash_key(&self, hcx: &CTX) -> DefPathHash {
336+
hcx.def_path_hash(*self)
337+
}
338+
}
339+
340+
impl<CTX: HashStableContext> ToStableHashKey<CTX> for LocalDefId {
341+
type KeyType = DefPathHash;
342+
343+
#[inline]
344+
fn to_stable_hash_key(&self, hcx: &CTX) -> DefPathHash {
345+
hcx.def_path_hash(self.to_def_id())
346+
}
347+
}
348+
349+
impl<CTX: HashStableContext> ToStableHashKey<CTX> for CrateNum {
350+
type KeyType = DefPathHash;
351+
352+
#[inline]
353+
fn to_stable_hash_key(&self, hcx: &CTX) -> DefPathHash {
354+
self.as_def_id().to_stable_hash_key(hcx)
319355
}
320356
}

compiler/rustc_span/src/hygiene.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use crate::symbol::{kw, sym, Symbol};
2929
use crate::SESSION_GLOBALS;
3030
use crate::{BytePos, CachingSourceMapView, ExpnIdCache, SourceFile, Span, DUMMY_SP};
3131

32-
use crate::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
32+
use crate::def_id::{CrateNum, DefId, DefPathHash, CRATE_DEF_INDEX, LOCAL_CRATE};
3333
use rustc_data_structures::fingerprint::Fingerprint;
3434
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
3535
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
@@ -1330,9 +1330,12 @@ fn update_disambiguator(expn_id: ExpnId) {
13301330
}
13311331

13321332
impl<'a> crate::HashStableContext for DummyHashStableContext<'a> {
1333-
fn hash_def_id(&mut self, def_id: DefId, hasher: &mut StableHasher) {
1334-
def_id.krate.as_u32().hash_stable(self, hasher);
1335-
def_id.index.as_u32().hash_stable(self, hasher);
1333+
#[inline]
1334+
fn def_path_hash(&self, def_id: DefId) -> DefPathHash {
1335+
DefPathHash(Fingerprint::new(
1336+
def_id.krate.as_u32().into(),
1337+
def_id.index.as_u32().into(),
1338+
))
13361339
}
13371340

13381341
fn expn_id_cache() -> &'static LocalKey<ExpnIdCache> {
@@ -1345,9 +1348,6 @@ fn update_disambiguator(expn_id: ExpnId) {
13451348
&CACHE
13461349
}
13471350

1348-
fn hash_crate_num(&mut self, krate: CrateNum, hasher: &mut StableHasher) {
1349-
krate.as_u32().hash_stable(self, hasher);
1350-
}
13511351
fn hash_spans(&self) -> bool {
13521352
true
13531353
}

compiler/rustc_span/src/lib.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub use hygiene::SyntaxContext;
4040
use hygiene::Transparency;
4141
pub use hygiene::{DesugaringKind, ExpnData, ExpnId, ExpnKind, ForLoopLoc, MacroKind};
4242
pub mod def_id;
43-
use def_id::{CrateNum, DefId, LOCAL_CRATE};
43+
use def_id::{CrateNum, DefId, DefPathHash, LOCAL_CRATE};
4444
pub mod lev_distance;
4545
mod span_encoding;
4646
pub use span_encoding::{Span, DUMMY_SP};
@@ -1928,13 +1928,12 @@ fn lookup_line(lines: &[BytePos], pos: BytePos) -> isize {
19281928
/// This is a hack to allow using the [`HashStable_Generic`] derive macro
19291929
/// instead of implementing everything in rustc_middle.
19301930
pub trait HashStableContext {
1931-
fn hash_def_id(&mut self, _: DefId, hasher: &mut StableHasher);
1931+
fn def_path_hash(&self, def_id: DefId) -> DefPathHash;
19321932
/// Obtains a cache for storing the `Fingerprint` of an `ExpnId`.
19331933
/// This method allows us to have multiple `HashStableContext` implementations
19341934
/// that hash things in a different way, without the results of one polluting
19351935
/// the cache of the other.
19361936
fn expn_id_cache() -> &'static LocalKey<ExpnIdCache>;
1937-
fn hash_crate_num(&mut self, _: CrateNum, hasher: &mut StableHasher);
19381937
fn hash_spans(&self) -> bool;
19391938
fn span_data_to_lines_and_cols(
19401939
&mut self,

0 commit comments

Comments
 (0)