diff options
| author | Jack Huey <jack.huey@umassmed.edu> | 2021-01-17 02:49:30 -0500 |
|---|---|---|
| committer | Jack Huey <jack.huey@umassmed.edu> | 2021-01-17 02:49:30 -0500 |
| commit | 674735b109cdfb815c02b9b60fb65e5495f7cfe1 (patch) | |
| tree | 9365131eb81a856441a944c9eef730ea28bd213e | |
| parent | f2ed9a3a8cc8d7972509143f2d8cb2a53e1a6b43 (diff) | |
| download | rust-674735b109cdfb815c02b9b60fb65e5495f7cfe1.tar.gz rust-674735b109cdfb815c02b9b60fb65e5495f7cfe1.zip | |
Impl EncodableWithShorthand for PredicateKind
| -rw-r--r-- | compiler/rustc_metadata/src/rmeta/encoder.rs | 6 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/codec.rs | 31 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/mod.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/query/on_disk_cache.rs | 5 |
4 files changed, 43 insertions, 1 deletions
diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 0b398c35db0..5e2674254b2 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -46,6 +46,7 @@ pub(super) struct EncodeContext<'a, 'tcx> { lazy_state: LazyState, type_shorthands: FxHashMap<Ty<'tcx>, usize>, + predicate_shorthands: FxHashMap<ty::PredicateKind<'tcx>, usize>, interpret_allocs: FxIndexSet<interpret::AllocId>, @@ -327,6 +328,10 @@ impl<'a, 'tcx> TyEncoder<'tcx> for EncodeContext<'a, 'tcx> { &mut self.type_shorthands } + fn predicate_shorthands(&mut self) -> &mut FxHashMap<ty::PredicateKind<'tcx>, usize> { + &mut self.predicate_shorthands + } + fn encode_alloc_id( &mut self, alloc_id: &rustc_middle::mir::interpret::AllocId, @@ -2146,6 +2151,7 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>) -> EncodedMetadata { tables: Default::default(), lazy_state: LazyState::NoNode, type_shorthands: Default::default(), + predicate_shorthands: Default::default(), source_file_cache, interpret_allocs: Default::default(), required_source_files, diff --git a/compiler/rustc_middle/src/ty/codec.rs b/compiler/rustc_middle/src/ty/codec.rs index 380fb8172a7..0aaba81819b 100644 --- a/compiler/rustc_middle/src/ty/codec.rs +++ b/compiler/rustc_middle/src/ty/codec.rs @@ -43,11 +43,21 @@ impl<'tcx, E: TyEncoder<'tcx>> EncodableWithShorthand<'tcx, E> for Ty<'tcx> { } } +impl<'tcx, E: TyEncoder<'tcx>> EncodableWithShorthand<'tcx, E> for ty::PredicateKind<'tcx> { + type Variant = ty::PredicateKind<'tcx>; + + #[inline] + fn variant(&self) -> &Self::Variant { + self + } +} + pub trait TyEncoder<'tcx>: Encoder { const CLEAR_CROSS_CRATE: bool; fn position(&self) -> usize; fn type_shorthands(&mut self) -> &mut FxHashMap<Ty<'tcx>, usize>; + fn predicate_shorthands(&mut self) -> &mut FxHashMap<ty::PredicateKind<'tcx>, usize>; fn encode_alloc_id(&mut self, alloc_id: &AllocId) -> Result<(), Self::Error>; } @@ -110,6 +120,12 @@ impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for Ty<'tcx> { } } +impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for ty::PredicateKind<'tcx> { + fn encode(&self, e: &mut E) -> Result<(), E::Error> { + encode_with_shorthand(e, self, TyEncoder::predicate_shorthands) + } +} + impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for ty::Predicate<'tcx> { fn encode(&self, e: &mut E) -> Result<(), E::Error> { self.kind().encode(e) @@ -210,6 +226,21 @@ impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for Ty<'tcx> { } } +impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::PredicateKind<'tcx> { + fn decode(decoder: &mut D) -> Result<ty::PredicateKind<'tcx>, D::Error> { + // Handle shorthands first, if we have an usize > 0x80. + if decoder.positioned_at_shorthand() { + let pos = decoder.read_usize()?; + assert!(pos >= SHORTHAND_OFFSET); + let shorthand = pos - SHORTHAND_OFFSET; + + decoder.with_position(shorthand, ty::PredicateKind::decode) + } else { + Ok(ty::PredicateKind::decode(decoder)?) + } + } +} + impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::Predicate<'tcx> { fn decode(decoder: &mut D) -> Result<ty::Predicate<'tcx>, D::Error> { let predicate_kind = Decodable::decode(decoder)?; diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 099c5aa84e5..88a2aac010c 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -1081,7 +1081,7 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for Predicate<'tcx> { } } -#[derive(Clone, Copy, PartialEq, Eq, Hash, TyEncodable, TyDecodable)] +#[derive(Clone, Copy, PartialEq, Eq, Hash)] #[derive(HashStable, TypeFoldable)] pub enum PredicateKind<'tcx> { /// Corresponds to `where Foo: Bar<A, B, C>`. `Foo` here would be diff --git a/compiler/rustc_middle/src/ty/query/on_disk_cache.rs b/compiler/rustc_middle/src/ty/query/on_disk_cache.rs index 914937134b4..cfe47004e01 100644 --- a/compiler/rustc_middle/src/ty/query/on_disk_cache.rs +++ b/compiler/rustc_middle/src/ty/query/on_disk_cache.rs @@ -293,6 +293,7 @@ impl<'sess> OnDiskCache<'sess> { tcx, encoder, type_shorthands: Default::default(), + predicate_shorthands: Default::default(), interpret_allocs: Default::default(), source_map: CachingSourceMapView::new(tcx.sess.source_map()), file_to_file_index, @@ -988,6 +989,7 @@ struct CacheEncoder<'a, 'tcx, E: OpaqueEncoder> { tcx: TyCtxt<'tcx>, encoder: &'a mut E, type_shorthands: FxHashMap<Ty<'tcx>, usize>, + predicate_shorthands: FxHashMap<ty::PredicateKind<'tcx>, usize>, interpret_allocs: FxIndexSet<interpret::AllocId>, source_map: CachingSourceMapView<'tcx>, file_to_file_index: FxHashMap<*const SourceFile, SourceFileIndex>, @@ -1101,6 +1103,9 @@ where fn type_shorthands(&mut self) -> &mut FxHashMap<Ty<'tcx>, usize> { &mut self.type_shorthands } + fn predicate_shorthands(&mut self) -> &mut FxHashMap<ty::PredicateKind<'tcx>, usize> { + &mut self.predicate_shorthands + } fn encode_alloc_id(&mut self, alloc_id: &interpret::AllocId) -> Result<(), Self::Error> { let (index, _) = self.interpret_allocs.insert_full(*alloc_id); |
