@@ -25,7 +25,6 @@ use rustc_span::hygiene::{
25
25
use rustc_span:: source_map:: { SourceMap , StableSourceFileId } ;
26
26
use rustc_span:: CachingSourceMapView ;
27
27
use rustc_span:: { BytePos , ExpnData , ExpnHash , Pos , SourceFile , Span } ;
28
- use std:: io;
29
28
use std:: mem;
30
29
31
30
const TAG_FILE_FOOTER : u128 = 0xC0FFEE_C0FFEE_C0FFEE_C0FFEE_C0FFEE ;
@@ -808,10 +807,21 @@ impl_ref_decoder! {<'tcx>
808
807
809
808
//- ENCODING -------------------------------------------------------------------
810
809
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
+
811
821
/// An encoder that can write to the incremental compilation cache.
812
- pub struct CacheEncoder < ' a , ' tcx > {
822
+ pub struct CacheEncoder < ' a , ' tcx , E : OpaqueEncoder > {
813
823
tcx : TyCtxt < ' tcx > ,
814
- encoder : FileEncoder ,
824
+ encoder : E ,
815
825
type_shorthands : FxHashMap < Ty < ' tcx > , usize > ,
816
826
predicate_shorthands : FxHashMap < ty:: PredicateKind < ' tcx > , usize > ,
817
827
interpret_allocs : FxIndexSet < interpret:: AllocId > ,
@@ -820,7 +830,10 @@ pub struct CacheEncoder<'a, 'tcx> {
820
830
hygiene_context : & ' a HygieneEncodeContext ,
821
831
}
822
832
823
- impl < ' a , ' tcx > CacheEncoder < ' a , ' tcx > {
833
+ impl < ' a , ' tcx , E > CacheEncoder < ' a , ' tcx , E >
834
+ where
835
+ E : OpaqueEncoder ,
836
+ {
824
837
fn source_file_index ( & mut self , source_file : Lrc < SourceFile > ) -> SourceFileIndex {
825
838
self . file_to_file_index [ & ( & * source_file as * const SourceFile ) ]
826
839
}
@@ -839,27 +852,32 @@ impl<'a, 'tcx> CacheEncoder<'a, 'tcx> {
839
852
let end_pos = self . position ( ) ;
840
853
( ( end_pos - start_pos) as u64 ) . encode ( self ) ;
841
854
}
842
-
843
- fn finish ( self ) -> Result < usize , io:: Error > {
844
- self . encoder . finish ( )
845
- }
846
855
}
847
856
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 > ) {
850
862
rustc_span:: hygiene:: raw_encode_syntax_context ( * self , s. hygiene_context , s) ;
851
863
}
852
864
}
853
865
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 > ) {
856
871
s. hygiene_context . schedule_expn_data_for_encoding ( * self ) ;
857
872
self . expn_hash ( ) . encode ( s) ;
858
873
}
859
874
}
860
875
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 > ) {
863
881
let span_data = self . data_untracked ( ) ;
864
882
span_data. ctxt . encode ( s) ;
865
883
span_data. parent . encode ( s) ;
@@ -902,7 +920,10 @@ impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for Span {
902
920
}
903
921
}
904
922
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
+ {
906
927
type I = TyCtxt < ' tcx > ;
907
928
const CLEAR_CROSS_CRATE : bool = false ;
908
929
@@ -922,20 +943,29 @@ impl<'a, 'tcx> TyEncoder for CacheEncoder<'a, 'tcx> {
922
943
}
923
944
}
924
945
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 > ) {
927
951
s. tcx . stable_crate_id ( * self ) . encode ( s) ;
928
952
}
929
953
}
930
954
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 > ) {
933
960
s. tcx . def_path_hash ( * self ) . encode ( s) ;
934
961
}
935
962
}
936
963
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 > ) {
939
969
bug ! ( "encoding `DefIndex` without context" ) ;
940
970
}
941
971
}
@@ -949,7 +979,13 @@ macro_rules! encoder_methods {
949
979
}
950
980
}
951
981
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
+
953
989
encoder_methods ! {
954
990
emit_usize( usize ) ;
955
991
emit_u128( u128 ) ;
@@ -972,26 +1008,30 @@ impl<'a, 'tcx> Encoder for CacheEncoder<'a, 'tcx> {
972
1008
emit_str( & str ) ;
973
1009
emit_raw_bytes( & [ u8 ] ) ;
974
1010
}
1011
+
1012
+ fn finish ( self ) -> Result < E :: Ok , E :: Err > {
1013
+ self . encoder . finish ( )
1014
+ }
975
1015
}
976
1016
977
1017
// This ensures that the `Encodable<opaque::FileEncoder>::encode` specialization for byte slices
978
1018
// is used when a `CacheEncoder` having an `opaque::FileEncoder` is passed to `Encodable::encode`.
979
1019
// Unfortunately, we have to manually opt into specializations this way, given how `CacheEncoder`
980
1020
// 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 > ) {
983
1023
self . encode ( & mut e. encoder ) ;
984
1024
}
985
1025
}
986
1026
987
1027
pub fn encode_query_results < ' a , ' tcx , CTX , Q > (
988
1028
tcx : CTX ,
989
- encoder : & mut CacheEncoder < ' a , ' tcx > ,
1029
+ encoder : & mut CacheEncoder < ' a , ' tcx , FileEncoder > ,
990
1030
query_result_index : & mut EncodedDepNodeIndex ,
991
1031
) where
992
1032
CTX : QueryContext + ' tcx ,
993
1033
Q : super :: QueryDescription < CTX > ,
994
- Q :: Value : Encodable < CacheEncoder < ' a , ' tcx > > ,
1034
+ Q :: Value : Encodable < CacheEncoder < ' a , ' tcx , FileEncoder > > ,
995
1035
{
996
1036
let _timer = tcx
997
1037
. dep_context ( )
0 commit comments