use crate::rmeta::{rustc_version, MetadataBlob, METADATA_HEADER};
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
+use rustc_data_structures::memmap::Mmap;
use rustc_data_structures::owning_ref::OwningRef;
use rustc_data_structures::svh::Svh;
use rustc_data_structures::sync::MetadataRef;
-use rustc_errors::struct_span_err;
-use rustc_middle::middle::cstore::{CrateSource, MetadataLoader};
+use rustc_errors::{struct_span_err, FatalError};
use rustc_session::config::{self, CrateType};
-use rustc_session::filesearch::{FileDoesntMatch, FileMatches, FileSearch};
+use rustc_session::cstore::{CrateSource, MetadataLoader};
+use rustc_session::filesearch::FileSearch;
use rustc_session::search_paths::PathKind;
use rustc_session::utils::CanonicalizedPath;
-use rustc_session::{CrateDisambiguator, Session};
+use rustc_session::Session;
use rustc_span::symbol::{sym, Symbol};
use rustc_span::Span;
use rustc_target::spec::{Target, TargetTriple};
use snap::read::FrameDecoder;
+use std::fmt::Write as _;
use std::io::{Read, Result as IoResult, Write};
-use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::{cmp, fmt, fs};
-use tracing::{debug, info, warn};
+use tracing::{debug, info};
#[derive(Clone)]
-crate struct CrateLocator<'a> {
+pub(crate) struct CrateLocator<'a> {
// Immutable per-session configuration.
- sess: &'a Session,
+ only_needs_metadata: bool,
+ sysroot: &'a Path,
metadata_loader: &'a dyn MetadataLoader,
// Immutable per-search configuration.
crate_name: Symbol,
exact_paths: Vec<CanonicalizedPath>,
pub hash: Option<Svh>,
- pub host_hash: Option<Svh>,
extra_filename: Option<&'a str>,
pub target: &'a Target,
pub triple: TargetTriple,
pub filesearch: FileSearch<'a>,
- root: Option<&'a CratePaths>,
- pub is_proc_macro: Option<bool>,
+ pub is_proc_macro: bool,
// Mutable in-progress state or output.
- rejected_via_hash: Vec<CrateMismatch>,
- rejected_via_triple: Vec<CrateMismatch>,
- rejected_via_kind: Vec<CrateMismatch>,
- rejected_via_version: Vec<CrateMismatch>,
- rejected_via_filename: Vec<CrateMismatch>,
+ crate_rejections: CrateRejections,
}
#[derive(Clone)]
-crate struct CratePaths {
+pub(crate) struct CratePaths {
name: Symbol,
source: CrateSource,
}
impl CratePaths {
- crate fn new(name: Symbol, source: CrateSource) -> CratePaths {
+ pub(crate) fn new(name: Symbol, source: CrateSource) -> CratePaths {
CratePaths { name, source }
}
}
#[derive(Copy, Clone, PartialEq)]
-crate enum CrateFlavor {
+pub(crate) enum CrateFlavor {
Rlib,
Rmeta,
Dylib,
}
impl<'a> CrateLocator<'a> {
- crate fn new(
+ pub(crate) fn new(
sess: &'a Session,
metadata_loader: &'a dyn MetadataLoader,
crate_name: Symbol,
hash: Option<Svh>,
- host_hash: Option<Svh>,
extra_filename: Option<&'a str>,
is_host: bool,
path_kind: PathKind,
- root: Option<&'a CratePaths>,
- is_proc_macro: Option<bool>,
) -> CrateLocator<'a> {
+ // The all loop is because `--crate-type=rlib --crate-type=rlib` is
+ // legal and produces both inside this type.
+ let is_rlib = sess.crate_types().iter().all(|c| *c == CrateType::Rlib);
+ let needs_object_code = sess.opts.output_types.should_codegen();
+ // If we're producing an rlib, then we don't need object code.
+ // Or, if we're not producing object code, then we don't need it either
+ // (e.g., if we're a cdylib but emitting just metadata).
+ let only_needs_metadata = is_rlib || !needs_object_code;
+
CrateLocator {
- sess,
+ only_needs_metadata,
+ sysroot: &sess.sysroot,
metadata_loader,
crate_name,
exact_paths: if hash.is_none() {
sess.opts
.externs
- .get(&crate_name.as_str())
+ .get(crate_name.as_str())
.into_iter()
.filter_map(|entry| entry.files())
.flatten()
Vec::new()
},
hash,
- host_hash,
extra_filename,
target: if is_host { &sess.host } else { &sess.target },
triple: if is_host {
} else {
sess.target_filesearch(path_kind)
},
- root,
- is_proc_macro,
- rejected_via_hash: Vec::new(),
- rejected_via_triple: Vec::new(),
- rejected_via_kind: Vec::new(),
- rejected_via_version: Vec::new(),
- rejected_via_filename: Vec::new(),
+ is_proc_macro: false,
+ crate_rejections: CrateRejections::default(),
}
}
- crate fn reset(&mut self) {
- self.rejected_via_hash.clear();
- self.rejected_via_triple.clear();
- self.rejected_via_kind.clear();
- self.rejected_via_version.clear();
- self.rejected_via_filename.clear();
+ pub(crate) fn reset(&mut self) {
+ self.crate_rejections.via_hash.clear();
+ self.crate_rejections.via_triple.clear();
+ self.crate_rejections.via_kind.clear();
+ self.crate_rejections.via_version.clear();
+ self.crate_rejections.via_filename.clear();
+ self.crate_rejections.via_invalid.clear();
}
- crate fn maybe_load_library_crate(&mut self) -> Result<Option<Library>, CrateError> {
+ pub(crate) fn maybe_load_library_crate(&mut self) -> Result<Option<Library>, CrateError> {
if !self.exact_paths.is_empty() {
return self.find_commandline_library();
}
extra_prefix: &str,
seen_paths: &mut FxHashSet<PathBuf>,
) -> Result<Option<Library>, CrateError> {
- // want: crate_name.dir_part() + prefix + crate_name.file_part + "-"
- let dylib_prefix = format!("{}{}{}", self.target.dll_prefix, self.crate_name, extra_prefix);
- let rlib_prefix = format!("lib{}{}", self.crate_name, extra_prefix);
+ let rmeta_prefix = &format!("lib{}{}", self.crate_name, extra_prefix);
+ let rlib_prefix = rmeta_prefix;
+ let dylib_prefix =
+ &format!("{}{}{}", self.target.dll_prefix, self.crate_name, extra_prefix);
let staticlib_prefix =
- format!("{}{}{}", self.target.staticlib_prefix, self.crate_name, extra_prefix);
+ &format!("{}{}{}", self.target.staticlib_prefix, self.crate_name, extra_prefix);
+
+ let rmeta_suffix = ".rmeta";
+ let rlib_suffix = ".rlib";
+ let dylib_suffix = &self.target.dll_suffix;
+ let staticlib_suffix = &self.target.staticlib_suffix;
let mut candidates: FxHashMap<_, (FxHashMap<_, _>, FxHashMap<_, _>, FxHashMap<_, _>)> =
Default::default();
- let mut staticlibs = vec![];
// First, find all possible candidate rlibs and dylibs purely based on
// the name of the files themselves. We're trying to match against an
// of the crate id (path/name/id).
//
// The goal of this step is to look at as little metadata as possible.
- self.filesearch.search(|spf, kind| {
- let file = match &spf.file_name_str {
- None => return FileDoesntMatch,
- Some(file) => file,
- };
- let (hash, found_kind) = if file.starts_with(&rlib_prefix) && file.ends_with(".rlib") {
- (&file[(rlib_prefix.len())..(file.len() - ".rlib".len())], CrateFlavor::Rlib)
- } else if file.starts_with(&rlib_prefix) && file.ends_with(".rmeta") {
- (&file[(rlib_prefix.len())..(file.len() - ".rmeta".len())], CrateFlavor::Rmeta)
- } else if file.starts_with(&dylib_prefix) && file.ends_with(&self.target.dll_suffix) {
- (
- &file[(dylib_prefix.len())..(file.len() - self.target.dll_suffix.len())],
- CrateFlavor::Dylib,
- )
- } else {
- if file.starts_with(&staticlib_prefix)
- && file.ends_with(&self.target.staticlib_suffix)
- {
- staticlibs
- .push(CrateMismatch { path: spf.path.clone(), got: "static".to_string() });
- }
- return FileDoesntMatch;
- };
+ // Unfortunately, the prefix-based matching sometimes is over-eager.
+ // E.g. if `rlib_suffix` is `libstd` it'll match the file
+ // `libstd_detect-8d6701fb958915ad.rlib` (incorrect) as well as
+ // `libstd-f3ab5b1dea981f17.rlib` (correct). But this is hard to avoid
+ // given that `extra_filename` comes from the `-C extra-filename`
+ // option and thus can be anything, and the incorrect match will be
+ // handled safely in `extract_one`.
+ for search_path in self.filesearch.search_paths() {
+ debug!("searching {}", search_path.dir.display());
+ for spf in search_path.files.iter() {
+ debug!("testing {}", spf.path.display());
- info!("lib candidate: {}", spf.path.display());
+ let f = &spf.file_name_str;
+ let (hash, kind) = if f.starts_with(rlib_prefix) && f.ends_with(rlib_suffix) {
+ (&f[rlib_prefix.len()..(f.len() - rlib_suffix.len())], CrateFlavor::Rlib)
+ } else if f.starts_with(rmeta_prefix) && f.ends_with(rmeta_suffix) {
+ (&f[rmeta_prefix.len()..(f.len() - rmeta_suffix.len())], CrateFlavor::Rmeta)
+ } else if f.starts_with(dylib_prefix) && f.ends_with(dylib_suffix.as_ref()) {
+ (&f[dylib_prefix.len()..(f.len() - dylib_suffix.len())], CrateFlavor::Dylib)
+ } else {
+ if f.starts_with(staticlib_prefix) && f.ends_with(staticlib_suffix.as_ref()) {
+ self.crate_rejections.via_kind.push(CrateMismatch {
+ path: spf.path.clone(),
+ got: "static".to_string(),
+ });
+ }
+ continue;
+ };
- let (rlibs, rmetas, dylibs) = candidates.entry(hash.to_string()).or_default();
- let path = fs::canonicalize(&spf.path).unwrap_or_else(|_| spf.path.clone());
- if seen_paths.contains(&path) {
- return FileDoesntMatch;
- };
- seen_paths.insert(path.clone());
- match found_kind {
- CrateFlavor::Rlib => rlibs.insert(path, kind),
- CrateFlavor::Rmeta => rmetas.insert(path, kind),
- CrateFlavor::Dylib => dylibs.insert(path, kind),
- };
- FileMatches
- });
- self.rejected_via_kind.extend(staticlibs);
+ info!("lib candidate: {}", spf.path.display());
+
+ let (rlibs, rmetas, dylibs) = candidates.entry(hash.to_string()).or_default();
+ let path = fs::canonicalize(&spf.path).unwrap_or_else(|_| spf.path.clone());
+ if seen_paths.contains(&path) {
+ continue;
+ };
+ seen_paths.insert(path.clone());
+ match kind {
+ CrateFlavor::Rlib => rlibs.insert(path, search_path.kind),
+ CrateFlavor::Rmeta => rmetas.insert(path, search_path.kind),
+ CrateFlavor::Dylib => dylibs.insert(path, search_path.kind),
+ };
+ }
+ }
// We have now collected all known libraries into a set of candidates
// keyed of the filename hash listed. For each filename, we also have a
}
fn needs_crate_flavor(&self, flavor: CrateFlavor) -> bool {
- if flavor == CrateFlavor::Dylib && self.is_proc_macro == Some(true) {
+ if flavor == CrateFlavor::Dylib && self.is_proc_macro {
return true;
}
- // The all loop is because `--crate-type=rlib --crate-type=rlib` is
- // legal and produces both inside this type.
- let is_rlib = self.sess.crate_types().iter().all(|c| *c == CrateType::Rlib);
- let needs_object_code = self.sess.opts.output_types.should_codegen();
- // If we're producing an rlib, then we don't need object code.
- // Or, if we're not producing object code, then we don't need it either
- // (e.g., if we're a cdylib but emitting just metadata).
- if is_rlib || !needs_object_code {
+ if self.only_needs_metadata {
flavor == CrateFlavor::Rmeta
} else {
// we need all flavors (perhaps not true, but what we do for now)
let mut err_data: Option<Vec<PathBuf>> = None;
for (lib, kind) in m {
info!("{} reading metadata from: {}", flavor, lib.display());
+ if flavor == CrateFlavor::Rmeta && lib.metadata().map_or(false, |m| m.len() == 0) {
+ // Empty files will cause get_metadata_section to fail. Rmeta
+ // files can be empty, for example with binaries (which can
+ // often appear with `cargo check` when checking a library as
+ // a unittest). We don't want to emit a user-visible warning
+ // in this case as it is not a real problem.
+ debug!("skipping empty file");
+ continue;
+ }
let (hash, metadata) =
match get_metadata_section(self.target, flavor, &lib, self.metadata_loader) {
Ok(blob) => {
continue;
}
}
- Err(err) => {
- warn!("no metadata found: {}", err);
+ Err(MetadataError::LoadFailure(err)) => {
+ info!("no metadata found: {}", err);
+ // The file was present and created by the same compiler version, but we
+ // couldn't load it for some reason. Give a hard error instead of silently
+ // ignoring it, but only if we would have given an error anyway.
+ self.crate_rejections
+ .via_invalid
+ .push(CrateMismatch { path: lib, got: err });
+ continue;
+ }
+ Err(err @ MetadataError::NotPresent(_)) => {
+ info!("no metadata found: {}", err);
continue;
}
};
// candidates are all canonicalized, so we canonicalize the sysroot
// as well.
if let Some((prev, _)) = &ret {
- let sysroot = &self.sess.sysroot;
+ let sysroot = self.sysroot;
let sysroot = sysroot.canonicalize().unwrap_or_else(|_| sysroot.to_path_buf());
if prev.starts_with(&sysroot) {
continue;
let found_version = metadata.get_rustc_version();
if found_version != rustc_version {
info!("Rejecting via version: expected {} got {}", rustc_version, found_version);
- self.rejected_via_version
+ self.crate_rejections
+ .via_version
.push(CrateMismatch { path: libpath.to_path_buf(), got: found_version });
return None;
}
let root = metadata.get_root();
- if let Some(expected_is_proc_macro) = self.is_proc_macro {
- let is_proc_macro = root.is_proc_macro_crate();
- if is_proc_macro != expected_is_proc_macro {
- info!(
- "Rejecting via proc macro: expected {} got {}",
- expected_is_proc_macro, is_proc_macro
- );
- return None;
- }
+ if root.is_proc_macro_crate() != self.is_proc_macro {
+ info!(
+ "Rejecting via proc macro: expected {} got {}",
+ self.is_proc_macro,
+ root.is_proc_macro_crate(),
+ );
+ return None;
}
if self.exact_paths.is_empty() && self.crate_name != root.name() {
if root.triple() != &self.triple {
info!("Rejecting via crate triple: expected {} got {}", self.triple, root.triple());
- self.rejected_via_triple.push(CrateMismatch {
+ self.crate_rejections.via_triple.push(CrateMismatch {
path: libpath.to_path_buf(),
got: root.triple().to_string(),
});
if let Some(expected_hash) = self.hash {
if hash != expected_hash {
info!("Rejecting via hash: expected {} got {}", expected_hash, hash);
- self.rejected_via_hash
+ self.crate_rejections
+ .via_hash
.push(CrateMismatch { path: libpath.to_path_buf(), got: hash.to_string() });
return None;
}
loc.original().clone(),
));
}
- let file = match loc.original().file_name().and_then(|s| s.to_str()) {
- Some(file) => file,
- None => {
- return Err(CrateError::ExternLocationNotFile(
- self.crate_name,
- loc.original().clone(),
- ));
- }
+ let Some(file) = loc.original().file_name().and_then(|s| s.to_str()) else {
+ return Err(CrateError::ExternLocationNotFile(
+ self.crate_name,
+ loc.original().clone(),
+ ));
};
if file.starts_with("lib") && (file.ends_with(".rlib") || file.ends_with(".rmeta"))
- || file.starts_with(&self.target.dll_prefix)
- && file.ends_with(&self.target.dll_suffix)
+ || file.starts_with(self.target.dll_prefix.as_ref())
+ && file.ends_with(self.target.dll_suffix.as_ref())
{
// Make sure there's at most one rlib and at most one dylib.
// Note to take care and match against the non-canonicalized name:
dylibs.insert(loc_canon, PathKind::ExternFlag);
}
} else {
- self.rejected_via_filename
+ self.crate_rejections
+ .via_filename
.push(CrateMismatch { path: loc.original().clone(), got: String::new() });
}
}
Ok(self.extract_lib(rlibs, rmetas, dylibs)?.map(|(_, lib)| lib))
}
- crate fn into_error(self) -> CrateError {
+ pub(crate) fn into_error(self, root: Option<CratePaths>) -> CrateError {
CrateError::LocatorCombined(CombinedLocatorError {
crate_name: self.crate_name,
- root: self.root.cloned(),
+ root,
triple: self.triple,
- dll_prefix: self.target.dll_prefix.clone(),
- dll_suffix: self.target.dll_suffix.clone(),
- rejected_via_hash: self.rejected_via_hash,
- rejected_via_triple: self.rejected_via_triple,
- rejected_via_kind: self.rejected_via_kind,
- rejected_via_version: self.rejected_via_version,
- rejected_via_filename: self.rejected_via_filename,
+ dll_prefix: self.target.dll_prefix.to_string(),
+ dll_suffix: self.target.dll_suffix.to_string(),
+ crate_rejections: self.crate_rejections,
})
}
}
-/// A trivial wrapper for `Mmap` that implements `StableDeref`.
-struct StableDerefMmap(memmap2::Mmap);
-
-impl Deref for StableDerefMmap {
- type Target = [u8];
-
- fn deref(&self) -> &[u8] {
- self.0.deref()
- }
-}
-
-unsafe impl stable_deref_trait::StableDeref for StableDerefMmap {}
-
-fn get_metadata_section(
+fn get_metadata_section<'p>(
target: &Target,
flavor: CrateFlavor,
- filename: &Path,
+ filename: &'p Path,
loader: &dyn MetadataLoader,
-) -> Result<MetadataBlob, String> {
+) -> Result<MetadataBlob, MetadataError<'p>> {
if !filename.exists() {
- return Err(format!("no such file: '{}'", filename.display()));
+ return Err(MetadataError::NotPresent(filename));
}
let raw_bytes: MetadataRef = match flavor {
- CrateFlavor::Rlib => loader.get_rlib_metadata(target, filename)?,
+ CrateFlavor::Rlib => {
+ loader.get_rlib_metadata(target, filename).map_err(MetadataError::LoadFailure)?
+ }
CrateFlavor::Dylib => {
- let buf = loader.get_dylib_metadata(target, filename)?;
+ let buf =
+ loader.get_dylib_metadata(target, filename).map_err(MetadataError::LoadFailure)?;
// The header is uncompressed
let header_len = METADATA_HEADER.len();
debug!("checking {} bytes of metadata-version stamp", header_len);
let header = &buf[..cmp::min(header_len, buf.len())];
if header != METADATA_HEADER {
- return Err(format!(
- "incompatible metadata version found: '{}'",
+ return Err(MetadataError::LoadFailure(format!(
+ "invalid metadata version found: {}",
filename.display()
- ));
+ )));
}
// Header is okay -> inflate the actual metadata
let compressed_bytes = &buf[header_len..];
debug!("inflating {} bytes of compressed metadata", compressed_bytes.len());
- let mut inflated = Vec::new();
+ // Assume the decompressed data will be at least the size of the compressed data, so we
+ // don't have to grow the buffer as much.
+ let mut inflated = Vec::with_capacity(compressed_bytes.len());
match FrameDecoder::new(compressed_bytes).read_to_end(&mut inflated) {
Ok(_) => rustc_erase_owner!(OwningRef::new(inflated).map_owner_box()),
Err(_) => {
- return Err(format!("failed to decompress metadata: {}", filename.display()));
+ return Err(MetadataError::LoadFailure(format!(
+ "failed to decompress metadata: {}",
+ filename.display()
+ )));
}
}
}
CrateFlavor::Rmeta => {
// mmap the file, because only a small fraction of it is read.
- let file = std::fs::File::open(filename)
- .map_err(|_| format!("failed to open rmeta metadata: '{}'", filename.display()))?;
- let mmap = unsafe { memmap2::Mmap::map(&file) };
- let mmap = mmap
- .map_err(|_| format!("failed to mmap rmeta metadata: '{}'", filename.display()))?;
+ let file = std::fs::File::open(filename).map_err(|_| {
+ MetadataError::LoadFailure(format!(
+ "failed to open rmeta metadata: '{}'",
+ filename.display()
+ ))
+ })?;
+ let mmap = unsafe { Mmap::map(file) };
+ let mmap = mmap.map_err(|_| {
+ MetadataError::LoadFailure(format!(
+ "failed to mmap rmeta metadata: '{}'",
+ filename.display()
+ ))
+ })?;
- rustc_erase_owner!(OwningRef::new(StableDerefMmap(mmap)).map_owner_box())
+ rustc_erase_owner!(OwningRef::new(mmap).map_owner_box())
}
};
let blob = MetadataBlob::new(raw_bytes);
if blob.is_compatible() {
Ok(blob)
} else {
- Err(format!("incompatible metadata version found: '{}'", filename.display()))
+ Err(MetadataError::LoadFailure(format!(
+ "invalid metadata version found: {}",
+ filename.display()
+ )))
}
}
metadata_loader: &dyn MetadataLoader,
span: Span,
name: Symbol,
-) -> (PathBuf, CrateDisambiguator) {
- match find_plugin_registrar_impl(sess, metadata_loader, name) {
- Ok(res) => res,
- Err(err) => err.report(sess, span),
- }
+) -> PathBuf {
+ find_plugin_registrar_impl(sess, metadata_loader, name).unwrap_or_else(|err| {
+ // `core` is always available if we got as far as loading plugins.
+ err.report(sess, span, false);
+ FatalError.raise()
+ })
}
fn find_plugin_registrar_impl<'a>(
sess: &'a Session,
metadata_loader: &dyn MetadataLoader,
name: Symbol,
-) -> Result<(PathBuf, CrateDisambiguator), CrateError> {
+) -> Result<PathBuf, CrateError> {
info!("find plugin registrar `{}`", name);
let mut locator = CrateLocator::new(
sess,
metadata_loader,
name,
None, // hash
- None, // host_hash
None, // extra_filename
true, // is_host
PathKind::Crate,
- None, // root
- None, // is_proc_macro
);
match locator.maybe_load_library_crate()? {
Some(library) => match library.source.dylib {
- Some(dylib) => Ok((dylib.0, library.metadata.get_root().disambiguator())),
+ Some(dylib) => Ok(dylib.0),
None => Err(CrateError::NonDylibPlugin(name)),
},
- None => Err(locator.into_error()),
+ None => Err(locator.into_error(None)),
}
}
got: String,
}
+#[derive(Clone, Default)]
+struct CrateRejections {
+ via_hash: Vec<CrateMismatch>,
+ via_triple: Vec<CrateMismatch>,
+ via_kind: Vec<CrateMismatch>,
+ via_version: Vec<CrateMismatch>,
+ via_filename: Vec<CrateMismatch>,
+ via_invalid: Vec<CrateMismatch>,
+}
+
/// Candidate rejection reasons collected during crate search.
/// If no candidate is accepted, then these reasons are presented to the user,
/// otherwise they are ignored.
-crate struct CombinedLocatorError {
+pub(crate) struct CombinedLocatorError {
crate_name: Symbol,
root: Option<CratePaths>,
triple: TargetTriple,
dll_prefix: String,
dll_suffix: String,
- rejected_via_hash: Vec<CrateMismatch>,
- rejected_via_triple: Vec<CrateMismatch>,
- rejected_via_kind: Vec<CrateMismatch>,
- rejected_via_version: Vec<CrateMismatch>,
- rejected_via_filename: Vec<CrateMismatch>,
+ crate_rejections: CrateRejections,
}
-crate enum CrateError {
+pub(crate) enum CrateError {
NonAsciiName(Symbol),
ExternLocationNotExist(Symbol, PathBuf),
ExternLocationNotFile(Symbol, PathBuf),
NonDylibPlugin(Symbol),
}
+enum MetadataError<'a> {
+ /// The file was missing.
+ NotPresent(&'a Path),
+ /// The file was present and invalid.
+ LoadFailure(String),
+}
+
+impl fmt::Display for MetadataError<'_> {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self {
+ MetadataError::NotPresent(filename) => {
+ f.write_str(&format!("no such file: '{}'", filename.display()))
+ }
+ MetadataError::LoadFailure(msg) => f.write_str(msg),
+ }
+ }
+}
+
impl CrateError {
- crate fn report(self, sess: &Session, span: Span) -> ! {
- let mut err = match self {
+ pub(crate) fn report(self, sess: &Session, span: Span, missing_core: bool) {
+ let mut diag = match self {
CrateError::NonAsciiName(crate_name) => sess.struct_span_err(
span,
&format!("cannot load a crate with a non-ascii name `{}`", crate_name),
"multiple matching crates for `{}`",
crate_name
);
+ let mut libraries: Vec<_> = libraries.into_values().collect();
+ // Make ordering of candidates deterministic.
+ // This has to `clone()` to work around lifetime restrictions with `sort_by_key()`.
+ // `sort_by()` could be used instead, but this is in the error path,
+ // so the performance shouldn't matter.
+ libraries.sort_by_cached_key(|lib| lib.source.paths().next().unwrap().clone());
let candidates = libraries
.iter()
- .filter_map(|(_, lib)| {
- let crate_name = &lib.metadata.get_root().name().as_str();
- match (&lib.source.dylib, &lib.source.rlib) {
- (Some((pd, _)), Some((pr, _))) => Some(format!(
- "\ncrate `{}`: {}\n{:>padding$}",
- crate_name,
- pd.display(),
- pr.display(),
- padding = 8 + crate_name.len()
- )),
- (Some((p, _)), None) | (None, Some((p, _))) => {
- Some(format!("\ncrate `{}`: {}", crate_name, p.display()))
- }
- (None, None) => None,
+ .map(|lib| {
+ let crate_name = lib.metadata.get_root().name();
+ let crate_name = crate_name.as_str();
+ let mut paths = lib.source.paths();
+
+ // This `unwrap()` should be okay because there has to be at least one
+ // source file. `CrateSource`'s docs confirm that too.
+ let mut s = format!(
+ "\ncrate `{}`: {}",
+ crate_name,
+ paths.next().unwrap().display()
+ );
+ let padding = 8 + crate_name.len();
+ for path in paths {
+ write!(s, "\n{:>padding$}", path.display(), padding = padding).unwrap();
}
+ s
})
.collect::<String>();
err.note(&format!("candidates:{}", candidates));
Some(r) => format!(" which `{}` depends on", r.name),
};
let mut msg = "the following crate versions were found:".to_string();
- let mut err = if !locator.rejected_via_hash.is_empty() {
+ let mut err = if !locator.crate_rejections.via_hash.is_empty() {
let mut err = struct_span_err!(
sess,
span,
add,
);
err.note("perhaps that crate needs to be recompiled?");
- let mismatches = locator.rejected_via_hash.iter();
+ let mismatches = locator.crate_rejections.via_hash.iter();
for CrateMismatch { path, .. } in mismatches {
msg.push_str(&format!("\ncrate `{}`: {}", crate_name, path.display()));
}
}
err.note(&msg);
err
- } else if !locator.rejected_via_triple.is_empty() {
+ } else if !locator.crate_rejections.via_triple.is_empty() {
let mut err = struct_span_err!(
sess,
span,
locator.triple,
add,
);
- let mismatches = locator.rejected_via_triple.iter();
+ let mismatches = locator.crate_rejections.via_triple.iter();
for CrateMismatch { path, got } in mismatches {
msg.push_str(&format!(
"\ncrate `{}`, target triple {}: {}",
}
err.note(&msg);
err
- } else if !locator.rejected_via_kind.is_empty() {
+ } else if !locator.crate_rejections.via_kind.is_empty() {
let mut err = struct_span_err!(
sess,
span,
add,
);
err.help("please recompile that crate using --crate-type lib");
- let mismatches = locator.rejected_via_kind.iter();
+ let mismatches = locator.crate_rejections.via_kind.iter();
for CrateMismatch { path, .. } in mismatches {
msg.push_str(&format!("\ncrate `{}`: {}", crate_name, path.display()));
}
err.note(&msg);
err
- } else if !locator.rejected_via_version.is_empty() {
+ } else if !locator.crate_rejections.via_version.is_empty() {
let mut err = struct_span_err!(
sess,
span,
add,
);
err.help(&format!(
- "please recompile that crate using this compiler ({})",
+ "please recompile that crate using this compiler ({}) \
+ (consider running `cargo clean` first)",
rustc_version(),
));
- let mismatches = locator.rejected_via_version.iter();
+ let mismatches = locator.crate_rejections.via_version.iter();
for CrateMismatch { path, got } in mismatches {
msg.push_str(&format!(
"\ncrate `{}` compiled by {}: {}",
}
err.note(&msg);
err
+ } else if !locator.crate_rejections.via_invalid.is_empty() {
+ let mut err = struct_span_err!(
+ sess,
+ span,
+ E0786,
+ "found invalid metadata files for crate `{}`{}",
+ crate_name,
+ add,
+ );
+ for CrateMismatch { path: _, got } in locator.crate_rejections.via_invalid {
+ err.note(&got);
+ }
+ err
} else {
let mut err = struct_span_err!(
sess,
if (crate_name == sym::std || crate_name == sym::core)
&& locator.triple != TargetTriple::from_triple(config::host_triple())
{
- err.note(&format!("the `{}` target may not be installed", locator.triple));
- } else if crate_name == sym::profiler_builtins {
- err.note(&"the compiler may have been built without the profiler runtime");
+ if missing_core {
+ err.note(&format!(
+ "the `{}` target may not be installed",
+ locator.triple
+ ));
+ } else {
+ err.note(&format!(
+ "the `{}` target may not support the standard library",
+ locator.triple
+ ));
+ }
+ // NOTE: this suggests using rustup, even though the user may not have it installed.
+ // That's because they could choose to install it; or this may give them a hint which
+ // target they need to install from their distro.
+ if missing_core {
+ err.help(&format!(
+ "consider downloading the target with `rustup target add {}`",
+ locator.triple
+ ));
+ }
+ // Suggest using #![no_std]. #[no_core] is unstable and not really supported anyway.
+ // NOTE: this is a dummy span if `extern crate std` was injected by the compiler.
+ // If it's not a dummy, that means someone added `extern crate std` explicitly and `#![no_std]` won't help.
+ if !missing_core && span.is_dummy() {
+ let current_crate =
+ sess.opts.crate_name.as_deref().unwrap_or("<unknown>");
+ err.note(&format!(
+ "`std` is required by `{}` because it does not declare `#![no_std]`",
+ current_crate
+ ));
+ }
+ if sess.is_nightly_build() {
+ err.help("consider building the standard library from source with `cargo build -Zbuild-std`");
+ }
+ } else if crate_name
+ == Symbol::intern(&sess.opts.debugging_opts.profiler_runtime)
+ {
+ err.note("the compiler may have been built without the profiler runtime");
+ } else if crate_name.as_str().starts_with("rustc_") {
+ err.help(
+ "maybe you need to install the missing components with: \
+ `rustup component add rust-src rustc-dev llvm-tools-preview`",
+ );
}
err.span_label(span, "can't find crate");
err
};
- if !locator.rejected_via_filename.is_empty() {
- let mismatches = locator.rejected_via_filename.iter();
+ if !locator.crate_rejections.via_filename.is_empty() {
+ let mismatches = locator.crate_rejections.via_filename.iter();
for CrateMismatch { path, .. } in mismatches {
err.note(&format!(
"extern location for {} is of an unknown type: {}",
),
};
- err.emit();
- sess.abort_if_errors();
- unreachable!();
+ diag.emit();
}
}