Skip to content

Commit 87a04f5

Browse files
committed
move WithCachedTypeInfo to rustc_type_ir
1 parent 3d31e5c commit 87a04f5

File tree

7 files changed

+101
-88
lines changed

7 files changed

+101
-88
lines changed

compiler/rustc_data_structures/src/intern.rs

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ use std::hash::{Hash, Hasher};
44
use std::ops::Deref;
55
use std::ptr;
66

7-
use crate::fingerprint::Fingerprint;
8-
97
mod private {
108
#[derive(Clone, Copy, Debug)]
119
pub struct PrivateZst;
@@ -110,86 +108,5 @@ where
110108
}
111109
}
112110

113-
/// A helper type that you can wrap round your own type in order to automatically
114-
/// cache the stable hash on creation and not recompute it whenever the stable hash
115-
/// of the type is computed.
116-
/// This is only done in incremental mode. You can also opt out of caching by using
117-
/// StableHash::ZERO for the hash, in which case the hash gets computed each time.
118-
/// This is useful if you have values that you intern but never (can?) use for stable
119-
/// hashing.
120-
#[derive(Copy, Clone)]
121-
pub struct WithCachedTypeInfo<T> {
122-
pub internee: T,
123-
pub stable_hash: Fingerprint,
124-
}
125-
126-
impl<T: PartialEq> PartialEq for WithCachedTypeInfo<T> {
127-
#[inline]
128-
fn eq(&self, other: &Self) -> bool {
129-
self.internee.eq(&other.internee)
130-
}
131-
}
132-
133-
impl<T: Eq> Eq for WithCachedTypeInfo<T> {}
134-
135-
impl<T: Ord> PartialOrd for WithCachedTypeInfo<T> {
136-
fn partial_cmp(&self, other: &WithCachedTypeInfo<T>) -> Option<Ordering> {
137-
Some(self.internee.cmp(&other.internee))
138-
}
139-
}
140-
141-
impl<T: Ord> Ord for WithCachedTypeInfo<T> {
142-
fn cmp(&self, other: &WithCachedTypeInfo<T>) -> Ordering {
143-
self.internee.cmp(&other.internee)
144-
}
145-
}
146-
147-
impl<T> Deref for WithCachedTypeInfo<T> {
148-
type Target = T;
149-
150-
#[inline]
151-
fn deref(&self) -> &T {
152-
&self.internee
153-
}
154-
}
155-
156-
impl<T: Hash> Hash for WithCachedTypeInfo<T> {
157-
#[inline]
158-
fn hash<H: Hasher>(&self, s: &mut H) {
159-
if self.stable_hash != Fingerprint::ZERO {
160-
self.stable_hash.hash(s)
161-
} else {
162-
self.internee.hash(s)
163-
}
164-
}
165-
}
166-
167-
impl<T: HashStable<CTX>, CTX> HashStable<CTX> for WithCachedTypeInfo<T> {
168-
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
169-
if self.stable_hash == Fingerprint::ZERO || cfg!(debug_assertions) {
170-
// No cached hash available. This can only mean that incremental is disabled.
171-
// We don't cache stable hashes in non-incremental mode, because they are used
172-
// so rarely that the performance actually suffers.
173-
174-
// We need to build the hash as if we cached it and then hash that hash, as
175-
// otherwise the hashes will differ between cached and non-cached mode.
176-
let stable_hash: Fingerprint = {
177-
let mut hasher = StableHasher::new();
178-
self.internee.hash_stable(hcx, &mut hasher);
179-
hasher.finish()
180-
};
181-
if cfg!(debug_assertions) && self.stable_hash != Fingerprint::ZERO {
182-
assert_eq!(
183-
stable_hash, self.stable_hash,
184-
"cached stable hash does not match freshly computed stable hash"
185-
);
186-
}
187-
stable_hash.hash_stable(hcx, hasher);
188-
} else {
189-
self.stable_hash.hash_stable(hcx, hasher);
190-
}
191-
}
192-
}
193-
194111
#[cfg(test)]
195112
mod tests;

