1 //! This module contains some shared code for encoding and decoding various
2 //! things from the `ty` module, and in particular implements support for
3 //! "shorthands" which allow to have pointers back into the already encoded
4 //! stream instead of re-encoding the same thing twice.
6 //! The functionality in here is shared between persisting to crate metadata and
7 //! persisting to incr. comp. caches.
9 use crate::arena::ArenaAllocatable;
10 use crate::infer::canonical::{CanonicalVarInfo, CanonicalVarInfos};
13 interpret::{AllocId, Allocation},
16 use crate::ty::subst::SubstsRef;
17 use crate::ty::{self, Ty, TyCtxt};
18 use rustc_data_structures::fx::FxHashMap;
19 use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
23 use std::marker::DiscriminantKind;
25 /// The shorthand encoding uses an enum's variant index `usize`
26 /// and is offset by this value so it never matches a real variant.
27 /// This offset is also chosen so that the first byte is never < 0x80.
28 pub const SHORTHAND_OFFSET: usize = 0x80;
30 pub trait EncodableWithShorthand<'tcx, E: TyEncoder<'tcx>>: Copy + Eq + Hash {
31 type Variant: Encodable<E>;
32 fn variant(&self) -> &Self::Variant;
35 #[allow(rustc::usage_of_ty_tykind)]
36 impl<'tcx, E: TyEncoder<'tcx>> EncodableWithShorthand<'tcx, E> for Ty<'tcx> {
37 type Variant = ty::TyKind<'tcx>;
40 fn variant(&self) -> &Self::Variant {
45 impl<'tcx, E: TyEncoder<'tcx>> EncodableWithShorthand<'tcx, E> for ty::PredicateKind<'tcx> {
46 type Variant = ty::PredicateKind<'tcx>;
49 fn variant(&self) -> &Self::Variant {
54 pub trait TyEncoder<'tcx>: Encoder {
55 const CLEAR_CROSS_CRATE: bool;
57 fn position(&self) -> usize;
58 fn type_shorthands(&mut self) -> &mut FxHashMap<Ty<'tcx>, usize>;
59 fn predicate_shorthands(&mut self) -> &mut FxHashMap<ty::PredicateKind<'tcx>, usize>;
60 fn encode_alloc_id(&mut self, alloc_id: &AllocId) -> Result<(), Self::Error>;
63 /// Trait for decoding to a reference.
65 /// This is a separate trait from `Decodable` so that we can implement it for
66 /// upstream types, such as `FxHashSet`.
68 /// The `TyDecodable` derive macro will use this trait for fields that are
69 /// references (and don't use a type alias to hide that).
71 /// `Decodable` can still be implemented in cases where `Decodable` is required
73 pub trait RefDecodable<'tcx, D: TyDecoder<'tcx>> {
74 fn decode(d: &mut D) -> &'tcx Self;
77 /// Encode the given value or a previously cached shorthand.
78 pub fn encode_with_shorthand<'tcx, E, T, M>(
82 ) -> Result<(), E::Error>
85 M: for<'b> Fn(&'b mut E) -> &'b mut FxHashMap<T, usize>,
86 T: EncodableWithShorthand<'tcx, E>,
87 // The discriminant and shorthand must have the same size.
88 T::Variant: DiscriminantKind<Discriminant = isize>,
90 let existing_shorthand = cache(encoder).get(value).copied();
91 if let Some(shorthand) = existing_shorthand {
92 return encoder.emit_usize(shorthand);
95 let variant = value.variant();
97 let start = encoder.position();
98 variant.encode(encoder)?;
99 let len = encoder.position() - start;
101 // The shorthand encoding uses the same usize as the
102 // discriminant, with an offset so they can't conflict.
103 let discriminant = intrinsics::discriminant_value(variant);
104 assert!(SHORTHAND_OFFSET > discriminant as usize);
106 let shorthand = start + SHORTHAND_OFFSET;
108 // Get the number of bits that leb128 could fit
109 // in the same space as the fully encoded type.
110 let leb128_bits = len * 7;
112 // Check that the shorthand is a not longer than the
113 // full encoding itself, i.e., it's an obvious win.
114 if leb128_bits >= 64 || (shorthand as u64) < (1 << leb128_bits) {
115 cache(encoder).insert(*value, shorthand);
121 impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for Ty<'tcx> {
122 fn encode(&self, e: &mut E) -> Result<(), E::Error> {
123 encode_with_shorthand(e, self, TyEncoder::type_shorthands)
127 impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for ty::Binder<'tcx, ty::PredicateKind<'tcx>> {
128 fn encode(&self, e: &mut E) -> Result<(), E::Error> {
129 self.bound_vars().encode(e)?;
130 encode_with_shorthand(e, &self.skip_binder(), TyEncoder::predicate_shorthands)
134 impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for ty::Predicate<'tcx> {
135 fn encode(&self, e: &mut E) -> Result<(), E::Error> {
136 self.kind().encode(e)
140 impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for AllocId {
141 fn encode(&self, e: &mut E) -> Result<(), E::Error> {
142 e.encode_alloc_id(self)
146 macro_rules! encodable_via_deref {
148 $(impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for $t {
149 fn encode(&self, e: &mut E) -> Result<(), E::Error> {
156 encodable_via_deref! {
157 &'tcx ty::TypeckResults<'tcx>,
159 &'tcx mir::Body<'tcx>,
160 &'tcx mir::UnsafetyCheckResult,
161 &'tcx mir::BorrowCheckResult<'tcx>,
162 &'tcx mir::coverage::CodeRegion,
166 pub trait TyDecoder<'tcx>: Decoder {
167 const CLEAR_CROSS_CRATE: bool;
169 fn tcx(&self) -> TyCtxt<'tcx>;
171 fn peek_byte(&self) -> u8;
173 fn position(&self) -> usize;
175 fn cached_ty_for_shorthand<F>(&mut self, shorthand: usize, or_insert_with: F) -> Ty<'tcx>
177 F: FnOnce(&mut Self) -> Ty<'tcx>;
179 fn with_position<F, R>(&mut self, pos: usize, f: F) -> R
181 F: FnOnce(&mut Self) -> R;
183 fn positioned_at_shorthand(&self) -> bool {
184 (self.peek_byte() & (SHORTHAND_OFFSET as u8)) != 0
187 fn decode_alloc_id(&mut self) -> AllocId;
191 fn decode_arena_allocable<'tcx, D, T: ArenaAllocatable<'tcx> + Decodable<D>>(
197 decoder.tcx().arena.alloc(Decodable::decode(decoder))
201 fn decode_arena_allocable_slice<'tcx, D, T: ArenaAllocatable<'tcx> + Decodable<D>>(
207 decoder.tcx().arena.alloc_from_iter(<Vec<T> as Decodable<D>>::decode(decoder))
210 impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for Ty<'tcx> {
211 #[allow(rustc::usage_of_ty_tykind)]
212 fn decode(decoder: &mut D) -> Ty<'tcx> {
213 // Handle shorthands first, if we have a usize > 0x80.
214 if decoder.positioned_at_shorthand() {
215 let pos = decoder.read_usize();
216 assert!(pos >= SHORTHAND_OFFSET);
217 let shorthand = pos - SHORTHAND_OFFSET;
219 decoder.cached_ty_for_shorthand(shorthand, |decoder| {
220 decoder.with_position(shorthand, Ty::decode)
223 let tcx = decoder.tcx();
224 tcx.mk_ty(ty::TyKind::decode(decoder))
229 impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::Binder<'tcx, ty::PredicateKind<'tcx>> {
230 fn decode(decoder: &mut D) -> ty::Binder<'tcx, ty::PredicateKind<'tcx>> {
231 let bound_vars = Decodable::decode(decoder);
232 // Handle shorthands first, if we have a usize > 0x80.
233 ty::Binder::bind_with_vars(
234 if decoder.positioned_at_shorthand() {
235 let pos = decoder.read_usize();
236 assert!(pos >= SHORTHAND_OFFSET);
237 let shorthand = pos - SHORTHAND_OFFSET;
239 decoder.with_position(shorthand, ty::PredicateKind::decode)
241 ty::PredicateKind::decode(decoder)
248 impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::Predicate<'tcx> {
249 fn decode(decoder: &mut D) -> ty::Predicate<'tcx> {
250 let predicate_kind = Decodable::decode(decoder);
251 decoder.tcx().mk_predicate(predicate_kind)
255 impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for SubstsRef<'tcx> {
256 fn decode(decoder: &mut D) -> Self {
257 let len = decoder.read_usize();
258 let tcx = decoder.tcx();
260 (0..len).map::<ty::subst::GenericArg<'tcx>, _>(|_| Decodable::decode(decoder)),
265 impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for mir::Place<'tcx> {
266 fn decode(decoder: &mut D) -> Self {
267 let local: mir::Local = Decodable::decode(decoder);
268 let len = decoder.read_usize();
269 let projection = decoder.tcx().mk_place_elems(
270 (0..len).map::<mir::PlaceElem<'tcx>, _>(|_| Decodable::decode(decoder)),
272 mir::Place { local, projection }
276 impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::Region<'tcx> {
277 fn decode(decoder: &mut D) -> Self {
278 decoder.tcx().mk_region(Decodable::decode(decoder))
282 impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for CanonicalVarInfos<'tcx> {
283 fn decode(decoder: &mut D) -> Self {
284 let len = decoder.read_usize();
285 let interned: Vec<CanonicalVarInfo<'tcx>> =
286 (0..len).map(|_| Decodable::decode(decoder)).collect();
287 decoder.tcx().intern_canonical_var_infos(interned.as_slice())
291 impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for AllocId {
292 fn decode(decoder: &mut D) -> Self {
293 decoder.decode_alloc_id()
297 impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::SymbolName<'tcx> {
298 fn decode(decoder: &mut D) -> Self {
299 ty::SymbolName::new(decoder.tcx(), &decoder.read_str())
303 macro_rules! impl_decodable_via_ref {
305 $(impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for $t {
306 fn decode(decoder: &mut D) -> Self {
307 RefDecodable::decode(decoder)
313 impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::List<Ty<'tcx>> {
314 fn decode(decoder: &mut D) -> &'tcx Self {
315 let len = decoder.read_usize();
316 decoder.tcx().mk_type_list((0..len).map::<Ty<'tcx>, _>(|_| Decodable::decode(decoder)))
320 impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D>
321 for ty::List<ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>>
323 fn decode(decoder: &mut D) -> &'tcx Self {
324 let len = decoder.read_usize();
325 decoder.tcx().mk_poly_existential_predicates(
326 (0..len).map::<ty::Binder<'tcx, _>, _>(|_| Decodable::decode(decoder)),
331 impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::Const<'tcx> {
332 fn decode(decoder: &mut D) -> &'tcx Self {
333 decoder.tcx().mk_const(Decodable::decode(decoder))
337 impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [ty::ValTree<'tcx>] {
338 fn decode(decoder: &mut D) -> &'tcx Self {
339 decoder.tcx().arena.alloc_from_iter(
340 (0..decoder.read_usize()).map(|_| Decodable::decode(decoder)).collect::<Vec<_>>(),
345 impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for Allocation {
346 fn decode(decoder: &mut D) -> &'tcx Self {
347 decoder.tcx().intern_const_alloc(Decodable::decode(decoder))
351 impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [(ty::Predicate<'tcx>, Span)] {
352 fn decode(decoder: &mut D) -> &'tcx Self {
353 decoder.tcx().arena.alloc_from_iter(
354 (0..decoder.read_usize()).map(|_| Decodable::decode(decoder)).collect::<Vec<_>>(),
359 impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [thir::abstract_const::Node<'tcx>] {
360 fn decode(decoder: &mut D) -> &'tcx Self {
361 decoder.tcx().arena.alloc_from_iter(
362 (0..decoder.read_usize()).map(|_| Decodable::decode(decoder)).collect::<Vec<_>>(),
367 impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [thir::abstract_const::NodeId] {
368 fn decode(decoder: &mut D) -> &'tcx Self {
369 decoder.tcx().arena.alloc_from_iter(
370 (0..decoder.read_usize()).map(|_| Decodable::decode(decoder)).collect::<Vec<_>>(),
375 impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::List<ty::BoundVariableKind> {
376 fn decode(decoder: &mut D) -> &'tcx Self {
377 let len = decoder.read_usize();
378 decoder.tcx().mk_bound_variable_kinds(
379 (0..len).map::<ty::BoundVariableKind, _>(|_| Decodable::decode(decoder)),
384 impl_decodable_via_ref! {
385 &'tcx ty::TypeckResults<'tcx>,
386 &'tcx ty::List<Ty<'tcx>>,
387 &'tcx ty::List<ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>>,
389 &'tcx mir::Body<'tcx>,
390 &'tcx mir::UnsafetyCheckResult,
391 &'tcx mir::BorrowCheckResult<'tcx>,
392 &'tcx mir::coverage::CodeRegion,
393 &'tcx ty::List<ty::BoundVariableKind>,
398 macro_rules! __impl_decoder_methods {
399 ($($name:ident -> $ty:ty;)*) => {
402 fn $name(&mut self) -> $ty {
409 macro_rules! impl_arena_allocatable_decoder {
411 ([decode $(, $attrs:ident)*]
412 [$name:ident: $ty:ty]) => {
413 impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for $ty {
415 fn decode(decoder: &mut D) -> &'tcx Self {
416 decode_arena_allocable(decoder)
420 impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [$ty] {
422 fn decode(decoder: &mut D) -> &'tcx Self {
423 decode_arena_allocable_slice(decoder)
427 ([$ignore:ident $(, $attrs:ident)*]$args:tt) => {
428 impl_arena_allocatable_decoder!([$($attrs),*]$args);
432 macro_rules! impl_arena_allocatable_decoders {
433 ([$($a:tt $name:ident: $ty:ty,)*]) => {
435 impl_arena_allocatable_decoder!($a [$name: $ty]);
440 rustc_hir::arena_types!(impl_arena_allocatable_decoders);
441 arena_types!(impl_arena_allocatable_decoders);
444 macro_rules! implement_ty_decoder {
445 ($DecoderName:ident <$($typaram:tt),*>) => {
446 mod __ty_decoder_impl {
447 use std::borrow::Cow;
448 use rustc_serialize::Decoder;
450 use super::$DecoderName;
452 impl<$($typaram ),*> Decoder for $DecoderName<$($typaram),*> {
453 $crate::__impl_decoder_methods! {
474 read_str -> Cow<'_, str>;
478 fn read_raw_bytes_into(&mut self, bytes: &mut [u8]) {
479 self.opaque.read_raw_bytes_into(bytes)
486 macro_rules! impl_binder_encode_decode {
487 ($($t:ty),+ $(,)?) => {
489 impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for ty::Binder<'tcx, $t> {
490 fn encode(&self, e: &mut E) -> Result<(), E::Error> {
491 self.bound_vars().encode(e)?;
492 self.as_ref().skip_binder().encode(e)
495 impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::Binder<'tcx, $t> {
496 fn decode(decoder: &mut D) -> Self {
497 let bound_vars = Decodable::decode(decoder);
498 ty::Binder::bind_with_vars(Decodable::decode(decoder), bound_vars)
505 impl_binder_encode_decode! {
506 &'tcx ty::List<Ty<'tcx>>,
508 ty::ExistentialPredicate<'tcx>,
510 Vec<ty::GeneratorInteriorTypeCause<'tcx>>,