Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove some suspicious cast truncations #110367

Merged
merged 1 commit into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably look into changing this to print the full fingerprint.

}
}

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 @@ -2159,9 +2159,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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this actually has a perf effect after all. The benchmarked version was merging the two u64s of the stable hash and hashing the resulting u64.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This landed out of order. The PR that makes this not a regression: #110410 is still in queue


// 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