@@ -22,8 +22,9 @@ use rustc_span::hygiene::{
22
22
ExpnId , HygieneDecodeContext , HygieneEncodeContext , SyntaxContext , SyntaxContextData ,
23
23
} ;
24
24
use rustc_span:: source_map:: { SourceMap , StableSourceFileId } ;
25
- use rustc_span:: CachingSourceMapView ;
26
25
use rustc_span:: { BytePos , ExpnData , ExpnHash , Pos , SourceFile , Span } ;
26
+ use rustc_span:: { CachingSourceMapView , Symbol } ;
27
+ use std:: collections:: hash_map:: Entry ;
27
28
use std:: io;
28
29
use std:: mem;
29
30
@@ -38,6 +39,10 @@ const TAG_RELATIVE_SPAN: u8 = 2;
38
39
const TAG_SYNTAX_CONTEXT : u8 = 0 ;
39
40
const TAG_EXPN_DATA : u8 = 1 ;
40
41
42
+ // Tags for encoding Symbol's
43
+ const SYMBOL_STR : u8 = 0 ;
44
+ const SYMBOL_OFFSET : u8 = 1 ;
45
+
41
46
/// Provides an interface to incremental compilation data cached from the
42
47
/// previous compilation session. This data will eventually include the results
43
48
/// 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> {
254
259
source_map : CachingSourceMapView :: new ( tcx. sess . source_map ( ) ) ,
255
260
file_to_file_index,
256
261
hygiene_context : & hygiene_encode_context,
262
+ symbol_table : Default :: default ( ) ,
257
263
} ;
258
264
259
265
// Encode query results.
@@ -714,6 +720,36 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for Span {
714
720
}
715
721
}
716
722
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
+
717
753
impl < ' a , ' tcx > Decodable < CacheDecoder < ' a , ' tcx > > for CrateNum {
718
754
fn decode ( d : & mut CacheDecoder < ' a , ' tcx > ) -> Self {
719
755
let stable_id = StableCrateId :: decode ( d) ;
@@ -815,6 +851,7 @@ pub struct CacheEncoder<'a, 'tcx> {
815
851
source_map : CachingSourceMapView < ' tcx > ,
816
852
file_to_file_index : FxHashMap < * const SourceFile , SourceFileIndex > ,
817
853
hygiene_context : & ' a HygieneEncodeContext ,
854
+ symbol_table : FxHashMap < Symbol , usize > ,
818
855
}
819
856
820
857
impl < ' a , ' tcx > CacheEncoder < ' a , ' tcx > {
@@ -899,6 +936,25 @@ impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for Span {
899
936
}
900
937
}
901
938
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
+
902
958
impl < ' a , ' tcx > TyEncoder for CacheEncoder < ' a , ' tcx > {
903
959
type I = TyCtxt < ' tcx > ;
904
960
const CLEAR_CROSS_CRATE : bool = false ;
0 commit comments