From a7a50b0c0ad7700f3e1e02160d2491290c0fb039 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Mon, 19 Apr 2021 22:27:49 +0200 Subject: Hash DefId in rustc_span. --- compiler/rustc_span/src/def_id.rs | 41 +++++++++++++++++++++++++++++++++++--- compiler/rustc_span/src/hygiene.rs | 14 ++++++------- compiler/rustc_span/src/lib.rs | 5 ++--- 3 files changed, 47 insertions(+), 13 deletions(-) (limited to 'compiler/rustc_span/src') diff --git a/compiler/rustc_span/src/def_id.rs b/compiler/rustc_span/src/def_id.rs index 6ee75376ad5..ae488563ccc 100644 --- a/compiler/rustc_span/src/def_id.rs +++ b/compiler/rustc_span/src/def_id.rs @@ -1,7 +1,7 @@ use crate::crate_disambiguator::CrateDisambiguator; use crate::HashStableContext; 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_index::vec::Idx; use rustc_macros::HashStable_Generic; @@ -309,12 +309,47 @@ rustc_data_structures::define_id_collections!(LocalDefIdMap, LocalDefIdSet, Loca impl HashStable for DefId { 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 HashStable 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 HashStable for CrateNum { 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 ToStableHashKey for DefId { + type KeyType = DefPathHash; + + #[inline] + fn to_stable_hash_key(&self, hcx: &CTX) -> DefPathHash { + hcx.def_path_hash(*self) + } +} + +impl ToStableHashKey for LocalDefId { + type KeyType = DefPathHash; + + #[inline] + fn to_stable_hash_key(&self, hcx: &CTX) -> DefPathHash { + hcx.def_path_hash(self.to_def_id()) + } +} + +impl ToStableHashKey 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) } } diff --git a/compiler/rustc_span/src/hygiene.rs b/compiler/rustc_span/src/hygiene.rs index 8f3b8cc2d0e..3915f7879b7 100644 --- a/compiler/rustc_span/src/hygiene.rs +++ b/compiler/rustc_span/src/hygiene.rs @@ -29,7 +29,7 @@ use crate::symbol::{kw, sym, Symbol}; use crate::SESSION_GLOBALS; 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::fx::{FxHashMap, FxHashSet}; 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> { - fn hash_def_id(&mut self, def_id: DefId, hasher: &mut StableHasher) { - def_id.krate.as_u32().hash_stable(self, hasher); - def_id.index.as_u32().hash_stable(self, hasher); + fn def_path_hash(&self, def_id: DefId) -> DefPathHash { + use std::hash::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 { @@ -1345,9 +1348,6 @@ fn update_disambiguator(expn_id: ExpnId) { &CACHE } - fn hash_crate_num(&mut self, krate: CrateNum, hasher: &mut StableHasher) { - krate.as_u32().hash_stable(self, hasher); - } fn hash_spans(&self) -> bool { true } diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index 9cf68cbd23e..b4fe7c980de 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -40,7 +40,7 @@ pub use hygiene::SyntaxContext; use hygiene::Transparency; pub use hygiene::{DesugaringKind, ExpnData, ExpnId, ExpnKind, ForLoopLoc, MacroKind}; pub mod def_id; -use def_id::{CrateNum, DefId, LOCAL_CRATE}; +use def_id::{CrateNum, DefId, DefPathHash, LOCAL_CRATE}; pub mod lev_distance; mod span_encoding; 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 /// instead of implementing everything in rustc_middle. 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`. /// This method allows us to have multiple `HashStableContext` implementations /// that hash things in a different way, without the results of one polluting /// the cache of the other. fn expn_id_cache() -> &'static LocalKey; - fn hash_crate_num(&mut self, _: CrateNum, hasher: &mut StableHasher); fn hash_spans(&self) -> bool; fn span_data_to_lines_and_cols( &mut self, -- cgit 1.4.1-3-g733a5 From d1931b6406af66520245f1a48de0b79d2d960549 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Fri, 11 Jun 2021 16:48:24 +0200 Subject: Sprinkle inline. --- compiler/rustc_span/src/def_id.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'compiler/rustc_span/src') diff --git a/compiler/rustc_span/src/def_id.rs b/compiler/rustc_span/src/def_id.rs index ae488563ccc..bb4ac22d9c8 100644 --- a/compiler/rustc_span/src/def_id.rs +++ b/compiler/rustc_span/src/def_id.rs @@ -308,21 +308,23 @@ impl Decodable for LocalDefId { rustc_data_structures::define_id_collections!(LocalDefIdMap, LocalDefIdSet, LocalDefId); impl HashStable for DefId { + #[inline] fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { - hcx.def_path_hash(*self).hash_stable(hcx, hasher); + self.to_stable_hash_key(hcx).hash_stable(hcx, hasher); } } impl HashStable 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); + self.to_stable_hash_key(hcx).hash_stable(hcx, hasher); } } impl HashStable for CrateNum { + #[inline] fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { - hcx.def_path_hash(DefId { krate: *self, index: CRATE_DEF_INDEX }).hash_stable(hcx, hasher); + self.to_stable_hash_key(hcx).hash_stable(hcx, hasher); } } @@ -349,7 +351,6 @@ impl ToStableHashKey for CrateNum { #[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) + self.as_def_id().to_stable_hash_key(hcx) } } -- cgit 1.4.1-3-g733a5 From 72e9589149ce50603afcca03b9ef3bc5475d71da Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Fri, 11 Jun 2021 16:53:27 +0200 Subject: Make DummyHashStableContext dummier. --- compiler/rustc_span/src/hygiene.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'compiler/rustc_span/src') diff --git a/compiler/rustc_span/src/hygiene.rs b/compiler/rustc_span/src/hygiene.rs index 3915f7879b7..23efaf6f4f3 100644 --- a/compiler/rustc_span/src/hygiene.rs +++ b/compiler/rustc_span/src/hygiene.rs @@ -1330,12 +1330,12 @@ fn update_disambiguator(expn_id: ExpnId) { } impl<'a> crate::HashStableContext for DummyHashStableContext<'a> { + #[inline] fn def_path_hash(&self, def_id: DefId) -> DefPathHash { - use std::hash::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()) + DefPathHash(Fingerprint::new( + def_id.krate.as_u32().into(), + def_id.index.as_u32().into(), + )) } fn expn_id_cache() -> &'static LocalKey { -- cgit 1.4.1-3-g733a5