Skip to content

Commit fbc9b94

Browse files
committed
Move stable_crate_ids from CrateStore to Untracked
This way it's like `Definitions`, which creates `DefId`s by interning `DefPathData`s, but for interning stable crate hashes
1 parent 10be745 commit fbc9b94

File tree

5 files changed

+42
-32
lines changed

5 files changed

+42
-32
lines changed

compiler/rustc_interface/src/queries.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_codegen_ssa::CodegenResults;
88
use rustc_data_structures::steal::Steal;
99
use rustc_data_structures::svh::Svh;
1010
use rustc_data_structures::sync::{AppendOnlyIndexVec, FreezeLock, OnceLock, WorkerLocal};
11-
use rustc_hir::def_id::{StableCrateId, LOCAL_CRATE};
11+
use rustc_hir::def_id::{StableCrateId, StableCrateIdMap, LOCAL_CRATE};
1212
use rustc_hir::definitions::Definitions;
1313
use rustc_incremental::setup_dep_graph;
1414
use rustc_metadata::creader::CStore;
@@ -140,11 +140,17 @@ impl<'tcx> Queries<'tcx> {
140140

141141
let cstore = FreezeLock::new(Box::new(CStore::new(
142142
self.compiler.codegen_backend.metadata_loader(),
143-
stable_crate_id,
144143
)) as _);
145144
let definitions = FreezeLock::new(Definitions::new(stable_crate_id));
146-
let untracked =
147-
Untracked { cstore, source_span: AppendOnlyIndexVec::new(), definitions };
145+
146+
let mut stable_crate_ids = StableCrateIdMap::default();
147+
stable_crate_ids.insert(stable_crate_id, LOCAL_CRATE);
148+
let untracked = Untracked {
149+
cstore,
150+
source_span: AppendOnlyIndexVec::new(),
151+
definitions,
152+
stable_crate_ids: FreezeLock::new(stable_crate_ids),
153+
};
148154

149155
let qcx = passes::create_global_ctxt(
150156
self.compiler,

compiler/rustc_metadata/src/creader.rs

+14-17
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_data_structures::sync::{self, FreezeReadGuard, FreezeWriteGuard};
1313
use rustc_errors::DiagCtxt;
1414
use rustc_expand::base::SyntaxExtension;
1515
use rustc_fs_util::try_canonicalize;
16-
use rustc_hir::def_id::{CrateNum, LocalDefId, StableCrateId, StableCrateIdMap, LOCAL_CRATE};
16+
use rustc_hir::def_id::{CrateNum, LocalDefId, StableCrateId, LOCAL_CRATE};
1717
use rustc_hir::definitions::Definitions;
1818
use rustc_index::IndexVec;
1919
use rustc_middle::ty::TyCtxt;
@@ -62,9 +62,6 @@ pub struct CStore {
6262
/// This crate has a `#[alloc_error_handler]` item.
6363
has_alloc_error_handler: bool,
6464

65-
/// The interned [StableCrateId]s.
66-
pub(crate) stable_crate_ids: StableCrateIdMap,
67-
6865
/// Unused externs of the crate
6966
unused_externs: Vec<Symbol>,
7067
}
@@ -165,9 +162,15 @@ impl CStore {
165162
})
166163
}
167164

