Skip to content

Commit 674735b

Browse files
committed
Impl EncodableWithShorthand for PredicateKind
1 parent f2ed9a3 commit 674735b

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ pub(super) struct EncodeContext<'a, 'tcx> {
4646

4747
lazy_state: LazyState,
4848
type_shorthands: FxHashMap<Ty<'tcx>, usize>,
49+
predicate_shorthands: FxHashMap<ty::PredicateKind<'tcx>, usize>,
4950

5051
interpret_allocs: FxIndexSet<interpret::AllocId>,
5152

@@ -327,6 +328,10 @@ impl<'a, 'tcx> TyEncoder<'tcx> for EncodeContext<'a, 'tcx> {
327328
&mut self.type_shorthands
328329
}
329330

331+
fn predicate_shorthands(&mut self) -> &mut FxHashMap<ty::PredicateKind<'tcx>, usize> {
332+
&mut self.predicate_shorthands
333+
}
334+
330335
fn encode_alloc_id(
331336
&mut self,
332337
alloc_id: &rustc_middle::mir::interpret::AllocId,
@@ -2146,6 +2151,7 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>) -> EncodedMetadata {
21462151
tables: Default::default(),
21472152
lazy_state: LazyState::NoNode,
21482153
type_shorthands: Default::default(),
2154+
predicate_shorthands: Default::default(),
21492155
source_file_cache,
21502156
interpret_allocs: Default::default(),
21512157
required_source_files,

compiler/rustc_middle/src/ty/codec.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,21 @@ impl<'tcx, E: TyEncoder<'tcx>> EncodableWithShorthand<'tcx, E> for Ty<'tcx> {
4343
}
4444
}
4545

46+
impl<'tcx, E: TyEncoder<'tcx>> EncodableWithShorthand<'tcx, E> for ty::PredicateKind<'tcx> {
47+
type Variant = ty::PredicateKind<'tcx>;
48+
49+
#[inline]
50+
fn variant(&self) -> &Self::Variant {
51+
self
52+
}
53+
}
54+
4655
pub trait TyEncoder<'tcx>: Encoder {
4756
const CLEAR_CROSS_CRATE: bool;
4857

4958
fn position(&self) -> usize;
5059
fn type_shorthands(&mut self) -> &mut FxHashMap<Ty<'tcx>, usize>;
60+
fn predicate_shorthands(&mut self) -> &mut FxHashMap<ty::PredicateKind<'tcx>, usize>;
5161
fn encode_alloc_id(&mut self, alloc_id: &AllocId) -> Result<(), Self::Error>;
5262
}
5363

@@ -110,6 +120,12 @@ impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for Ty<'tcx> {
110120
}
111121
}
112122

123+
impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for ty::PredicateKind<'tcx> {
124+
fn encode(&self, e: &mut E) -> Result<(), E::Error> {
125+
encode_with_shorthand(e, self, TyEncoder::predicate_shorthands)
126+
}
127+
}
128+
113129
impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for ty::Predicate<'tcx> {
114130
fn encode(&self, e: &mut E) -> Result<(), E::Error> {
115131
self.kind().encode(e)
@@ -210,6 +226,21 @@ impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for Ty<'tcx> {
210226
}
211227
}
212228

229+
impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::PredicateKind<'tcx> {
230+
fn decode(decoder: &mut D) -> Result<ty::PredicateKind<'tcx>, D::Error> {
231+
// Handle shorthands first, if we have an usize > 0x80.
232+
if decoder.positioned_at_shorthand() {
233+
let pos = decoder.read_usize()?;
234+
assert!(pos >= SHORTHAND_OFFSET);
235+
let shorthand = pos - SHORTHAND_OFFSET;
236+
237+
decoder.with_position(shorthand, ty::PredicateKind::decode)
238+
} else {
239+
Ok(ty::PredicateKind::decode(decoder)?)
240+
}
241+
}
242+
}
243+
213244
impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::Predicate<'tcx> {
214245
fn decode(decoder: &mut D) -> Result<ty::Predicate<'tcx>, D::Error> {
215246
let predicate_kind = Decodable::decode(decoder)?;

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,7 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for Predicate<'tcx> {
10811081
}
10821082
}
10831083

1084-
#[derive(Clone, Copy, PartialEq, Eq, Hash, TyEncodable, TyDecodable)]
1084+
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
10851085
#[derive(HashStable, TypeFoldable)]
10861086
pub enum PredicateKind<'tcx> {
10871087
/// Corresponds to `where Foo: Bar<A, B, C>`. `Foo` here would be

compiler/rustc_middle/src/ty/query/on_disk_cache.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ impl<'sess> OnDiskCache<'sess> {
293293
tcx,
294294
encoder,
295295
type_shorthands: Default::default(),
296+
predicate_shorthands: Default::default(),
296297
interpret_allocs: Default::default(),
297298
source_map: CachingSourceMapView::new(tcx.sess.source_map()),
298299
file_to_file_index,
@@ -988,6 +989,7 @@ struct CacheEncoder<'a, 'tcx, E: OpaqueEncoder> {
988989
tcx: TyCtxt<'tcx>,
989990
encoder: &'a mut E,
990991
type_shorthands: FxHashMap<Ty<'tcx>, usize>,
992+
predicate_shorthands: FxHashMap<ty::PredicateKind<'tcx>, usize>,
991993
interpret_allocs: FxIndexSet<interpret::AllocId>,
992994
source_map: CachingSourceMapView<'tcx>,
993995
file_to_file_index: FxHashMap<*const SourceFile, SourceFileIndex>,
@@ -1101,6 +1103,9 @@ where
11011103
fn type_shorthands(&mut self) -> &mut FxHashMap<Ty<'tcx>, usize> {
11021104
&mut self.type_shorthands
11031105
}
1106+
fn predicate_shorthands(&mut self) -> &mut FxHashMap<ty::PredicateKind<'tcx>, usize> {
1107+
&mut self.predicate_shorthands
1108+
}
11041109
fn encode_alloc_id(&mut self, alloc_id: &interpret::AllocId) -> Result<(), Self::Error> {
11051110
let (index, _) = self.interpret_allocs.insert_full(*alloc_id);
11061111

0 commit comments

Comments
 (0)