@@ -22,8 +22,9 @@ use rustc_span::hygiene::{
2222 ExpnId , HygieneDecodeContext , HygieneEncodeContext , SyntaxContext , SyntaxContextData ,
2323} ;
2424use rustc_span:: source_map:: { SourceMap , StableSourceFileId } ;
25- use rustc_span:: CachingSourceMapView ;
2625use rustc_span:: { BytePos , ExpnData , ExpnHash , Pos , SourceFile , Span } ;
26+ use rustc_span:: { CachingSourceMapView , Symbol } ;
27+ use std:: collections:: hash_map:: Entry ;
2728use std:: io;
2829use std:: mem;
2930
@@ -38,6 +39,10 @@ const TAG_RELATIVE_SPAN: u8 = 2;
3839const TAG_SYNTAX_CONTEXT : u8 = 0 ;
3940const TAG_EXPN_DATA : u8 = 1 ;
4041
42+ // Tags for encoding Symbol's
43+ const SYMBOL_STR : u8 = 0 ;
44+ const SYMBOL_OFFSET : u8 = 1 ;
45+
4146/// Provides an interface to incremental compilation data cached from the
4247/// previous compilation session. This data will eventually include the results
4348/// of a few selected queries (like `typeck` and `mir_optimized`) and
@@ -254,6 +259,7 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> {
254259 source_map : CachingSourceMapView :: new ( tcx. sess . source_map ( ) ) ,
255260 file_to_file_index,
256261 hygiene_context : & hygiene_encode_context,
262+ symbol_table : Default :: default ( ) ,
257263 } ;
258264
259265 // Encode query results.
@@ -714,6 +720,36 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for Span {
714720 }
715721}
716722
723+ // copy&paste impl from rustc_metadata
724+ impl < ' a , ' tcx > Decodable < CacheDecoder < ' a , ' tcx > > for Symbol {
725+ fn decode ( d : & mut CacheDecoder < ' a , ' tcx > ) -> Self {
726+ let tag = d. read_u8 ( ) ;
727+
728+ match tag {
729+ SYMBOL_STR => {
730+ let s = d. read_str ( ) ;
731+ Symbol :: intern ( s)
732+ }
733+ SYMBOL_OFFSET => {
734+ // read str offset
735+ let pos = d. read_usize ( ) ;
736+ let old_pos = d. opaque . position ( ) ;
737+
738+ // move to str ofset and read
739+ d. opaque . set_position ( pos) ;
740+ let s = d. read_str ( ) ;
741+ let sym = Symbol :: intern ( s) ;
742+
743+ // restore position
744+ d. opaque . set_position ( old_pos) ;
745+
746+ sym
747+ }
748+ _ => unreachable ! ( ) ,
749+ }
750+ }
751+ }
752+
717753impl < ' a , ' tcx > Decodable < CacheDecoder < ' a , ' tcx > > for CrateNum {
718754 fn decode ( d : & mut CacheDecoder < ' a , ' tcx > ) -> Self {
719755 let stable_id = StableCrateId :: decode ( d) ;
@@ -815,6 +851,7 @@ pub struct CacheEncoder<'a, 'tcx> {
815851 source_map : CachingSourceMapView < ' tcx > ,
816852 file_to_file_index : FxHashMap < * const SourceFile , SourceFileIndex > ,
817853 hygiene_context : & ' a HygieneEncodeContext ,
854+ symbol_table : FxHashMap < Symbol , usize > ,
818855}
819856
820857impl < ' a , ' tcx > CacheEncoder < ' a , ' tcx > {
@@ -899,6 +936,25 @@ impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for Span {
899936 }
900937}
901938
939+ // copy&paste impl from rustc_metadata
940+ impl < ' a , ' tcx > Encodable < CacheEncoder < ' a , ' tcx > > for Symbol {
941+ fn encode ( & self , s : & mut CacheEncoder < ' a , ' tcx > ) {
942+ match s. symbol_table . entry ( * self ) {
943+ Entry :: Vacant ( o) => {
944+ s. encoder . emit_u8 ( SYMBOL_STR ) ;
945+ let pos = s. encoder . position ( ) ;
946+ o. insert ( pos) ;
947+ s. emit_str ( self . as_str ( ) ) ;
948+ }
949+ Entry :: Occupied ( o) => {
950+ let x = o. get ( ) . clone ( ) ;
951+ s. emit_u8 ( SYMBOL_OFFSET ) ;
952+ s. emit_usize ( x) ;
953+ }
954+ }
955+ }
956+ }
957+
902958impl < ' a , ' tcx > TyEncoder for CacheEncoder < ' a , ' tcx > {
903959 type I = TyCtxt < ' tcx > ;
904960 const CLEAR_CROSS_CRATE : bool = false ;
0 commit comments