Skip to content

Commit 5c804cf

Browse files
committed
Replace CStore::stable_crate_ids with a fed query.
1 parent eb909d8 commit 5c804cf

File tree

8 files changed

+30
-36
lines changed

8 files changed

+30
-36
lines changed

compiler/rustc_interface/src/queries.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ impl<'tcx> Queries<'tcx> {
192192

193193
let sess = self.session();
194194

195-
let cstore = RwLock::new(Box::new(CStore::new(sess)) as _);
195+
let cstore = RwLock::new(Box::new(CStore::new()) as _);
196196
let definitions = RwLock::new(Definitions::new(sess.local_stable_crate_id()));
197197
let source_span = AppendOnlyVec::new();
198198
let _id = source_span.push(krate.spans.inner_span);

compiler/rustc_metadata/src/creader.rs

+3-24
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::rmeta::{CrateDep, CrateMetadata, CrateNumMap, CrateRoot, MetadataBlob
66

77
use rustc_ast::expand::allocator::AllocatorKind;
88
use rustc_ast::{self as ast, *};
9-
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
9+
use rustc_data_structures::fx::FxHashSet;
1010
use rustc_data_structures::svh::Svh;
1111
use rustc_data_structures::sync::MappedReadGuard;
1212
use rustc_expand::base::SyntaxExtension;
@@ -46,10 +46,6 @@ pub struct CStore {
4646
/// This crate has a `#[alloc_error_handler]` item.
4747
has_alloc_error_handler: bool,
4848

49-
/// This map is used to verify we get no hash conflicts between
50-
/// `StableCrateId` values.
51-
pub(crate) stable_crate_ids: FxHashMap<StableCrateId, CrateNum>,
52-
5349
/// Unused externs of the crate
5450
unused_externs: Vec<Symbol>,
5551
}
@@ -240,9 +236,7 @@ impl CStore {
240236
}
241237
}
242238

243-
pub fn new(sess: &Session) -> CStore {
244-
let mut stable_crate_ids = FxHashMap::default();
245-
stable_crate_ids.insert(sess.local_stable_crate_id(), LOCAL_CRATE);
239+
pub fn new() -> CStore {
246240
CStore {
247241
// We add an empty entry for LOCAL_CRATE (which maps to zero) in
248242
// order to make array indices in `metas` match with the
@@ -254,7 +248,6 @@ impl CStore {
254248
alloc_error_handler_kind: None,
255249
has_global_allocator: false,
256250
has_alloc_error_handler: false,
257-
stable_crate_ids,
258251
unused_externs: Vec::new(),
259252
}
260253
}
@@ -361,20 +354,6 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
361354
Ok(())
362355
}
363356

364-
fn verify_no_stable_crate_id_hash_conflicts(
365-
&mut self,
366-
root: &CrateRoot,
367-
cnum: CrateNum,
368-
) -> Result<(), CrateError> {
369-
if let Some(existing) = self.cstore.stable_crate_ids.insert(root.stable_crate_id(), cnum) {
370-
let crate_name0 = root.name();
371-
let crate_name1 = self.cstore.get_crate_data(existing).name();
372-
return Err(CrateError::StableCrateIdCollision(crate_name0, crate_name1));
373-
}
374-
375-
Ok(())
376-
}
377-
378357
fn register_crate(
379358
&mut self,
380359
host_lib: Option<Library>,
@@ -435,7 +414,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
435414
// and dependency resolution and the verification code would produce
436415
// ICEs in that case (see #83045).
437416
self.verify_no_symbol_conflicts(&crate_root)?;
438-
self.verify_no_stable_crate_id_hash_conflicts(&crate_root, cnum)?;
417+
self.tcx.feed_stable_crate_id(crate_root.stable_crate_id(), cnum);
439418

440419
let crate_metadata = CrateMetadata::new(
441420
self.sess,

compiler/rustc_metadata/src/locator.rs

-4
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,6 @@ pub(crate) enum CrateError {
945945
ExternLocationNotFile(Symbol, PathBuf),
946946
MultipleCandidates(Symbol, CrateFlavor, Vec<PathBuf>),
947947
SymbolConflictsCurrent(Symbol),
948-
StableCrateIdCollision(Symbol, Symbol),
949948
DlOpen(String),
950949
DlSym(String),
951950
LocatorCombined(CombinedLocatorError),
@@ -988,9 +987,6 @@ impl CrateError {
988987
CrateError::SymbolConflictsCurrent(root_name) => {
989988
sess.emit_err(errors::SymbolConflictsCurrent { span, crate_name: root_name });
990989
}
991-
CrateError::StableCrateIdCollision(crate_name0, crate_name1) => {
992-
sess.emit_err(errors::StableCrateIdCollision { span, crate_name0, crate_name1 });
993-
}
994990
CrateError::DlOpen(s) | CrateError::DlSym(s) => {
995991
sess.emit_err(errors::DlError { span, err: s });
996992
}

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

-4
Original file line numberDiff line numberDiff line change
@@ -635,10 +635,6 @@ impl CrateStore for CStore {
635635
self.get_crate_data(cnum).root.stable_crate_id
636636
}
637637

638-
fn stable_crate_id_to_crate_num(&self, stable_crate_id: StableCrateId) -> CrateNum {
639-
self.stable_crate_ids[&stable_crate_id]
640-
}
641-
642638
/// Returns the `DefKey` for a given `DefId`. This indicates the
643639
/// parent `DefId` as well as some idea of what kind of data the
644640
/// `DefId` refers to.

compiler/rustc_middle/src/query/keys.rs

+13
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::ty::{self, layout::TyAndLayout, Ty, TyCtxt};
99
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
1010
use rustc_hir::hir_id::{HirId, OwnerId};
1111
use rustc_query_system::query::{DefaultCacheSelector, SingleCacheSelector, VecCacheSelector};
12+
use rustc_session::StableCrateId;
1213
use rustc_span::symbol::{Ident, Symbol};
1314
use rustc_span::{Span, DUMMY_SP};
1415

@@ -134,6 +135,18 @@ impl Key for CrateNum {
134135
}
135136
}
136137

138+
impl Key for StableCrateId {
139+
type CacheSelector = DefaultCacheSelector<Self>;
140+
141+
#[inline(always)]
142+
fn query_crate_is_local(&self) -> bool {
143+
true
144+
}
145+
fn default_span(&self, _: TyCtxt<'_>) -> Span {
146+
DUMMY_SP
147+
}
148+
}
149+
137150
impl Key for OwnerId {
138151
type CacheSelector = VecCacheSelector<Self>;
139152

compiler/rustc_middle/src/query/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1611,6 +1611,14 @@ rustc_queries! {
16111611
separate_provide_extern
16121612
}
16131613

1614+
/// Maps a StableCrateId to the corresponding CrateNum. This method assumes
1615+
/// that the crate in question has already been loaded by the CrateStore.
1616+
query stable_crate_id_to_crate_num_raw(_: rustc_span::def_id::StableCrateId) -> CrateNum {
1617+
feedable
1618+
no_hash
1619+
desc { "looking up the CrateNum for a crate's stable hash" }
1620+
}
1621+
16141622
/// Gets the hash for the host proc macro. Used to support -Z dual-proc-macro.
16151623
query crate_host_hash(_: CrateNum) -> Option<Svh> {
16161624
eval_always

compiler/rustc_middle/src/ty/context.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,9 @@ impl<'tcx> TyCtxt<'tcx> {
447447
pub fn feed_local_crate(self) -> TyCtxtFeed<'tcx, CrateNum> {
448448
TyCtxtFeed { tcx: self, key: LOCAL_CRATE }
449449
}
450+
pub fn feed_stable_crate_id(self, key: rustc_span::def_id::StableCrateId, cnum: CrateNum) {
451+
TyCtxtFeed { tcx: self, key }.stable_crate_id_to_crate_num_raw(cnum);
452+
}
450453
}
451454

452455
impl<'tcx, KEY: Copy> TyCtxtFeed<'tcx, KEY> {
@@ -880,7 +883,7 @@ impl<'tcx> TyCtxt<'tcx> {
880883
if stable_crate_id == self.sess.local_stable_crate_id() {
881884
LOCAL_CRATE
882885
} else {
883-
self.cstore_untracked().stable_crate_id_to_crate_num(stable_crate_id)
886+
self.dep_graph.with_ignore(|| self.stable_crate_id_to_crate_num_raw(stable_crate_id))
884887
}
885888
}
886889

@@ -900,7 +903,7 @@ impl<'tcx> TyCtxt<'tcx> {
900903
// If this is a DefPathHash from an upstream crate, let the CrateStore map
901904
// it to a DefId.
902905
let cstore = &*self.cstore_untracked();
903-
let cnum = cstore.stable_crate_id_to_crate_num(stable_crate_id);
906+
let cnum = self.stable_crate_id_to_crate_num(stable_crate_id);
904907
cstore.def_path_hash_to_def_id(cnum, hash)
905908
}
906909
}

compiler/rustc_session/src/cstore.rs

-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,6 @@ pub trait CrateStore: std::fmt::Debug {
230230
// incr. comp. uses to identify a CrateNum.
231231
fn crate_name(&self, cnum: CrateNum) -> Symbol;
232232
fn stable_crate_id(&self, cnum: CrateNum) -> StableCrateId;
233-
fn stable_crate_id_to_crate_num(&self, stable_crate_id: StableCrateId) -> CrateNum;
234233

235234
/// Fetch a DefId from a DefPathHash for a foreign crate.
236235
fn def_path_hash_to_def_id(&self, cnum: CrateNum, hash: DefPathHash) -> DefId;

0 commit comments

Comments
 (0)