Skip to content

Commit 912c7d3

Browse files
committed
Use HashStable derive in more places
1 parent d8b2222 commit 912c7d3

File tree

9 files changed

+31
-94
lines changed

9 files changed

+31
-94
lines changed

compiler/rustc_data_structures/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ use std::fmt;
4747
#[cfg(not(bootstrap))]
4848
pub use std::{assert_matches, debug_assert_matches};
4949

50+
// This allows derive macros to reference this crate
51+
extern crate self as rustc_data_structures;
52+
5053
pub use atomic_ref::AtomicRef;
5154
pub use ena::{snapshot_vec, undo_log, unify};
5255
// Re-export `hashbrown::hash_table`, because it's part of our API

compiler/rustc_data_structures/src/sorted_map/index_map.rs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
use std::hash::{Hash, Hasher};
44

55
use rustc_index::{Idx, IndexVec};
6-
7-
use crate::stable_hasher::{HashStable, StableHasher};
6+
use rustc_macros::HashStable_NoContext;
87

98
/// An indexed multi-map that preserves insertion order while permitting both *O*(log *n*) lookup of
109
/// an item by key and *O*(1) lookup by index.
@@ -24,11 +23,13 @@ use crate::stable_hasher::{HashStable, StableHasher};
2423
/// in-place.
2524
///
2625
/// [`SortedMap`]: super::SortedMap
27-
#[derive(Clone, Debug)]
26+
#[derive(Clone, Debug, HashStable_NoContext)]
2827
pub struct SortedIndexMultiMap<I: Idx, K, V> {
2928
/// The elements of the map in insertion order.
3029
items: IndexVec<I, (K, V)>,
3130

31+
// We can ignore this field because it is not observable from the outside.
32+
#[stable_hasher(ignore)]
3233
/// Indices of the items in the set, sorted by the item's key.
3334
idx_sorted_by_item_key: Vec<I>,
3435
}
@@ -126,22 +127,6 @@ where
126127
}
127128
}
128129

129-
impl<I: Idx, K, V, C> HashStable<C> for SortedIndexMultiMap<I, K, V>
130-
where
131-
K: HashStable<C>,
132-
V: HashStable<C>,
133-
{
134-
fn hash_stable(&self, ctx: &mut C, hasher: &mut StableHasher) {
135-
let SortedIndexMultiMap {
136-
items,
137-
// We can ignore this field because it is not observable from the outside.
138-
idx_sorted_by_item_key: _,
139-
} = self;
140-
141-
items.hash_stable(ctx, hasher)
142-
}
143-
}
144-
145130
impl<I: Idx, K: Ord, V> FromIterator<(K, V)> for SortedIndexMultiMap<I, K, V> {
146131
fn from_iter<J>(iter: J) -> Self
147132
where

compiler/rustc_data_structures/src/svh.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,21 @@
77
88
use std::fmt;
99

10-
use rustc_macros::{Decodable_NoContext, Encodable_NoContext};
10+
use rustc_macros::{Decodable_NoContext, Encodable_NoContext, HashStable_NoContext};
1111

1212
use crate::fingerprint::Fingerprint;
13-
use crate::stable_hasher;
1413

15-
#[derive(Copy, Clone, PartialEq, Eq, Debug, Encodable_NoContext, Decodable_NoContext, Hash)]
14+
#[derive(
15+
Copy,
16+
Clone,
17+
PartialEq,
18+
Eq,
19+
Debug,
20+
Encodable_NoContext,
21+
Decodable_NoContext,
22+
Hash,
23+
HashStable_NoContext
24+
)]
1625
pub struct Svh {
1726
hash: Fingerprint,
1827
}
@@ -39,11 +48,3 @@ impl fmt::Display for Svh {
3948
f.pad(&self.to_hex())
4049
}
4150
}
42-
43-
impl<T> stable_hasher::HashStable<T> for Svh {
44-
#[inline]
45-
fn hash_stable(&self, ctx: &mut T, hasher: &mut stable_hasher::StableHasher) {
46-
let Svh { hash } = *self;
47-
hash.hash_stable(ctx, hasher);
48-
}
49-
}
Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
use rustc_data_structures::fx::FxIndexMap;
2-
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
2+
use rustc_macros::HashStable_Generic;
33
use rustc_span::Symbol;
44
use rustc_span::def_id::DefIdMap;
55

66
use crate::def_id::DefId;
77

8-
#[derive(Debug, Default)]
8+
#[derive(Debug, Default, HashStable_Generic)]
99
pub struct DiagnosticItems {
10+
#[stable_hasher(ignore)]
1011
pub id_to_name: DefIdMap<Symbol>,
1112
pub name_to_id: FxIndexMap<Symbol, DefId>,
1213
}
13-
14-
impl<CTX: crate::HashStableContext> HashStable<CTX> for DiagnosticItems {
15-
#[inline]
16-
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
17-
self.name_to_id.hash_stable(ctx, hasher);
18-
}
19-
}

compiler/rustc_macros/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ decl_derive!(
6464
hash_stable::hash_stable_generic_derive
6565
);
6666
decl_derive!(
67-
[HashStable_NoContext] =>
67+
[HashStable_NoContext, attributes(stable_hasher)] =>
6868
/// `HashStable` implementation that has no `HashStableContext` bound and
6969
/// which adds `where` bounds for `HashStable` based off of fields and not
7070
/// generics. This is suitable for use in crates like `rustc_type_ir`.

compiler/rustc_middle/src/dep_graph/dep_node.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ use rustc_data_structures::fingerprint::{Fingerprint, PackedFingerprint};
5454
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, StableOrd, ToStableHashKey};
5555
use rustc_hir::def_id::DefId;
5656
use rustc_hir::definitions::DefPathHash;
57-
use rustc_macros::{Decodable, Encodable};
57+
use rustc_macros::{Decodable, Encodable, HashStable};
5858
use rustc_span::Symbol;
5959

