Skip to content

Commit

Permalink
Auto merge of rust-lang#110367 - saethlin:no-truncations, r=oli-obk
Browse files Browse the repository at this point in the history
Remove some suspicious cast truncations

These truncations were added a long time ago, and as best I can tell without a perf justification. And with rust-lang#110410 it has become perf-neutral to not truncate anymore. We worked hard for all these bits, let's use them.
  • Loading branch information
bors committed Apr 17, 2023
2 parents bdb32bd + 84facac commit e49122f
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 33 deletions.
34 changes: 6 additions & 28 deletions compiler/rustc_data_structures/src/svh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,30 @@
//! mismatches where we have two versions of the same crate that were
//! compiled from distinct sources.

use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use crate::fingerprint::Fingerprint;
use std::fmt;
use std::hash::{Hash, Hasher};

use crate::stable_hasher;

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[derive(Copy, Clone, PartialEq, Eq, Debug, Encodable, Decodable, Hash)]
pub struct Svh {
hash: u64,
hash: Fingerprint,
}

impl Svh {
/// Creates a new `Svh` given the hash. If you actually want to
/// compute the SVH from some HIR, you want the `calculate_svh`
/// function found in `rustc_incremental`.
pub fn new(hash: u64) -> Svh {
pub fn new(hash: Fingerprint) -> Svh {
Svh { hash }
}

pub fn as_u64(&self) -> u64 {
self.hash
self.hash.to_smaller_hash()
}

pub fn to_string(&self) -> String {
format!("{:016x}", self.hash)
}
}

impl Hash for Svh {
fn hash<H>(&self, state: &mut H)
where
H: Hasher,
{
self.hash.to_le().hash(state);
format!("{:016x}", self.hash.to_smaller_hash())
}
}

Expand All @@ -48,18 +38,6 @@ impl fmt::Display for Svh {
}
}

impl<S: Encoder> Encodable<S> for Svh {
fn encode(&self, s: &mut S) {
s.emit_u64(self.as_u64().to_le());
}
}

impl<D: Decoder> Decodable<D> for Svh {
fn decode(d: &mut D) -> Svh {
Svh::new(u64::from_le(d.read_u64()))
}
}

impl<T> stable_hasher::HashStable<T> for Svh {
#[inline]
fn hash_stable(&self, ctx: &mut T, hasher: &mut stable_hasher::StableHasher) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1199,7 +1199,7 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, _: LocalCrate) -> Svh {
stable_hasher.finish()
});

Svh::new(crate_hash.to_smaller_hash())
Svh::new(crate_hash)
}

fn upstream_crates(tcx: TyCtxt<'_>) -> Vec<(StableCrateId, Svh)> {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_query_system/src/ich/impls_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl<'a> HashStable<StableHashingContext<'a>> for SourceFile {
ref normalized_pos,
} = *self;

(name_hash as u64).hash_stable(hcx, hasher);
name_hash.hash_stable(hcx, hasher);

src_hash.hash_stable(hcx, hasher);

Expand Down
4 changes: 1 addition & 3 deletions compiler/rustc_span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2160,9 +2160,7 @@ where
};

Hash::hash(&TAG_VALID_SPAN, hasher);
// We truncate the stable ID hash and line and column numbers. The chances
// of causing a collision this way should be minimal.
Hash::hash(&(file.name_hash as u64), hasher);
Hash::hash(&file.name_hash, hasher);

// Hash both the length and the end location (line/column) of a span. If we
// hash only the length, for example, then two otherwise equal spans with
Expand Down

0 comments on commit e49122f

Please sign in to comment.