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.
5 //!
6 //! The functionality in here is shared between persisting to crate metadata and
7 //! persisting to incr. comp. caches.
8 
9 use crate::arena::ArenaAllocatable;
10 use crate::infer::canonical::{CanonicalVarInfo, CanonicalVarInfos};
11 use crate::mir::{
12     self,
13     interpret::{AllocId, Allocation},
14 };
15 use crate::thir;
16 use crate::ty::subst::SubstsRef;
17 use crate::ty::{self, List, Ty, TyCtxt};
18 use rustc_data_structures::fx::FxHashMap;
19 use rustc_hir::def_id::DefId;
20 use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
21 use rustc_span::Span;
22 use std::hash::Hash;
23 use std::intrinsics;
24 use std::marker::DiscriminantKind;
25 
26 /// The shorthand encoding uses an enum's variant index `usize`
27 /// and is offset by this value so it never matches a real variant.
28 /// This offset is also chosen so that the first byte is never < 0x80.
29 pub const SHORTHAND_OFFSET: usize = 0x80;
30 
31 pub trait EncodableWithShorthand<'tcx, E: TyEncoder<'tcx>>: Copy + Eq + Hash {
32     type Variant: Encodable<E>;
variant(&self) -> &Self::Variant33     fn variant(&self) -> &Self::Variant;
34 }
35 
36 #[allow(rustc::usage_of_ty_tykind)]
37 impl<'tcx, E: TyEncoder<'tcx>> EncodableWithShorthand<'tcx, E> for Ty<'tcx> {
38     type Variant = ty::TyKind<'tcx>;
39 
40     #[inline]
variant(&self) -> &Self::Variant41     fn variant(&self) -> &Self::Variant {
42         self.kind()
43     }
44 }
45 
46 impl<'tcx, E: TyEncoder<'tcx>> EncodableWithShorthand<'tcx, E> for ty::PredicateKind<'tcx> {
47     type Variant = ty::PredicateKind<'tcx>;
48 
49     #[inline]
variant(&self) -> &Self::Variant50     fn variant(&self) -> &Self::Variant {
51         self
52     }
53 }
54 
55 pub trait TyEncoder<'tcx>: Encoder {
56     const CLEAR_CROSS_CRATE: bool;
57 
position(&self) -> usize58     fn position(&self) -> usize;
type_shorthands(&mut self) -> &mut FxHashMap<Ty<'tcx>, usize>59     fn type_shorthands(&mut self) -> &mut FxHashMap<Ty<'tcx>, usize>;
predicate_shorthands(&mut self) -> &mut FxHashMap<ty::PredicateKind<'tcx>, usize>60     fn predicate_shorthands(&mut self) -> &mut FxHashMap<ty::PredicateKind<'tcx>, usize>;
encode_alloc_id(&mut self, alloc_id: &AllocId) -> Result<(), Self::Error>61     fn encode_alloc_id(&mut self, alloc_id: &AllocId) -> Result<(), Self::Error>;
62 }
63 
64 /// Trait for decoding to a reference.
65 ///
66 /// This is a separate trait from `Decodable` so that we can implement it for
67 /// upstream types, such as `FxHashSet`.
68 ///
69 /// The `TyDecodable` derive macro will use this trait for fields that are
70 /// references (and don't use a type alias to hide that).
71 ///
72 /// `Decodable` can still be implemented in cases where `Decodable` is required
73 /// by a trait bound.
74 pub trait RefDecodable<'tcx, D: TyDecoder<'tcx>> {
decode(d: &mut D) -> Result<&'tcx Self, D::Error>75     fn decode(d: &mut D) -> Result<&'tcx Self, D::Error>;
76 }
77 
78 /// Encode the given value or a previously cached shorthand.
encode_with_shorthand<E, T, M>(encoder: &mut E, value: &T, cache: M) -> Result<(), E::Error> where E: TyEncoder<'tcx>, M: for<'b> Fn(&'b mut E) -> &'b mut FxHashMap<T, usize>, T: EncodableWithShorthand<'tcx, E>, T::Variant: DiscriminantKind<Discriminant = isize>,79 pub fn encode_with_shorthand<E, T, M>(encoder: &mut E, value: &T, cache: M) -> Result<(), E::Error>
80 where
81     E: TyEncoder<'tcx>,
82     M: for<'b> Fn(&'b mut E) -> &'b mut FxHashMap<T, usize>,
83     T: EncodableWithShorthand<'tcx, E>,
84     // The discriminant and shorthand must have the same size.
85     T::Variant: DiscriminantKind<Discriminant = isize>,
86 {
87     let existing_shorthand = cache(encoder).get(value).copied();
88     if let Some(shorthand) = existing_shorthand {
89         return encoder.emit_usize(shorthand);
90     }
91 
92     let variant = value.variant();
93 
94     let start = encoder.position();
95     variant.encode(encoder)?;
96     let len = encoder.position() - start;
97 
98     // The shorthand encoding uses the same usize as the
99     // discriminant, with an offset so they can't conflict.
100     let discriminant = intrinsics::discriminant_value(variant);
101     assert!(SHORTHAND_OFFSET > discriminant as usize);
102 
103     let shorthand = start + SHORTHAND_OFFSET;
104 
105     // Get the number of bits that leb128 could fit
106     // in the same space as the fully encoded type.
107     let leb128_bits = len * 7;
108 
109     // Check that the shorthand is a not longer than the
110     // full encoding itself, i.e., it's an obvious win.
111     if leb128_bits >= 64 || (shorthand as u64) < (1 << leb128_bits) {
112         cache(encoder).insert(*value, shorthand);
113     }
114 
115     Ok(())
116 }
117 
118 impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for Ty<'tcx> {
encode(&self, e: &mut E) -> Result<(), E::Error>119     fn encode(&self, e: &mut E) -> Result<(), E::Error> {
120         encode_with_shorthand(e, self, TyEncoder::type_shorthands)
121     }
122 }
123 
124 impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for ty::Binder<'tcx, ty::PredicateKind<'tcx>> {
encode(&self, e: &mut E) -> Result<(), E::Error>125     fn encode(&self, e: &mut E) -> Result<(), E::Error> {
126         self.bound_vars().encode(e)?;
127         encode_with_shorthand(e, &self.skip_binder(), TyEncoder::predicate_shorthands)
128     }
129 }
130 
131 impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for ty::Predicate<'tcx> {
encode(&self, e: &mut E) -> Result<(), E::Error>132     fn encode(&self, e: &mut E) -> Result<(), E::Error> {
133         self.kind().encode(e)
134     }
135 }
136 
137 impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for AllocId {
encode(&self, e: &mut E) -> Result<(), E::Error>138     fn encode(&self, e: &mut E) -> Result<(), E::Error> {
139         e.encode_alloc_id(self)
140     }
141 }
142 
143 macro_rules! encodable_via_deref {
144     ($($t:ty),+) => {
145         $(impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for $t {
146             fn encode(&self, e: &mut E) -> Result<(), E::Error> {
147                 (**self).encode(e)
148             }
149         })*
150     }
151 }
152 
153 encodable_via_deref! {
154     &'tcx ty::TypeckResults<'tcx>,
155     ty::Region<'tcx>,
156     &'tcx mir::Body<'tcx>,
157     &'tcx mir::UnsafetyCheckResult,
158     &'tcx mir::BorrowCheckResult<'tcx>,
159     &'tcx mir::coverage::CodeRegion
160 }
161 
162 pub trait TyDecoder<'tcx>: Decoder {
163     const CLEAR_CROSS_CRATE: bool;
164 
tcx(&self) -> TyCtxt<'tcx>165     fn tcx(&self) -> TyCtxt<'tcx>;
166 
peek_byte(&self) -> u8167     fn peek_byte(&self) -> u8;
168 
position(&self) -> usize169     fn position(&self) -> usize;
170 
cached_ty_for_shorthand<F>( &mut self, shorthand: usize, or_insert_with: F, ) -> Result<Ty<'tcx>, Self::Error> where F: FnOnce(&mut Self) -> Result<Ty<'tcx>, Self::Error>171     fn cached_ty_for_shorthand<F>(
172         &mut self,
173         shorthand: usize,
174         or_insert_with: F,
175     ) -> Result<Ty<'tcx>, Self::Error>
176     where
177         F: FnOnce(&mut Self) -> Result<Ty<'tcx>, Self::Error>;
178 
with_position<F, R>(&mut self, pos: usize, f: F) -> R where F: FnOnce(&mut Self) -> R179     fn with_position<F, R>(&mut self, pos: usize, f: F) -> R
180     where
181         F: FnOnce(&mut Self) -> R;
182 
positioned_at_shorthand(&self) -> bool183     fn positioned_at_shorthand(&self) -> bool {
184         (self.peek_byte() & (SHORTHAND_OFFSET as u8)) != 0
185     }
186 
decode_alloc_id(&mut self) -> Result<AllocId, Self::Error>187     fn decode_alloc_id(&mut self) -> Result<AllocId, Self::Error>;
188 }
189 
190 #[inline]
decode_arena_allocable<'tcx, D, T: ArenaAllocatable<'tcx> + Decodable<D>>( decoder: &mut D, ) -> Result<&'tcx T, D::Error> where D: TyDecoder<'tcx>,191 fn decode_arena_allocable<'tcx, D, T: ArenaAllocatable<'tcx> + Decodable<D>>(
192     decoder: &mut D,
193 ) -> Result<&'tcx T, D::Error>
194 where
195     D: TyDecoder<'tcx>,
196 {
197     Ok(decoder.tcx().arena.alloc(Decodable::decode(decoder)?))
198 }
199 
200 #[inline]
decode_arena_allocable_slice<'tcx, D, T: ArenaAllocatable<'tcx> + Decodable<D>>( decoder: &mut D, ) -> Result<&'tcx [T], D::Error> where D: TyDecoder<'tcx>,201 fn decode_arena_allocable_slice<'tcx, D, T: ArenaAllocatable<'tcx> + Decodable<D>>(
202     decoder: &mut D,
203 ) -> Result<&'tcx [T], D::Error>
204 where
205     D: TyDecoder<'tcx>,
206 {
207     Ok(decoder.tcx().arena.alloc_from_iter(<Vec<T> as Decodable<D>>::decode(decoder)?))
208 }
209 
210 impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for Ty<'tcx> {
211     #[allow(rustc::usage_of_ty_tykind)]
decode(decoder: &mut D) -> Result<Ty<'tcx>, D::Error>212     fn decode(decoder: &mut D) -> Result<Ty<'tcx>, D::Error> {
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;
218 
219             decoder.cached_ty_for_shorthand(shorthand, |decoder| {
220                 decoder.with_position(shorthand, Ty::decode)
221             })
222         } else {
223             let tcx = decoder.tcx();
224             Ok(tcx.mk_ty(ty::TyKind::decode(decoder)?))
225         }
226     }
227 }
228 
229 impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::Binder<'tcx, ty::PredicateKind<'tcx>> {
decode(decoder: &mut D) -> Result<ty::Binder<'tcx, ty::PredicateKind<'tcx>>, D::Error>230     fn decode(decoder: &mut D) -> Result<ty::Binder<'tcx, ty::PredicateKind<'tcx>>, D::Error> {
231         let bound_vars = Decodable::decode(decoder)?;
232         // Handle shorthands first, if we have a usize > 0x80.
233         Ok(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;
238 
239                 decoder.with_position(shorthand, ty::PredicateKind::decode)?
240             } else {
241                 ty::PredicateKind::decode(decoder)?
242             },
243             bound_vars,
244         ))
245     }
246 }
247 
248 impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::Predicate<'tcx> {
decode(decoder: &mut D) -> Result<ty::Predicate<'tcx>, D::Error>249     fn decode(decoder: &mut D) -> Result<ty::Predicate<'tcx>, D::Error> {
250         let predicate_kind = Decodable::decode(decoder)?;
251         let predicate = decoder.tcx().mk_predicate(predicate_kind);
252         Ok(predicate)
253     }
254 }
255 
256 impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for SubstsRef<'tcx> {
decode(decoder: &mut D) -> Result<Self, D::Error>257     fn decode(decoder: &mut D) -> Result<Self, D::Error> {
258         let len = decoder.read_usize()?;
259         let tcx = decoder.tcx();
260         tcx.mk_substs((0..len).map(|_| Decodable::decode(decoder)))
261     }
262 }
263 
264 impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for mir::Place<'tcx> {
decode(decoder: &mut D) -> Result<Self, D::Error>265     fn decode(decoder: &mut D) -> Result<Self, D::Error> {
266         let local: mir::Local = Decodable::decode(decoder)?;
267         let len = decoder.read_usize()?;
268         let projection: &'tcx List<mir::PlaceElem<'tcx>> =
269             decoder.tcx().mk_place_elems((0..len).map(|_| Decodable::decode(decoder)))?;
270         Ok(mir::Place { local, projection })
271     }
272 }
273 
274 impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::Region<'tcx> {
decode(decoder: &mut D) -> Result<Self, D::Error>275     fn decode(decoder: &mut D) -> Result<Self, D::Error> {
276         Ok(decoder.tcx().mk_region(Decodable::decode(decoder)?))
277     }
278 }
279 
280 impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for CanonicalVarInfos<'tcx> {
decode(decoder: &mut D) -> Result<Self, D::Error>281     fn decode(decoder: &mut D) -> Result<Self, D::Error> {
282         let len = decoder.read_usize()?;
283         let interned: Result<Vec<CanonicalVarInfo<'tcx>>, _> =
284             (0..len).map(|_| Decodable::decode(decoder)).collect();
285         Ok(decoder.tcx().intern_canonical_var_infos(interned?.as_slice()))
286     }
287 }
288 
289 impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for AllocId {
decode(decoder: &mut D) -> Result<Self, D::Error>290     fn decode(decoder: &mut D) -> Result<Self, D::Error> {
291         decoder.decode_alloc_id()
292     }
293 }
294 
295 impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::SymbolName<'tcx> {
decode(decoder: &mut D) -> Result<Self, D::Error>296     fn decode(decoder: &mut D) -> Result<Self, D::Error> {
297         Ok(ty::SymbolName::new(decoder.tcx(), &decoder.read_str()?))
298     }
299 }
300 
301 macro_rules! impl_decodable_via_ref {
302     ($($t:ty),+) => {
303         $(impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for $t {
304             fn decode(decoder: &mut D) -> Result<Self, D::Error> {
305                 RefDecodable::decode(decoder)
306             }
307         })*
308     }
309 }
310 
311 impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::AdtDef {
decode(decoder: &mut D) -> Result<&'tcx Self, D::Error>312     fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> {
313         let def_id = <DefId as Decodable<D>>::decode(decoder)?;
314         Ok(decoder.tcx().adt_def(def_id))
315     }
316 }
317 
318 impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::List<Ty<'tcx>> {
decode(decoder: &mut D) -> Result<&'tcx Self, D::Error>319     fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> {
320         let len = decoder.read_usize()?;
321         decoder.tcx().mk_type_list((0..len).map(|_| Decodable::decode(decoder)))
322     }
323 }
324 
325 impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D>
326     for ty::List<ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>>
327 {
decode(decoder: &mut D) -> Result<&'tcx Self, D::Error>328     fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> {
329         let len = decoder.read_usize()?;
330         decoder.tcx().mk_poly_existential_predicates((0..len).map(|_| Decodable::decode(decoder)))
331     }
332 }
333 
334 impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::Const<'tcx> {
decode(decoder: &mut D) -> Result<&'tcx Self, D::Error>335     fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> {
336         Ok(decoder.tcx().mk_const(Decodable::decode(decoder)?))
337     }
338 }
339 
340 impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [ty::ValTree<'tcx>] {
decode(decoder: &mut D) -> Result<&'tcx Self, D::Error>341     fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> {
342         Ok(decoder.tcx().arena.alloc_from_iter(
343             (0..decoder.read_usize()?)
344                 .map(|_| Decodable::decode(decoder))
345                 .collect::<Result<Vec<_>, _>>()?,
346         ))
347     }
348 }
349 
350 impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for Allocation {
decode(decoder: &mut D) -> Result<&'tcx Self, D::Error>351     fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> {
352         Ok(decoder.tcx().intern_const_alloc(Decodable::decode(decoder)?))
353     }
354 }
355 
356 impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [(ty::Predicate<'tcx>, Span)] {
decode(decoder: &mut D) -> Result<&'tcx Self, D::Error>357     fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> {
358         Ok(decoder.tcx().arena.alloc_from_iter(
359             (0..decoder.read_usize()?)
360                 .map(|_| Decodable::decode(decoder))
361                 .collect::<Result<Vec<_>, _>>()?,
362         ))
363     }
364 }
365 
366 impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [thir::abstract_const::Node<'tcx>] {
decode(decoder: &mut D) -> Result<&'tcx Self, D::Error>367     fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> {
368         Ok(decoder.tcx().arena.alloc_from_iter(
369             (0..decoder.read_usize()?)
370                 .map(|_| Decodable::decode(decoder))
371                 .collect::<Result<Vec<_>, _>>()?,
372         ))
373     }
374 }
375 
376 impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [thir::abstract_const::NodeId] {
decode(decoder: &mut D) -> Result<&'tcx Self, D::Error>377     fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> {
378         Ok(decoder.tcx().arena.alloc_from_iter(
379             (0..decoder.read_usize()?)
380                 .map(|_| Decodable::decode(decoder))
381                 .collect::<Result<Vec<_>, _>>()?,
382         ))
383     }
384 }
385 
386 impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::List<ty::BoundVariableKind> {
decode(decoder: &mut D) -> Result<&'tcx Self, D::Error>387     fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> {
388         let len = decoder.read_usize()?;
389         decoder.tcx().mk_bound_variable_kinds((0..len).map(|_| Decodable::decode(decoder)))
390     }
391 }
392 
393 impl_decodable_via_ref! {
394     &'tcx ty::TypeckResults<'tcx>,
395     &'tcx ty::List<Ty<'tcx>>,
396     &'tcx ty::List<ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>>,
397     &'tcx Allocation,
398     &'tcx mir::Body<'tcx>,
399     &'tcx mir::UnsafetyCheckResult,
400     &'tcx mir::BorrowCheckResult<'tcx>,
401     &'tcx mir::coverage::CodeRegion,
402     &'tcx ty::List<ty::BoundVariableKind>
403 }
404 
405 #[macro_export]
406 macro_rules! __impl_decoder_methods {
407     ($($name:ident -> $ty:ty;)*) => {
408         $(
409             #[inline]
410             fn $name(&mut self) -> Result<$ty, Self::Error> {
411                 self.opaque.$name()
412             }
413         )*
414     }
415 }
416 
417 macro_rules! impl_arena_allocatable_decoder {
418     ([]$args:tt) => {};
419     ([decode $(, $attrs:ident)*]
420      [$name:ident: $ty:ty]) => {
421         impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for $ty {
422             #[inline]
423             fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> {
424                 decode_arena_allocable(decoder)
425             }
426         }
427 
428         impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [$ty] {
429             #[inline]
430             fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> {
431                 decode_arena_allocable_slice(decoder)
432             }
433         }
434     };
435     ([$ignore:ident $(, $attrs:ident)*]$args:tt) => {
436         impl_arena_allocatable_decoder!([$($attrs),*]$args);
437     };
438 }
439 
440 macro_rules! impl_arena_allocatable_decoders {
441     ([$($a:tt $name:ident: $ty:ty,)*]) => {
442         $(
443             impl_arena_allocatable_decoder!($a [$name: $ty]);
444         )*
445     }
446 }
447 
448 rustc_hir::arena_types!(impl_arena_allocatable_decoders);
449 arena_types!(impl_arena_allocatable_decoders);
450 
451 #[macro_export]
452 macro_rules! implement_ty_decoder {
453     ($DecoderName:ident <$($typaram:tt),*>) => {
454         mod __ty_decoder_impl {
455             use std::borrow::Cow;
456             use rustc_serialize::Decoder;
457 
458             use super::$DecoderName;
459 
460             impl<$($typaram ),*> Decoder for $DecoderName<$($typaram),*> {
461                 type Error = String;
462 
463                 $crate::__impl_decoder_methods! {
464                     read_nil -> ();
465 
466                     read_u128 -> u128;
467                     read_u64 -> u64;
468                     read_u32 -> u32;
469                     read_u16 -> u16;
470                     read_u8 -> u8;
471                     read_usize -> usize;
472 
473                     read_i128 -> i128;
474                     read_i64 -> i64;
475                     read_i32 -> i32;
476                     read_i16 -> i16;
477                     read_i8 -> i8;
478                     read_isize -> isize;
479 
480                     read_bool -> bool;
481                     read_f64 -> f64;
482                     read_f32 -> f32;
483                     read_char -> char;
484                     read_str -> Cow<'_, str>;
485                 }
486 
487                 #[inline]
488                 fn read_raw_bytes_into(&mut self, bytes: &mut [u8]) -> Result<(), Self::Error> {
489                     self.opaque.read_raw_bytes_into(bytes)
490                 }
491 
492                 fn error(&mut self, err: &str) -> Self::Error {
493                     self.opaque.error(err)
494                 }
495             }
496         }
497     }
498 }
499 
500 macro_rules! impl_binder_encode_decode {
501     ($($t:ty),+ $(,)?) => {
502         $(
503             impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for ty::Binder<'tcx, $t> {
504                 fn encode(&self, e: &mut E) -> Result<(), E::Error> {
505                     self.bound_vars().encode(e)?;
506                     self.as_ref().skip_binder().encode(e)
507                 }
508             }
509             impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::Binder<'tcx, $t> {
510                 fn decode(decoder: &mut D) -> Result<Self, D::Error> {
511                     let bound_vars = Decodable::decode(decoder)?;
512                     Ok(ty::Binder::bind_with_vars(Decodable::decode(decoder)?, bound_vars))
513                 }
514             }
515         )*
516     }
517 }
518 
519 impl_binder_encode_decode! {
520     &'tcx ty::List<Ty<'tcx>>,
521     ty::FnSig<'tcx>,
522     ty::ExistentialPredicate<'tcx>,
523     ty::TraitRef<'tcx>,
524     Vec<ty::GeneratorInteriorTypeCause<'tcx>>,
525 }
526