6060
use super::{KeyFingerprintStyle, SerializedDepNodeIndex};
@@ -290,7 +290,9 @@ pub struct DepKindVTable<'tcx> {
290290
/// some independent path or string that persists between runs without
291291
/// the need to be mapped or unmapped. (This ensures we can serialize
292292
/// them even in the absence of a tcx.)
293-
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
293+
#[derive(
294+
Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable, HashStable
295+
)]
294296
pub struct WorkProductId {
295297
hash: Fingerprint,
296298
}
@@ -302,13 +304,6 @@ impl WorkProductId {
302304
WorkProductId { hash: hasher.finish() }
303305
}
304306
}
305-
306-
impl<HCX> HashStable<HCX> for WorkProductId {
307-
#[inline]
308-
fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {
309-
self.hash.hash_stable(hcx, hasher)
310-
}
311-
}
312307
impl<HCX> ToStableHashKey<HCX> for WorkProductId {
313308
type KeyType = Fingerprint;
314309
#[inline]

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ pub struct VarBindingForm<'tcx> {
891891
pub introductions: Vec<VarBindingIntroduction>,
892892
}
893893

894-
#[derive(Clone, Debug, TyEncodable, TyDecodable)]
894+
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
895895
pub enum BindingForm<'tcx> {
896896
/// This is a binding for a non-`self` binding, or a `self` that has an explicit type.
897897
Var(VarBindingForm<'tcx>),
@@ -909,25 +909,6 @@ pub struct VarBindingIntroduction {
909909
pub is_shorthand: bool,
910910
}
911911

912-
mod binding_form_impl {
913-
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
914-
915-
use crate::ich::StableHashingContext;
916-
917-
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for super::BindingForm<'tcx> {
918-
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
919-
use super::BindingForm::*;
920-
std::mem::discriminant(self).hash_stable(hcx, hasher);
921-
922-
match self {
923-
Var(binding) => binding.hash_stable(hcx, hasher),
924-
ImplicitSelf(kind) => kind.hash_stable(hcx, hasher),
925-
RefForGuard(local) => local.hash_stable(hcx, hasher),
926-
}
927-
}
928-
}
929-
}
930-
931912
/// `BlockTailInfo` is attached to the `LocalDecl` for temporaries
932913
/// created during evaluation of expressions in a block tail
933914
/// expression; that is, a block like `{ STMT_1; STMT_2; EXPR }`.

compiler/rustc_span/src/lib.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2653,7 +2653,7 @@ impl_pos! {
26532653
pub struct BytePos(pub u32);
26542654

26552655
/// A byte offset relative to file beginning.
2656-
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
2656+
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, HashStable_Generic)]
26572657
pub struct RelativeBytePos(pub u32);
26582658

26592659
/// A character offset.
@@ -2677,12 +2677,6 @@ impl<D: Decoder> Decodable<D> for BytePos {
26772677
}
26782678
}
26792679

2680-
impl<H: HashStableContext> HashStable<H> for RelativeBytePos {
2681-
fn hash_stable(&self, hcx: &mut H, hasher: &mut StableHasher) {
2682-
self.0.hash_stable(hcx, hasher);
2683-
}
2684-
}
2685-
26862680
impl<S: Encoder> Encodable<S> for RelativeBytePos {
26872681
fn encode(&self, s: &mut S) {
26882682
s.emit_u32(self.0);

compiler/rustc_target/src/target_features.rs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! Note that these are similar to but not always identical to LLVM's feature names,
33
//! and Rust adds some features that do not correspond to LLVM features at all.
44
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
5-
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
5+
use rustc_macros::HashStable_Generic;
66
use rustc_span::{Symbol, sym};
77

88
use crate::spec::{Abi, Arch, FloatAbi, RustcAbi, Target};
@@ -12,7 +12,7 @@ use crate::spec::{Abi, Arch, FloatAbi, RustcAbi, Target};
1212
pub const RUSTC_SPECIFIC_FEATURES: &[&str] = &["crt-static"];
1313

1414
/// Stability information for target features.
15-
#[derive(Debug, Copy, Clone)]
15+
#[derive(Debug, Copy, Clone, HashStable_Generic)]
1616
pub enum Stability {
1717
/// This target feature is stable, it can be used in `#[target_feature]` and
1818
/// `#[cfg(target_feature)]`.
@@ -32,22 +32,6 @@ pub enum Stability {
3232
}
3333
use Stability::*;
3434

35-
impl<CTX> HashStable<CTX> for Stability {
36-
#[inline]
37-
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
38-
std::mem::discriminant(self).hash_stable(hcx, hasher);
39-
match self {
40-
Stability::Stable => {}
41-
Stability::Unstable(nightly_feature) => {
42-
nightly_feature.hash_stable(hcx, hasher);
43-
}
44-
Stability::Forbidden { reason } => {
45-
reason.hash_stable(hcx, hasher);
46-
}
47-
}
48-
}
49-
}
50-
5135
impl Stability {
5236
/// Returns whether the feature can be used in `cfg(target_feature)` ever.
5337
/// (It might still be nightly-only even if this returns `true`, so make sure to also check

0 commit comments

Comments
 (0)