@@ -9,7 +9,7 @@ use rustc_data_structures::sync::{HashMapExt, Lock, Lrc, OnceCell};
9
9
use rustc_data_structures:: thin_vec:: ThinVec ;
10
10
use rustc_data_structures:: unhash:: UnhashMap ;
11
11
use rustc_errors:: Diagnostic ;
12
- use rustc_hir:: def_id:: { CrateNum , DefId , DefIndex , LocalDefId , LOCAL_CRATE } ;
12
+ use rustc_hir:: def_id:: { CrateNum , DefId , DefIndex , LocalDefId , StableCrateId , LOCAL_CRATE } ;
13
13
use rustc_hir:: definitions:: DefPathHash ;
14
14
use rustc_index:: vec:: { Idx , IndexVec } ;
15
15
use rustc_query_system:: dep_graph:: DepContext ;
@@ -18,7 +18,7 @@ use rustc_serialize::{
18
18
opaque:: { self , FileEncodeResult , FileEncoder , IntEncodedWithFixedSize } ,
19
19
Decodable , Decoder , Encodable , Encoder ,
20
20
} ;
21
- use rustc_session:: { CrateDisambiguator , Session } ;
21
+ use rustc_session:: Session ;
22
22
use rustc_span:: hygiene:: {
23
23
ExpnDataDecodeMode , ExpnDataEncodeMode , ExpnId , HygieneDecodeContext , HygieneEncodeContext ,
24
24
SyntaxContext , SyntaxContextData ,
@@ -51,8 +51,7 @@ pub struct OnDiskCache<'sess> {
51
51
// session.
52
52
current_diagnostics : Lock < FxHashMap < DepNodeIndex , Vec < Diagnostic > > > ,
53
53
54
- prev_cnums : Vec < ( u32 , String , CrateDisambiguator ) > ,
55
- cnum_map : OnceCell < IndexVec < CrateNum , Option < CrateNum > > > ,
54
+ cnum_map : OnceCell < UnhashMap < StableCrateId , CrateNum > > ,
56
55
57
56
source_map : & ' sess SourceMap ,
58
57
file_index_to_stable_id : FxHashMap < SourceFileIndex , StableSourceFileId > ,
@@ -113,7 +112,6 @@ pub struct OnDiskCache<'sess> {
113
112
#[ derive( Encodable , Decodable ) ]
114
113
struct Footer {
115
114
file_index_to_stable_id : FxHashMap < SourceFileIndex , StableSourceFileId > ,
116
- prev_cnums : Vec < ( u32 , String , CrateDisambiguator ) > ,
117
115
query_result_index : EncodedQueryResultIndex ,
118
116
diagnostics_index : EncodedQueryResultIndex ,
119
117
// The location of all allocations.
@@ -186,7 +184,6 @@ impl<'sess> OnDiskCache<'sess> {
186
184
serialized_data : data,
187
185
file_index_to_stable_id : footer. file_index_to_stable_id ,
188
186
file_index_to_file : Default :: default ( ) ,
189
- prev_cnums : footer. prev_cnums ,
190
187
cnum_map : OnceCell :: new ( ) ,
191
188
source_map : sess. source_map ( ) ,
192
189
current_diagnostics : Default :: default ( ) ,
@@ -207,7 +204,6 @@ impl<'sess> OnDiskCache<'sess> {
207
204
serialized_data : Vec :: new ( ) ,
208
205
file_index_to_stable_id : Default :: default ( ) ,
209
206
file_index_to_file : Default :: default ( ) ,
210
- prev_cnums : vec ! [ ] ,
211
207
cnum_map : OnceCell :: new ( ) ,
212
208
source_map,
213
209
current_diagnostics : Default :: default ( ) ,
@@ -327,16 +323,6 @@ impl<'sess> OnDiskCache<'sess> {
327
323
interpret_alloc_index
328
324
} ;
329
325
330
- let sorted_cnums = sorted_cnums_including_local_crate ( tcx) ;
331
- let prev_cnums: Vec < _ > = sorted_cnums
332
- . iter ( )
333
- . map ( |& cnum| {
334
- let crate_name = tcx. crate_name ( cnum) . to_string ( ) ;
335
- let crate_disambiguator = tcx. crate_disambiguator ( cnum) ;
336
- ( cnum. as_u32 ( ) , crate_name, crate_disambiguator)
337
- } )
338
- . collect ( ) ;
339
-
340
326
let mut syntax_contexts = FxHashMap :: default ( ) ;
341
327
let mut expn_ids = FxHashMap :: default ( ) ;
342
328
@@ -368,7 +354,6 @@ impl<'sess> OnDiskCache<'sess> {
368
354
TAG_FILE_FOOTER ,
369
355
& Footer {
370
356
file_index_to_stable_id,
371
- prev_cnums,
372
357
query_result_index,
373
358
diagnostics_index,
374
359
interpret_alloc_index,
@@ -385,16 +370,7 @@ impl<'sess> OnDiskCache<'sess> {
385
370
// DO NOT WRITE ANYTHING TO THE ENCODER AFTER THIS POINT! The address
386
371
// of the footer must be the last thing in the data stream.
387
372
388
- return Ok ( ( ) ) ;
389
-
390
- fn sorted_cnums_including_local_crate ( tcx : TyCtxt < ' _ > ) -> Vec < CrateNum > {
391
- let mut cnums = vec ! [ LOCAL_CRATE ] ;
392
- cnums. extend_from_slice ( tcx. crates ( ) ) ;
393
- cnums. sort_unstable ( ) ;
394
- // Just to be sure...
395
- cnums. dedup ( ) ;
396
- cnums
397
- }
373
+ Ok ( ( ) )
398
374
} )
399
375
}
400
376
@@ -429,12 +405,11 @@ impl<'sess> OnDiskCache<'sess> {
429
405
self . foreign_def_path_hashes . get ( hash) . copied ( )
430
406
}
431
407
432
- fn try_remap_cnum ( & self , tcx : TyCtxt < ' _ > , cnum : u32 ) -> Option < CrateNum > {
433
- let cnum_map =
434
- self . cnum_map . get_or_init ( || Self :: compute_cnum_map ( tcx, & self . prev_cnums [ ..] ) ) ;
435
- debug ! ( "try_remap_cnum({}): cnum_map={:?}" , cnum, cnum_map) ;
408
+ fn try_remap_cnum ( & self , tcx : TyCtxt < ' _ > , stable_crate_id : StableCrateId ) -> Option < CrateNum > {
409
+ let cnum_map = self . cnum_map . get_or_init ( || Self :: compute_cnum_map ( tcx) ) ;
410
+ debug ! ( "try_remap_cnum({:?}): cnum_map={:?}" , stable_crate_id, cnum_map) ;
436
411
437
- cnum_map[ CrateNum :: from_u32 ( cnum ) ]
412
+ cnum_map. get ( & stable_crate_id ) . copied ( )
438
413
}
439
414
440
415
pub ( crate ) fn store_foreign_def_id_hash ( & self , def_id : DefId , hash : DefPathHash ) {
@@ -533,8 +508,7 @@ impl<'sess> OnDiskCache<'sess> {
533
508
where
534
509
T : Decodable < CacheDecoder < ' a , ' tcx > > ,
535
510
{
536
- let cnum_map =
537
- self . cnum_map . get_or_init ( || Self :: compute_cnum_map ( tcx, & self . prev_cnums [ ..] ) ) ;
511
+ let cnum_map = self . cnum_map . get_or_init ( || Self :: compute_cnum_map ( tcx) ) ;
538
512
539
513
let mut decoder = CacheDecoder {
540
514
tcx,
@@ -555,31 +529,16 @@ impl<'sess> OnDiskCache<'sess> {
555
529
// current-session-`CrateNum`. There might be `CrateNum`s from the previous
556
530
// `Session` that don't occur in the current one. For these, the mapping
557
531
// maps to None.
558
- fn compute_cnum_map (
559
- tcx : TyCtxt < ' _ > ,
560
- prev_cnums : & [ ( u32 , String , CrateDisambiguator ) ] ,
561
- ) -> IndexVec < CrateNum , Option < CrateNum > > {
532
+ fn compute_cnum_map ( tcx : TyCtxt < ' _ > ) -> UnhashMap < StableCrateId , CrateNum > {
562
533
tcx. dep_graph . with_ignore ( || {
563
- let current_cnums = tcx
564
- . all_crate_nums ( ( ) )
534
+ tcx. all_crate_nums ( ( ) )
565
535
. iter ( )
536
+ . chain ( std:: iter:: once ( & LOCAL_CRATE ) )
566
537
. map ( |& cnum| {
567
- let crate_name = tcx. crate_name ( cnum) . to_string ( ) ;
568
- let crate_disambiguator = tcx. crate_disambiguator ( cnum) ;
569
- ( ( crate_name, crate_disambiguator) , cnum)
538
+ let hash = tcx. def_path_hash ( cnum. as_def_id ( ) ) . stable_crate_id ( ) ;
539
+ ( hash, cnum)
570
540
} )
571
- . collect :: < FxHashMap < _ , _ > > ( ) ;
572
-
573
- let map_size = prev_cnums. iter ( ) . map ( |& ( cnum, ..) | cnum) . max ( ) . unwrap_or ( 0 ) + 1 ;
574
- let mut map = IndexVec :: from_elem_n ( None , map_size as usize ) ;
575
-
576
- for & ( prev_cnum, ref crate_name, crate_disambiguator) in prev_cnums {
577
- let key = ( crate_name. clone ( ) , crate_disambiguator) ;
578
- map[ CrateNum :: from_u32 ( prev_cnum) ] = current_cnums. get ( & key) . cloned ( ) ;
579
- }
580
-
581
- map[ LOCAL_CRATE ] = Some ( LOCAL_CRATE ) ;
582
- map
541
+ . collect ( )
583
542
} )
584
543
}
585
544
@@ -612,7 +571,7 @@ impl<'sess> OnDiskCache<'sess> {
612
571
debug ! ( "def_path_hash_to_def_id({:?}): raw_def_id = {:?}" , hash, raw_def_id) ;
613
572
// If the owning crate no longer exists, the corresponding definition definitely
614
573
// no longer exists.
615
- let krate = self . try_remap_cnum ( tcx, raw_def_id . krate ) ?;
574
+ let krate = self . try_remap_cnum ( tcx, hash . stable_crate_id ( ) ) ?;
616
575
debug ! ( "def_path_hash_to_def_id({:?}): krate = {:?}" , hash, krate) ;
617
576
// If our `DefPathHash` corresponded to a definition in the local crate,
618
577
// we should have either found it in `local_def_path_hash_to_def_id`, or
@@ -644,7 +603,7 @@ pub struct CacheDecoder<'a, 'tcx> {
644
603
tcx : TyCtxt < ' tcx > ,
645
604
opaque : opaque:: Decoder < ' a > ,
646
605
source_map : & ' a SourceMap ,
647
- cnum_map : & ' a IndexVec < CrateNum , Option < CrateNum > > ,
606
+ cnum_map : & ' a UnhashMap < StableCrateId , CrateNum > ,
648
607
file_index_to_file : & ' a Lock < FxHashMap < SourceFileIndex , Lrc < SourceFile > > > ,
649
608
file_index_to_stable_id : & ' a FxHashMap < SourceFileIndex , StableSourceFileId > ,
650
609
alloc_decoding_session : AllocDecodingSession < ' a > ,
@@ -765,10 +724,6 @@ impl<'a, 'tcx> TyDecoder<'tcx> for CacheDecoder<'a, 'tcx> {
765
724
r
766
725
}
767
726
768
- fn map_encoded_cnum_to_current ( & self , cnum : CrateNum ) -> CrateNum {
769
- self . cnum_map [ cnum] . unwrap_or_else ( || bug ! ( "could not find new `CrateNum` for {:?}" , cnum) )
770
- }
771
-
772
727
fn decode_alloc_id ( & mut self ) -> Result < interpret:: AllocId , Self :: Error > {
773
728
let alloc_decoding_session = self . alloc_decoding_session ;
774
729
alloc_decoding_session. decode_alloc_id ( self )
@@ -850,8 +805,9 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for Span {
850
805
851
806
impl < ' a , ' tcx > Decodable < CacheDecoder < ' a , ' tcx > > for CrateNum {
852
807
fn decode ( d : & mut CacheDecoder < ' a , ' tcx > ) -> Result < Self , String > {
853
- let cnum = CrateNum :: from_u32 ( u32:: decode ( d) ?) ;
854
- Ok ( d. map_encoded_cnum_to_current ( cnum) )
808
+ let stable_id = StableCrateId :: decode ( d) ?;
809
+ let cnum = d. cnum_map [ & stable_id] ;
810
+ Ok ( cnum)
855
811
}
856
812
}
857
813
@@ -1061,6 +1017,23 @@ where
1061
1017
}
1062
1018
}
1063
1019
1020
+ impl < ' a , ' tcx , E > Encodable < CacheEncoder < ' a , ' tcx , E > > for CrateNum
1021
+ where
1022
+ E : ' a + OpaqueEncoder ,
1023
+ {
1024
+ fn encode ( & self , s : & mut CacheEncoder < ' a , ' tcx , E > ) -> Result < ( ) , E :: Error > {
1025
+ let def_path_hash = s. tcx . def_path_hash ( self . as_def_id ( ) ) ;
1026
+ // Store additional information when we encode a foreign `DefId`,
1027
+ // so that we can map its `DefPathHash` back to a `DefId` in the next
1028
+ // compilation session.
1029
+ if * self != LOCAL_CRATE {
1030
+ s. latest_foreign_def_path_hashes
1031
+ . insert ( def_path_hash, RawDefId { krate : self . as_u32 ( ) , index : 0 } ) ;
1032
+ }
1033
+ def_path_hash. stable_crate_id ( ) . encode ( s)
1034
+ }
1035
+ }
1036
+
1064
1037
impl < ' a , ' tcx , E > Encodable < CacheEncoder < ' a , ' tcx , E > > for DefId
1065
1038
where
1066
1039
E : ' a + OpaqueEncoder ,
0 commit comments