@@ -25,7 +25,6 @@ use rustc_span::hygiene::{
2525use rustc_span:: source_map:: { SourceMap , StableSourceFileId } ;
2626use rustc_span:: CachingSourceMapView ;
2727use rustc_span:: { BytePos , ExpnData , ExpnHash , Pos , SourceFile , Span } ;
28- use std:: io;
2928use std:: mem;
3029
3130const TAG_FILE_FOOTER : u128 = 0xC0FFEE_C0FFEE_C0FFEE_C0FFEE_C0FFEE ;
@@ -808,10 +807,21 @@ impl_ref_decoder! {<'tcx>
808807
809808//- ENCODING -------------------------------------------------------------------
810809
810+ pub trait OpaqueEncoder : Encoder {
811+ fn position ( & self ) -> usize ;
812+ }
813+
814+ impl OpaqueEncoder for FileEncoder {
815+ #[ inline]
816+ fn position ( & self ) -> usize {
817+ FileEncoder :: position ( self )
818+ }
819+ }
820+
811821/// An encoder that can write to the incremental compilation cache.
812- pub struct CacheEncoder < ' a , ' tcx > {
822+ pub struct CacheEncoder < ' a , ' tcx , E : OpaqueEncoder > {
813823 tcx : TyCtxt < ' tcx > ,
814- encoder : FileEncoder ,
824+ encoder : E ,
815825 type_shorthands : FxHashMap < Ty < ' tcx > , usize > ,
816826 predicate_shorthands : FxHashMap < ty:: PredicateKind < ' tcx > , usize > ,
817827 interpret_allocs : FxIndexSet < interpret:: AllocId > ,
@@ -820,7 +830,10 @@ pub struct CacheEncoder<'a, 'tcx> {
820830 hygiene_context : & ' a HygieneEncodeContext ,
821831}
822832
823- impl < ' a , ' tcx > CacheEncoder < ' a , ' tcx > {
833+ impl < ' a , ' tcx , E > CacheEncoder < ' a , ' tcx , E >
834+ where
835+ E : OpaqueEncoder ,
836+ {
824837 fn source_file_index ( & mut self , source_file : Lrc < SourceFile > ) -> SourceFileIndex {
825838 self . file_to_file_index [ & ( & * source_file as * const SourceFile ) ]
826839 }
@@ -839,27 +852,32 @@ impl<'a, 'tcx> CacheEncoder<'a, 'tcx> {
839852 let end_pos = self . position ( ) ;
840853 ( ( end_pos - start_pos) as u64 ) . encode ( self ) ;
841854 }
842-
843- fn finish ( self ) -> Result < usize , io:: Error > {
844- self . encoder . finish ( )
845- }
846855}
847856
848- impl < ' a , ' tcx > Encodable < CacheEncoder < ' a , ' tcx > > for SyntaxContext {
849- fn encode ( & self , s : & mut CacheEncoder < ' a , ' tcx > ) {
857+ impl < ' a , ' tcx , E > Encodable < CacheEncoder < ' a , ' tcx , E > > for SyntaxContext
858+ where
859+ E : OpaqueEncoder ,
860+ {
861+ fn encode ( & self , s : & mut CacheEncoder < ' a , ' tcx , E > ) {
850862 rustc_span:: hygiene:: raw_encode_syntax_context ( * self , s. hygiene_context , s) ;
851863 }
852864}
853865
854- impl < ' a , ' tcx > Encodable < CacheEncoder < ' a , ' tcx > > for ExpnId {
855- fn encode ( & self , s : & mut CacheEncoder < ' a , ' tcx > ) {
866+ impl < ' a , ' tcx , E > Encodable < CacheEncoder < ' a , ' tcx , E > > for ExpnId
867+ where
868+ E : OpaqueEncoder ,
869+ {
870+ fn encode ( & self , s : & mut CacheEncoder < ' a , ' tcx , E > ) {
856871 s. hygiene_context . schedule_expn_data_for_encoding ( * self ) ;
857872 self . expn_hash ( ) . encode ( s) ;
858873 }
859874}
860875
861- impl < ' a , ' tcx > Encodable < CacheEncoder < ' a , ' tcx > > for Span {
862- fn encode ( & self , s : & mut CacheEncoder < ' a , ' tcx > ) {
876+ impl < ' a , ' tcx , E > Encodable < CacheEncoder < ' a , ' tcx , E > > for Span
877+ where
878+ E : OpaqueEncoder ,
879+ {
880+ fn encode ( & self , s : & mut CacheEncoder < ' a , ' tcx , E > ) {
863881 let span_data = self . data_untracked ( ) ;
864882 span_data. ctxt . encode ( s) ;
865883 span_data. parent . encode ( s) ;
@@ -902,7 +920,10 @@ impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for Span {
902920 }
903921}
904922
905- impl < ' a , ' tcx > TyEncoder for CacheEncoder < ' a , ' tcx > {
923+ impl < ' a , ' tcx , E > TyEncoder for CacheEncoder < ' a , ' tcx , E >
924+ where
925+ E : OpaqueEncoder ,
926+ {
906927 type I = TyCtxt < ' tcx > ;
907928 const CLEAR_CROSS_CRATE : bool = false ;
908929
@@ -922,20 +943,29 @@ impl<'a, 'tcx> TyEncoder for CacheEncoder<'a, 'tcx> {
922943 }
923944}
924945
925- impl < ' a , ' tcx > Encodable < CacheEncoder < ' a , ' tcx > > for CrateNum {
926- fn encode ( & self , s : & mut CacheEncoder < ' a , ' tcx > ) {
946+ impl < ' a , ' tcx , E > Encodable < CacheEncoder < ' a , ' tcx , E > > for CrateNum
947+ where
948+ E : OpaqueEncoder ,
949+ {
950+ fn encode ( & self , s : & mut CacheEncoder < ' a , ' tcx , E > ) {
927951 s. tcx . stable_crate_id ( * self ) . encode ( s) ;
928952 }
929953}
930954
931- impl < ' a , ' tcx > Encodable < CacheEncoder < ' a , ' tcx > > for DefId {
932- fn encode ( & self , s : & mut CacheEncoder < ' a , ' tcx > ) {
955+ impl < ' a , ' tcx , E > Encodable < CacheEncoder < ' a , ' tcx , E > > for DefId
956+ where
957+ E : OpaqueEncoder ,
958+ {
959+ fn encode ( & self , s : & mut CacheEncoder < ' a , ' tcx , E > ) {
933960 s. tcx . def_path_hash ( * self ) . encode ( s) ;
934961 }
935962}
936963
937- impl < ' a , ' tcx > Encodable < CacheEncoder < ' a , ' tcx > > for DefIndex {
938- fn encode ( & self , _: & mut CacheEncoder < ' a , ' tcx > ) {
964+ impl < ' a , ' tcx , E > Encodable < CacheEncoder < ' a , ' tcx , E > > for DefIndex
965+ where
966+ E : OpaqueEncoder ,
967+ {
968+ fn encode ( & self , _: & mut CacheEncoder < ' a , ' tcx , E > ) {
939969 bug ! ( "encoding `DefIndex` without context" ) ;
940970 }
941971}
@@ -949,7 +979,13 @@ macro_rules! encoder_methods {
949979 }
950980}
951981
952- impl < ' a , ' tcx > Encoder for CacheEncoder < ' a , ' tcx > {
982+ impl < ' a , ' tcx , E > Encoder for CacheEncoder < ' a , ' tcx , E >
983+ where
984+ E : OpaqueEncoder ,
985+ {
986+ type Ok = E :: Ok ;
987+ type Err = E :: Err ;
988+
953989 encoder_methods ! {
954990 emit_usize( usize ) ;
955991 emit_u128( u128 ) ;
@@ -972,26 +1008,30 @@ impl<'a, 'tcx> Encoder for CacheEncoder<'a, 'tcx> {
9721008 emit_str( & str ) ;
9731009 emit_raw_bytes( & [ u8 ] ) ;
9741010 }
1011+
1012+ fn finish ( self ) -> Result < E :: Ok , E :: Err > {
1013+ self . encoder . finish ( )
1014+ }
9751015}
9761016
9771017// This ensures that the `Encodable<opaque::FileEncoder>::encode` specialization for byte slices
9781018// is used when a `CacheEncoder` having an `opaque::FileEncoder` is passed to `Encodable::encode`.
9791019// Unfortunately, we have to manually opt into specializations this way, given how `CacheEncoder`
9801020// and the encoding traits currently work.
981- impl < ' a , ' tcx > Encodable < CacheEncoder < ' a , ' tcx > > for [ u8 ] {
982- fn encode ( & self , e : & mut CacheEncoder < ' a , ' tcx > ) {
1021+ impl < ' a , ' tcx > Encodable < CacheEncoder < ' a , ' tcx , FileEncoder > > for [ u8 ] {
1022+ fn encode ( & self , e : & mut CacheEncoder < ' a , ' tcx , FileEncoder > ) {
9831023 self . encode ( & mut e. encoder ) ;
9841024 }
9851025}
9861026
9871027pub fn encode_query_results < ' a , ' tcx , CTX , Q > (
9881028 tcx : CTX ,
989- encoder : & mut CacheEncoder < ' a , ' tcx > ,
1029+ encoder : & mut CacheEncoder < ' a , ' tcx , FileEncoder > ,
9901030 query_result_index : & mut EncodedDepNodeIndex ,
9911031) where
9921032 CTX : QueryContext + ' tcx ,
9931033 Q : super :: QueryDescription < CTX > ,
994- Q :: Value : Encodable < CacheEncoder < ' a , ' tcx > > ,
1034+ Q :: Value : Encodable < CacheEncoder < ' a , ' tcx , FileEncoder > > ,
9951035{
9961036 let _timer = tcx
9971037 . dep_context ( )
0 commit comments