1
Fork 0

Hash DefId in rustc_span.

This commit is contained in:
Camille GILLOT 2021-04-19 22:27:49 +02:00
parent 1f949e94e8
commit a7a50b0c0a
6 changed files with 55 additions and 71 deletions

View file

@ -5,7 +5,7 @@ use crate::hir::{
TraitItem, TraitItemId, Ty, VisibilityKind, TraitItem, TraitItemId, Ty, VisibilityKind,
}; };
use crate::hir_id::{HirId, ItemLocalId}; use crate::hir_id::{HirId, ItemLocalId};
use rustc_span::def_id::{DefPathHash, LocalDefId}; use rustc_span::def_id::DefPathHash;
/// Requirements for a `StableHashingContext` to be used in this crate. /// Requirements for a `StableHashingContext` to be used in this crate.
/// This is a hack to allow using the `HashStable_Generic` derive macro /// This is a hack to allow using the `HashStable_Generic` derive macro
@ -21,7 +21,6 @@ pub trait HashStableContext:
fn hash_hir_ty(&mut self, _: &Ty<'_>, hasher: &mut StableHasher); fn hash_hir_ty(&mut self, _: &Ty<'_>, hasher: &mut StableHasher);
fn hash_hir_visibility_kind(&mut self, _: &VisibilityKind<'_>, hasher: &mut StableHasher); fn hash_hir_visibility_kind(&mut self, _: &VisibilityKind<'_>, hasher: &mut StableHasher);
fn hash_hir_item_like<F: FnOnce(&mut Self)>(&mut self, f: F); fn hash_hir_item_like<F: FnOnce(&mut Self)>(&mut self, f: F);
fn local_def_path_hash(&self, def_id: LocalDefId) -> DefPathHash;
} }
impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for HirId { impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for HirId {
@ -29,7 +28,7 @@ impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for HirId {
#[inline] #[inline]
fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) { fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) {
let def_path_hash = hcx.local_def_path_hash(self.owner); let def_path_hash = self.owner.to_stable_hash_key(hcx);
(def_path_hash, self.local_id) (def_path_hash, self.local_id)
} }
} }
@ -39,7 +38,7 @@ impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ItemId {
#[inline] #[inline]
fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash { fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
hcx.local_def_path_hash(self.def_id) self.def_id.to_stable_hash_key(hcx)
} }
} }
@ -48,7 +47,7 @@ impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for TraitItemId {
#[inline] #[inline]
fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash { fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
hcx.local_def_path_hash(self.def_id) self.def_id.to_stable_hash_key(hcx)
} }
} }
@ -57,7 +56,7 @@ impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ImplItemId {
#[inline] #[inline]
fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash { fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
hcx.local_def_path_hash(self.def_id) self.def_id.to_stable_hash_key(hcx)
} }
} }
@ -66,7 +65,7 @@ impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ForeignItemId
#[inline] #[inline]
fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash { fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
hcx.local_def_path_hash(self.def_id) self.def_id.to_stable_hash_key(hcx)
} }
} }

View file

