@@ -10,7 +10,7 @@ use crate::arena::ArenaAllocatable;
10
10
use crate :: infer:: canonical:: { CanonicalVarInfo , CanonicalVarInfos } ;
11
11
use crate :: mir:: { self , interpret:: Allocation } ;
12
12
use crate :: ty:: subst:: SubstsRef ;
13
- use crate :: ty:: { self , List , ToPredicate , Ty , TyCtxt } ;
13
+ use crate :: ty:: { self , List , Ty , TyCtxt } ;
14
14
use rustc_data_structures:: fx:: FxHashMap ;
15
15
use rustc_hir:: def_id:: { CrateNum , DefId } ;
16
16
use rustc_serialize:: { opaque, Decodable , Decoder , Encodable , Encoder } ;
95
95
Ok ( ( ) )
96
96
}
97
97
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
-
115
98
pub trait TyDecoder < ' tcx > : Decoder {
116
99
fn tcx ( & self ) -> TyCtxt < ' tcx > ;
117
100
@@ -127,6 +110,14 @@ pub trait TyDecoder<'tcx>: Decoder {
127
110
where
128
111
F : FnOnce ( & mut Self ) -> Result < Ty < ' tcx > , Self :: Error > ;
129
112
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
+
130
121
fn with_position < F , R > ( & mut self , pos : usize , f : F ) -> R
131
122
where
132
123
F : FnOnce ( & mut Self ) -> R ;
@@ -188,6 +179,26 @@ where
188
179
}
189
180
}
190
181
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
+
191
202
#[ inline]
192
203
pub fn decode_spanned_predicates < D > (
193
204
decoder : & mut D ,
@@ -198,20 +209,7 @@ where
198
209
let tcx = decoder. tcx ( ) ;
199
210
Ok ( tcx. arena . alloc_from_iter (
200
211
( 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) )
215
213
. collect :: < Result < Vec < _ > , _ > > ( ) ?,
216
214
) )
217
215
}
@@ -421,7 +419,6 @@ macro_rules! implement_ty_decoder {
421
419
// FIXME(#36588): These impls are horribly unsound as they allow
422
420
// the caller to pick any lifetime for `'tcx`, including `'static`.
423
421
424
- rustc_hir:: arena_types!( impl_arena_allocatable_decoders, [ $DecoderName [ $( $typaram) ,* ] ] , ' tcx) ;
425
422
arena_types!( impl_arena_allocatable_decoders, [ $DecoderName [ $( $typaram) ,* ] ] , ' tcx) ;
426
423
427
424
impl <$( $typaram) ,* > SpecializedDecoder <CrateNum >
@@ -436,7 +433,24 @@ macro_rules! implement_ty_decoder {
436
433
where & ' _x ty:: TyS <' _y>: UseSpecializedDecodable
437
434
{
438
435
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
+ }
440
454
}
441
455
}
442
456
0 commit comments