Skip to content

Commit 7f46d73

Browse files
authored
Rollup merge of rust-lang#101549 - eholk:type-ir-derive-hashstable_generic, r=jackh726
Use HashStable_Generic in rustc_type_ir A lot of the types in this crate implemented HashStable directly to avoid circular dependencies. One way around that is to use HashStable_Generic. We adopt that here to avoid a lot of boilerplate. This doesn't update all the types, because some would require `I: Interner + HashStable`. r? `@cjgillot`
2 parents 12b8100 + 578fc49 commit 7f46d73

File tree

5 files changed

+16
-43
lines changed

5 files changed

+16
-43
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3958,6 +3958,7 @@ dependencies = [
39583958
"rustc_session",
39593959
"rustc_span",
39603960
"rustc_target",
3961+
"rustc_type_ir",
39613962
"smallvec",
39623963
"thin-vec",
39633964
"tracing",

compiler/rustc_query_system/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ rustc_serialize = { path = "../rustc_serialize" }
2121
rustc_session = { path = "../rustc_session" }
2222
rustc_span = { path = "../rustc_span" }
2323
rustc_target = { path = "../rustc_target" }
24+
rustc_type_ir = { path = "../rustc_type_ir" }
2425
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
2526
thin-vec = "0.2.8"
2627
tracing = "0.1"

compiler/rustc_query_system/src/ich/impls_syntax.rs

+2
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,5 @@ impl<'tcx> HashStable<StableHashingContext<'tcx>> for rustc_feature::Features {
148148
});
149149
}
150150
}
151+
152+
impl<'ctx> rustc_type_ir::HashStableContext for StableHashingContext<'ctx> {}

compiler/rustc_type_ir/src/lib.rs

+9-40
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ pub mod sty;
2323
pub use codec::*;
2424
pub use sty::*;
2525

26+
/// Needed so we can use #[derive(HashStable_Generic)]
27+
pub trait HashStableContext {}
28+
2629
pub trait Interner {
2730
type AdtDef: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord;
2831
type SubstsRef: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord;
@@ -295,6 +298,7 @@ rustc_index::newtype_index! {
295298
/// is the outer fn.
296299
///
297300
/// [dbi]: https://en.wikipedia.org/wiki/De_Bruijn_index
301+
#[derive(HashStable_Generic)]
298302
pub struct DebruijnIndex {
299303
DEBUG_FORMAT = "DebruijnIndex({})",
300304
const INNERMOST = 0,
@@ -366,7 +370,7 @@ impl DebruijnIndex {
366370
}
367371

368372
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
369-
#[derive(Encodable, Decodable)]
373+
#[derive(Encodable, Decodable, HashStable_Generic)]
370374
pub enum IntTy {
371375
Isize,
372376
I8,
@@ -413,7 +417,7 @@ impl IntTy {
413417
}
414418

415419
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, Debug)]
416-
#[derive(Encodable, Decodable)]
420+
#[derive(Encodable, Decodable, HashStable_Generic)]
417421
pub enum UintTy {
418422
Usize,
419423
U8,
@@ -460,7 +464,7 @@ impl UintTy {
460464
}
461465

462466
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
463-
#[derive(Encodable, Decodable)]
467+
#[derive(Encodable, Decodable, HashStable_Generic)]
464468
pub enum FloatTy {
465469
F32,
466470
F64,
@@ -597,7 +601,7 @@ impl UnifyKey for FloatVid {
597601
}
598602
}
599603

600-
#[derive(Copy, Clone, PartialEq, Decodable, Encodable, Hash)]
604+
#[derive(Copy, Clone, PartialEq, Decodable, Encodable, Hash, HashStable_Generic)]
601605
#[rustc_pass_by_value]
602606
pub enum Variance {
603607
Covariant, // T<A> <: T<B> iff A <: B -- e.g., function return type
@@ -666,30 +670,6 @@ impl Variance {
666670
}
667671
}
668672

669-
impl<CTX> HashStable<CTX> for DebruijnIndex {
670-
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
671-
self.as_u32().hash_stable(ctx, hasher);
672-
}
673-
}
674-
675-
impl<CTX> HashStable<CTX> for IntTy {
676-
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
677-
discriminant(self).hash_stable(ctx, hasher);
678-
}
679-
}
680-
681-
impl<CTX> HashStable<CTX> for UintTy {
682-
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
683-
discriminant(self).hash_stable(ctx, hasher);
684-
}
685-
}
686-
687-
impl<CTX> HashStable<CTX> for FloatTy {
688-
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
689-
discriminant(self).hash_stable(ctx, hasher);
690-
}
691-
}
692-
693673
impl<CTX> HashStable<CTX> for InferTy {
694674
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
695675
use InferTy::*;
@@ -703,12 +683,6 @@ impl<CTX> HashStable<CTX> for InferTy {
703683
}
704684
}
705685

706-
impl<CTX> HashStable<CTX> for Variance {
707-
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
708-
discriminant(self).hash_stable(ctx, hasher);
709-
}
710-
}
711-
712686
impl fmt::Debug for IntVarValue {
713687
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
714688
match *self {
@@ -811,6 +785,7 @@ rustc_index::newtype_index! {
811785
/// declared, but a type name in a non-zero universe is a placeholder
812786
/// type -- an idealized representative of "types in general" that we
813787
/// use for checking generic functions.
788+
#[derive(HashStable_Generic)]
814789
pub struct UniverseIndex {
815790
DEBUG_FORMAT = "U{}",
816791
}
@@ -850,9 +825,3 @@ impl UniverseIndex {
850825
self.private < other.private
851826
}
852827
}
853-
854-
impl<CTX> HashStable<CTX> for UniverseIndex {
855-
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
856-
self.private.hash_stable(ctx, hasher);
857-
}
858-
}

compiler/rustc_type_ir/src/sty.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
use std::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd};
44
use std::{fmt, hash};
55

6-
use crate::DebruijnIndex;
76
use crate::FloatTy;
87
use crate::IntTy;
98
use crate::Interner;
109
use crate::TyDecoder;
1110
use crate::TyEncoder;
1211
use crate::UintTy;
1312
use crate::UniverseIndex;
13+
use crate::{DebruijnIndex, HashStableContext};
1414

1515
use self::RegionKind::*;
1616
use self::TyKind::*;
@@ -774,7 +774,7 @@ where
774774

775775
// This is not a derived impl because a derive would require `I: HashStable`
776776
#[allow(rustc::usage_of_ty_tykind)]
777-
impl<CTX, I: Interner> HashStable<CTX> for TyKind<I>
777+
impl<CTX: HashStableContext, I: Interner> HashStable<CTX> for TyKind<I>
778778
where
779779
I::AdtDef: HashStable<CTX>,
780780
I::DefId: HashStable<CTX>,
@@ -1286,7 +1286,7 @@ where
12861286
}
12871287

12881288
// This is not a derived impl because a derive would require `I: HashStable`
1289-
impl<CTX, I: Interner> HashStable<CTX> for RegionKind<I>
1289+
impl<CTX: HashStableContext, I: Interner> HashStable<CTX> for RegionKind<I>
12901290
where
12911291
I::EarlyBoundRegion: HashStable<CTX>,
12921292
I::BoundRegion: HashStable<CTX>,

0 commit comments

Comments
 (0)