@ -14,7 +14,6 @@ use rustc_span::source_map::SourceMap;
use rustc_span::symbol::Symbol; use rustc_span::symbol::Symbol;
use rustc_span::{BytePos, CachingSourceMapView, SourceFile, SpanData}; use rustc_span::{BytePos, CachingSourceMapView, SourceFile, SpanData};
use rustc_span::def_id::{CrateNum, CRATE_DEF_INDEX};
use smallvec::SmallVec; use smallvec::SmallVec;
use std::cmp::Ord; use std::cmp::Ord;
use std::thread::LocalKey; use std::thread::LocalKey;
@ -227,15 +226,8 @@ impl<'a> rustc_span::HashStableContext for StableHashingContext<'a> {
} }
#[inline] #[inline]
fn hash_crate_num(&mut self, cnum: CrateNum, hasher: &mut StableHasher) { fn def_path_hash(&self, def_id: DefId) -> DefPathHash {
let hcx = self; self.def_path_hash(def_id)
hcx.def_path_hash(DefId { krate: cnum, index: CRATE_DEF_INDEX }).hash_stable(hcx, hasher);
}
#[inline]
fn hash_def_id(&mut self, def_id: DefId, hasher: &mut StableHasher) {
let hcx = self;
hcx.def_path_hash(def_id).hash_stable(hcx, hasher);
} }
fn expn_id_cache() -> &'static LocalKey<rustc_span::ExpnIdCache> { fn expn_id_cache() -> &'static LocalKey<rustc_span::ExpnIdCache> {

View file

@ -6,7 +6,6 @@ use rustc_attr as attr;
use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX};
use rustc_hir::definitions::DefPathHash; use rustc_hir::definitions::DefPathHash;
use smallvec::SmallVec; use smallvec::SmallVec;
use std::mem; use std::mem;
@ -113,46 +112,6 @@ impl<'ctx> rustc_hir::HashStableContext for StableHashingContext<'ctx> {
self.node_id_hashing_mode = prev_hash_node_ids; self.node_id_hashing_mode = prev_hash_node_ids;
} }
#[inline]
fn local_def_path_hash(&self, def_id: LocalDefId) -> DefPathHash {
self.local_def_path_hash(def_id)
}
}
impl<'a> ToStableHashKey<StableHashingContext<'a>> for DefId {
type KeyType = DefPathHash;
#[inline]
fn to_stable_hash_key(&self, hcx: &StableHashingContext<'a>) -> DefPathHash {
hcx.def_path_hash(*self)
}
}
impl<'a> HashStable<StableHashingContext<'a>> for LocalDefId {
#[inline]
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
hcx.def_path_hash(self.to_def_id()).hash_stable(hcx, hasher);
}
}
impl<'a> ToStableHashKey<StableHashingContext<'a>> for LocalDefId {
type KeyType = DefPathHash;
#[inline]
fn to_stable_hash_key(&self, hcx: &StableHashingContext<'a>) -> DefPathHash {
hcx.def_path_hash(self.to_def_id())
}
}
impl<'a> ToStableHashKey<StableHashingContext<'a>> for CrateNum {
type KeyType = DefPathHash;
#[inline]
fn to_stable_hash_key(&self, hcx: &StableHashingContext<'a>) -> DefPathHash {
let def_id = DefId { krate: *self, index: CRATE_DEF_INDEX };
def_id.to_stable_hash_key(hcx)
}
} }
impl<'a> ToStableHashKey<StableHashingContext<'a>> for hir::ItemLocalId { impl<'a> ToStableHashKey<StableHashingContext<'a>> for hir::ItemLocalId {

View file

@ -1,7 +1,7 @@
use crate::crate_disambiguator::CrateDisambiguator; use crate::crate_disambiguator::CrateDisambiguator;
use crate::HashStableContext; use crate::HashStableContext;
use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
use rustc_data_structures::AtomicRef; use rustc_data_structures::AtomicRef;
use rustc_index::vec::Idx; use rustc_index::vec::Idx;
use rustc_macros::HashStable_Generic; use rustc_macros::HashStable_Generic;
@ -309,12 +309,47 @@ rustc_data_structures::define_id_collections!(LocalDefIdMap, LocalDefIdSet, Loca
impl<CTX: HashStableContext> HashStable<CTX> for DefId { impl<CTX: HashStableContext> HashStable<CTX> for DefId {
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
hcx.hash_def_id(*self, hasher) hcx.def_path_hash(*self).hash_stable(hcx, hasher);
}
}
impl<CTX: HashStableContext> HashStable<CTX> for LocalDefId {
#[inline]
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
hcx.def_path_hash(self.to_def_id()).hash_stable(hcx, hasher);
} }
} }
impl<CTX: HashStableContext> HashStable<CTX> for CrateNum { impl<CTX: HashStableContext> HashStable<CTX> for CrateNum {
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
hcx.hash_crate_num(*self, hasher) hcx.def_path_hash(DefId { krate: *self, index: CRATE_DEF_INDEX }).hash_stable(hcx, hasher);
}
}
impl<CTX: HashStableContext> ToStableHashKey<CTX> for DefId {
type KeyType = DefPathHash;
#[inline]
fn to_stable_hash_key(&self, hcx: &CTX) -> DefPathHash {
hcx.def_path_hash(*self)
}
}
impl<CTX: HashStableContext> ToStableHashKey<CTX> for LocalDefId {
type KeyType = DefPathHash;
#[inline]
fn to_stable_hash_key(&self, hcx: &CTX) -> DefPathHash {
hcx.def_path_hash(self.to_def_id())
}
}
impl<CTX: HashStableContext> ToStableHashKey<CTX> for CrateNum {
type KeyType = DefPathHash;
#[inline]
fn to_stable_hash_key(&self, hcx: &CTX) -> DefPathHash {
let def_id = DefId { krate: *self, index: CRATE_DEF_INDEX };
def_id.to_stable_hash_key(hcx)
} }
} }

