Skip to content

Commit

Permalink
Document and rename the new wrapper type
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Apr 7, 2022
1 parent 6ffd654 commit 2e0ef70
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 33 deletions.
27 changes: 17 additions & 10 deletions compiler/rustc_data_structures/src/intern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,34 +115,41 @@ pub trait InternedHashingContext {
fn with_def_path_and_no_spans(&mut self, f: impl FnOnce(&mut Self));
}

/// A helper type that you can wrap round your own type in order to automatically
/// cache the stable hash on creation and not recompute it whenever the stable hash
/// of the type is computed.
/// This is only done in incremental mode. You can also opt out of caching by using
/// StableHash::ZERO for the hash, in which case the hash gets computed each time.
/// This is useful if you have values that you intern but never (can?) use for stable
/// hashing.
#[derive(Copy, Clone)]
pub struct InTy<T> {
pub struct WithStableHash<T> {
pub internee: T,
pub stable_hash: Fingerprint,
}

impl<T: PartialEq> PartialEq for InTy<T> {
impl<T: PartialEq> PartialEq for WithStableHash<T> {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.internee.eq(&other.internee)
}
}

impl<T: Eq> Eq for InTy<T> {}
impl<T: Eq> Eq for WithStableHash<T> {}

impl<T: Ord> PartialOrd for InTy<T> {
fn partial_cmp(&self, other: &InTy<T>) -> Option<Ordering> {
impl<T: Ord> PartialOrd for WithStableHash<T> {
fn partial_cmp(&self, other: &WithStableHash<T>) -> Option<Ordering> {
Some(self.internee.cmp(&other.internee))
}
}

impl<T: Ord> Ord for InTy<T> {
fn cmp(&self, other: &InTy<T>) -> Ordering {
impl<T: Ord> Ord for WithStableHash<T> {
fn cmp(&self, other: &WithStableHash<T>) -> Ordering {
self.internee.cmp(&other.internee)
}
}

impl<T> Deref for InTy<T> {
impl<T> Deref for WithStableHash<T> {
type Target = T;

#[inline]
Expand All @@ -151,14 +158,14 @@ impl<T> Deref for InTy<T> {
}
}

impl<T: Hash> Hash for InTy<T> {
impl<T: Hash> Hash for WithStableHash<T> {
#[inline]
fn hash<H: Hasher>(&self, s: &mut H) {
self.internee.hash(s)
}
}

impl<T: HashStable<CTX>, CTX: InternedHashingContext> HashStable<CTX> for InTy<T> {
impl<T: HashStable<CTX>, CTX: InternedHashingContext> HashStable<CTX> for WithStableHash<T> {
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
if self.stable_hash == Fingerprint::ZERO || cfg!(debug_assertions) {
// No cached hash available. This can only mean that incremental is disabled.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ macro_rules! arena_types {
[] hir_id_set: rustc_hir::HirIdSet,

// Interned types
[] tys: rustc_data_structures::intern::InTy<rustc_middle::ty::TyS<'tcx>>,
[] tys: rustc_data_structures::intern::WithStableHash<rustc_middle::ty::TyS<'tcx>>,
[] predicates: rustc_middle::ty::PredicateS<'tcx>,
[] consts: rustc_middle::ty::ConstS<'tcx>,

Expand Down
18 changes: 10 additions & 8 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::ty::{
use rustc_ast as ast;
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::intern::{InTy, Interned};
use rustc_data_structures::intern::{Interned, WithStableHash};
use rustc_data_structures::memmap::Mmap;
use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_data_structures::sharded::{IntoPointer, ShardedHashMap};
Expand Down Expand Up @@ -105,7 +105,7 @@ pub struct CtxtInterners<'tcx> {

// Specifically use a speedy hash algorithm for these hash sets, since
// they're accessed quite often.
type_: InternedSet<'tcx, InTy<TyS<'tcx>>>,
type_: InternedSet<'tcx, WithStableHash<TyS<'tcx>>>,
substs: InternedSet<'tcx, InternalSubsts<'tcx>>,
canonical_var_infos: InternedSet<'tcx, List<CanonicalVarInfo<'tcx>>>,
region: InternedSet<'tcx, RegionKind>,
Expand Down Expand Up @@ -180,7 +180,9 @@ impl<'tcx> CtxtInterners<'tcx> {
outer_exclusive_binder: flags.outer_exclusive_binder,
};

InternedInSet(self.arena.alloc(InTy { internee: ty_struct, stable_hash }))
InternedInSet(
self.arena.alloc(WithStableHash { internee: ty_struct, stable_hash }),
)
})
.0,
))
Expand Down Expand Up @@ -2047,23 +2049,23 @@ impl<'tcx, T: 'tcx + ?Sized> IntoPointer for InternedInSet<'tcx, T> {
}

#[allow(rustc::usage_of_ty_tykind)]
impl<'tcx> Borrow<TyKind<'tcx>> for InternedInSet<'tcx, InTy<TyS<'tcx>>> {
impl<'tcx> Borrow<TyKind<'tcx>> for InternedInSet<'tcx, WithStableHash<TyS<'tcx>>> {
fn borrow<'a>(&'a self) -> &'a TyKind<'tcx> {
&self.0.kind
}
}

impl<'tcx> PartialEq for InternedInSet<'tcx, InTy<TyS<'tcx>>> {
fn eq(&self, other: &InternedInSet<'tcx, InTy<TyS<'tcx>>>) -> bool {
impl<'tcx> PartialEq for InternedInSet<'tcx, WithStableHash<TyS<'tcx>>> {
fn eq(&self, other: &InternedInSet<'tcx, WithStableHash<TyS<'tcx>>>) -> bool {
// The `Borrow` trait requires that `x.borrow() == y.borrow()` equals
// `x == y`.
self.0.kind == other.0.kind
}
}

impl<'tcx> Eq for InternedInSet<'tcx, InTy<TyS<'tcx>>> {}
impl<'tcx> Eq for InternedInSet<'tcx, WithStableHash<TyS<'tcx>>> {}

impl<'tcx> Hash for InternedInSet<'tcx, InTy<TyS<'tcx>>> {
impl<'tcx> Hash for InternedInSet<'tcx, WithStableHash<TyS<'tcx>>> {
fn hash<H: Hasher>(&self, s: &mut H) {
// The `Borrow` trait requires that `x.borrow().hash(s) == x.hash(s)`.
self.0.kind.hash(s)
Expand Down
15 changes: 11 additions & 4 deletions compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::ty::util::Discr;
use rustc_ast as ast;
use rustc_attr as attr;
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
use rustc_data_structures::intern::{InTy, Interned};
use rustc_data_structures::intern::{Interned, WithStableHash};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::tagged_ptr::CopyTaggedPtr;
use rustc_hir as hir;
Expand Down Expand Up @@ -439,15 +439,22 @@ crate struct TyS<'tcx> {
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
static_assert_size!(TyS<'_>, 40);

// We are actually storing a stable hash cache next to the type, so let's
// also check the full size
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
static_assert_size!(WithStableHash<TyS<'_>>, 56);

/// Use this rather than `TyS`, whenever possible.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, HashStable)]
#[rustc_diagnostic_item = "Ty"]
#[rustc_pass_by_value]
pub struct Ty<'tcx>(Interned<'tcx, InTy<TyS<'tcx>>>);
pub struct Ty<'tcx>(Interned<'tcx, WithStableHash<TyS<'tcx>>>);

// Statics only used for internal testing.
pub static BOOL_TY: Ty<'static> =
Ty(Interned::new_unchecked(&InTy { internee: BOOL_TYS, stable_hash: Fingerprint::ZERO }));
pub static BOOL_TY: Ty<'static> = Ty(Interned::new_unchecked(&WithStableHash {
internee: BOOL_TYS,
stable_hash: Fingerprint::ZERO,
}));
const BOOL_TYS: TyS<'static> = TyS {
kind: ty::Bool,
flags: TypeFlags::empty(),
Expand Down
13 changes: 8 additions & 5 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::ty::subst::{GenericArg, GenericArgKind, Subst};
use crate::ty::{self, ConstInt, DefIdTree, ParamConst, ScalarInt, Term, Ty, TyCtxt, TypeFoldable};
use rustc_apfloat::ieee::{Double, Single};
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::intern::{InTy, Interned};
use rustc_data_structures::intern::{Interned, WithStableHash};
use rustc_data_structures::sso::SsoHashSet;
use rustc_hir as hir;
use rustc_hir::def::{self, CtorKind, DefKind, Namespace};
Expand Down Expand Up @@ -1266,13 +1266,13 @@ pub trait PrettyPrinter<'tcx>:
ty::Ref(
_,
Ty(Interned(
InTy {
WithStableHash {
internee:
ty::TyS {
kind:
ty::Array(
Ty(Interned(
InTy {
WithStableHash {
internee:
ty::TyS { kind: ty::Uint(ty::UintTy::U8), .. },
..
Expand Down Expand Up @@ -1452,7 +1452,10 @@ pub trait PrettyPrinter<'tcx>:
ConstValue::Slice { data, start, end },
ty::Ref(
_,
Ty(Interned(InTy { internee: ty::TyS { kind: ty::Slice(t), .. }, .. }, _)),
Ty(Interned(
WithStableHash { internee: ty::TyS { kind: ty::Slice(t), .. }, .. },
_,
)),
_,
),
) if *t == u8_type => {
Expand All @@ -1467,7 +1470,7 @@ pub trait PrettyPrinter<'tcx>:
ConstValue::Slice { data, start, end },
ty::Ref(
_,
Ty(Interned(InTy { internee: ty::TyS { kind: ty::Str, .. }, .. }, _)),
Ty(Interned(WithStableHash { internee: ty::TyS { kind: ty::Str, .. }, .. }, _)),
_,
),
) => {
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_middle/src/ty/subst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeFolder, TypeVisitor}
use crate::ty::sty::{ClosureSubsts, GeneratorSubsts, InlineConstSubsts};
use crate::ty::{self, Lift, List, ParamConst, Ty, TyCtxt};

use rustc_data_structures::intern::{InTy, Interned};
use rustc_data_structures::intern::{Interned, WithStableHash};
use rustc_hir::def_id::DefId;
use rustc_macros::HashStable;
use rustc_serialize::{self, Decodable, Encodable};
Expand Down Expand Up @@ -85,7 +85,7 @@ impl<'tcx> GenericArgKind<'tcx> {
GenericArgKind::Type(ty) => {
// Ensure we can use the tag bits.
assert_eq!(mem::align_of_val(ty.0.0) & TAG_MASK, 0);
(TYPE_TAG, ty.0.0 as *const InTy<ty::TyS<'tcx>> as usize)
(TYPE_TAG, ty.0.0 as *const WithStableHash<ty::TyS<'tcx>> as usize)
}
GenericArgKind::Const(ct) => {
// Ensure we can use the tag bits.
Expand Down Expand Up @@ -154,7 +154,7 @@ impl<'tcx> GenericArg<'tcx> {
&*((ptr & !TAG_MASK) as *const ty::RegionKind),
))),
TYPE_TAG => GenericArgKind::Type(Ty(Interned::new_unchecked(
&*((ptr & !TAG_MASK) as *const InTy<ty::TyS<'tcx>>),
&*((ptr & !TAG_MASK) as *const WithStableHash<ty::TyS<'tcx>>),
))),
CONST_TAG => GenericArgKind::Const(ty::Const(Interned::new_unchecked(
&*((ptr & !TAG_MASK) as *const ty::ConstS<'tcx>),
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_middle/src/ty/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rustc_apfloat::Float as _;
use rustc_ast as ast;
use rustc_attr::{self as attr, SignedInt, UnsignedInt};
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::intern::{InTy, Interned};
use rustc_data_structures::intern::{Interned, WithStableHash};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_errors::ErrorGuaranteed;
use rustc_hir as hir;
Expand Down Expand Up @@ -427,7 +427,9 @@ impl<'tcx> TyCtxt<'tcx> {
!impl_generics.region_param(ebr, self).pure_wrt_drop
}
GenericArgKind::Type(Ty(Interned(
InTy { internee: ty::TyS { kind: ty::Param(ref pt), .. }, .. },
WithStableHash {
internee: ty::TyS { kind: ty::Param(ref pt), .. }, ..
},
_,
))) => !impl_generics.type_param(pt, self).pure_wrt_drop,
GenericArgKind::Const(Const(Interned(
Expand Down

0 comments on commit 2e0ef70

Please sign in to comment.