diff options
Diffstat (limited to 'compiler/rustc_metadata/src/creader.rs')
-rw-r--r-- | compiler/rustc_metadata/src/creader.rs | 46 |
1 files changed, 35 insertions, 11 deletions
diff --git a/compiler/rustc_metadata/src/creader.rs b/compiler/rustc_metadata/src/creader.rs index 23aceca06..aaf72ab94 100644 --- a/compiler/rustc_metadata/src/creader.rs +++ b/compiler/rustc_metadata/src/creader.rs @@ -4,7 +4,7 @@ use crate::errors; use crate::locator::{CrateError, CrateLocator, CratePaths}; use crate::rmeta::{CrateDep, CrateMetadata, CrateNumMap, CrateRoot, MetadataBlob}; -use rustc_ast::expand::allocator::AllocatorKind; +use rustc_ast::expand::allocator::{alloc_error_handler_name, global_fn_name, AllocatorKind}; use rustc_ast::{self as ast, *}; use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::svh::Svh; @@ -12,7 +12,7 @@ use rustc_data_structures::sync::{MappedReadGuard, MappedWriteGuard, ReadGuard, use rustc_expand::base::SyntaxExtension; use rustc_hir::def_id::{CrateNum, LocalDefId, StableCrateId, StableCrateIdMap, LOCAL_CRATE}; use rustc_hir::definitions::Definitions; -use rustc_index::vec::IndexVec; +use rustc_index::IndexVec; use rustc_middle::ty::TyCtxt; use rustc_session::config::{self, CrateType, ExternLocation}; use rustc_session::cstore::ExternCrateSource; @@ -27,6 +27,7 @@ use rustc_span::{Span, DUMMY_SP}; use rustc_target::spec::{PanicStrategy, TargetTriple}; use proc_macro::bridge::client::ProcMacro; +use std::error::Error; use std::ops::Fn; use std::path::Path; use std::time::Duration; @@ -147,11 +148,15 @@ impl CStore { assert_eq!(self.metas.len(), self.stable_crate_ids.len()); let num = CrateNum::new(self.stable_crate_ids.len()); if let Some(&existing) = self.stable_crate_ids.get(&root.stable_crate_id()) { - let crate_name0 = root.name(); - if let Some(crate_name1) = self.metas[existing].as_ref().map(|data| data.name()) { + // Check for (potential) conflicts with the local crate + if existing == LOCAL_CRATE { + Err(CrateError::SymbolConflictsCurrent(root.name())) + } else if let Some(crate_name1) = self.metas[existing].as_ref().map(|data| data.name()) + { + let crate_name0 = root.name(); Err(CrateError::StableCrateIdCollision(crate_name0, crate_name1)) } else { - Err(CrateError::SymbolConflictsCurrent(crate_name0)) + Err(CrateError::NotFound(root.name())) } } else { self.metas.push(None); @@ -368,7 +373,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> { let host_hash = host_lib.as_ref().map(|lib| lib.metadata.get_root().hash()); let private_dep = - self.sess.opts.externs.get(name.as_str()).map_or(false, |e| e.is_private_dep); + self.sess.opts.externs.get(name.as_str()).is_some_and(|e| e.is_private_dep); // Claim this crate number and cache it let cnum = self.cstore.intern_stable_crate_id(&crate_root)?; @@ -864,6 +869,17 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> { } } + fn inject_forced_externs(&mut self) { + for (name, entry) in self.sess.opts.externs.iter() { + if entry.force { + let name_interned = Symbol::intern(name); + if !self.used_extern_options.contains(&name_interned) { + self.resolve_crate(name_interned, DUMMY_SP, CrateDepKind::Explicit); + } + } + } + } + fn inject_dependency_if( &self, krate: CrateNum, @@ -912,7 +928,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> { // Don't worry about pathless `--extern foo` sysroot references continue; } - if entry.nounused_dep { + if entry.nounused_dep || entry.force { // We're not worried about this one continue; } @@ -931,7 +947,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> { lint::builtin::UNUSED_CRATE_DEPENDENCIES, span, ast::CRATE_NODE_ID, - &format!( + format!( "external crate `{}` unused in `{}`: remove the dependency or add `use {} as _;`", name, self.tcx.crate_name(LOCAL_CRATE), @@ -941,6 +957,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> { } pub fn postprocess(&mut self, krate: &ast::Crate) { + self.inject_forced_externs(); self.inject_profiler_runtime(krate); self.inject_allocator_crate(krate); self.inject_panic_runtime(krate); @@ -1031,7 +1048,7 @@ fn global_allocator_spans(krate: &ast::Crate) -> Vec<Span> { } } - let name = Symbol::intern(&AllocatorKind::Global.fn_name(sym::alloc)); + let name = Symbol::intern(&global_fn_name(sym::alloc)); let mut f = Finder { name, spans: Vec::new() }; visit::walk_crate(&mut f, krate); f.spans @@ -1053,7 +1070,7 @@ fn alloc_error_handler_spans(krate: &ast::Crate) -> Vec<Span> { } } - let name = Symbol::intern(&AllocatorKind::Global.fn_name(sym::oom)); + let name = Symbol::intern(alloc_error_handler_name(AllocatorKind::Global)); let mut f = Finder { name, spans: Vec::new() }; visit::walk_crate(&mut f, krate); f.spans @@ -1094,5 +1111,12 @@ fn load_dylib(path: &Path, max_attempts: usize) -> Result<libloading::Library, S } debug!("Failed to load proc-macro `{}` even after {} attempts.", path.display(), max_attempts); - Err(format!("{} (retried {} times)", last_error.unwrap(), max_attempts)) + + let last_error = last_error.unwrap(); + let message = if let Some(src) = last_error.source() { + format!("{last_error} ({src}) (retried {max_attempts} times)") + } else { + format!("{last_error} (retried {max_attempts} times)") + }; + Err(message) } |