@@ -15,7 +15,8 @@ use rustc_data_structures::fx::FxHashMap;
1515use rustc_hir:: def_id:: LocalDefId ;
1616use rustc_middle:: ty:: Const ;
1717use rustc_serialize:: { Decodable , Encodable } ;
18- use rustc_span:: { Span , SpanDecoder , SpanEncoder , Spanned } ;
18+ use rustc_span:: { Span , Spanned } ;
19+ use rustc_type_ir:: codec:: { SHORTHAND_OFFSET , TyDecoder as IrTyDecoder , TyEncoder as IrTyEncoder } ;
1920
2021use crate :: arena:: ArenaAllocatable ;
2122use crate :: infer:: canonical:: { CanonicalVarKind , CanonicalVarKinds } ;
@@ -24,58 +25,19 @@ use crate::mir::mono::MonoItem;
2425use crate :: ty:: { self , AdtDef , GenericArgsRef , Ty , TyCtxt } ;
2526use crate :: { mir, traits} ;
2627
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.
30- pub const SHORTHAND_OFFSET : usize = 0x80 ;
28+ pub trait TyEncoder < ' tcx > : IrTyEncoder < ' tcx , Interner = TyCtxt < ' tcx > > { }
3129
32- pub trait TyEncoder < ' tcx > : SpanEncoder {
33- const CLEAR_CROSS_CRATE : bool ;
30+ impl < ' tcx , T > TyEncoder < ' tcx > for T where T : IrTyEncoder < ' tcx , Interner = TyCtxt < ' tcx > > { }
3431
35- fn position ( & self ) -> usize ;
32+ pub trait TyDecoder < ' tcx > : IrTyDecoder < ' tcx , Interner = TyCtxt < ' tcx > > { }
3633
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-
44- pub 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- }
34+ impl < ' tcx , T > TyDecoder < ' tcx > for T where T : IrTyDecoder < ' tcx , Interner = TyCtxt < ' tcx > > { }
6335
6436pub trait EncodableWithShorthand < ' tcx , E : TyEncoder < ' tcx > > : Copy + Eq + Hash {
65- type Variant : Encodable < E > ;
37+ type Variant ;
6638 fn variant ( & self ) -> & Self :: Variant ;
6739}
6840
69- #[ allow( rustc:: usage_of_ty_tykind) ]
70- impl < ' 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-
7941impl < ' tcx , E : TyEncoder < ' tcx > > EncodableWithShorthand < ' tcx , E > for ty:: PredicateKind < ' tcx > {
8042 type Variant = ty:: PredicateKind < ' tcx > ;
8143
10668 M : for < ' b > Fn ( & ' b mut E ) -> & ' b mut FxHashMap < T , usize > ,
10769 T : EncodableWithShorthand < ' tcx , E > ,
10870 // The discriminant and shorthand must have the same size.
109- T :: Variant : DiscriminantKind < Discriminant = isize > ,
71+ T :: Variant : DiscriminantKind < Discriminant = isize > + Encodable < E > ,
11072{
11173 let existing_shorthand = cache ( encoder) . get ( value) . copied ( ) ;
11274 if let Some ( shorthand) = existing_shorthand {
@@ -138,17 +100,11 @@ where
138100 }
139101}
140102
141- impl < ' 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-
147103impl < ' tcx , E : TyEncoder < ' tcx > > Encodable < E > for ty:: Predicate < ' tcx > {
148104 fn encode ( & self , e : & mut E ) {
149105 let kind = self . kind ( ) ;
150106 kind. bound_vars ( ) . encode ( e) ;
151- encode_with_shorthand ( e, & kind. skip_binder ( ) , TyEncoder :: predicate_shorthands) ;
107+ encode_with_shorthand ( e, & kind. skip_binder ( ) , |e| e . predicate_shorthands ( ) ) ;
152108 }
153109}
154110
@@ -230,25 +186,6 @@ fn decode_arena_allocable_slice<
230186 decoder. interner ( ) . arena . alloc_from_iter ( <Vec < T > as Decodable < D > >:: decode ( decoder) )
231187}
232188
233- impl < ' 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- 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-
252189impl < ' tcx , D : TyDecoder < ' tcx > > Decodable < D > for ty:: Predicate < ' tcx > {
253190 fn decode ( decoder : & mut D ) -> ty:: Predicate < ' tcx > {
254191 let bound_vars = Decodable :: decode ( decoder) ;
0 commit comments