compiler/rustc_middle/src/arena.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ macro_rules! arena_types {
8888
[] hir_id_set: rustc_hir::HirIdSet,
8989

9090
// Interned types
91-
[] tys: rustc_data_structures::intern::WithCachedTypeInfo<rustc_middle::ty::TyS<'tcx>>,
92-
[] predicates: rustc_data_structures::intern::WithCachedTypeInfo<rustc_middle::ty::PredicateS<'tcx>>,
91+
[] tys: rustc_type_ir::WithCachedTypeInfo<rustc_middle::ty::TyS<'tcx>>,
92+
[] predicates: rustc_type_ir::WithCachedTypeInfo<rustc_middle::ty::PredicateS<'tcx>>,
9393
[] consts: rustc_middle::ty::ConstS<'tcx>,
9494

9595
// Note that this deliberately duplicates items in the `rustc_hir::arena`,

compiler/rustc_middle/src/ty/context.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::ty::{GenericArg, GenericArgKind, InternalSubsts, SubstsRef, UserSubst
2727
use rustc_ast as ast;
2828
use rustc_data_structures::fingerprint::Fingerprint;
2929
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
30-
use rustc_data_structures::intern::{Interned, WithCachedTypeInfo};
30+
use rustc_data_structures::intern::Interned;
3131
use rustc_data_structures::memmap::Mmap;
3232
use rustc_data_structures::profiling::SelfProfilerRef;
3333
use rustc_data_structures::sharded::{IntoPointer, ShardedHashMap};
@@ -67,6 +67,7 @@ use rustc_span::{Span, DUMMY_SP};
6767
use rustc_target::abi::{Layout, LayoutS, TargetDataLayout, VariantIdx};
6868
use rustc_target::spec::abi;
6969
use rustc_type_ir::sty::TyKind::*;
70+
use rustc_type_ir::WithCachedTypeInfo;
7071
use rustc_type_ir::{DynKind, InternAs, InternIteratorElement, Interner, TypeFlags};
7172

7273
use std::any::Any;

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use rustc_ast::node_id::NodeMap;
3232
use rustc_attr as attr;
3333
use rustc_data_structures::fingerprint::Fingerprint;
3434
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
35-
use rustc_data_structures::intern::{Interned, WithCachedTypeInfo};
35+
use rustc_data_structures::intern::Interned;
3636
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
3737
use rustc_data_structures::tagged_ptr::CopyTaggedPtr;
3838
use rustc_hir as hir;
@@ -50,6 +50,7 @@ use rustc_span::symbol::{kw, sym, Ident, Symbol};
5050
use rustc_span::{ExpnId, Span};
5151
use rustc_target::abi::{Align, Integer, IntegerType, VariantIdx};
5252
pub use rustc_target::abi::{ReprFlags, ReprOptions};
53+
use rustc_type_ir::WithCachedTypeInfo;
5354
pub use subst::*;
5455
pub use vtable::*;
5556

compiler/rustc_middle/src/ty/subst.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ use crate::ty::sty::{ClosureSubsts, GeneratorSubsts, InlineConstSubsts};
66
use crate::ty::visit::{TypeVisitable, TypeVisitor};
77
use crate::ty::{self, Lift, List, ParamConst, Ty, TyCtxt};
88

9-
use rustc_data_structures::intern::{Interned, WithCachedTypeInfo};
9+
use rustc_data_structures::intern::Interned;
1010
use rustc_hir::def_id::DefId;
1111
use rustc_macros::HashStable;
1212
use rustc_serialize::{self, Decodable, Encodable};
13+
use rustc_type_ir::WithCachedTypeInfo;
1314
use smallvec::SmallVec;
1415

1516
use core::intrinsics;

compiler/rustc_type_ir/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ use std::mem::discriminant;
1919

2020
pub mod codec;
2121
pub mod sty;
22+
pub mod ty_info;
2223

2324
pub use codec::*;
2425
pub use sty::*;
26+
pub use ty_info::*;
2527

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

compiler/rustc_type_ir/src/ty_info.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
use std::{
2+
cmp::Ordering,
3+
hash::{Hash, Hasher},
4+
ops::Deref,
5+
};
6+
7+
use rustc_data_structures::{
8+
fingerprint::Fingerprint,
9+
stable_hasher::{HashStable, StableHasher},
10+
};
11+
12+
/// A helper type that you can wrap round your own type in order to automatically
13+
/// cache the stable hash on creation and not recompute it whenever the stable hash
14+
/// of the type is computed.
15+
/// This is only done in incremental mode. You can also opt out of caching by using
16+
/// StableHash::ZERO for the hash, in which case the hash gets computed each time.
17+
/// This is useful if you have values that you intern but never (can?) use for stable
18+
/// hashing.
19+
#[derive(Copy, Clone)]
20+
pub struct WithCachedTypeInfo<T> {
21+
pub internee: T,
22+
pub stable_hash: Fingerprint,
23+
}
24+
25+
impl<T: PartialEq> PartialEq for WithCachedTypeInfo<T> {
26+
#[inline]
27+
fn eq(&self, other: &Self) -> bool {
28+
self.internee.eq(&other.internee)
29+
}
30+
}
31+
32+
impl<T: Eq> Eq for WithCachedTypeInfo<T> {}
33+
34+
impl<T: Ord> PartialOrd for WithCachedTypeInfo<T> {
35+
fn partial_cmp(&self, other: &WithCachedTypeInfo<T>) -> Option<Ordering> {
36+
Some(self.internee.cmp(&other.internee))
37+
}
38+
}
39+
40+
impl<T: Ord> Ord for WithCachedTypeInfo<T> {
41+
fn cmp(&self, other: &WithCachedTypeInfo<T>) -> Ordering {
42+
self.internee.cmp(&other.internee)
43+
}
44+
}
45+
46+
impl<T> Deref for WithCachedTypeInfo<T> {
47+
type Target = T;
48+
49+
#[inline]
50+
fn deref(&self) -> &T {
51+
&self.internee
52+
}
53+
}
54+
55+
impl<T: Hash> Hash for WithCachedTypeInfo<T> {
56+
#[inline]
57+
fn hash<H: Hasher>(&self, s: &mut H) {
58+
if self.stable_hash != Fingerprint::ZERO {
59+
self.stable_hash.hash(s)
60+
} else {
61+
self.internee.hash(s)
62+
}
63+
}
64+
}
65+
66+
impl<T: HashStable<CTX>, CTX> HashStable<CTX> for WithCachedTypeInfo<T> {
67+
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
68+
if self.stable_hash == Fingerprint::ZERO || cfg!(debug_assertions) {
69+
// No cached hash available. This can only mean that incremental is disabled.
70+
// We don't cache stable hashes in non-incremental mode, because they are used
71+
// so rarely that the performance actually suffers.
72+
73+
// We need to build the hash as if we cached it and then hash that hash, as
74+
// otherwise the hashes will differ between cached and non-cached mode.
75+
let stable_hash: Fingerprint = {
76+
let mut hasher = StableHasher::new();
77+
self.internee.hash_stable(hcx, &mut hasher);
78+
hasher.finish()
79+
};
80+
if cfg!(debug_assertions) && self.stable_hash != Fingerprint::ZERO {
81+
assert_eq!(
82+
stable_hash, self.stable_hash,
83+
"cached stable hash does not match freshly computed stable hash"
84+
);
85+
}
86+
stable_hash.hash_stable(hcx, hasher);
87+
} else {
88+
self.stable_hash.hash_stable(hcx, hasher);
89+
}
90+
}
91+
}

0 commit comments

Comments
 (0)