@@ -54,7 +54,7 @@ pub struct OnDiskCache<'sess> {
54
54
cnum_map : OnceCell < UnhashMap < StableCrateId , CrateNum > > ,
55
55
56
56
source_map : & ' sess SourceMap ,
57
- file_index_to_stable_id : FxHashMap < SourceFileIndex , StableSourceFileId > ,
57
+ file_index_to_stable_id : FxHashMap < SourceFileIndex , EncodedSourceFileId > ,
58
58
59
59
// Caches that are populated lazily during decoding.
60
60
file_index_to_file : Lock < FxHashMap < SourceFileIndex , Lrc < SourceFile > > > ,
@@ -111,7 +111,7 @@ pub struct OnDiskCache<'sess> {
111
111
// This type is used only for serialization and deserialization.
112
112
#[ derive( Encodable , Decodable ) ]
113
113
struct Footer {
114
- file_index_to_stable_id : FxHashMap < SourceFileIndex , StableSourceFileId > ,
114
+ file_index_to_stable_id : FxHashMap < SourceFileIndex , EncodedSourceFileId > ,
115
115
query_result_index : EncodedQueryResultIndex ,
116
116
diagnostics_index : EncodedQueryResultIndex ,
117
117
// The location of all allocations.
@@ -157,6 +157,32 @@ crate struct RawDefId {
157
157
pub index : u32 ,
158
158
}
159
159
160
+ /// An `EncodedSourceFileId` is the same as a `StableSourceFileId` except that
161
+ /// the source crate is represented as a [StableCrateId] instead of as a
162
+ /// `CrateNum`. This way `EncodedSourceFileId` can be encoded and decoded
163
+ /// without any additional context, i.e. with a simple `opaque::Decoder` (which
164
+ /// is the only thing available when decoding the cache's [Footer].
165
+ #[ derive( Encodable , Decodable , Clone , Debug ) ]
166
+ struct EncodedSourceFileId {
167
+ file_name_hash : u64 ,
168
+ stable_crate_id : StableCrateId ,
169
+ }
170
+
171
+ impl EncodedSourceFileId {
172
+ fn translate ( & self , cnum_map : & UnhashMap < StableCrateId , CrateNum > ) -> StableSourceFileId {
173
+ let cnum = cnum_map[ & self . stable_crate_id ] ;
174
+ StableSourceFileId { file_name_hash : self . file_name_hash , cnum }
175
+ }
176
+
177
+ fn new ( tcx : TyCtxt < ' _ > , file : & SourceFile ) -> EncodedSourceFileId {
178
+ let source_file_id = StableSourceFileId :: new ( file) ;
179
+ EncodedSourceFileId {
180
+ file_name_hash : source_file_id. file_name_hash ,
181
+ stable_crate_id : tcx. stable_crate_id ( source_file_id. cnum ) ,
182
+ }
183
+ }
184
+ }
185
+
160
186
impl < ' sess > OnDiskCache < ' sess > {
161
187
/// Creates a new `OnDiskCache` instance from the serialized data in `data`.
162
188
pub fn new ( sess : & ' sess Session , data : Vec < u8 > , start_pos : usize ) -> Self {
@@ -238,7 +264,8 @@ impl<'sess> OnDiskCache<'sess> {
238
264
let index = SourceFileIndex ( index as u32 ) ;
239
265
let file_ptr: * const SourceFile = & * * file as * const _ ;
240
266
file_to_file_index. insert ( file_ptr, index) ;
241
- file_index_to_stable_id. insert ( index, StableSourceFileId :: new ( & file) ) ;
267
+ let source_file_id = EncodedSourceFileId :: new ( tcx, & file) ;
268
+ file_index_to_stable_id. insert ( index, source_file_id) ;
242
269
}
243
270
244
271
( file_to_file_index, file_index_to_stable_id)
@@ -605,7 +632,7 @@ pub struct CacheDecoder<'a, 'tcx> {
605
632
source_map : & ' a SourceMap ,
606
633
cnum_map : & ' a UnhashMap < StableCrateId , CrateNum > ,
607
634
file_index_to_file : & ' a Lock < FxHashMap < SourceFileIndex , Lrc < SourceFile > > > ,
608
- file_index_to_stable_id : & ' a FxHashMap < SourceFileIndex , StableSourceFileId > ,
635
+ file_index_to_stable_id : & ' a FxHashMap < SourceFileIndex , EncodedSourceFileId > ,
609
636
alloc_decoding_session : AllocDecodingSession < ' a > ,
610
637
syntax_contexts : & ' a FxHashMap < u32 , AbsoluteBytePos > ,
611
638
expn_data : & ' a FxHashMap < u32 , AbsoluteBytePos > ,
@@ -618,14 +645,15 @@ impl<'a, 'tcx> CacheDecoder<'a, 'tcx> {
618
645
ref file_index_to_file,
619
646
ref file_index_to_stable_id,
620
647
ref source_map,
648
+ ref cnum_map,
621
649
..
622
650
} = * self ;
623
651
624
652
file_index_to_file
625
653
. borrow_mut ( )
626
654
. entry ( index)
627
655
. or_insert_with ( || {
628
- let stable_id = file_index_to_stable_id[ & index] ;
656
+ let stable_id = file_index_to_stable_id[ & index] . translate ( cnum_map ) ;
629
657
source_map
630
658
. source_file_by_stable_id ( stable_id)
631
659
. expect ( "failed to lookup `SourceFile` in new context" )
0 commit comments