Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace CStore::stable_crate_ids with a fed query. #108390

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_interface/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
27 changes: 3 additions & 24 deletions compiler/rustc_metadata/src/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<StableCrateId, CrateNum>,

/// Unused externs of the crate
unused_externs: Vec<Symbol>,
}
Expand Down Expand Up @@ -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
Expand All @@ -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(),
}
}
Expand Down Expand Up @@ -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<Library>,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 0 additions & 4 deletions compiler/rustc_metadata/src/locator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,6 @@ pub(crate) enum CrateError {
ExternLocationNotFile(Symbol, PathBuf),
MultipleCandidates(Symbol, CrateFlavor, Vec<PathBuf>),
SymbolConflictsCurrent(Symbol),
StableCrateIdCollision(Symbol, Symbol),
DlOpen(String),
DlSym(String),
LocatorCombined(CombinedLocatorError),
Expand Down Expand Up @@ -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 });
}
Expand Down
4 changes: 0 additions & 4 deletions compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions compiler/rustc_middle/src/query/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -134,6 +135,18 @@ impl Key for CrateNum {
}
}

impl Key for StableCrateId {
type CacheSelector = DefaultCacheSelector<Self>;

#[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<Self>;

Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Svh> {
eval_always
Expand Down
7 changes: 5 additions & 2 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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))
}
}

Expand All @@ -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)
}
}
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_session/src/cstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down