Skip to main content

rustc_middle/ty/
codec.rs

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
9use std::hash::Hash;
10use std::intrinsics;
11use std::marker::{DiscriminantKind, PointeeSized};
12
13use rustc_abi::FieldIdx;
14use rustc_data_structures::fx::FxHashMap;
15use rustc_hir::def_id::LocalDefId;
16use rustc_serialize::{Decodable, Encodable};
17use rustc_span::source_map::Spanned;
18use rustc_span::{Span, SpanDecoder, SpanEncoder};
19
20use crate::arena::ArenaAllocatable;
21use crate::infer::canonical::{CanonicalVarKind, CanonicalVarKinds};
22use crate::mir::interpret::{AllocId, ConstAllocation, CtfeProvenance};
23use crate::mir::mono::MonoItem;
24use crate::ty::{self, AdtDef, GenericArgsRef, Ty, TyCtxt};
25use crate::{mir, traits};
26
27/// The shorthand encoding uses an enum's variant index `usize`
28/// and is offset by this value so it never matches a real variant.
29/// This offset is also chosen so that the first byte is never < 0x80.
30pub const SHORTHAND_OFFSET: usize = 0x80;
31
32pub trait TyEncoder<'tcx>: SpanEncoder {
33    const CLEAR_CROSS_CRATE: bool;
34
35    fn position(&self) -> usize;
36
37    fn type_shorthands(&mut self) -> &mut FxHashMap<Ty<'tcx>, usize>;
38
39    fn predicate_shorthands(&mut self) -> &mut FxHashMap<ty::PredicateKind<'tcx>, usize>;
40
41    fn encode_alloc_id(&mut self, alloc_id: &AllocId);
42}
43
44pub trait TyDecoder<'tcx>: SpanDecoder {
45    const CLEAR_CROSS_CRATE: bool;
46
47    fn interner(&self) -> TyCtxt<'tcx>;
48
49    fn cached_ty_for_shorthand<F>(&mut self, shorthand: usize, or_insert_with: F) -> Ty<'tcx>
50    where
51        F: FnOnce(&mut Self) -> Ty<'tcx>;
52
53    fn with_position<F, R>(&mut self, pos: usize, f: F) -> R
54    where
55        F: FnOnce(&mut Self) -> R;
56
57    fn positioned_at_shorthand(&self) -> bool {
58        (self.peek_byte() & (SHORTHAND_OFFSET as u8)) != 0
59    }
60
61    fn decode_alloc_id(&mut self) -> AllocId;
62}
63
64pub trait EncodableWithShorthand<'tcx, E: TyEncoder<'tcx>>: Copy + Eq + Hash {
65    type Variant: Encodable<E>;
66    fn variant(&self) -> &Self::Variant;
67}
68
69#[allow(rustc::usage_of_ty_tykind)]
70impl<'tcx, E: TyEncoder<'tcx>> EncodableWithShorthand<'tcx, E> for Ty<'tcx> {
71    type Variant = ty::TyKind<'tcx>;
72
73    #[inline]
74    fn variant(&self) -> &Self::Variant {
75        self.kind()
76    }
77}
78
79impl<'tcx, E: TyEncoder<'tcx>> EncodableWithShorthand<'tcx, E> for ty::PredicateKind<'tcx> {
80    type Variant = ty::PredicateKind<'tcx>;
81
82    #[inline]
83    fn variant(&self) -> &Self::Variant {
84        self
85    }
86}
87
88/// Trait for decoding to a reference.
89///
90/// This is a separate trait from `Decodable` so that we can implement it for
91/// upstream types, such as `FxHashSet`.
92///
93/// The `TyDecodable` derive macro will use this trait for fields that are
94/// references (and don't use a type alias to hide that).
95///
96/// `Decodable` can still be implemented in cases where `Decodable` is required
97/// by a trait bound.
98pub trait RefDecodable<'tcx, D: TyDecoder<'tcx>>: PointeeSized {
99    fn decode(d: &mut D) -> &'tcx Self;
100}
101
102/// Encode the given value or a previously cached shorthand.
103pub fn encode_with_shorthand<'tcx, E, T, M>(encoder: &mut E, value: &T, cache: M)
104where
105    E: TyEncoder<'tcx>,
106    M: for<'b> Fn(&'b mut E) -> &'b mut FxHashMap<T, usize>,
107    T: EncodableWithShorthand<'tcx, E>,
108    // The discriminant and shorthand must have the same size.
109    T::Variant: DiscriminantKind<Discriminant = isize>,
110{
111    let existing_shorthand = cache(encoder).get(value).copied();
112    if let Some(shorthand) = existing_shorthand {
113        encoder.emit_usize(shorthand);
114        return;
115    }
116
117    let variant = value.variant();
118
119    let start = encoder.position();
120    variant.encode(encoder);
121    let len = encoder.position() - start;
122
123    // The shorthand encoding uses the same usize as the
124    // discriminant, with an offset so they can't conflict.
125    let discriminant = intrinsics::discriminant_value(variant);
126    if !(SHORTHAND_OFFSET > discriminant as usize) {
    ::core::panicking::panic("assertion failed: SHORTHAND_OFFSET > discriminant as usize")
};assert!(SHORTHAND_OFFSET > discriminant as usize);
127
128    let shorthand = start + SHORTHAND_OFFSET;
129
130    // Get the number of bits that leb128 could fit
131    // in the same space as the fully encoded type.
132    let leb128_bits = len * 7;
133
134    // Check that the shorthand is a not longer than the
135    // full encoding itself, i.e., it's an obvious win.
136    if leb128_bits >= 64 || (shorthand as u64) < (1 << leb128_bits) {
137        cache(encoder).insert(*value, shorthand);
138    }
139}
140
141impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for Ty<'tcx> {
142    fn encode(&self, e: &mut E) {
143        encode_with_shorthand(e, self, TyEncoder::type_shorthands);
144    }
145}
146
147impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for ty::Predicate<'tcx> {
148    fn encode(&self, e: &mut E) {
149        let kind = self.kind();
150        kind.bound_vars().encode(e);
151        encode_with_shorthand(e, &kind.skip_binder(), TyEncoder::predicate_shorthands);
152    }
153}
154
155impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for ty::Clause<'tcx> {
156    fn encode(&self, e: &mut E) {
157        self.as_predicate().encode(e);
158    }
159}
160
161impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for ty::Region<'tcx> {
162    fn encode(&self, e: &mut E) {
163        self.kind().encode(e);
164    }
165}
166
167impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for ty::Const<'tcx> {
168    fn encode(&self, e: &mut E) {
169        self.0.0.encode(e);
170    }
171}
172
173impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for ty::Pattern<'tcx> {
174    fn encode(&self, e: &mut E) {
175        self.0.0.encode(e);
176    }
177}
178
179impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for ty::ValTree<'tcx> {
180    fn encode(&self, e: &mut E) {
181        self.0.0.encode(e);
182    }
183}
184
185impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for ConstAllocation<'tcx> {
186    fn encode(&self, e: &mut E) {
187        self.inner().encode(e)
188    }
189}
190
191impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for AdtDef<'tcx> {
192    fn encode(&self, e: &mut E) {
193        self.0.0.encode(e)
194    }
195}
196
197impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for AllocId {
198    fn encode(&self, e: &mut E) {
199        e.encode_alloc_id(self)
200    }
201}
202
203impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for CtfeProvenance {
204    fn encode(&self, e: &mut E) {
205        self.into_parts().encode(e);
206    }
207}
208
209impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for ty::ParamEnv<'tcx> {
210    fn encode(&self, e: &mut E) {
211        self.caller_bounds().encode(e);
212    }
213}
214
215#[inline]
216fn decode_arena_allocable<'tcx, D: TyDecoder<'tcx>, T: ArenaAllocatable<'tcx> + Decodable<D>>(
217    decoder: &mut D,
218) -> &'tcx T {
219    decoder.interner().arena.alloc(Decodable::decode(decoder))
220}
221
222#[inline]
223fn decode_arena_allocable_slice<
224    'tcx,
225    D: TyDecoder<'tcx>,
226    T: ArenaAllocatable<'tcx> + Decodable<D>,
227>(
228    decoder: &mut D,
229) -> &'tcx [T] {
230    decoder.interner().arena.alloc_from_iter(<Vec<T> as Decodable<D>>::decode(decoder))
231}
232
233impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for Ty<'tcx> {
234    #[allow(rustc::usage_of_ty_tykind)]
235    fn decode(decoder: &mut D) -> Ty<'tcx> {
236        // Handle shorthands first, if we have a usize > 0x80.
237        if decoder.positioned_at_shorthand() {
238            let pos = decoder.read_usize();
239            if !(pos >= SHORTHAND_OFFSET) {
    ::core::panicking::panic("assertion failed: pos >= SHORTHAND_OFFSET")
};assert!(pos >= SHORTHAND_OFFSET);
240            let shorthand = pos - SHORTHAND_OFFSET;
241
242            decoder.cached_ty_for_shorthand(shorthand, |decoder| {
243                decoder.with_position(shorthand, Ty::decode)
244            })
245        } else {
246            let tcx = decoder.interner();
247            tcx.mk_ty_from_kind(ty::TyKind::decode(decoder))
248        }
249    }
250}
251
252impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::Predicate<'tcx> {
253    fn decode(decoder: &mut D) -> ty::Predicate<'tcx> {
254        let bound_vars = Decodable::decode(decoder);
255        // Handle shorthands first, if we have a usize > 0x80.
256        let predicate_kind = ty::Binder::bind_with_vars(
257            if decoder.positioned_at_shorthand() {
258                let pos = decoder.read_usize();
259                if !(pos >= SHORTHAND_OFFSET) {
    ::core::panicking::panic("assertion failed: pos >= SHORTHAND_OFFSET")
};assert!(pos >= SHORTHAND_OFFSET);
260                let shorthand = pos - SHORTHAND_OFFSET;
261
262                decoder.with_position(shorthand, <ty::PredicateKind<'tcx> as Decodable<D>>::decode)
263            } else {
264                <ty::PredicateKind<'tcx> as Decodable<D>>::decode(decoder)
265            },
266            bound_vars,
267        );
268        decoder.interner().mk_predicate(predicate_kind)
269    }
270}
271
272impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::Clause<'tcx> {
273    fn decode(decoder: &mut D) -> ty::Clause<'tcx> {
274        let pred: ty::Predicate<'tcx> = Decodable::decode(decoder);
275        pred.expect_clause()
276    }
277}
278
279impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for GenericArgsRef<'tcx> {
280    fn decode(decoder: &mut D) -> Self {
281        let len = decoder.read_usize();
282        let tcx = decoder.interner();
283        tcx.mk_args_from_iter(
284            (0..len).map::<ty::GenericArg<'tcx>, _>(|_| Decodable::decode(decoder)),
285        )
286    }
287}
288
289impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for mir::Place<'tcx> {
290    fn decode(decoder: &mut D) -> Self {
291        let local: mir::Local = Decodable::decode(decoder);
292        let len = decoder.read_usize();
293        let projection = decoder.interner().mk_place_elems_from_iter(
294            (0..len).map::<mir::PlaceElem<'tcx>, _>(|_| Decodable::decode(decoder)),
295        );
296        mir::Place { local, projection }
297    }
298}
299
300impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::Region<'tcx> {
301    fn decode(decoder: &mut D) -> Self {
302        ty::Region::new_from_kind(decoder.interner(), Decodable::decode(decoder))
303    }
304}
305
306impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for CanonicalVarKinds<'tcx> {
307    fn decode(decoder: &mut D) -> Self {
308        let len = decoder.read_usize();
309        decoder.interner().mk_canonical_var_infos_from_iter(
310            (0..len).map::<CanonicalVarKind<'tcx>, _>(|_| Decodable::decode(decoder)),
311        )
312    }
313}
314
315impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for AllocId {
316    fn decode(decoder: &mut D) -> Self {
317        decoder.decode_alloc_id()
318    }
319}
320
321impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for CtfeProvenance {
322    fn decode(decoder: &mut D) -> Self {
323        let parts = Decodable::decode(decoder);
324        CtfeProvenance::from_parts(parts)
325    }
326}
327
328impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::SymbolName<'tcx> {
329    fn decode(decoder: &mut D) -> Self {
330        ty::SymbolName::new(decoder.interner(), decoder.read_str())
331    }
332}
333
334impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::ParamEnv<'tcx> {
335    fn decode(d: &mut D) -> Self {
336        let caller_bounds = Decodable::decode(d);
337        ty::ParamEnv::new(caller_bounds)
338    }
339}
340
341macro_rules! impl_decodable_via_ref {
342    ($($t:ty,)+) => {
343        $(impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for $t {
344            fn decode(decoder: &mut D) -> Self {
345                RefDecodable::decode(decoder)
346            }
347        })*
348    }
349}
350
351impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::List<Ty<'tcx>> {
352    fn decode(decoder: &mut D) -> &'tcx Self {
353        let len = decoder.read_usize();
354        decoder
355            .interner()
356            .mk_type_list_from_iter((0..len).map::<Ty<'tcx>, _>(|_| Decodable::decode(decoder)))
357    }
358}
359
360impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D>
361    for ty::List<ty::PolyExistentialPredicate<'tcx>>
362{
363    fn decode(decoder: &mut D) -> &'tcx Self {
364        let len = decoder.read_usize();
365        decoder.interner().mk_poly_existential_predicates_from_iter(
366            (0..len).map::<ty::Binder<'tcx, _>, _>(|_| Decodable::decode(decoder)),
367        )
368    }
369}
370
371impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::Const<'tcx> {
372    fn decode(decoder: &mut D) -> Self {
373        let kind: ty::ConstKind<'tcx> = Decodable::decode(decoder);
374        decoder.interner().mk_ct_from_kind(kind)
375    }
376}
377
378impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::Pattern<'tcx> {
379    fn decode(decoder: &mut D) -> Self {
380        decoder.interner().mk_pat(Decodable::decode(decoder))
381    }
382}
383
384impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::ValTree<'tcx> {
385    fn decode(decoder: &mut D) -> Self {
386        decoder.interner().intern_valtree(Decodable::decode(decoder))
387    }
388}
389
390impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ConstAllocation<'tcx> {
391    fn decode(decoder: &mut D) -> Self {
392        decoder.interner().mk_const_alloc(Decodable::decode(decoder))
393    }
394}
395
396impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for AdtDef<'tcx> {
397    fn decode(decoder: &mut D) -> Self {
398        decoder.interner().mk_adt_def_from_data(Decodable::decode(decoder))
399    }
400}
401
402impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [(ty::Clause<'tcx>, Span)] {
403    fn decode(decoder: &mut D) -> &'tcx Self {
404        decoder
405            .interner()
406            .arena
407            .alloc_from_iter((0..decoder.read_usize()).map(|_| Decodable::decode(decoder)))
408    }
409}
410
411impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [(ty::PolyTraitRef<'tcx>, Span)] {
412    fn decode(decoder: &mut D) -> &'tcx Self {
413        decoder
414            .interner()
415            .arena
416            .alloc_from_iter((0..decoder.read_usize()).map(|_| Decodable::decode(decoder)))
417    }
418}
419
420impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [Spanned<MonoItem<'tcx>>] {
421    fn decode(decoder: &mut D) -> &'tcx Self {
422        decoder
423            .interner()
424            .arena
425            .alloc_from_iter((0..decoder.read_usize()).map(|_| Decodable::decode(decoder)))
426    }
427}
428
429impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::List<ty::BoundVariableKind<'tcx>> {
430    fn decode(decoder: &mut D) -> &'tcx Self {
431        let len = decoder.read_usize();
432        decoder.interner().mk_bound_variable_kinds_from_iter(
433            (0..len).map::<ty::BoundVariableKind<'tcx>, _>(|_| Decodable::decode(decoder)),
434        )
435    }
436}
437
438impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::List<ty::Pattern<'tcx>> {
439    fn decode(decoder: &mut D) -> &'tcx Self {
440        let len = decoder.read_usize();
441        decoder.interner().mk_patterns_from_iter(
442            (0..len).map::<ty::Pattern<'tcx>, _>(|_| Decodable::decode(decoder)),
443        )
444    }
445}
446
447impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::List<ty::Const<'tcx>> {
448    fn decode(decoder: &mut D) -> &'tcx Self {
449        let len = decoder.read_usize();
450        decoder.interner().mk_const_list_from_iter(
451            (0..len).map::<ty::Const<'tcx>, _>(|_| Decodable::decode(decoder)),
452        )
453    }
454}
455
456impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D>
457    for ty::ListWithCachedTypeInfo<ty::Clause<'tcx>>
458{
459    fn decode(decoder: &mut D) -> &'tcx Self {
460        let len = decoder.read_usize();
461        decoder.interner().mk_clauses_from_iter(
462            (0..len).map::<ty::Clause<'tcx>, _>(|_| Decodable::decode(decoder)),
463        )
464    }
465}
466
467impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::List<FieldIdx> {
468    fn decode(decoder: &mut D) -> &'tcx Self {
469        let len = decoder.read_usize();
470        decoder
471            .interner()
472            .mk_fields_from_iter((0..len).map::<FieldIdx, _>(|_| Decodable::decode(decoder)))
473    }
474}
475
476impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::List<LocalDefId> {
477    fn decode(decoder: &mut D) -> &'tcx Self {
478        let len = decoder.read_usize();
479        decoder.interner().mk_local_def_ids_from_iter(
480            (0..len).map::<LocalDefId, _>(|_| Decodable::decode(decoder)),
481        )
482    }
483}
484
485impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for &'tcx ty::List<LocalDefId> {
486    fn decode(d: &mut D) -> Self {
487        RefDecodable::decode(d)
488    }
489}
490
491impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for
    &'tcx ty::ListWithCachedTypeInfo<ty::Clause<'tcx>> {
    fn decode(decoder: &mut D) -> Self { RefDecodable::decode(decoder) }
}impl_decodable_via_ref! {
492    &'tcx ty::TypeckResults<'tcx>,
493    &'tcx ty::List<Ty<'tcx>>,
494    &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
495    &'tcx traits::ImplSource<'tcx, ()>,
496    &'tcx mir::Body<'tcx>,
497    &'tcx ty::List<ty::BoundVariableKind<'tcx>>,
498    &'tcx ty::List<ty::Pattern<'tcx>>,
499    &'tcx ty::ListWithCachedTypeInfo<ty::Clause<'tcx>>,
500}
501
502#[macro_export]
503macro_rules! __impl_decoder_methods {
504    ($($name:ident -> $ty:ty;)*) => {
505        $(
506            #[inline]
507            fn $name(&mut self) -> $ty {
508                self.opaque.$name()
509            }
510        )*
511    }
512}
513
514macro_rules! impl_arena_allocatable_decoder {
515    ([]$args:tt) => {};
516    ([decode $(, $attrs:ident)*]
517     [$name:ident: $ty:ty]) => {
518        impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for $ty {
519            #[inline]
520            fn decode(decoder: &mut D) -> &'tcx Self {
521                decode_arena_allocable(decoder)
522            }
523        }
524
525        impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [$ty] {
526            #[inline]
527            fn decode(decoder: &mut D) -> &'tcx Self {
528                decode_arena_allocable_slice(decoder)
529            }
530        }
531    };
532}
533
534macro_rules! impl_arena_allocatable_decoders {
535    ([$($a:tt $name:ident: $ty:ty,)*]) => {
536        $(
537            impl_arena_allocatable_decoder!($a [$name: $ty]);
538        )*
539    }
540}
541
542rustc_hir::arena_types!(impl_arena_allocatable_decoders);
543impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for
    rustc_ast::tokenstream::TokenStream {
    #[inline]
    fn decode(decoder: &mut D) -> &'tcx Self {
        decode_arena_allocable(decoder)
    }
}
impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for
    [rustc_ast::tokenstream::TokenStream] {
    #[inline]
    fn decode(decoder: &mut D) -> &'tcx Self {
        decode_arena_allocable_slice(decoder)
    }
}arena_types!(impl_arena_allocatable_decoders);
544
545macro_rules! impl_arena_copy_decoder {
546    (<$tcx:tt> $($ty:ty,)*) => {
547        $(impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for $ty {
548            #[inline]
549            fn decode(decoder: &mut D) -> &'tcx Self {
550                decoder.interner().arena.alloc(Decodable::decode(decoder))
551            }
552        }
553
554        impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [$ty] {
555            #[inline]
556            fn decode(decoder: &mut D) -> &'tcx Self {
557                decoder.interner().arena.alloc_from_iter(<Vec<_> as Decodable<D>>::decode(decoder))
558            }
559        })*
560    };
561}
562
563impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for
    rustc_middle::middle::deduced_param_attrs::DeducedParamAttrs {
    #[inline]
    fn decode(decoder: &mut D) -> &'tcx Self {
        decoder.interner().arena.alloc(Decodable::decode(decoder))
    }
}
impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for
    [rustc_middle::middle::deduced_param_attrs::DeducedParamAttrs] {
    #[inline]
    fn decode(decoder: &mut D) -> &'tcx Self {
        decoder.interner().arena.alloc_from_iter(<Vec<_> as
                    Decodable<D>>::decode(decoder))
    }
}impl_arena_copy_decoder! {<'tcx>
564    Span,
565    rustc_span::Ident,
566    ty::Variance,
567    rustc_span::def_id::DefId,
568    rustc_span::def_id::LocalDefId,
569    (rustc_middle::middle::exported_symbols::ExportedSymbol<'tcx>, rustc_middle::middle::exported_symbols::SymbolExportInfo),
570    rustc_middle::middle::deduced_param_attrs::DeducedParamAttrs,
571}
572
573#[macro_export]
574macro_rules! implement_ty_decoder {
575    ($DecoderName:ident <$($typaram:tt),*>) => {
576        mod __ty_decoder_impl {
577            use rustc_serialize::Decoder;
578
579            use super::$DecoderName;
580
581            impl<$($typaram ),*> Decoder for $DecoderName<$($typaram),*> {
582                $crate::__impl_decoder_methods! {
583                    read_usize -> usize;
584                    read_u128 -> u128;
585                    read_u64 -> u64;
586                    read_u32 -> u32;
587                    read_u16 -> u16;
588                    read_u8 -> u8;
589
590                    read_isize -> isize;
591                    read_i128 -> i128;
592                    read_i64 -> i64;
593                    read_i32 -> i32;
594                    read_i16 -> i16;
595                }
596
597                #[inline]
598                fn read_raw_bytes(&mut self, len: usize) -> &[u8] {
599                    self.opaque.read_raw_bytes(len)
600                }
601
602                #[inline]
603                fn peek_byte(&self) -> u8 {
604                    self.opaque.peek_byte()
605                }
606
607                #[inline]
608                fn position(&self) -> usize {
609                    self.opaque.position()
610                }
611            }
612        }
613    }
614}