Skip to content

Commit 7b99665

Browse files
committed
Move hashes from rustc_data_structure to rustc_hashes so they can be shared with rust-analyzer
1 parent 1d7cf0f commit 7b99665

File tree

49 files changed

+150
-67
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+150
-67
lines changed

Cargo.lock

+21
Original file line numberDiff line numberDiff line change
@@ -3317,6 +3317,7 @@ dependencies = [
33173317
"rand 0.8.5",
33183318
"rand_xoshiro",
33193319
"rustc_data_structures",
3320+
"rustc_hashes",
33203321
"rustc_index",
33213322
"rustc_macros",
33223323
"rustc_serialize",
@@ -3544,6 +3545,7 @@ dependencies = [
35443545
"rustc_errors",
35453546
"rustc_fluent_macro",
35463547
"rustc_fs_util",
3548+
"rustc_hashes",
35473549
"rustc_hir",
35483550
"rustc_index",
35493551
"rustc_llvm",
@@ -3586,6 +3588,7 @@ dependencies = [
35863588
"rustc_errors",
35873589
"rustc_fluent_macro",
35883590
"rustc_fs_util",
3591+
"rustc_hashes",
35893592
"rustc_hir",
35903593
"rustc_hir_pretty",
35913594
"rustc_incremental",
@@ -3658,6 +3661,7 @@ dependencies = [
36583661
"rustc-stable-hash",
36593662
"rustc_arena",
36603663
"rustc_graphviz",
3664+
"rustc_hashes",
36613665
"rustc_index",
36623666
"rustc_macros",
36633667
"rustc_serialize",
@@ -3768,6 +3772,7 @@ dependencies = [
37683772
"rustc_error_codes",
37693773
"rustc_error_messages",
37703774
"rustc_fluent_macro",
3775+
"rustc_hashes",
37713776
"rustc_hir",
37723777
"rustc_index",
37733778
"rustc_lexer",
@@ -3840,6 +3845,13 @@ version = "0.0.0"
38403845
name = "rustc_graphviz"
38413846
version = "0.0.0"
38423847

3848+
[[package]]
3849+
name = "rustc_hashes"
3850+
version = "0.0.0"
3851+
dependencies = [
3852+
"rustc-stable-hash",
3853+
]
3854+
38433855
[[package]]
38443856
name = "rustc_hir"
38453857
version = "0.0.0"
@@ -3849,6 +3861,7 @@ dependencies = [
38493861
"rustc_arena",
38503862
"rustc_ast",
38513863
"rustc_data_structures",
3864+
"rustc_hashes",
38523865
"rustc_index",
38533866
"rustc_macros",
38543867
"rustc_serialize",
@@ -4169,6 +4182,7 @@ dependencies = [
41694182
"rustc_feature",
41704183
"rustc_fluent_macro",
41714184
"rustc_graphviz",
4185+
"rustc_hashes",
41724186
"rustc_hir",
41734187
"rustc_hir_pretty",
41744188
"rustc_index",
@@ -4405,6 +4419,7 @@ dependencies = [
44054419
"measureme",
44064420
"rustc_data_structures",
44074421
"rustc_errors",
4422+
"rustc_hashes",
44084423
"rustc_hir",
44094424
"rustc_index",
44104425
"rustc_middle",
@@ -4428,6 +4443,7 @@ dependencies = [
44284443
"rustc_errors",
44294444
"rustc_feature",
44304445
"rustc_fluent_macro",
4446+
"rustc_hashes",
44314447
"rustc_hir",
44324448
"rustc_index",
44334449
"rustc_macros",
@@ -4488,6 +4504,7 @@ name = "rustc_serialize"
44884504
version = "0.0.0"
44894505
dependencies = [
44904506
"indexmap",
4507+
"rustc_hashes",
44914508
"rustc_macros",
44924509
"smallvec",
44934510
"tempfile",
@@ -4508,6 +4525,7 @@ dependencies = [
45084525
"rustc_feature",
45094526
"rustc_fluent_macro",
45104527
"rustc_fs_util",
4528+
"rustc_hashes",
45114529
"rustc_hir",
45124530
"rustc_lint_defs",
45134531
"rustc_macros",
@@ -4549,6 +4567,7 @@ dependencies = [
45494567
"md-5",
45504568
"rustc_arena",
45514569
"rustc_data_structures",
4570+
"rustc_hashes",
45524571
"rustc_index",
45534572
"rustc_macros",
45544573
"rustc_serialize",
@@ -4568,6 +4587,7 @@ dependencies = [
45684587
"rustc_abi",
45694588
"rustc_data_structures",
45704589
"rustc_errors",
4590+
"rustc_hashes",
45714591
"rustc_hir",
45724592
"rustc_middle",
45734593
"rustc_session",
@@ -4663,6 +4683,7 @@ dependencies = [
46634683
"rustc_data_structures",
46644684
"rustc_errors",
46654685
"rustc_fluent_macro",
4686+
"rustc_hashes",
46664687
"rustc_hir",
46674688
"rustc_index",
46684689
"rustc_infer",

compiler/rustc_abi/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ bitflags = "2.4.1"
99
rand = { version = "0.8.4", default-features = false, optional = true }
1010
rand_xoshiro = { version = "0.6.0", optional = true }
1111
rustc_data_structures = { path = "../rustc_data_structures", optional = true }
12+
rustc_hashes = { path = "../rustc_hashes" }
1213
rustc_index = { path = "../rustc_index", default-features = false }
1314
rustc_macros = { path = "../rustc_macros", optional = true }
1415
rustc_serialize = { path = "../rustc_serialize", optional = true }

compiler/rustc_abi/src/layout.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::fmt::{self, Write};
22
use std::ops::{Bound, Deref};
33
use std::{cmp, iter};
44

5-
use rustc_data_structures::stable_hasher::Hash64;
5+
use rustc_hashes::Hash64;
66
use rustc_index::Idx;
77
use tracing::debug;
88

compiler/rustc_abi/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ use std::ops::{Add, AddAssign, Mul, RangeInclusive, Sub};
4848
use std::str::FromStr;
4949

5050
use bitflags::bitflags;
51-
use rustc_data_structures::stable_hasher::Hash64;
5251
#[cfg(feature = "nightly")]
5352
use rustc_data_structures::stable_hasher::StableOrd;
53+
use rustc_hashes::Hash64;
5454
use rustc_index::{Idx, IndexSlice, IndexVec};
5555
#[cfg(feature = "nightly")]
5656
use rustc_macros::{Decodable_Generic, Encodable_Generic, HashStable_Generic};

compiler/rustc_codegen_llvm/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ rustc_data_structures = { path = "../rustc_data_structures" }
2525
rustc_errors = { path = "../rustc_errors" }
2626
rustc_fluent_macro = { path = "../rustc_fluent_macro" }
2727
rustc_fs_util = { path = "../rustc_fs_util" }
28+
rustc_hashes = { path = "../rustc_hashes" }
2829
rustc_hir = { path = "../rustc_hir" }
2930
rustc_index = { path = "../rustc_index" }
3031
rustc_llvm = { path = "../rustc_llvm" }

compiler/rustc_codegen_llvm/src/common.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use rustc_abi::{AddressSpace, HasDataLayout};
77
use rustc_ast::Mutability;
88
use rustc_codegen_ssa::common::TypeKind;
99
use rustc_codegen_ssa::traits::*;
10-
use rustc_data_structures::stable_hasher::{Hash128, HashStable, StableHasher};
10+
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
11+
use rustc_hashes::Hash128;
1112
use rustc_hir::def_id::DefId;
1213
use rustc_middle::bug;
1314
use rustc_middle::mir::interpret::{ConstAllocation, GlobalAlloc, Scalar};

compiler/rustc_codegen_ssa/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ rustc_data_structures = { path = "../rustc_data_structures" }
2525
rustc_errors = { path = "../rustc_errors" }
2626
rustc_fluent_macro = { path = "../rustc_fluent_macro" }
2727
rustc_fs_util = { path = "../rustc_fs_util" }
28+
rustc_hashes = { path = "../rustc_hashes" }
2829
rustc_hir = { path = "../rustc_hir" }
2930
rustc_hir_pretty = { path = "../rustc_hir_pretty" }
3031
rustc_incremental = { path = "../rustc_incremental" }

compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ use std::fmt::Write;
1515

1616
use rustc_abi::Integer;
1717
use rustc_data_structures::fx::FxHashSet;
18-
use rustc_data_structures::stable_hasher::{Hash64, HashStable, StableHasher};
18+
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
19+
use rustc_hashes::Hash64;
1920
use rustc_hir::def_id::DefId;
2021
use rustc_hir::definitions::{DefPathData, DefPathDataName, DisambiguatedDefPathData};
2122
use rustc_hir::{CoroutineDesugaring, CoroutineKind, CoroutineSource, Mutability};

compiler/rustc_data_structures/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ rustc-rayon = { version = "0.5.1", features = ["indexmap"] }
1818
rustc-stable-hash = { version = "0.1.0", features = ["nightly"] }
1919
rustc_arena = { path = "../rustc_arena" }
2020
rustc_graphviz = { path = "../rustc_graphviz" }
21+
rustc_hashes = { path = "../rustc_hashes" }
2122
rustc_index = { path = "../rustc_index", package = "rustc_index" }
2223
rustc_macros = { path = "../rustc_macros" }
2324
rustc_serialize = { path = "../rustc_serialize" }

compiler/rustc_data_structures/src/fingerprint.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use std::hash::{Hash, Hasher};
22

3+
use rustc_hashes::Hash64;
34
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
45

5-
use crate::stable_hasher::{
6-
FromStableHash, Hash64, StableHasherHash, impl_stable_traits_for_trivial_type,
7-
};
6+
use crate::stable_hasher::{FromStableHash, StableHasherHash, impl_stable_traits_for_trivial_type};
87

98
#[cfg(test)]
109
mod tests;

compiler/rustc_data_structures/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ pub mod vec_cache;
8484
pub mod work_queue;
8585

8686
mod atomic_ref;
87-
mod hashes;
8887

8988
/// This calls the passed function while ensuring it won't be inlined into the caller.
9089
#[inline(never)]

compiler/rustc_data_structures/src/stable_hasher.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ use smallvec::SmallVec;
1010
#[cfg(test)]
1111
mod tests;
1212

13+
use rustc_hashes::{Hash64, Hash128};
1314
pub use rustc_stable_hash::{
1415
FromStableHash, SipHasher128Hash as StableHasherHash, StableSipHasher128 as StableHasher,
1516
};
1617

17-
pub use crate::hashes::{Hash64, Hash128};
18-
1918
/// Something that implements `HashStable<CTX>` can be hashed in a way that is
2019
/// stable across multiple compilation sessions.
2120
///

compiler/rustc_data_structures/src/tagged_ptr/tests.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use std::ptr;
22

3+
use rustc_hashes::Hash128;
4+
35
use super::*;
4-
use crate::hashes::Hash128;
56
use crate::stable_hasher::{HashStable, StableHasher};
67

78
/// A tag type used in [`TaggedRef`] tests.

compiler/rustc_errors/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ rustc_data_structures = { path = "../rustc_data_structures" }
1414
rustc_error_codes = { path = "../rustc_error_codes" }
1515
rustc_error_messages = { path = "../rustc_error_messages" }
1616
rustc_fluent_macro = { path = "../rustc_fluent_macro" }
17+
rustc_hashes = { path = "../rustc_hashes" }
1718
rustc_hir = { path = "../rustc_hir" }
1819
rustc_index = { path = "../rustc_index" }
1920
rustc_lexer = { path = "../rustc_lexer" }

compiler/rustc_errors/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,13 @@ pub use emitter::ColorConfig;
5858
use emitter::{DynEmitter, Emitter, is_case_difference, is_different};
5959
use rustc_data_structures::AtomicRef;
6060
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
61-
use rustc_data_structures::stable_hasher::{Hash128, StableHasher};
61+
use rustc_data_structures::stable_hasher::StableHasher;
6262
use rustc_data_structures::sync::{DynSend, Lock};
6363
pub use rustc_error_messages::{
6464
DiagMessage, FluentBundle, LanguageIdentifier, LazyFallbackBundle, MultiSpan, SpanLabel,
6565
SubdiagMessage, fallback_fluent_bundle, fluent_bundle,
6666
};
67+
use rustc_hashes::Hash128;
6768
use rustc_lint_defs::LintExpectationId;
6869
pub use rustc_lint_defs::{Applicability, listify, pluralize};
6970
use rustc_macros::{Decodable, Encodable};

compiler/rustc_hashes/Cargo.toml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "rustc_hashes"
3+
version = "0.0.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
# tidy-alphabetical-start
8+
rustc-stable-hash = { version = "0.1.0" }
9+
# tidy-alphabetical-end

compiler/rustc_data_structures/src/hashes.rs compiler/rustc_hashes/src/lib.rs

+11-32
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! rustc encodes a lot of hashes. If hashes are stored as `u64` or `u128`, a `derive(Encodable)`
22
//! will apply varint encoding to the hashes, which is less efficient than directly encoding the 8
3-
//! or 16 bytes of the hash.
3+
//! or 16 bytes of the hash. And if that hash depends on the `StableCrateHash` (which most in rustc
4+
//! do), the varint encoding will make the number of bytes encoded fluctuate between compiler
5+
//! versions.
46
//!
57
//! The types in this module represent 64-bit or 128-bit hashes produced by a `StableHasher`.
68
//! `Hash64` and `Hash128` expose some utility functions to encourage users to not extract the inner
@@ -14,10 +16,9 @@
1416
use std::fmt;
1517
use std::ops::BitXorAssign;
1618

17-
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
18-
19-
use crate::stable_hasher::{FromStableHash, StableHasherHash};
19+
use rustc_stable_hash::{FromStableHash, SipHasher128Hash as StableHasherHash};
2020

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

52-
impl<S: Encoder> Encodable<S> for Hash64 {
53-
#[inline]
54-
fn encode(&self, s: &mut S) {
55-
s.emit_raw_bytes(&self.inner.to_le_bytes());
56-
}
57-
}
58-
59-
impl<D: Decoder> Decodable<D> for Hash64 {
60-
#[inline]
61-
fn decode(d: &mut D) -> Self {
62-
Self { inner: u64::from_le_bytes(d.read_raw_bytes(8).try_into().unwrap()) }
63-
}
64-
}
65-
6653
impl FromStableHash for Hash64 {
6754
type Hash = StableHasherHash;
6855

@@ -84,6 +71,7 @@ impl fmt::LowerHex for Hash64 {
8471
}
8572
}
8673

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

10290
impl Hash128 {
91+
#[inline]
92+
pub fn new(n: u128) -> Self {
93+
Self { inner: n }
94+
}
95+
10396
#[inline]
10497
pub fn truncate(self) -> Hash64 {
10598
Hash64 { inner: self.inner as u64 }
@@ -116,20 +109,6 @@ impl Hash128 {
116109
}
117110
}
118111

119-
impl<S: Encoder> Encodable<S> for Hash128 {
120-
#[inline]
121-
fn encode(&self, s: &mut S) {
122-
s.emit_raw_bytes(&self.inner.to_le_bytes());
123-
}
124-
}
125-
126-
impl<D: Decoder> Decodable<D> for Hash128 {
127-
#[inline]
128-
fn decode(d: &mut D) -> Self {
129-
Self { inner: u128::from_le_bytes(d.read_raw_bytes(16).try_into().unwrap()) }
130-
}
131-
}
132-
133112
impl FromStableHash for Hash128 {
134113
type Hash = StableHasherHash;
135114

compiler/rustc_hir/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ rustc_abi = { path = "../rustc_abi" }
1010
rustc_arena = { path = "../rustc_arena" }
1111
rustc_ast = { path = "../rustc_ast" }
1212
rustc_data_structures = { path = "../rustc_data_structures" }
13+
rustc_hashes = { path = "../rustc_hashes" }
1314
rustc_index = { path = "../rustc_index" }
1415
rustc_macros = { path = "../rustc_macros" }
1516
rustc_serialize = { path = "../rustc_serialize" }

compiler/rustc_hir/src/def_path_hash_map.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_data_structures::stable_hasher::Hash64;
1+
use rustc_hashes::Hash64;
22
use rustc_span::def_id::DefIndex;
33

44
#[derive(Clone, Default)]

compiler/rustc_hir/src/definitions.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
use std::fmt::{self, Write};
88
use std::hash::Hash;
99

10-
use rustc_data_structures::stable_hasher::{Hash64, StableHasher};
10+
use rustc_data_structures::stable_hasher::StableHasher;
1111
use rustc_data_structures::unord::UnordMap;
12+
use rustc_hashes::Hash64;
1213
use rustc_index::IndexVec;
1314
use rustc_macros::{Decodable, Encodable};
1415
use rustc_span::{Symbol, kw, sym};

compiler/rustc_hir/src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(rustc::symbol_intern_string_literal)]
22

3-
use rustc_data_structures::stable_hasher::Hash64;
3+
use rustc_hashes::Hash64;
44
use rustc_span::def_id::{DefPathHash, StableCrateId};
55
use rustc_span::edition::Edition;
66
use rustc_span::{Symbol, create_session_globals_then};

0 commit comments

Comments
 (0)