Skip to content

Commit

Permalink
Auto merge of #83064 - cjgillot:fhash, r=jackh726
Browse files Browse the repository at this point in the history
Tweaks to stable hashing
  • Loading branch information
bors committed Mar 13, 2021
2 parents e7e1dc1 + 34e92bb commit acca818
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 20 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_data_structures/src/stable_hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl StableHasher {
StableHasher { state: SipHasher128::new_with_keys(0, 0) }
}

#[inline]
pub fn finish<W: StableHasherResult>(self) -> W {
W::finish(self)
}
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_macros/src/hash_stable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub fn hash_stable_generic_derive(mut s: synstructure::Structure<'_>) -> proc_ma
s.bound_impl(
quote!(::rustc_data_structures::stable_hasher::HashStable<__CTX>),
quote! {
#[inline]
fn hash_stable(
&self,
__hcx: &mut __CTX,
Expand Down Expand Up @@ -119,6 +120,7 @@ pub fn hash_stable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::To
>
),
quote! {
#[inline]
fn hash_stable(
&self,
__hcx: &mut ::rustc_middle::ich::StableHashingContext<'__ctx>,
Expand Down
7 changes: 0 additions & 7 deletions compiler/rustc_middle/src/ich/hcx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,6 @@ impl<'a> rustc_span::HashStableContext for StableHashingContext<'a> {
&CACHE
}

fn byte_pos_to_line_and_col(
&mut self,
byte: BytePos,
) -> Option<(Lrc<SourceFile>, usize, BytePos)> {
self.source_map().byte_pos_to_line_and_col(byte)
}

fn span_data_to_lines_and_cols(
&mut self,
span: &SpanData,
Expand Down
6 changes: 0 additions & 6 deletions compiler/rustc_span/src/hygiene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1362,12 +1362,6 @@ fn update_disambiguator(expn_id: ExpnId) {
fn hash_spans(&self) -> bool {
true
}
fn byte_pos_to_line_and_col(
&mut self,
byte: BytePos,
) -> Option<(Lrc<SourceFile>, usize, BytePos)> {
self.caching_source_map.byte_pos_to_line_and_col(byte)
}
fn span_data_to_lines_and_cols(
&mut self,
span: &crate::SpanData,
Expand Down
9 changes: 2 additions & 7 deletions compiler/rustc_span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1874,10 +1874,6 @@ pub trait HashStableContext {
fn expn_id_cache() -> &'static LocalKey<ExpnIdCache>;
fn hash_crate_num(&mut self, _: CrateNum, hasher: &mut StableHasher);
fn hash_spans(&self) -> bool;
fn byte_pos_to_line_and_col(
&mut self,
byte: BytePos,
) -> Option<(Lrc<SourceFile>, usize, BytePos)>;
fn span_data_to_lines_and_cols(
&mut self,
span: &SpanData,
Expand Down Expand Up @@ -1906,9 +1902,10 @@ where
return;
}

self.ctxt().hash_stable(ctx, hasher);

if self.is_dummy() {
Hash::hash(&TAG_INVALID_SPAN, hasher);
self.ctxt().hash_stable(ctx, hasher);
return;
}

Expand All @@ -1921,7 +1918,6 @@ where
Some(pos) => pos,
None => {
Hash::hash(&TAG_INVALID_SPAN, hasher);
span.ctxt.hash_stable(ctx, hasher);
return;
}
};
Expand All @@ -1948,7 +1944,6 @@ where
let len = (span.hi - span.lo).0;
Hash::hash(&col_line, hasher);
Hash::hash(&len, hasher);
span.ctxt.hash_stable(ctx, hasher);
}
}

Expand Down
13 changes: 13 additions & 0 deletions library/core/src/hash/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,10 +548,12 @@ mod impls {
($(($ty:ident, $meth:ident),)*) => {$(
#[stable(feature = "rust1", since = "1.0.0")]
impl Hash for $ty {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
state.$meth(*self)
}

#[inline]
fn hash_slice<H: Hasher>(data: &[$ty], state: &mut H) {
let newlen = data.len() * mem::size_of::<$ty>();
let ptr = data.as_ptr() as *const u8;
Expand Down Expand Up @@ -582,20 +584,23 @@ mod impls {

#[stable(feature = "rust1", since = "1.0.0")]
impl Hash for bool {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
state.write_u8(*self as u8)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl Hash for char {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
state.write_u32(*self as u32)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl Hash for str {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
state.write(self.as_bytes());
state.write_u8(0xff)
Expand All @@ -604,6 +609,7 @@ mod impls {

#[stable(feature = "never_hash", since = "1.29.0")]
impl Hash for ! {
#[inline]
fn hash<H: Hasher>(&self, _: &mut H) {
*self
}
Expand All @@ -613,6 +619,7 @@ mod impls {
() => (
#[stable(feature = "rust1", since = "1.0.0")]
impl Hash for () {
#[inline]
fn hash<H: Hasher>(&self, _state: &mut H) {}
}
);
Expand All @@ -621,6 +628,7 @@ mod impls {
#[stable(feature = "rust1", since = "1.0.0")]
impl<$($name: Hash),+> Hash for ($($name,)+) where last_type!($($name,)+): ?Sized {
#[allow(non_snake_case)]
#[inline]
fn hash<S: Hasher>(&self, state: &mut S) {
let ($(ref $name,)+) = *self;
$($name.hash(state);)+
Expand Down Expand Up @@ -650,6 +658,7 @@ mod impls {

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: Hash> Hash for [T] {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
self.len().hash(state);
Hash::hash_slice(self, state)
Expand All @@ -658,20 +667,23 @@ mod impls {

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized + Hash> Hash for &T {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
(**self).hash(state);
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized + Hash> Hash for &mut T {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
(**self).hash(state);
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized> Hash for *const T {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
#[cfg(not(bootstrap))]
{
Expand Down Expand Up @@ -701,6 +713,7 @@ mod impls {

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized> Hash for *mut T {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
#[cfg(not(bootstrap))]
{
Expand Down

0 comments on commit acca818

Please sign in to comment.