Skip to content

Commit 2e0ef70

Browse files
committed
Document and rename the new wrapper type
1 parent 6ffd654 commit 2e0ef70

File tree

7 files changed

+54
-33
lines changed

7 files changed

+54
-33
lines changed

compiler/rustc_data_structures/src/intern.rs

+17-10
Original file line numberDiff line numberDiff line change
@@ -115,34 +115,41 @@ pub trait InternedHashingContext {
115115
fn with_def_path_and_no_spans(&mut self, f: impl FnOnce(&mut Self));
116116
}
117117

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

124-
impl<T: PartialEq> PartialEq for InTy<T> {
131+
impl<T: PartialEq> PartialEq for WithStableHash<T> {
125132
#[inline]
126133
fn eq(&self, other: &Self) -> bool {
127134
self.internee.eq(&other.internee)
128135
}
129136
}
130137

131-
impl<T: Eq> Eq for InTy<T> {}
138+
impl<T: Eq> Eq for WithStableHash<T> {}
132139

133-
impl<T: Ord> PartialOrd for InTy<T> {
134-
fn partial_cmp(&self, other: &InTy<T>) -> Option<Ordering> {
140+
impl<T: Ord> PartialOrd for WithStableHash<T> {
141+
fn partial_cmp(&self, other: &WithStableHash<T>) -> Option<Ordering> {
135142
Some(self.internee.cmp(&other.internee))
136143
}
137144
}
138145

139-
impl<T: Ord> Ord for InTy<T> {
140-
fn cmp(&self, other: &InTy<T>) -> Ordering {
146+
impl<T: Ord> Ord for WithStableHash<T> {
147+
fn cmp(&self, other: &WithStableHash<T>) -> Ordering {
141148
self.internee.cmp(&other.internee)
142149
}
143150
}
144151

145-
impl<T> Deref for InTy<T> {
152+
impl<T> Deref for WithStableHash<T> {
146153
type Target = T;
147154

148155
#[inline]
@@ -151,14 +158,14 @@ impl<T> Deref for InTy<T> {
151158
}
152159
}
153160

154-
impl<T: Hash> Hash for InTy<T> {
161+
impl<T: Hash> Hash for WithStableHash<T> {
155162
#[inline]
156163
fn hash<H: Hasher>(&self, s: &mut H) {
157164
self.internee.hash(s)
158165
}
159166
}
160167

161-
impl<T: HashStable<CTX>, CTX: InternedHashingContext> HashStable<CTX> for InTy<T> {
168+
impl<T: HashStable<CTX>, CTX: InternedHashingContext> HashStable<CTX> for WithStableHash<T> {
162169
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
163170
if self.stable_hash == Fingerprint::ZERO || cfg!(debug_assertions) {
164171
// No cached hash available. This can only mean that incremental is disabled.

compiler/rustc_middle/src/arena.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ macro_rules! arena_types {
8787
[] hir_id_set: rustc_hir::HirIdSet,
8888

8989
// Interned types
90-
[] tys: rustc_data_structures::intern::InTy<rustc_middle::ty::TyS<'tcx>>,
90+
[] tys: rustc_data_structures::intern::WithStableHash<rustc_middle::ty::TyS<'tcx>>,
9191
[] predicates: rustc_middle::ty::PredicateS<'tcx>,
9292
[] consts: rustc_middle::ty::ConstS<'tcx>,
9393

compiler/rustc_middle/src/ty/context.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::ty::{
2626
use rustc_ast as ast;
2727
use rustc_data_structures::fingerprint::Fingerprint;
2828
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
29-
use rustc_data_structures::intern::{InTy, Interned};
29+
use rustc_data_structures::intern::{Interned, WithStableHash};
3030
use rustc_data_structures::memmap::Mmap;
3131
use rustc_data_structures::profiling::SelfProfilerRef;
3232
use rustc_data_structures::sharded::{IntoPointer, ShardedHashMap};
@@ -105,7 +105,7 @@ pub struct CtxtInterners<'tcx> {
105105

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

183-
InternedInSet(self.arena.alloc(InTy { internee: ty_struct, stable_hash }))
183+
InternedInSet(
184+
self.arena.alloc(WithStableHash { internee: ty_struct, stable_hash }),
185+
)
184186
})
185187
.0,
186188
))
@@ -2047,23 +2049,23 @@ impl<'tcx, T: 'tcx + ?Sized> IntoPointer for InternedInSet<'tcx, T> {
20472049
}
20482050

20492051
#[allow(rustc::usage_of_ty_tykind)]
2050-
impl<'tcx> Borrow<TyKind<'tcx>> for InternedInSet<'tcx, InTy<TyS<'tcx>>> {
2052+
impl<'tcx> Borrow<TyKind<'tcx>> for InternedInSet<'tcx, WithStableHash<TyS<'tcx>>> {
20512053
fn borrow<'a>(&'a self) -> &'a TyKind<'tcx> {
20522054
&self.0.kind
20532055
}
20542056
}
20552057

2056-
impl<'tcx> PartialEq for InternedInSet<'tcx, InTy<TyS<'tcx>>> {
2057-
fn eq(&self, other: &InternedInSet<'tcx, InTy<TyS<'tcx>>>) -> bool {
2058+
impl<'tcx> PartialEq for InternedInSet<'tcx, WithStableHash<TyS<'tcx>>> {
2059+
fn eq(&self, other: &InternedInSet<'tcx, WithStableHash<TyS<'tcx>>>) -> bool {
20582060
// The `Borrow` trait requires that `x.borrow() == y.borrow()` equals
20592061
// `x == y`.
20602062
self.0.kind == other.0.kind
20612063
}
20622064
}
20632065

2064-
impl<'tcx> Eq for InternedInSet<'tcx, InTy<TyS<'tcx>>> {}
2066+
impl<'tcx> Eq for InternedInSet<'tcx, WithStableHash<TyS<'tcx>>> {}
20652067

2066-
impl<'tcx> Hash for InternedInSet<'tcx, InTy<TyS<'tcx>>> {
2068+
impl<'tcx> Hash for InternedInSet<'tcx, WithStableHash<TyS<'tcx>>> {
20672069
fn hash<H: Hasher>(&self, s: &mut H) {
20682070
// The `Borrow` trait requires that `x.borrow().hash(s) == x.hash(s)`.
20692071
self.0.kind.hash(s)

compiler/rustc_middle/src/ty/mod.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use crate::ty::util::Discr;
3131
use rustc_ast as ast;
3232
use rustc_attr as attr;
3333
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
34-
use rustc_data_structures::intern::{InTy, Interned};
34+
use rustc_data_structures::intern::{Interned, WithStableHash};
3535
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
3636
use rustc_data_structures::tagged_ptr::CopyTaggedPtr;
3737
use rustc_hir as hir;
@@ -439,15 +439,22 @@ crate struct TyS<'tcx> {
439439
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
440440
static_assert_size!(TyS<'_>, 40);
441441

442+
// We are actually storing a stable hash cache next to the type, so let's
443+
// also check the full size
444+
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
445+
static_assert_size!(WithStableHash<TyS<'_>>, 56);
446+
442447
/// Use this rather than `TyS`, whenever possible.
443448
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, HashStable)]
444449
#[rustc_diagnostic_item = "Ty"]
445450
#[rustc_pass_by_value]
446-
pub struct Ty<'tcx>(Interned<'tcx, InTy<TyS<'tcx>>>);
451+
pub struct Ty<'tcx>(Interned<'tcx, WithStableHash<TyS<'tcx>>>);
447452

448453
// Statics only used for internal testing.
449-
pub static BOOL_TY: Ty<'static> =
450-
Ty(Interned::new_unchecked(&InTy { internee: BOOL_TYS, stable_hash: Fingerprint::ZERO }));
454+
pub static BOOL_TY: Ty<'static> = Ty(Interned::new_unchecked(&WithStableHash {
455+
internee: BOOL_TYS,
456+
stable_hash: Fingerprint::ZERO,
457+
}));
451458
const BOOL_TYS: TyS<'static> = TyS {
452459
kind: ty::Bool,
453460
flags: TypeFlags::empty(),

compiler/rustc_middle/src/ty/print/pretty.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::ty::subst::{GenericArg, GenericArgKind, Subst};
33
use crate::ty::{self, ConstInt, DefIdTree, ParamConst, ScalarInt, Term, Ty, TyCtxt, TypeFoldable};
44
use rustc_apfloat::ieee::{Double, Single};
55
use rustc_data_structures::fx::FxHashMap;
6-
use rustc_data_structures::intern::{InTy, Interned};
6+
use rustc_data_structures::intern::{Interned, WithStableHash};
77
use rustc_data_structures::sso::SsoHashSet;
88
use rustc_hir as hir;
99
use rustc_hir::def::{self, CtorKind, DefKind, Namespace};
@@ -1266,13 +1266,13 @@ pub trait PrettyPrinter<'tcx>:
12661266
ty::Ref(
12671267
_,
12681268
Ty(Interned(
1269-
InTy {
1269+
WithStableHash {
12701270
internee:
12711271
ty::TyS {
12721272
kind:
12731273
ty::Array(
12741274
Ty(Interned(
1275-
InTy {
1275+
WithStableHash {
12761276
internee:
12771277
ty::TyS { kind: ty::Uint(ty::UintTy::U8), .. },
12781278
..
@@ -1452,7 +1452,10 @@ pub trait PrettyPrinter<'tcx>:
14521452
ConstValue::Slice { data, start, end },
14531453
ty::Ref(
14541454
_,
1455-
Ty(Interned(InTy { internee: ty::TyS { kind: ty::Slice(t), .. }, .. }, _)),
1455+
Ty(Interned(
1456+
WithStableHash { internee: ty::TyS { kind: ty::Slice(t), .. }, .. },
1457+
_,
1458+
)),
14561459
_,
14571460
),
14581461
) if *t == u8_type => {
@@ -1467,7 +1470,7 @@ pub trait PrettyPrinter<'tcx>:
14671470
ConstValue::Slice { data, start, end },
14681471
ty::Ref(
14691472
_,
1470-
Ty(Interned(InTy { internee: ty::TyS { kind: ty::Str, .. }, .. }, _)),
1473+
Ty(Interned(WithStableHash { internee: ty::TyS { kind: ty::Str, .. }, .. }, _)),
14711474
_,
14721475
),
14731476
) => {

compiler/rustc_middle/src/ty/subst.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeFolder, TypeVisitor}
66
use crate::ty::sty::{ClosureSubsts, GeneratorSubsts, InlineConstSubsts};
77
use crate::ty::{self, Lift, List, ParamConst, Ty, TyCtxt};
88

9-
use rustc_data_structures::intern::{InTy, Interned};
9+
use rustc_data_structures::intern::{Interned, WithStableHash};
1010
use rustc_hir::def_id::DefId;
1111
use rustc_macros::HashStable;
1212
use rustc_serialize::{self, Decodable, Encodable};
@@ -85,7 +85,7 @@ impl<'tcx> GenericArgKind<'tcx> {
8585
GenericArgKind::Type(ty) => {
8686
// Ensure we can use the tag bits.
8787
assert_eq!(mem::align_of_val(ty.0.0) & TAG_MASK, 0);
88-
(TYPE_TAG, ty.0.0 as *const InTy<ty::TyS<'tcx>> as usize)
88+
(TYPE_TAG, ty.0.0 as *const WithStableHash<ty::TyS<'tcx>> as usize)
8989
}
9090
GenericArgKind::Const(ct) => {
9191
// Ensure we can use the tag bits.
@@ -154,7 +154,7 @@ impl<'tcx> GenericArg<'tcx> {
154154
&*((ptr & !TAG_MASK) as *const ty::RegionKind),
155155
))),
156156
TYPE_TAG => GenericArgKind::Type(Ty(Interned::new_unchecked(
157-
&*((ptr & !TAG_MASK) as *const InTy<ty::TyS<'tcx>>),
157+
&*((ptr & !TAG_MASK) as *const WithStableHash<ty::TyS<'tcx>>),
158158
))),
159159
CONST_TAG => GenericArgKind::Const(ty::Const(Interned::new_unchecked(
160160
&*((ptr & !TAG_MASK) as *const ty::ConstS<'tcx>),

compiler/rustc_middle/src/ty/util.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_apfloat::Float as _;
1313
use rustc_ast as ast;
1414
use rustc_attr::{self as attr, SignedInt, UnsignedInt};
1515
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
16-
use rustc_data_structures::intern::{InTy, Interned};
16+
use rustc_data_structures::intern::{Interned, WithStableHash};
1717
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1818
use rustc_errors::ErrorGuaranteed;
1919
use rustc_hir as hir;
@@ -427,7 +427,9 @@ impl<'tcx> TyCtxt<'tcx> {
427427
!impl_generics.region_param(ebr, self).pure_wrt_drop
428428
}
429429
GenericArgKind::Type(Ty(Interned(
430-
InTy { internee: ty::TyS { kind: ty::Param(ref pt), .. }, .. },
430+
WithStableHash {
431+
internee: ty::TyS { kind: ty::Param(ref pt), .. }, ..
432+
},
431433
_,
432434
))) => !impl_generics.type_param(pt, self).pure_wrt_drop,
433435
GenericArgKind::Const(Const(Interned(

0 commit comments

Comments
 (0)