diff options
| author | Michael Woerister <michaelwoerister@posteo> | 2021-01-27 14:28:07 +0100 |
|---|---|---|
| committer | Michael Woerister <michaelwoerister@posteo.de> | 2021-02-02 17:40:29 +0100 |
| commit | 22d489be76271e36259ab1c7f76dbd88e6fdca2e (patch) | |
| tree | 92663d83350193797f1d201b16416056aea80491 /compiler/rustc_metadata/src | |
| parent | a3ed564c130ec3f19e933a9ea31faca5a717ce91 (diff) | |
| download | rust-22d489be76271e36259ab1c7f76dbd88e6fdca2e.tar.gz rust-22d489be76271e36259ab1c7f76dbd88e6fdca2e.zip | |
Let a portion of DefPathHash uniquely identify the DefPath's crate.
This allows to directly map from a DefPathHash to the crate it originates from, without constructing side tables to do that mapping. It also allows to reliably and cheaply check for DefPathHash collisions.
Diffstat (limited to 'compiler/rustc_metadata/src')
| -rw-r--r-- | compiler/rustc_metadata/src/creader.rs | 30 | ||||
| -rw-r--r-- | compiler/rustc_metadata/src/locator.rs | 8 | ||||
| -rw-r--r-- | compiler/rustc_metadata/src/rmeta/decoder.rs | 4 | ||||
| -rw-r--r-- | compiler/rustc_metadata/src/rmeta/encoder.rs | 1 | ||||
| -rw-r--r-- | compiler/rustc_metadata/src/rmeta/mod.rs | 3 |
5 files changed, 43 insertions, 3 deletions
diff --git a/compiler/rustc_metadata/src/creader.rs b/compiler/rustc_metadata/src/creader.rs index e3fbd1a2b29..42e4f1f8fff 100644 --- a/compiler/rustc_metadata/src/creader.rs +++ b/compiler/rustc_metadata/src/creader.rs @@ -6,11 +6,11 @@ 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::FxHashSet; +use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::svh::Svh; use rustc_data_structures::sync::Lrc; use rustc_expand::base::SyntaxExtension; -use rustc_hir::def_id::{CrateNum, LocalDefId, LOCAL_CRATE}; +use rustc_hir::def_id::{CrateNum, LocalDefId, StableCrateId, LOCAL_CRATE}; use rustc_hir::definitions::Definitions; use rustc_index::vec::IndexVec; use rustc_middle::middle::cstore::{CrateDepKind, CrateSource, ExternCrate}; @@ -40,6 +40,10 @@ pub struct CStore { allocator_kind: Option<AllocatorKind>, /// This crate has a `#[global_allocator]` item. has_global_allocator: bool, + + /// This map is used to verify we get no hash conflicts between + /// `StableCrateId` values. + stable_crate_ids: FxHashMap<StableCrateId, CrateNum>, } pub struct CrateLoader<'a> { @@ -192,6 +196,11 @@ impl<'a> CrateLoader<'a> { metadata_loader: &'a MetadataLoaderDyn, local_crate_name: &str, ) -> Self { + let local_crate_stable_id = + StableCrateId::new(local_crate_name, sess.local_crate_disambiguator()); + let mut stable_crate_ids = FxHashMap::default(); + stable_crate_ids.insert(local_crate_stable_id, LOCAL_CRATE); + CrateLoader { sess, metadata_loader, @@ -205,6 +214,7 @@ impl<'a> CrateLoader<'a> { injected_panic_runtime: None, allocator_kind: None, has_global_allocator: false, + stable_crate_ids, }, used_extern_options: Default::default(), } @@ -311,6 +321,20 @@ impl<'a> CrateLoader<'a> { res } + 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>, @@ -332,6 +356,8 @@ impl<'a> CrateLoader<'a> { // Claim this crate number and cache it let cnum = self.cstore.alloc_new_crate_num(); + self.verify_no_stable_crate_id_hash_conflicts(&crate_root, cnum)?; + info!( "register crate `{}` (cnum = {}. private_dep = {})", crate_root.name(), diff --git a/compiler/rustc_metadata/src/locator.rs b/compiler/rustc_metadata/src/locator.rs index b66c6cffb1b..a9f58b08f58 100644 --- a/compiler/rustc_metadata/src/locator.rs +++ b/compiler/rustc_metadata/src/locator.rs @@ -888,6 +888,7 @@ crate enum CrateError { MultipleMatchingCrates(Symbol, FxHashMap<Svh, Library>), SymbolConflictsCurrent(Symbol), SymbolConflictsOthers(Symbol), + StableCrateIdCollision(Symbol, Symbol), DlOpen(String), DlSym(String), LocatorCombined(CombinedLocatorError), @@ -970,6 +971,13 @@ impl CrateError { `-C metadata`. This will result in symbol conflicts between the two.", root_name, ), + CrateError::StableCrateIdCollision(crate_name0, crate_name1) => { + let msg = format!( + "found crates (`{}` and `{}`) with colliding StableCrateId values.", + crate_name0, crate_name1 + ); + sess.struct_span_err(span, &msg) + } CrateError::DlOpen(s) | CrateError::DlSym(s) => sess.struct_span_err(span, &s), CrateError::LocatorCombined(locator) => { let crate_name = locator.crate_name; diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index e3c35390798..e9b8388c1c9 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -635,6 +635,10 @@ impl CrateRoot<'_> { self.hash } + crate fn stable_crate_id(&self) -> StableCrateId { + self.stable_crate_id + } + crate fn triple(&self) -> &TargetTriple { &self.triple } diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 3961adaceca..25d70d9768a 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -651,6 +651,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { triple: tcx.sess.opts.target_triple.clone(), hash: tcx.crate_hash(LOCAL_CRATE), disambiguator: tcx.sess.local_crate_disambiguator(), + stable_crate_id: tcx.def_path_hash(LOCAL_CRATE.as_def_id()).stable_crate_id(), panic_strategy: tcx.sess.panic_strategy(), edition: tcx.sess.edition(), has_global_allocator: tcx.has_global_allocator(LOCAL_CRATE), diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index b44c3bfcac6..610528956d0 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -7,7 +7,7 @@ use rustc_data_structures::svh::Svh; use rustc_data_structures::sync::MetadataRef; use rustc_hir as hir; use rustc_hir::def::{CtorKind, DefKind}; -use rustc_hir::def_id::{DefId, DefIndex, DefPathHash}; +use rustc_hir::def_id::{DefId, DefIndex, DefPathHash, StableCrateId}; use rustc_hir::definitions::DefKey; use rustc_hir::lang_items; use rustc_index::{bit_set::FiniteBitSet, vec::IndexVec}; @@ -203,6 +203,7 @@ crate struct CrateRoot<'tcx> { extra_filename: String, hash: Svh, disambiguator: CrateDisambiguator, + stable_crate_id: StableCrateId, panic_strategy: PanicStrategy, edition: Edition, has_global_allocator: bool, |
