From 5c804cf747b9b53d5e8151e914fc45ed18b840a3 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Tue, 21 Feb 2023 11:52:57 +0000 Subject: [PATCH] Replace `CStore::stable_crate_ids` with a fed query. --- compiler/rustc_interface/src/queries.rs | 2 +- compiler/rustc_metadata/src/creader.rs | 27 +++---------------- compiler/rustc_metadata/src/locator.rs | 4 --- .../src/rmeta/decoder/cstore_impl.rs | 4 --- compiler/rustc_middle/src/query/keys.rs | 13 +++++++++ compiler/rustc_middle/src/query/mod.rs | 8 ++++++ compiler/rustc_middle/src/ty/context.rs | 7 +++-- compiler/rustc_session/src/cstore.rs | 1 - 8 files changed, 30 insertions(+), 36 deletions(-) diff --git a/compiler/rustc_interface/src/queries.rs b/compiler/rustc_interface/src/queries.rs index a96cc95a38446..bd03f997b0b33 100644 --- a/compiler/rustc_interface/src/queries.rs +++ b/compiler/rustc_interface/src/queries.rs @@ -192,7 +192,7 @@ impl<'tcx> Queries<'tcx> { let sess = self.session(); - let cstore = RwLock::new(Box::new(CStore::new(sess)) as _); + let cstore = RwLock::new(Box::new(CStore::new()) as _); let definitions = RwLock::new(Definitions::new(sess.local_stable_crate_id())); let source_span = AppendOnlyVec::new(); let _id = source_span.push(krate.spans.inner_span); diff --git a/compiler/rustc_metadata/src/creader.rs b/compiler/rustc_metadata/src/creader.rs index b05626311e8e1..5036cf716f392 100644 --- a/compiler/rustc_metadata/src/creader.rs +++ b/compiler/rustc_metadata/src/creader.rs @@ -6,7 +6,7 @@ use crate::rmeta::{CrateDep, CrateMetadata, CrateNumMap, CrateRoot, MetadataBlob use rustc_ast::expand::allocator::AllocatorKind; use rustc_ast::{self as ast, *}; -use rustc_data_structures::fx::{FxHashMap, FxHashSet}; +use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::svh::Svh; use rustc_data_structures::sync::MappedReadGuard; use rustc_expand::base::SyntaxExtension; @@ -46,10 +46,6 @@ pub struct CStore { /// This crate has a `#[alloc_error_handler]` item. has_alloc_error_handler: bool, - /// This map is used to verify we get no hash conflicts between - /// `StableCrateId` values. - pub(crate) stable_crate_ids: FxHashMap, - /// Unused externs of the crate unused_externs: Vec, } @@ -240,9 +236,7 @@ impl CStore { } } - pub fn new(sess: &Session) -> CStore { - let mut stable_crate_ids = FxHashMap::default(); - stable_crate_ids.insert(sess.local_stable_crate_id(), LOCAL_CRATE); + pub fn new() -> CStore { CStore { // We add an empty entry for LOCAL_CRATE (which maps to zero) in // order to make array indices in `metas` match with the @@ -254,7 +248,6 @@ impl CStore { alloc_error_handler_kind: None, has_global_allocator: false, has_alloc_error_handler: false, - stable_crate_ids, unused_externs: Vec::new(), } } @@ -361,20 +354,6 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> { Ok(()) } - fn verify_no_stable_crate_id_hash_conflicts( - &mut self, - root: &CrateRoot, - cnum: CrateNum, - ) -> Result<(), CrateError> { - if let Some(existing) = self.cstore.stable_crate_ids.insert(root.stable_crate_id(), cnum) { - let crate_name0 = root.name(); - let crate_name1 = self.cstore.get_crate_data(existing).name(); - return Err(CrateError::StableCrateIdCollision(crate_name0, crate_name1)); - } - - Ok(()) - } - fn register_crate( &mut self, host_lib: Option, @@ -435,7 +414,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> { // and dependency resolution and the verification code would produce // ICEs in that case (see #83045). self.verify_no_symbol_conflicts(&crate_root)?; - self.verify_no_stable_crate_id_hash_conflicts(&crate_root, cnum)?; + self.tcx.feed_stable_crate_id(crate_root.stable_crate_id(), cnum); let crate_metadata = CrateMetadata::new( self.sess, diff --git a/compiler/rustc_metadata/src/locator.rs b/compiler/rustc_metadata/src/locator.rs index 755a24253504e..6eff9fb25627c 100644 --- a/compiler/rustc_metadata/src/locator.rs +++ b/compiler/rustc_metadata/src/locator.rs @@ -945,7 +945,6 @@ pub(crate) enum CrateError { ExternLocationNotFile(Symbol, PathBuf), MultipleCandidates(Symbol, CrateFlavor, Vec), SymbolConflictsCurrent(Symbol), - StableCrateIdCollision(Symbol, Symbol), DlOpen(String), DlSym(String), LocatorCombined(CombinedLocatorError), @@ -988,9 +987,6 @@ impl CrateError { CrateError::SymbolConflictsCurrent(root_name) => { sess.emit_err(errors::SymbolConflictsCurrent { span, crate_name: root_name }); } - CrateError::StableCrateIdCollision(crate_name0, crate_name1) => { - sess.emit_err(errors::StableCrateIdCollision { span, crate_name0, crate_name1 }); - } CrateError::DlOpen(s) | CrateError::DlSym(s) => { sess.emit_err(errors::DlError { span, err: s }); } diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index 83a0e833edc1d..a3a87c9953a11 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -635,10 +635,6 @@ impl CrateStore for CStore { self.get_crate_data(cnum).root.stable_crate_id } - fn stable_crate_id_to_crate_num(&self, stable_crate_id: StableCrateId) -> CrateNum { - self.stable_crate_ids[&stable_crate_id] - } - /// Returns the `DefKey` for a given `DefId`. This indicates the /// parent `DefId` as well as some idea of what kind of data the /// `DefId` refers to. diff --git a/compiler/rustc_middle/src/query/keys.rs b/compiler/rustc_middle/src/query/keys.rs index dc02fd53ed02c..46bdcf1bfdc69 100644 --- a/compiler/rustc_middle/src/query/keys.rs +++ b/compiler/rustc_middle/src/query/keys.rs @@ -9,6 +9,7 @@ use crate::ty::{self, layout::TyAndLayout, Ty, TyCtxt}; use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE}; use rustc_hir::hir_id::{HirId, OwnerId}; use rustc_query_system::query::{DefaultCacheSelector, SingleCacheSelector, VecCacheSelector}; +use rustc_session::StableCrateId; use rustc_span::symbol::{Ident, Symbol}; use rustc_span::{Span, DUMMY_SP}; @@ -134,6 +135,18 @@ impl Key for CrateNum { } } +impl Key for StableCrateId { + type CacheSelector = DefaultCacheSelector; + + #[inline(always)] + fn query_crate_is_local(&self) -> bool { + true + } + fn default_span(&self, _: TyCtxt<'_>) -> Span { + DUMMY_SP + } +} + impl Key for OwnerId { type CacheSelector = VecCacheSelector; diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 6a34e5ede1938..3a90d2437b1fc 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -1611,6 +1611,14 @@ rustc_queries! { separate_provide_extern } + /// Maps a StableCrateId to the corresponding CrateNum. This method assumes + /// that the crate in question has already been loaded by the CrateStore. + query stable_crate_id_to_crate_num_raw(_: rustc_span::def_id::StableCrateId) -> CrateNum { + feedable + no_hash + desc { "looking up the CrateNum for a crate's stable hash" } + } + /// Gets the hash for the host proc macro. Used to support -Z dual-proc-macro. query crate_host_hash(_: CrateNum) -> Option { eval_always diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 6bf54fa5417ff..d7cc143fce3a0 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -447,6 +447,9 @@ impl<'tcx> TyCtxt<'tcx> { pub fn feed_local_crate(self) -> TyCtxtFeed<'tcx, CrateNum> { TyCtxtFeed { tcx: self, key: LOCAL_CRATE } } + pub fn feed_stable_crate_id(self, key: rustc_span::def_id::StableCrateId, cnum: CrateNum) { + TyCtxtFeed { tcx: self, key }.stable_crate_id_to_crate_num_raw(cnum); + } } impl<'tcx, KEY: Copy> TyCtxtFeed<'tcx, KEY> { @@ -880,7 +883,7 @@ impl<'tcx> TyCtxt<'tcx> { if stable_crate_id == self.sess.local_stable_crate_id() { LOCAL_CRATE } else { - self.cstore_untracked().stable_crate_id_to_crate_num(stable_crate_id) + self.dep_graph.with_ignore(|| self.stable_crate_id_to_crate_num_raw(stable_crate_id)) } } @@ -900,7 +903,7 @@ impl<'tcx> TyCtxt<'tcx> { // If this is a DefPathHash from an upstream crate, let the CrateStore map // it to a DefId. let cstore = &*self.cstore_untracked(); - let cnum = cstore.stable_crate_id_to_crate_num(stable_crate_id); + let cnum = self.stable_crate_id_to_crate_num(stable_crate_id); cstore.def_path_hash_to_def_id(cnum, hash) } } diff --git a/compiler/rustc_session/src/cstore.rs b/compiler/rustc_session/src/cstore.rs index 868ffdf0f1da0..d3a741e99106f 100644 --- a/compiler/rustc_session/src/cstore.rs +++ b/compiler/rustc_session/src/cstore.rs @@ -230,7 +230,6 @@ pub trait CrateStore: std::fmt::Debug { // incr. comp. uses to identify a CrateNum. fn crate_name(&self, cnum: CrateNum) -> Symbol; fn stable_crate_id(&self, cnum: CrateNum) -> StableCrateId; - fn stable_crate_id_to_crate_num(&self, stable_crate_id: StableCrateId) -> CrateNum; /// Fetch a DefId from a DefPathHash for a foreign crate. fn def_path_hash_to_def_id(&self, cnum: CrateNum, hash: DefPathHash) -> DefId;