View file

@ -29,7 +29,7 @@ use crate::symbol::{kw, sym, Symbol};
use crate::SESSION_GLOBALS; use crate::SESSION_GLOBALS;
use crate::{BytePos, CachingSourceMapView, ExpnIdCache, SourceFile, Span, DUMMY_SP}; use crate::{BytePos, CachingSourceMapView, ExpnIdCache, SourceFile, Span, DUMMY_SP};
use crate::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE}; use crate::def_id::{CrateNum, DefId, DefPathHash, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
@ -1330,9 +1330,12 @@ fn update_disambiguator(expn_id: ExpnId) {
} }
impl<'a> crate::HashStableContext for DummyHashStableContext<'a> { impl<'a> crate::HashStableContext for DummyHashStableContext<'a> {
fn hash_def_id(&mut self, def_id: DefId, hasher: &mut StableHasher) { fn def_path_hash(&self, def_id: DefId) -> DefPathHash {
def_id.krate.as_u32().hash_stable(self, hasher); use std::hash::Hasher;
def_id.index.as_u32().hash_stable(self, hasher); let mut hasher = StableHasher::new();
hasher.write_u32(def_id.krate.as_u32());
hasher.write_u32(def_id.index.as_u32());
DefPathHash(hasher.finish())
} }
fn expn_id_cache() -> &'static LocalKey<ExpnIdCache> { fn expn_id_cache() -> &'static LocalKey<ExpnIdCache> {
@ -1345,9 +1348,6 @@ fn update_disambiguator(expn_id: ExpnId) {
&CACHE &CACHE
} }
fn hash_crate_num(&mut self, krate: CrateNum, hasher: &mut StableHasher) {
krate.as_u32().hash_stable(self, hasher);
}
fn hash_spans(&self) -> bool { fn hash_spans(&self) -> bool {
true true
} }

View file

@ -40,7 +40,7 @@ pub use hygiene::SyntaxContext;
use hygiene::Transparency; use hygiene::Transparency;
pub use hygiene::{DesugaringKind, ExpnData, ExpnId, ExpnKind, ForLoopLoc, MacroKind}; pub use hygiene::{DesugaringKind, ExpnData, ExpnId, ExpnKind, ForLoopLoc, MacroKind};
pub mod def_id; pub mod def_id;
use def_id::{CrateNum, DefId, LOCAL_CRATE}; use def_id::{CrateNum, DefId, DefPathHash, LOCAL_CRATE};
pub mod lev_distance; pub mod lev_distance;
mod span_encoding; mod span_encoding;
pub use span_encoding::{Span, DUMMY_SP}; pub use span_encoding::{Span, DUMMY_SP};
@ -1928,13 +1928,12 @@ fn lookup_line(lines: &[BytePos], pos: BytePos) -> isize {
/// This is a hack to allow using the [`HashStable_Generic`] derive macro /// This is a hack to allow using the [`HashStable_Generic`] derive macro
/// instead of implementing everything in rustc_middle. /// instead of implementing everything in rustc_middle.
pub trait HashStableContext { pub trait HashStableContext {
fn hash_def_id(&mut self, _: DefId, hasher: &mut StableHasher); fn def_path_hash(&self, def_id: DefId) -> DefPathHash;
/// Obtains a cache for storing the `Fingerprint` of an `ExpnId`. /// Obtains a cache for storing the `Fingerprint` of an `ExpnId`.
/// This method allows us to have multiple `HashStableContext` implementations /// This method allows us to have multiple `HashStableContext` implementations
/// that hash things in a different way, without the results of one polluting /// that hash things in a different way, without the results of one polluting
/// the cache of the other. /// the cache of the other.
fn expn_id_cache() -> &'static LocalKey<ExpnIdCache>; fn expn_id_cache() -> &'static LocalKey<ExpnIdCache>;
fn hash_crate_num(&mut self, _: CrateNum, hasher: &mut StableHasher);
fn hash_spans(&self) -> bool; fn hash_spans(&self) -> bool;
fn span_data_to_lines_and_cols( fn span_data_to_lines_and_cols(
&mut self, &mut self,