@@ -10,7 +10,7 @@ use crate::arena::ArenaAllocatable;
1010use crate :: infer:: canonical:: { CanonicalVarInfo , CanonicalVarInfos } ;
1111use crate :: mir:: { self , interpret:: Allocation } ;
1212use crate :: ty:: subst:: SubstsRef ;
13- use crate :: ty:: { self , List , ToPredicate , Ty , TyCtxt } ;
13+ use crate :: ty:: { self , List , Ty , TyCtxt } ;
1414use rustc_data_structures:: fx:: FxHashMap ;
1515use rustc_hir:: def_id:: { CrateNum , DefId } ;
1616use rustc_serialize:: { opaque, Decodable , Decoder , Encodable , Encoder } ;
9595 Ok ( ( ) )
9696}
9797
98- pub fn encode_spanned_predicates < ' tcx , E , C > (
99- encoder : & mut E ,
100- predicates : & [ ( ty:: Predicate < ' tcx > , Span ) ] ,
101- cache : C ,
102- ) -> Result < ( ) , E :: Error >
103- where
104- E : TyEncoder ,
105- C : for < ' b > Fn ( & ' b mut E ) -> & ' b mut FxHashMap < ty:: Predicate < ' tcx > , usize > ,
106- {
107- predicates. len ( ) . encode ( encoder) ?;
108- for ( predicate, span) in predicates {
109- encode_with_shorthand ( encoder, predicate, & cache) ?;
110- span. encode ( encoder) ?;
111- }
112- Ok ( ( ) )
113- }
114-
11598pub trait TyDecoder < ' tcx > : Decoder {
11699 fn tcx ( & self ) -> TyCtxt < ' tcx > ;
117100
@@ -127,6 +110,14 @@ pub trait TyDecoder<'tcx>: Decoder {
127110 where
128111 F : FnOnce ( & mut Self ) -> Result < Ty < ' tcx > , Self :: Error > ;
129112
113+ fn cached_predicate_for_shorthand < F > (
114+ & mut self ,
115+ shorthand : usize ,
116+ or_insert_with : F ,
117+ ) -> Result < ty:: Predicate < ' tcx > , Self :: Error >
118+ where
119+ F : FnOnce ( & mut Self ) -> Result < ty:: Predicate < ' tcx > , Self :: Error > ;
120+
130121 fn with_position < F , R > ( & mut self , pos : usize , f : F ) -> R
131122 where
132123 F : FnOnce ( & mut Self ) -> R ;
@@ -188,6 +179,26 @@ where
188179 }
189180}
190181
182+ #[ inline]
183+ pub fn decode_predicate < D > ( decoder : & mut D ) -> Result < ty:: Predicate < ' tcx > , D :: Error >
184+ where
185+ D : TyDecoder < ' tcx > ,
186+ {
187+ // Handle shorthands first, if we have an usize > 0x80.
188+ if decoder. positioned_at_shorthand ( ) {
189+ let pos = decoder. read_usize ( ) ?;
190+ assert ! ( pos >= SHORTHAND_OFFSET ) ;
191+ let shorthand = pos - SHORTHAND_OFFSET ;
192+
193+ decoder. cached_predicate_for_shorthand ( shorthand, |decoder| {
194+ decoder. with_position ( shorthand, ty:: Predicate :: decode)
195+ } )
196+ } else {
197+ let tcx = decoder. tcx ( ) ;
198+ Ok ( tcx. mk_predicate ( ty:: PredicateKind :: decode ( decoder) ?) )
199+ }
200+ }
201+
191202#[ inline]
192203pub fn decode_spanned_predicates < D > (
193204 decoder : & mut D ,
@@ -198,20 +209,7 @@ where
198209 let tcx = decoder. tcx ( ) ;
199210 Ok ( tcx. arena . alloc_from_iter (
200211 ( 0 ..decoder. read_usize ( ) ?)
201- . map ( |_| {
202- // Handle shorthands first, if we have an usize > 0x80.
203- let predicate_kind = if decoder. positioned_at_shorthand ( ) {
204- let pos = decoder. read_usize ( ) ?;
205- assert ! ( pos >= SHORTHAND_OFFSET ) ;
206- let shorthand = pos - SHORTHAND_OFFSET ;
207-
208- decoder. with_position ( shorthand, ty:: PredicateKind :: decode)
209- } else {
210- ty:: PredicateKind :: decode ( decoder)
211- } ?;
212- let predicate = predicate_kind. to_predicate ( tcx) ;
213- Ok ( ( predicate, Decodable :: decode ( decoder) ?) )
214- } )
212+ . map ( |_| Decodable :: decode ( decoder) )
215213 . collect :: < Result < Vec < _ > , _ > > ( ) ?,
216214 ) )
217215}
@@ -421,7 +419,6 @@ macro_rules! implement_ty_decoder {
421419 // FIXME(#36588): These impls are horribly unsound as they allow
422420 // the caller to pick any lifetime for `'tcx`, including `'static`.
423421
424- rustc_hir:: arena_types!( impl_arena_allocatable_decoders, [ $DecoderName [ $( $typaram) ,* ] ] , ' tcx) ;
425422 arena_types!( impl_arena_allocatable_decoders, [ $DecoderName [ $( $typaram) ,* ] ] , ' tcx) ;
426423
427424 impl <$( $typaram) ,* > SpecializedDecoder <CrateNum >
@@ -436,7 +433,24 @@ macro_rules! implement_ty_decoder {
436433 where & ' _x ty:: TyS <' _y>: UseSpecializedDecodable
437434 {
438435 fn specialized_decode( & mut self ) -> Result <& ' _x ty:: TyS <' _y>, Self :: Error > {
439- unsafe { transmute:: <Result <ty:: Ty <' tcx>, Self :: Error >, Result <& ' _x ty:: TyS <' _y>, Self :: Error >>( decode_ty( self ) ) }
436+ unsafe {
437+ transmute:: <
438+ Result <ty:: Ty <' tcx>, Self :: Error >,
439+ Result <& ' _x ty:: TyS <' _y>, Self :: Error >,
440+ >( decode_ty( self ) )
441+ }
442+ }
443+ }
444+
445+ impl <' _x, $( $typaram) ,* > SpecializedDecoder <ty:: Predicate <' _x>>
446+ for $DecoderName<$( $typaram) ,* > {
447+ fn specialized_decode( & mut self ) -> Result <ty:: Predicate <' _x>, Self :: Error > {
448+ unsafe {
449+ transmute:: <
450+ Result <ty:: Predicate <' tcx>, Self :: Error >,
451+ Result <ty:: Predicate <' _x>, Self :: Error >,
452+ >( decode_predicate( self ) )
453+ }
440454 }
441455 }
442456
0 commit comments