Skip to content

Commit

Permalink
Move hashes from rustc_data_structure to rustc_hashes so they can be …
Browse files Browse the repository at this point in the history
…shared with rust-analyzer
  • Loading branch information
saethlin committed Feb 16, 2025
1 parent 1d7cf0f commit 4cf2186
Show file tree
Hide file tree
Showing 50 changed files with 152 additions and 67 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3317,6 +3317,7 @@ dependencies = [
"rand 0.8.5",
"rand_xoshiro",
"rustc_data_structures",
"rustc_hashes",
"rustc_index",
"rustc_macros",
"rustc_serialize",
Expand Down Expand Up @@ -3544,6 +3545,7 @@ dependencies = [
"rustc_errors",
"rustc_fluent_macro",
"rustc_fs_util",
"rustc_hashes",
"rustc_hir",
"rustc_index",
"rustc_llvm",
Expand Down Expand Up @@ -3586,6 +3588,7 @@ dependencies = [
"rustc_errors",
"rustc_fluent_macro",
"rustc_fs_util",
"rustc_hashes",
"rustc_hir",
"rustc_hir_pretty",
"rustc_incremental",
Expand Down Expand Up @@ -3658,6 +3661,7 @@ dependencies = [
"rustc-stable-hash",
"rustc_arena",
"rustc_graphviz",
"rustc_hashes",
"rustc_index",
"rustc_macros",
"rustc_serialize",
Expand Down Expand Up @@ -3768,6 +3772,7 @@ dependencies = [
"rustc_error_codes",
"rustc_error_messages",
"rustc_fluent_macro",
"rustc_hashes",
"rustc_hir",
"rustc_index",
"rustc_lexer",
Expand Down Expand Up @@ -3840,6 +3845,13 @@ version = "0.0.0"
name = "rustc_graphviz"
version = "0.0.0"

[[package]]
name = "rustc_hashes"
version = "0.0.0"
dependencies = [
"rustc-stable-hash",
]

[[package]]
name = "rustc_hir"
version = "0.0.0"
Expand All @@ -3849,6 +3861,7 @@ dependencies = [
"rustc_arena",
"rustc_ast",
"rustc_data_structures",
"rustc_hashes",
"rustc_index",
"rustc_macros",
"rustc_serialize",
Expand Down Expand Up @@ -4169,6 +4182,7 @@ dependencies = [
"rustc_feature",
"rustc_fluent_macro",
"rustc_graphviz",
"rustc_hashes",
"rustc_hir",
"rustc_hir_pretty",
"rustc_index",
Expand Down Expand Up @@ -4405,6 +4419,7 @@ dependencies = [
"measureme",
"rustc_data_structures",
"rustc_errors",
"rustc_hashes",
"rustc_hir",
"rustc_index",
"rustc_middle",
Expand All @@ -4428,6 +4443,7 @@ dependencies = [
"rustc_errors",
"rustc_feature",
"rustc_fluent_macro",
"rustc_hashes",
"rustc_hir",
"rustc_index",
"rustc_macros",
Expand Down Expand Up @@ -4488,6 +4504,7 @@ name = "rustc_serialize"
version = "0.0.0"
dependencies = [
"indexmap",
"rustc_hashes",
"rustc_macros",
"smallvec",
"tempfile",
Expand All @@ -4508,6 +4525,7 @@ dependencies = [
"rustc_feature",
"rustc_fluent_macro",
"rustc_fs_util",
"rustc_hashes",
"rustc_hir",
"rustc_lint_defs",
"rustc_macros",
Expand Down Expand Up @@ -4549,6 +4567,7 @@ dependencies = [
"md-5",
"rustc_arena",
"rustc_data_structures",
"rustc_hashes",
"rustc_index",
"rustc_macros",
"rustc_serialize",
Expand All @@ -4568,6 +4587,7 @@ dependencies = [
"rustc_abi",
"rustc_data_structures",
"rustc_errors",
"rustc_hashes",
"rustc_hir",
"rustc_middle",
"rustc_session",
Expand Down Expand Up @@ -4663,6 +4683,7 @@ dependencies = [
"rustc_data_structures",
"rustc_errors",
"rustc_fluent_macro",
"rustc_hashes",
"rustc_hir",
"rustc_index",
"rustc_infer",
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_abi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ bitflags = "2.4.1"
rand = { version = "0.8.4", default-features = false, optional = true }
rand_xoshiro = { version = "0.6.0", optional = true }
rustc_data_structures = { path = "../rustc_data_structures", optional = true }
rustc_hashes = { path = "../rustc_hashes" }
rustc_index = { path = "../rustc_index", default-features = false }
rustc_macros = { path = "../rustc_macros", optional = true }
rustc_serialize = { path = "../rustc_serialize", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_abi/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt::{self, Write};
use std::ops::{Bound, Deref};
use std::{cmp, iter};

use rustc_data_structures::stable_hasher::Hash64;
use rustc_hashes::Hash64;
use rustc_index::Idx;
use tracing::debug;

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ use std::ops::{Add, AddAssign, Mul, RangeInclusive, Sub};
use std::str::FromStr;

use bitflags::bitflags;
use rustc_data_structures::stable_hasher::Hash64;
#[cfg(feature = "nightly")]
use rustc_data_structures::stable_hasher::StableOrd;
use rustc_hashes::Hash64;
use rustc_index::{Idx, IndexSlice, IndexVec};
#[cfg(feature = "nightly")]
use rustc_macros::{Decodable_Generic, Encodable_Generic, HashStable_Generic};
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_codegen_llvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ rustc_data_structures = { path = "../rustc_data_structures" }
rustc_errors = { path = "../rustc_errors" }
rustc_fluent_macro = { path = "../rustc_fluent_macro" }
rustc_fs_util = { path = "../rustc_fs_util" }
rustc_hashes = { path = "../rustc_hashes" }
rustc_hir = { path = "../rustc_hir" }
rustc_index = { path = "../rustc_index" }
rustc_llvm = { path = "../rustc_llvm" }
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_codegen_llvm/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use rustc_abi::{AddressSpace, HasDataLayout};
use rustc_ast::Mutability;
use rustc_codegen_ssa::common::TypeKind;
use rustc_codegen_ssa::traits::*;
use rustc_data_structures::stable_hasher::{Hash128, HashStable, StableHasher};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_hashes::Hash128;
use rustc_hir::def_id::DefId;
use rustc_middle::bug;
use rustc_middle::mir::interpret::{ConstAllocation, GlobalAlloc, Scalar};
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_codegen_ssa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ rustc_data_structures = { path = "../rustc_data_structures" }
rustc_errors = { path = "../rustc_errors" }
rustc_fluent_macro = { path = "../rustc_fluent_macro" }
rustc_fs_util = { path = "../rustc_fs_util" }
rustc_hashes = { path = "../rustc_hashes" }
rustc_hir = { path = "../rustc_hir" }
rustc_hir_pretty = { path = "../rustc_hir_pretty" }
rustc_incremental = { path = "../rustc_incremental" }
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ use std::fmt::Write;

use rustc_abi::Integer;
use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::stable_hasher::{Hash64, HashStable, StableHasher};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_hashes::Hash64;
use rustc_hir::def_id::DefId;
use rustc_hir::definitions::{DefPathData, DefPathDataName, DisambiguatedDefPathData};
use rustc_hir::{CoroutineDesugaring, CoroutineKind, CoroutineSource, Mutability};
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_data_structures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ rustc-rayon = { version = "0.5.1", features = ["indexmap"] }
rustc-stable-hash = { version = "0.1.0", features = ["nightly"] }
rustc_arena = { path = "../rustc_arena" }
rustc_graphviz = { path = "../rustc_graphviz" }
rustc_hashes = { path = "../rustc_hashes" }
rustc_index = { path = "../rustc_index", package = "rustc_index" }
rustc_macros = { path = "../rustc_macros" }
rustc_serialize = { path = "../rustc_serialize" }
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_data_structures/src/fingerprint.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::hash::{Hash, Hasher};

use rustc_hashes::Hash64;
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};

use crate::stable_hasher::{
FromStableHash, Hash64, StableHasherHash, impl_stable_traits_for_trivial_type,
};
use crate::stable_hasher::{FromStableHash, StableHasherHash, impl_stable_traits_for_trivial_type};

#[cfg(test)]
mod tests;
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_data_structures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ pub mod vec_cache;
pub mod work_queue;

mod atomic_ref;
mod hashes;

/// This calls the passed function while ensuring it won't be inlined into the caller.
#[inline(never)]
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_data_structures/src/stable_hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ use smallvec::SmallVec;
#[cfg(test)]
mod tests;

use rustc_hashes::{Hash64, Hash128};
pub use rustc_stable_hash::{
FromStableHash, SipHasher128Hash as StableHasherHash, StableSipHasher128 as StableHasher,
};

pub use crate::hashes::{Hash64, Hash128};

/// Something that implements `HashStable<CTX>` can be hashed in a way that is
/// stable across multiple compilation sessions.
///
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_data_structures/src/tagged_ptr/tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::ptr;

use rustc_hashes::Hash128;

use super::*;
use crate::hashes::Hash128;
use crate::stable_hasher::{HashStable, StableHasher};

/// A tag type used in [`TaggedRef`] tests.
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_errors/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ rustc_data_structures = { path = "../rustc_data_structures" }
rustc_error_codes = { path = "../rustc_error_codes" }
rustc_error_messages = { path = "../rustc_error_messages" }
rustc_fluent_macro = { path = "../rustc_fluent_macro" }
rustc_hashes = { path = "../rustc_hashes" }
rustc_hir = { path = "../rustc_hir" }
rustc_index = { path = "../rustc_index" }
rustc_lexer = { path = "../rustc_lexer" }
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ pub use emitter::ColorConfig;
use emitter::{DynEmitter, Emitter, is_case_difference, is_different};
use rustc_data_structures::AtomicRef;
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
use rustc_data_structures::stable_hasher::{Hash128, StableHasher};
use rustc_data_structures::stable_hasher::StableHasher;
use rustc_data_structures::sync::{DynSend, Lock};
pub use rustc_error_messages::{
DiagMessage, FluentBundle, LanguageIdentifier, LazyFallbackBundle, MultiSpan, SpanLabel,
SubdiagMessage, fallback_fluent_bundle, fluent_bundle,
};
use rustc_hashes::Hash128;
use rustc_lint_defs::LintExpectationId;
pub use rustc_lint_defs::{Applicability, listify, pluralize};
use rustc_macros::{Decodable, Encodable};
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_hashes/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "rustc_hashes"
version = "0.0.0"
edition = "2021"

[dependencies]
# tidy-alphabetical-start
rustc-stable-hash = { version = "0.1.0" }
# tidy-alphabetical-end
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! rustc encodes a lot of hashes. If hashes are stored as `u64` or `u128`, a `derive(Encodable)`
//! will apply varint encoding to the hashes, which is less efficient than directly encoding the 8
//! or 16 bytes of the hash.
//! or 16 bytes of the hash. And if that hash depends on the `StableCrateHash` (which most in rustc
//! do), the varint encoding will make the number of bytes encoded fluctuate between compiler
//! versions.
//!
//! The types in this module represent 64-bit or 128-bit hashes produced by a `StableHasher`.
//! `Hash64` and `Hash128` expose some utility functions to encourage users to not extract the inner
Expand All @@ -14,10 +16,9 @@
use std::fmt;
use std::ops::BitXorAssign;

use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};

use crate::stable_hasher::{FromStableHash, StableHasherHash};
use rustc_stable_hash::{FromStableHash, SipHasher128Hash as StableHasherHash};

/// A `u64` but encoded with a fixed size; for hashes this encoding is more compact than `u64`.
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
pub struct Hash64 {
inner: u64,
Expand Down Expand Up @@ -49,20 +50,6 @@ impl BitXorAssign<u64> for Hash64 {
}
}

impl<S: Encoder> Encodable<S> for Hash64 {
#[inline]
fn encode(&self, s: &mut S) {
s.emit_raw_bytes(&self.inner.to_le_bytes());
}
}

impl<D: Decoder> Decodable<D> for Hash64 {
#[inline]
fn decode(d: &mut D) -> Self {
Self { inner: u64::from_le_bytes(d.read_raw_bytes(8).try_into().unwrap()) }
}
}

impl FromStableHash for Hash64 {
type Hash = StableHasherHash;

Expand All @@ -84,6 +71,7 @@ impl fmt::LowerHex for Hash64 {
}
}

/// A `u128` but encoded with a fixed size; for hashes this encoding is more compact than `u128`.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)]
pub struct Hash128 {
inner: u128,
Expand All @@ -100,6 +88,11 @@ impl std::hash::Hash for Hash128 {
}

impl Hash128 {
#[inline]
pub fn new(n: u128) -> Self {
Self { inner: n }
}

#[inline]
pub fn truncate(self) -> Hash64 {
Hash64 { inner: self.inner as u64 }
Expand All @@ -116,20 +109,6 @@ impl Hash128 {
}
}

impl<S: Encoder> Encodable<S> for Hash128 {
#[inline]
fn encode(&self, s: &mut S) {
s.emit_raw_bytes(&self.inner.to_le_bytes());
}
}

impl<D: Decoder> Decodable<D> for Hash128 {
#[inline]
fn decode(d: &mut D) -> Self {
Self { inner: u128::from_le_bytes(d.read_raw_bytes(16).try_into().unwrap()) }
}
}

impl FromStableHash for Hash128 {
type Hash = StableHasherHash;

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ rustc_abi = { path = "../rustc_abi" }
rustc_arena = { path = "../rustc_arena" }
rustc_ast = { path = "../rustc_ast" }
rustc_data_structures = { path = "../rustc_data_structures" }
rustc_hashes = { path = "../rustc_hashes" }
rustc_index = { path = "../rustc_index" }
rustc_macros = { path = "../rustc_macros" }
rustc_serialize = { path = "../rustc_serialize" }
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir/src/def_path_hash_map.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rustc_data_structures::stable_hasher::Hash64;
use rustc_hashes::Hash64;
use rustc_span::def_id::DefIndex;

#[derive(Clone, Default)]
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_hir/src/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
use std::fmt::{self, Write};
use std::hash::Hash;

use rustc_data_structures::stable_hasher::{Hash64, StableHasher};
use rustc_data_structures::stable_hasher::StableHasher;
use rustc_data_structures::unord::UnordMap;
use rustc_hashes::Hash64;
use rustc_index::IndexVec;
use rustc_macros::{Decodable, Encodable};
use rustc_span::{Symbol, kw, sym};
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(rustc::symbol_intern_string_literal)]

use rustc_data_structures::stable_hasher::Hash64;
use rustc_hashes::Hash64;
use rustc_span::def_id::{DefPathHash, StableCrateId};
use rustc_span::edition::Edition;
use rustc_span::{Symbol, create_session_globals_then};
Expand Down
Loading

0 comments on commit 4cf2186

Please sign in to comment.