Skip to content

Commit 4bd030e

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 cd6eadd commit 4bd030e

File tree

5 files changed

+36
-32
lines changed

5 files changed

+36
-32
lines changed

compiler/rustc_interface/src/queries.rs

Lines changed: 10 additions & 4 deletions
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

Lines changed: 14 additions & 17 deletions
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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -628,13 +628,6 @@ impl CrateStore for CStore {
628628
self.get_crate_data(cnum).root.stable_crate_id
629629
}
630630

631-
fn stable_crate_id_to_crate_num(&self, stable_crate_id: StableCrateId) -> CrateNum {
632-
*self
633-
.stable_crate_ids
634-
.get(&stable_crate_id)
635-
.unwrap_or_else(|| bug!("uninterned StableCrateId: {stable_crate_id:?}"))
636-
}
637-
638631
/// Returns the `DefKey` for a given `DefId`. This indicates the
639632
/// parent `DefId` as well as some idea of what kind of data the
640633
/// `DefId` refers to.

compiler/rustc_middle/src/ty/context.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,12 @@ impl<'tcx> TyCtxt<'tcx> {
10561056
if stable_crate_id == self.stable_crate_id(LOCAL_CRATE) {
10571057
LOCAL_CRATE
10581058
} else {
1059-
self.cstore_untracked().stable_crate_id_to_crate_num(stable_crate_id)
1059+
*self
1060+
.untracked()
1061+
.stable_crate_ids
1062+
.read()
1063+
.get(&stable_crate_id)
1064+
.unwrap_or_else(|| bug!("uninterned StableCrateId: {stable_crate_id:?}"))
10601065
}
10611066
}
10621067

@@ -1076,7 +1081,7 @@ impl<'tcx> TyCtxt<'tcx> {
10761081
// If this is a DefPathHash from an upstream crate, let the CrateStore map
10771082
// it to a DefId.
10781083
let cstore = &*self.cstore_untracked();
1079-
let cnum = cstore.stable_crate_id_to_crate_num(stable_crate_id);
1084+
let cnum = self.stable_crate_id_to_crate_num(stable_crate_id);
10801085
cstore.def_path_hash_to_def_id(cnum, hash)
10811086
}
10821087
}

compiler/rustc_session/src/cstore.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use crate::utils::NativeLibKind;
77
use crate::Session;
88
use rustc_ast as ast;
99
use rustc_data_structures::sync::{self, AppendOnlyIndexVec, FreezeLock};
10-
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, StableCrateId, LOCAL_CRATE};
10+
use rustc_hir::def_id::{
11+
CrateNum, DefId, LocalDefId, StableCrateId, StableCrateIdMap, LOCAL_CRATE,
12+
};
1113
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash, Definitions};
1214
use rustc_span::hygiene::{ExpnHash, ExpnId};
1315
use rustc_span::symbol::Symbol;
@@ -219,7 +221,6 @@ pub trait CrateStore: std::fmt::Debug {
219221
// incr. comp. uses to identify a CrateNum.
220222
fn crate_name(&self, cnum: CrateNum) -> Symbol;
221223
fn stable_crate_id(&self, cnum: CrateNum) -> StableCrateId;
222-
fn stable_crate_id_to_crate_num(&self, stable_crate_id: StableCrateId) -> CrateNum;
223224

224225
/// Fetch a DefId from a DefPathHash for a foreign crate.
225226
fn def_path_hash_to_def_id(&self, cnum: CrateNum, hash: DefPathHash) -> DefId;
@@ -245,4 +246,6 @@ pub struct Untracked {
245246
/// Reference span for definitions.
246247
pub source_span: AppendOnlyIndexVec<LocalDefId, Span>,
247248
pub definitions: FreezeLock<Definitions>,
249+
/// The interned [StableCrateId]s.
250+
pub stable_crate_ids: FreezeLock<StableCrateIdMap>,
248251
}

0 commit comments

Comments
 (0)