168-
fn intern_stable_crate_id(&mut self, root: &CrateRoot) -> Result<CrateNum, CrateError> {
169-
assert_eq!(self.metas.len(), self.stable_crate_ids.len());
170-
if let Some(&existing) = self.stable_crate_ids.get(&root.stable_crate_id()) {
165+
fn intern_stable_crate_id<'tcx>(
166+
&mut self,
167+
root: &CrateRoot,
168+
tcx: TyCtxt<'tcx>,
169+
) -> Result<CrateNum, CrateError> {
170+
assert_eq!(self.metas.len(), tcx.untracked().stable_crate_ids.read().len());
171+
if let Some(&existing) =
172+
tcx.untracked().stable_crate_ids.read().get(&root.stable_crate_id())
173+
{
171174
// Check for (potential) conflicts with the local crate
172175
if existing == LOCAL_CRATE {
173176
Err(CrateError::SymbolConflictsCurrent(root.name()))
@@ -180,8 +183,8 @@ impl CStore {
180183
}
181184
} else {
182185
self.metas.push(None);
183-
let num = CrateNum::new(self.stable_crate_ids.len());
184-
self.stable_crate_ids.insert(root.stable_crate_id(), num);
186+
let num = CrateNum::new(tcx.untracked().stable_crate_ids.read().len());
187+
tcx.untracked().stable_crate_ids.write().insert(root.stable_crate_id(), num);
185188
Ok(num)
186189
}
187190
}
@@ -289,12 +292,7 @@ impl CStore {
289292
}
290293
}
291294

292-
pub fn new(
293-
metadata_loader: Box<MetadataLoaderDyn>,
294-
local_stable_crate_id: StableCrateId,
295-
) -> CStore {
296-
let mut stable_crate_ids = StableCrateIdMap::default();
297-
stable_crate_ids.insert(local_stable_crate_id, LOCAL_CRATE);
295+
pub fn new(metadata_loader: Box<MetadataLoaderDyn>) -> CStore {
298296
CStore {
299297
metadata_loader,
300298
// We add an empty entry for LOCAL_CRATE (which maps to zero) in
@@ -307,7 +305,6 @@ impl CStore {
307305
alloc_error_handler_kind: None,
308306
has_global_allocator: false,
309307
has_alloc_error_handler: false,
310-
stable_crate_ids,
311308
unused_externs: Vec::new(),
312309
}
313310
}
@@ -416,7 +413,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
416413
let private_dep = self.is_private_dep(name.as_str(), private_dep);
417414

418415
// Claim this crate number and cache it
419-
let cnum = self.cstore.intern_stable_crate_id(&crate_root)?;
416+
let cnum = self.cstore.intern_stable_crate_id(&crate_root, self.tcx)?;
420417

421418
info!(
422419
"register crate `{}` (cnum = {}. private_dep = {})",

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -629,13 +629,6 @@ impl CrateStore for CStore {
629629
self.get_crate_data(cnum).root.stable_crate_id
630630
}
631631

632-
fn stable_crate_id_to_crate_num(&self, stable_crate_id: StableCrateId) -> CrateNum {
633-
*self
634-
.stable_crate_ids
635-
.get(&stable_crate_id)
636-
.unwrap_or_else(|| bug!("uninterned StableCrateId: {stable_crate_id:?}"))
637-
}
638-
639632
/// Returns the `DefKey` for a given `DefId`. This indicates the
640633
/// parent `DefId` as well as some idea of what kind of data the
641634
/// `DefId` refers to.
@@ -657,7 +650,13 @@ fn provide_cstore_hooks(providers: &mut Providers) {
657650
// If this is a DefPathHash from an upstream crate, let the CrateStore map
658651
// it to a DefId.
659652
let cstore = CStore::from_tcx(tcx.tcx);
660-
let cnum = cstore.stable_crate_id_to_crate_num(stable_crate_id);
653+
let cnum = *tcx
654+
.untracked()
655+
.stable_crate_ids
656+
.read()
657+
.get(&stable_crate_id)
658+
.unwrap_or_else(|| bug!("uninterned StableCrateId: {stable_crate_id:?}"));
659+
assert_ne!(cnum, LOCAL_CRATE);
661660
let def_index = cstore.get_crate_data(cnum).def_path_hash_to_def_index(hash);
662661
DefId { krate: cnum, index: def_index }
663662
};

compiler/rustc_middle/src/ty/context.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,12 @@ impl<'tcx> TyCtxt<'tcx> {
11401140
if stable_crate_id == self.stable_crate_id(LOCAL_CRATE) {
11411141
LOCAL_CRATE
11421142
} else {
1143-
self.cstore_untracked().stable_crate_id_to_crate_num(stable_crate_id)
1143+
*self
1144+
.untracked()
1145+
.stable_crate_ids
1146+
.read()
1147+
.get(&stable_crate_id)
1148+
.unwrap_or_else(|| bug!("uninterned StableCrateId: {stable_crate_id:?}"))
11441149
}
11451150
}
11461151

compiler/rustc_session/src/cstore.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ use crate::search_paths::PathKind;
66
use crate::utils::NativeLibKind;
77
use rustc_ast as ast;
88
use rustc_data_structures::sync::{self, AppendOnlyIndexVec, FreezeLock};
9-
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, StableCrateId, LOCAL_CRATE};
9+
use rustc_hir::def_id::{
10+
CrateNum, DefId, LocalDefId, StableCrateId, StableCrateIdMap, LOCAL_CRATE,
11+
};
1012
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash, Definitions};
1113
use rustc_span::symbol::Symbol;
1214
use rustc_span::Span;
@@ -217,7 +219,6 @@ pub trait CrateStore: std::fmt::Debug {
217219
// incr. comp. uses to identify a CrateNum.
218220
fn crate_name(&self, cnum: CrateNum) -> Symbol;
219221
fn stable_crate_id(&self, cnum: CrateNum) -> StableCrateId;
220-
fn stable_crate_id_to_crate_num(&self, stable_crate_id: StableCrateId) -> CrateNum;
221222
}
222223

223224
pub type CrateStoreDyn = dyn CrateStore + sync::DynSync + sync::DynSend;
@@ -227,4 +228,6 @@ pub struct Untracked {
227228
/// Reference span for definitions.
228229
pub source_span: AppendOnlyIndexVec<LocalDefId, Span>,
229230
pub definitions: FreezeLock<Definitions>,
231+
/// The interned [StableCrateId]s.
232+
pub stable_crate_ids: FreezeLock<StableCrateIdMap>,
230233
}

0 commit comments

Comments
 (0)