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

Use HashStable_Generic in rustc_type_ir #101549

Merged
merged 1 commit into from
Sep 8, 2022
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
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3958,6 +3958,7 @@ dependencies = [
"rustc_session",
"rustc_span",
"rustc_target",
"rustc_type_ir",
"smallvec",
"thin-vec",
"tracing",
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_query_system/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ rustc_serialize = { path = "../rustc_serialize" }
rustc_session = { path = "../rustc_session" }
rustc_span = { path = "../rustc_span" }
rustc_target = { path = "../rustc_target" }
rustc_type_ir = { path = "../rustc_type_ir" }
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this bad? It adds a new dependency edge that might make the critical path longer (since query_system AFAIK is a very slow crate).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wonder if we could move the HashStableContext trait to a higher level crate (like whatever one defines HashStable_Generic) and do a use HashStableContext at the root? It seems like if this is going to be a common pattern where we need these marker traits for anyone who is going to use HashStable_Generic, it'd be nice to use the same one if possible.

Copy link
Member

Choose a reason for hiding this comment

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

FWIW, I don't think it's so straightforward whether this will regression compile times, since many of the other dependencies of rustc_query_system are not dependencies of rustc_type_ir.

smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
thin-vec = "0.2.8"
tracing = "0.1"
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_query_system/src/ich/impls_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,5 @@ impl<'tcx> HashStable<StableHashingContext<'tcx>> for rustc_feature::Features {
});
}
}

impl<'ctx> rustc_type_ir::HashStableContext for StableHashingContext<'ctx> {}
49 changes: 9 additions & 40 deletions compiler/rustc_type_ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ pub mod sty;
pub use codec::*;
pub use sty::*;

/// Needed so we can use #[derive(HashStable_Generic)]
pub trait HashStableContext {}

pub trait Interner {
type AdtDef: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord;
type SubstsRef: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord;
Expand Down Expand Up @@ -295,6 +298,7 @@ rustc_index::newtype_index! {
/// is the outer fn.
///
/// [dbi]: https://en.wikipedia.org/wiki/De_Bruijn_index
#[derive(HashStable_Generic)]
pub struct DebruijnIndex {
DEBUG_FORMAT = "DebruijnIndex({})",
const INNERMOST = 0,
Expand Down Expand Up @@ -366,7 +370,7 @@ impl DebruijnIndex {
}

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[derive(Encodable, Decodable)]
#[derive(Encodable, Decodable, HashStable_Generic)]
pub enum IntTy {
Isize,
I8,
Expand Down Expand Up @@ -413,7 +417,7 @@ impl IntTy {
}

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, Debug)]
#[derive(Encodable, Decodable)]
#[derive(Encodable, Decodable, HashStable_Generic)]
pub enum UintTy {
Usize,
U8,
Expand Down Expand Up @@ -460,7 +464,7 @@ impl UintTy {
}

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[derive(Encodable, Decodable)]
#[derive(Encodable, Decodable, HashStable_Generic)]
pub enum FloatTy {
F32,
F64,
Expand Down Expand Up @@ -597,7 +601,7 @@ impl UnifyKey for FloatVid {
}
}

#[derive(Copy, Clone, PartialEq, Decodable, Encodable, Hash)]
#[derive(Copy, Clone, PartialEq, Decodable, Encodable, Hash, HashStable_Generic)]
#[rustc_pass_by_value]
pub enum Variance {
Covariant, // T<A> <: T<B> iff A <: B -- e.g., function return type
Expand Down Expand Up @@ -666,30 +670,6 @@ impl Variance {
}
}

impl<CTX> HashStable<CTX> for DebruijnIndex {
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
self.as_u32().hash_stable(ctx, hasher);
}
}

impl<CTX> HashStable<CTX> for IntTy {
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
discriminant(self).hash_stable(ctx, hasher);
}
}

impl<CTX> HashStable<CTX> for UintTy {
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
discriminant(self).hash_stable(ctx, hasher);
}
}

impl<CTX> HashStable<CTX> for FloatTy {
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
discriminant(self).hash_stable(ctx, hasher);
}
}

impl<CTX> HashStable<CTX> for InferTy {
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
use InferTy::*;
Expand All @@ -703,12 +683,6 @@ impl<CTX> HashStable<CTX> for InferTy {
}
}

impl<CTX> HashStable<CTX> for Variance {
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
discriminant(self).hash_stable(ctx, hasher);
}
}

impl fmt::Debug for IntVarValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Expand Down Expand Up @@ -811,6 +785,7 @@ rustc_index::newtype_index! {
/// declared, but a type name in a non-zero universe is a placeholder
/// type -- an idealized representative of "types in general" that we
/// use for checking generic functions.
#[derive(HashStable_Generic)]
pub struct UniverseIndex {
DEBUG_FORMAT = "U{}",
}
Expand Down Expand Up @@ -850,9 +825,3 @@ impl UniverseIndex {
self.private < other.private
}
}

impl<CTX> HashStable<CTX> for UniverseIndex {
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
self.private.hash_stable(ctx, hasher);
}
}
6 changes: 3 additions & 3 deletions compiler/rustc_type_ir/src/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
use std::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd};
use std::{fmt, hash};

use crate::DebruijnIndex;
use crate::FloatTy;
use crate::IntTy;
use crate::Interner;
use crate::TyDecoder;
use crate::TyEncoder;
use crate::UintTy;
use crate::UniverseIndex;
use crate::{DebruijnIndex, HashStableContext};

use self::RegionKind::*;
use self::TyKind::*;
Expand Down Expand Up @@ -774,7 +774,7 @@ where

// This is not a derived impl because a derive would require `I: HashStable`
#[allow(rustc::usage_of_ty_tykind)]
impl<CTX, I: Interner> HashStable<CTX> for TyKind<I>
impl<CTX: HashStableContext, I: Interner> HashStable<CTX> for TyKind<I>
where
I::AdtDef: HashStable<CTX>,
I::DefId: HashStable<CTX>,
Expand Down Expand Up @@ -1286,7 +1286,7 @@ where
}

// This is not a derived impl because a derive would require `I: HashStable`
impl<CTX, I: Interner> HashStable<CTX> for RegionKind<I>
impl<CTX: HashStableContext, I: Interner> HashStable<CTX> for RegionKind<I>
where
I::EarlyBoundRegion: HashStable<CTX>,
I::BoundRegion: HashStable<CTX>,
Expand Down