Skip to content

Commit 66cf8ea

Browse files
Replace cnum_map with tcx.stable_crate_id_to_crate_num() in OnDiskCache.
1 parent 919497c commit 66cf8ea

File tree

1 file changed

+7
-32
lines changed

1 file changed

+7
-32
lines changed

compiler/rustc_query_impl/src/on_disk_cache.rs

+7-32
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::QueryCtxt;
22
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
33
use rustc_data_structures::memmap::Mmap;
4-
use rustc_data_structures::sync::{HashMapExt, Lock, Lrc, OnceCell, RwLock};
4+
use rustc_data_structures::sync::{HashMapExt, Lock, Lrc, RwLock};
55
use rustc_data_structures::unhash::UnhashMap;
66
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, StableCrateId, LOCAL_CRATE};
77
use rustc_hir::definitions::DefPathHash;
@@ -50,8 +50,6 @@ pub struct OnDiskCache<'sess> {
5050
// session.
5151
current_side_effects: Lock<FxHashMap<DepNodeIndex, QuerySideEffects>>,
5252

53-
cnum_map: OnceCell<UnhashMap<StableCrateId, CrateNum>>,
54-
5553
source_map: &'sess SourceMap,
5654
file_index_to_stable_id: FxHashMap<SourceFileIndex, EncodedSourceFileId>,
5755

@@ -139,8 +137,8 @@ struct EncodedSourceFileId {
139137
}
140138

141139
impl EncodedSourceFileId {
142-
fn translate(&self, cnum_map: &UnhashMap<StableCrateId, CrateNum>) -> StableSourceFileId {
143-
let cnum = cnum_map[&self.stable_crate_id];
140+
fn translate(&self, tcx: TyCtxt<'_>) -> StableSourceFileId {
141+
let cnum = tcx.stable_crate_id_to_crate_num(self.stable_crate_id);
144142
StableSourceFileId { file_name_hash: self.file_name_hash, cnum }
145143
}
146144

@@ -180,7 +178,6 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> {
180178
serialized_data: RwLock::new(Some(data)),
181179
file_index_to_stable_id: footer.file_index_to_stable_id,
182180
file_index_to_file: Default::default(),
183-
cnum_map: OnceCell::new(),
184181
source_map: sess.source_map(),
185182
current_side_effects: Default::default(),
186183
query_result_index: footer.query_result_index.into_iter().collect(),
@@ -198,7 +195,6 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> {
198195
serialized_data: RwLock::new(None),
199196
file_index_to_stable_id: Default::default(),
200197
file_index_to_file: Default::default(),
201-
cnum_map: OnceCell::new(),
202198
source_map,
203199
current_side_effects: Default::default(),
204200
query_result_index: Default::default(),
@@ -466,14 +462,11 @@ impl<'sess> OnDiskCache<'sess> {
466462
where
467463
T: Decodable<CacheDecoder<'a, 'tcx>>,
468464
{
469-
let cnum_map = self.cnum_map.get_or_init(|| Self::compute_cnum_map(tcx));
470-
471465
let serialized_data = self.serialized_data.read();
472466
let mut decoder = CacheDecoder {
473467
tcx,
474468
opaque: opaque::Decoder::new(serialized_data.as_deref().unwrap_or(&[]), pos.to_usize()),
475469
source_map: self.source_map,
476-
cnum_map,
477470
file_index_to_file: &self.file_index_to_file,
478471
file_index_to_stable_id: &self.file_index_to_stable_id,
479472
alloc_decoding_session: self.alloc_decoding_state.new_decoding_session(),
@@ -484,23 +477,6 @@ impl<'sess> OnDiskCache<'sess> {
484477
};
485478
f(&mut decoder)
486479
}
487-
488-
// This function builds mapping from previous-session-`CrateNum` to
489-
// current-session-`CrateNum`. There might be `CrateNum`s from the previous
490-
// `Session` that don't occur in the current one. For these, the mapping
491-
// maps to None.
492-
fn compute_cnum_map(tcx: TyCtxt<'_>) -> UnhashMap<StableCrateId, CrateNum> {
493-
tcx.dep_graph.with_ignore(|| {
494-
tcx.crates(())
495-
.iter()
496-
.chain(std::iter::once(&LOCAL_CRATE))
497-
.map(|&cnum| {
498-
let hash = tcx.def_path_hash(cnum.as_def_id()).stable_crate_id();
499-
(hash, cnum)
500-
})
501-
.collect()
502-
})
503-
}
504480
}
505481

506482
//- DECODING -------------------------------------------------------------------
@@ -512,7 +488,6 @@ pub struct CacheDecoder<'a, 'tcx> {
512488
tcx: TyCtxt<'tcx>,
513489
opaque: opaque::Decoder<'a>,
514490
source_map: &'a SourceMap,
515-
cnum_map: &'a UnhashMap<StableCrateId, CrateNum>,
516491
file_index_to_file: &'a Lock<FxHashMap<SourceFileIndex, Lrc<SourceFile>>>,
517492
file_index_to_stable_id: &'a FxHashMap<SourceFileIndex, EncodedSourceFileId>,
518493
alloc_decoding_session: AllocDecodingSession<'a>,
@@ -525,18 +500,18 @@ pub struct CacheDecoder<'a, 'tcx> {
525500
impl<'a, 'tcx> CacheDecoder<'a, 'tcx> {
526501
fn file_index_to_file(&self, index: SourceFileIndex) -> Lrc<SourceFile> {
527502
let CacheDecoder {
503+
tcx,
528504
ref file_index_to_file,
529505
ref file_index_to_stable_id,
530506
ref source_map,
531-
ref cnum_map,
532507
..
533508
} = *self;
534509

535510
file_index_to_file
536511
.borrow_mut()
537512
.entry(index)
538513
.or_insert_with(|| {
539-
let stable_id = file_index_to_stable_id[&index].translate(cnum_map);
514+
let stable_id = file_index_to_stable_id[&index].translate(tcx);
540515
source_map
541516
.source_file_by_stable_id(stable_id)
542517
.expect("failed to lookup `SourceFile` in new context")
@@ -678,7 +653,7 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for ExpnId {
678653
return Ok(expn_id);
679654
}
680655

681-
let krate = decoder.cnum_map[&hash.stable_crate_id()];
656+
let krate = decoder.tcx.stable_crate_id_to_crate_num(hash.stable_crate_id());
682657

683658
let expn_id = if krate == LOCAL_CRATE {
684659
// We look up the position of the associated `ExpnData` and decode it.
@@ -751,7 +726,7 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for Span {
751726
impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for CrateNum {
752727
fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
753728
let stable_id = StableCrateId::decode(d)?;
754-
let cnum = d.cnum_map[&stable_id];
729+
let cnum = d.tcx.stable_crate_id_to_crate_num(stable_id);
755730
Ok(cnum)
756731
}
757732
}

0 commit comments

Comments
 (0)