port of locator.rs to SessionDiagnostics, fix some of the errors

revealed by tests, manually add a panic to test for dead code
This commit is contained in:
Nathan Stocks 2022-08-24 12:14:41 -06:00
parent bd8e312d73
commit d0ba1fbaa4
4 changed files with 450 additions and 215 deletions

View File

@ -170,3 +170,58 @@ metadata_failed_create_file =
metadata_failed_create_encoded_metadata = metadata_failed_create_encoded_metadata =
failed to create encoded metadata from file: {$err} failed to create encoded metadata from file: {$err}
metadata_non_ascii_name =
cannot load a crate with a non-ascii name `{$crate_name}`
metadata_extern_location_not_exist =
extern location for {$crate_name} does not exist: {$location}
metadata_extern_location_not_file =
extern location for {$crate_name} is not a file: {$location}
metadata_multiple_candidates =
multiple {$flavor} candidates for `{$crate_name}` found
metadata_multiple_matching_crates =
multiple matching crates for `{$crate_name}`
.note = candidates:{$candidates}
metadata_symbol_conflicts_current =
the current crate is indistinguishable from one of its dependencies: it has the same crate-name `{$crate_name}` and was compiled with the same `-C metadata` arguments. This will result in symbol conflicts between the two.
metadata_symbol_conflicts_others =
found two different crates with name `{$crate_name}` that are not distinguished by differing `-C metadata`. This will result in symbol conflicts between the two.
metadata_stable_crate_id_collision =
found crates (`{$crate_name0}` and `{$crate_name1}`) with colliding StableCrateId values.
metadata_dl_error =
{$err}
metadata_newer_crate_version =
found possibly newer version of crate `{$crate_name}`{$add_info}
.note = perhaps that crate needs to be recompiled?
metadata_found_crate_versions =
the following crate versions were found:{$found_crates}
metadata_no_crate_with_triple =
couldn't find crate `{$crate_name}` with expected target triple {$locator_triple}{$add_info}
metadata_found_staticlib =
found staticlib `{$crate_name}` instead of rlib or dylib{$add_info}
.help = please recompile that crate using --crate-type lib
metadata_incompatible_rustc =
found crate `{$crate_name}` compiled by an incompatible version of rustc{$add_info}
.help = please recompile that crate using this compiler ({$rustc_version}) (consider running `cargo clean` first)
metadata_invalid_meta_files =
found invalid metadata files for crate `{$crate_name}`{$add_info}
metadata_cannot_find_crate =
can't find crate for `{$crate_name}`{$add_info}
metadata_no_dylib_plugin =
plugin `{$crate_name}` only found in rlib format, but must be available in dylib format

View File

@ -1,6 +1,10 @@
// use rustc_errors::ErrorGuaranteed; use std::path::PathBuf;
use rustc_errors::{DiagnosticId, ErrorGuaranteed};
use rustc_macros::SessionDiagnostic; use rustc_macros::SessionDiagnostic;
use rustc_span::Span; use rustc_session::{config, SessionDiagnostic};
use rustc_span::{sym, Span, Symbol};
use rustc_target::spec::TargetTriple;
#[derive(SessionDiagnostic)] #[derive(SessionDiagnostic)]
#[diag(metadata::rlib_required)] #[diag(metadata::rlib_required)]
@ -104,8 +108,8 @@ pub struct WasmImportForm {
#[derive(SessionDiagnostic)] #[derive(SessionDiagnostic)]
#[diag(metadata::empty_link_name, code = "E0454")] #[diag(metadata::empty_link_name, code = "E0454")]
pub struct EmptyLinkName { pub struct EmptyLinkName {
#[label]
#[primary_span] #[primary_span]
#[label]
pub span: Span, pub span: Span,
} }
@ -126,8 +130,8 @@ pub struct FrameworkOnlyWindows {
#[derive(SessionDiagnostic)] #[derive(SessionDiagnostic)]
#[diag(metadata::unknown_link_kind, code = "E0458")] #[diag(metadata::unknown_link_kind, code = "E0458")]
pub struct UnknownLinkKind { pub struct UnknownLinkKind {
#[label]
#[primary_span] #[primary_span]
#[label]
pub span: Span, pub span: Span,
pub kind: String, pub kind: String,
} }
@ -221,8 +225,8 @@ pub struct IncompatibleWasmLink {
#[derive(SessionDiagnostic)] #[derive(SessionDiagnostic)]
#[diag(metadata::link_requires_name, code = "E0459")] #[diag(metadata::link_requires_name, code = "E0459")]
pub struct LinkRequiresName { pub struct LinkRequiresName {
#[label]
#[primary_span] #[primary_span]
#[label]
pub span: Span, pub span: Span,
} }
@ -378,3 +382,246 @@ pub struct FailedCreateFile {
pub struct FailedCreateEncodedMetadata { pub struct FailedCreateEncodedMetadata {
pub err: String, pub err: String,
} }
#[derive(SessionDiagnostic)]
#[diag(metadata::non_ascii_name)]
pub struct NonAsciiName {
#[primary_span]
pub span: Span,
pub crate_name: String,
}
#[derive(SessionDiagnostic)]
#[diag(metadata::extern_location_not_exist)]
pub struct ExternLocationNotExist {
#[primary_span]
pub span: Span,
pub crate_name: String,
pub location: String,
}
#[derive(SessionDiagnostic)]
#[diag(metadata::extern_location_not_file)]
pub struct ExternLocationNotFile {
#[primary_span]
pub span: Span,
pub crate_name: String,
pub location: String,
}
pub struct MultipleCandidates {
pub span: Span,
pub flavor: String,
pub crate_name: String,
pub candidates: Vec<PathBuf>,
}
impl SessionDiagnostic<'_> for MultipleCandidates {
fn into_diagnostic(
self,
sess: &'_ rustc_session::parse::ParseSess,
) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
let mut diag = sess.struct_err(rustc_errors::fluent::metadata::multiple_candidates);
diag.set_arg("crate_name", self.crate_name);
diag.set_arg("flavor", self.flavor);
diag.code(DiagnosticId::Error("E0465".into()));
diag.set_span(self.span);
for (i, candidate) in self.candidates.iter().enumerate() {
diag.span_note(self.span, &format!("candidate #{}: {}", i + 1, candidate.display()));
}
diag
}
}
#[derive(SessionDiagnostic)]
#[diag(metadata::multiple_matching_crates, code = "E0464")]
#[note]
pub struct MultipleMatchingCrates {
#[primary_span]
pub span: Span,
pub crate_name: String,
pub candidates: String,
}
#[derive(SessionDiagnostic)]
#[diag(metadata::symbol_conflicts_current, code = "E0519")]
pub struct SymbolConflictsCurrent {
#[primary_span]
pub span: Span,
pub crate_name: String,
}
#[derive(SessionDiagnostic)]
#[diag(metadata::symbol_conflicts_others, code = "E0523")]
pub struct SymbolConflictsOthers {
#[primary_span]
pub span: Span,
pub crate_name: String,
}
#[derive(SessionDiagnostic)]
#[diag(metadata::stable_crate_id_collision)]
pub struct StableCrateIdCollision {
#[primary_span]
pub span: Span,
pub crate_name0: String,
pub crate_name1: String,
}
#[derive(SessionDiagnostic)]
#[diag(metadata::dl_error)]
pub struct DlError {
#[primary_span]
pub span: Span,
pub err: String,
}
#[derive(SessionDiagnostic)]
#[diag(metadata::newer_crate_version, code = "E0460")]
#[note]
#[note(metadata::found_crate_versions)]
pub struct NewerCrateVersion {
#[primary_span]
pub span: Span,
pub crate_name: String,
pub add_info: String,
pub found_crates: String,
}
#[derive(SessionDiagnostic)]
#[diag(metadata::no_crate_with_triple, code = "E0461")]
#[note(metadata::found_crate_versions)]
pub struct NoCrateWithTriple {
#[primary_span]
pub span: Span,
pub crate_name: String,
pub locator_triple: String,
pub add_info: String,
pub found_crates: String,
}
#[derive(SessionDiagnostic)]
#[diag(metadata::found_staticlib, code = "E0462")]
#[note(metadata::found_crate_versions)]
#[help]
pub struct FoundStaticlib {
#[primary_span]
pub span: Span,
pub crate_name: String,
pub add_info: String,
pub found_crates: String,
}
#[derive(SessionDiagnostic)]
#[diag(metadata::incompatible_rustc, code = "E0514")]
#[note(metadata::found_crate_versions)]
#[help]
pub struct IncompatibleRustc {
#[primary_span]
pub span: Span,
pub crate_name: String,
pub add_info: String,
pub found_crates: String,
pub rustc_version: String,
}
pub struct InvalidMetadataFiles {
pub span: Span,
pub crate_name: String,
pub add_info: String,
pub crate_rejections: Vec<String>,
}
impl SessionDiagnostic<'_> for InvalidMetadataFiles {
fn into_diagnostic(
self,
sess: &'_ rustc_session::parse::ParseSess,
) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
let mut diag = sess.struct_err(rustc_errors::fluent::metadata::invalid_meta_files);
diag.set_arg("crate_name", self.crate_name);
diag.set_arg("add_info", self.add_info);
diag.code(DiagnosticId::Error("E0786".into()));
diag.set_span(self.span);
for crate_rejection in self.crate_rejections {
diag.note(crate_rejection);
}
diag
}
}
pub struct CannotFindCrate {
pub span: Span,
pub crate_name: String,
pub crate_name_symbol: Symbol,
pub add_info: String,
pub missing_core: bool,
pub current_crate: String,
pub is_nightly_build: bool,
pub profiler_runtime: Symbol,
pub locator_triple: TargetTriple,
}
impl SessionDiagnostic<'_> for CannotFindCrate {
fn into_diagnostic(
self,
sess: &'_ rustc_session::parse::ParseSess,
) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
let mut diag = sess.struct_err(rustc_errors::fluent::metadata::cannot_find_crate);
diag.set_arg("crate_name", self.crate_name.clone());
diag.set_arg("add_info", self.add_info);
diag.code(DiagnosticId::Error("E0463".into()));
diag.set_span(self.span);
// FIXME: Find a way to distill this logic down into the derived SessionDiagnostic form
if (self.crate_name_symbol == sym::std || self.crate_name_symbol == sym::core)
&& self.locator_triple != TargetTriple::from_triple(config::host_triple())
{
if self.missing_core {
diag.note(&format!("the `{}` target may not be installed", self.locator_triple));
} else {
diag.note(&format!(
"the `{}` target may not support the standard library",
self.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 self.missing_core {
diag.help(&format!(
"consider downloading the target with `rustup target add {}`",
self.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 !self.missing_core && self.span.is_dummy() {
diag.note(&format!(
"`std` is required by `{}` because it does not declare `#![no_std]`",
self.current_crate
));
}
if self.is_nightly_build {
diag.help("consider building the standard library from source with `cargo build -Zbuild-std`");
}
} else if self.crate_name_symbol == self.profiler_runtime {
diag.note("the compiler may have been built without the profiler runtime");
} else if self.crate_name.starts_with("rustc_") {
diag.help(
"maybe you need to install the missing components with: \
`rustup component add rust-src rustc-dev llvm-tools-preview`",
);
}
diag.span_label(self.span, "can't find crate");
diag
}
}
#[derive(SessionDiagnostic)]
#[diag(metadata::no_dylib_plugin, code = "E0457")]
pub struct NoDylibPlugin {
#[primary_span]
pub span: Span,
pub crate_name: String,
}

View File

@ -213,6 +213,12 @@
//! metadata::locator or metadata::creader for all the juicy details! //! metadata::locator or metadata::creader for all the juicy details!
use crate::creader::Library; use crate::creader::Library;
use crate::errors::{
CannotFindCrate, DlError, ExternLocationNotExist, ExternLocationNotFile, FoundStaticlib,
IncompatibleRustc, InvalidMetadataFiles, MultipleCandidates, MultipleMatchingCrates,
NewerCrateVersion, NoCrateWithTriple, NoDylibPlugin, NonAsciiName, StableCrateIdCollision,
SymbolConflictsCurrent, SymbolConflictsOthers,
};
use crate::rmeta::{rustc_version, MetadataBlob, METADATA_HEADER}; use crate::rmeta::{rustc_version, MetadataBlob, METADATA_HEADER};
use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::fx::{FxHashMap, FxHashSet};
@ -220,14 +226,14 @@ use rustc_data_structures::memmap::Mmap;
use rustc_data_structures::owning_ref::OwningRef; use rustc_data_structures::owning_ref::OwningRef;
use rustc_data_structures::svh::Svh; use rustc_data_structures::svh::Svh;
use rustc_data_structures::sync::MetadataRef; use rustc_data_structures::sync::MetadataRef;
use rustc_errors::{struct_span_err, FatalError}; use rustc_errors::FatalError;
use rustc_session::config::{self, CrateType}; use rustc_session::config::{self, CrateType};
use rustc_session::cstore::{CrateSource, MetadataLoader}; use rustc_session::cstore::{CrateSource, MetadataLoader};
use rustc_session::filesearch::FileSearch; use rustc_session::filesearch::FileSearch;
use rustc_session::search_paths::PathKind; use rustc_session::search_paths::PathKind;
use rustc_session::utils::CanonicalizedPath; use rustc_session::utils::CanonicalizedPath;
use rustc_session::Session; use rustc_session::Session;
use rustc_span::symbol::{sym, Symbol}; use rustc_span::symbol::Symbol;
use rustc_span::Span; use rustc_span::Span;
use rustc_target::spec::{Target, TargetTriple}; use rustc_target::spec::{Target, TargetTriple};
@ -938,41 +944,33 @@ impl fmt::Display for MetadataError<'_> {
impl CrateError { impl CrateError {
pub(crate) fn report(self, sess: &Session, span: Span, missing_core: bool) { pub(crate) fn report(self, sess: &Session, span: Span, missing_core: bool) {
let mut diag = match self { match self {
CrateError::NonAsciiName(crate_name) => sess.struct_span_err( CrateError::NonAsciiName(crate_name) => {
span, sess.emit_err(NonAsciiName { span, crate_name: crate_name.to_string() });
&format!("cannot load a crate with a non-ascii name `{}`", crate_name), }
), CrateError::ExternLocationNotExist(crate_name, loc) => {
CrateError::ExternLocationNotExist(crate_name, loc) => sess.struct_span_err( sess.emit_err(ExternLocationNotExist {
span,
&format!("extern location for {} does not exist: {}", crate_name, loc.display()),
),
CrateError::ExternLocationNotFile(crate_name, loc) => sess.struct_span_err(
span,
&format!("extern location for {} is not a file: {}", crate_name, loc.display()),
),
CrateError::MultipleCandidates(crate_name, flavor, candidates) => {
let mut err = struct_span_err!(
sess,
span, span,
E0465, crate_name: crate_name.to_string(),
"multiple {} candidates for `{}` found", location: loc.display().to_string(),
flavor, });
crate_name, }
); CrateError::ExternLocationNotFile(crate_name, loc) => {
for (i, candidate) in candidates.iter().enumerate() { sess.emit_err(ExternLocationNotFile {
err.span_note(span, &format!("candidate #{}: {}", i + 1, candidate.display())); span,
} crate_name: crate_name.to_string(),
err location: loc.display().to_string(),
});
}
CrateError::MultipleCandidates(crate_name, flavor, candidates) => {
sess.emit_err(MultipleCandidates {
span,
flavor: flavor.to_string(),
crate_name: crate_name.to_string(),
candidates,
});
} }
CrateError::MultipleMatchingCrates(crate_name, libraries) => { CrateError::MultipleMatchingCrates(crate_name, libraries) => {
let mut err = struct_span_err!(
sess,
span,
E0464,
"multiple matching crates for `{}`",
crate_name
);
let mut libraries: Vec<_> = libraries.into_values().collect(); let mut libraries: Vec<_> = libraries.into_values().collect();
// Make ordering of candidates deterministic. // Make ordering of candidates deterministic.
// This has to `clone()` to work around lifetime restrictions with `sort_by_key()`. // This has to `clone()` to work around lifetime restrictions with `sort_by_key()`.
@ -1000,223 +998,159 @@ impl CrateError {
s s
}) })
.collect::<String>(); .collect::<String>();
err.note(&format!("candidates:{}", candidates)); sess.emit_err(MultipleMatchingCrates {
err span,
crate_name: crate_name.to_string(),
candidates,
});
}
CrateError::SymbolConflictsCurrent(root_name) => {
sess.emit_err(SymbolConflictsCurrent { span, crate_name: root_name.to_string() });
}
CrateError::SymbolConflictsOthers(root_name) => {
sess.emit_err(SymbolConflictsOthers { span, crate_name: root_name.to_string() });
} }
CrateError::SymbolConflictsCurrent(root_name) => struct_span_err!(
sess,
span,
E0519,
"the current crate is indistinguishable from one of its dependencies: it has the \
same crate-name `{}` and was compiled with the same `-C metadata` arguments. \
This will result in symbol conflicts between the two.",
root_name,
),
CrateError::SymbolConflictsOthers(root_name) => struct_span_err!(
sess,
span,
E0523,
"found two different crates with name `{}` that are not distinguished by differing \
`-C metadata`. This will result in symbol conflicts between the two.",
root_name,
),
CrateError::StableCrateIdCollision(crate_name0, crate_name1) => { CrateError::StableCrateIdCollision(crate_name0, crate_name1) => {
let msg = format!( sess.emit_err(StableCrateIdCollision {
"found crates (`{}` and `{}`) with colliding StableCrateId values.", span,
crate_name0, crate_name1 crate_name0: crate_name0.to_string(),
); crate_name1: crate_name1.to_string(),
sess.struct_span_err(span, &msg) });
}
CrateError::DlOpen(s) | CrateError::DlSym(s) => {
sess.emit_err(DlError { span, err: s.to_string() });
} }
CrateError::DlOpen(s) | CrateError::DlSym(s) => sess.struct_span_err(span, &s),
CrateError::LocatorCombined(locator) => { CrateError::LocatorCombined(locator) => {
let crate_name = locator.crate_name; let crate_name = locator.crate_name;
let add = match &locator.root { let add_info = match &locator.root {
None => String::new(), None => String::new(),
Some(r) => format!(" which `{}` depends on", r.name), Some(r) => format!(" which `{}` depends on", r.name),
}; };
let mut msg = "the following crate versions were found:".to_string(); // FIXME: Is there any way to get these notes and helps onto every diagnostic in this
let mut err = if !locator.crate_rejections.via_hash.is_empty() { // huge branch arm without changing them all to manual implementations?
let mut err = struct_span_err!( let mut global_loc_notes = Vec::new();
sess, let mut global_loc_helps = Vec::new();
span, if !locator.crate_rejections.via_filename.is_empty() {
E0460, let mismatches = locator.crate_rejections.via_filename.iter();
"found possibly newer version of crate `{}`{}", for CrateMismatch { path, .. } in mismatches {
crate_name, global_loc_notes.push(format!(
add, "extern location for {} is of an unknown type: {}",
); crate_name,
err.note("perhaps that crate needs to be recompiled?"); path.display(),
));
global_loc_helps.push(format!(
"file name should be lib*.rlib or {}*.{}",
locator.dll_prefix, locator.dll_suffix
));
}
panic!("!!!!! REVERT THIS COMMIT !!!!!");
}
let mut found_crates = String::new();
if !locator.crate_rejections.via_hash.is_empty() {
let mismatches = locator.crate_rejections.via_hash.iter(); let mismatches = locator.crate_rejections.via_hash.iter();
for CrateMismatch { path, .. } in mismatches { for CrateMismatch { path, .. } in mismatches {
msg.push_str(&format!("\ncrate `{}`: {}", crate_name, path.display())); found_crates.push_str(&format!(
"\ncrate `{}`: {}",
crate_name,
path.display()
));
} }
if let Some(r) = locator.root { if let Some(r) = locator.root {
for path in r.source.paths() { for path in r.source.paths() {
msg.push_str(&format!("\ncrate `{}`: {}", r.name, path.display())); found_crates.push_str(&format!(
"\ncrate `{}`: {}",
r.name,
path.display()
));
} }
} }
err.note(&msg); sess.emit_err(NewerCrateVersion {
err
} else if !locator.crate_rejections.via_triple.is_empty() {
let mut err = struct_span_err!(
sess,
span, span,
E0461, crate_name: crate_name.to_string(),
"couldn't find crate `{}` with expected target triple {}{}", add_info,
crate_name, found_crates,
locator.triple, });
add, } else if !locator.crate_rejections.via_triple.is_empty() {
);
let mismatches = locator.crate_rejections.via_triple.iter(); let mismatches = locator.crate_rejections.via_triple.iter();
for CrateMismatch { path, got } in mismatches { for CrateMismatch { path, got } in mismatches {
msg.push_str(&format!( found_crates.push_str(&format!(
"\ncrate `{}`, target triple {}: {}", "\ncrate `{}`, target triple {}: {}",
crate_name, crate_name,
got, got,
path.display(), path.display(),
)); ));
} }
err.note(&msg); sess.emit_err(NoCrateWithTriple {
err
} else if !locator.crate_rejections.via_kind.is_empty() {
let mut err = struct_span_err!(
sess,
span, span,
E0462, crate_name: crate_name.to_string(),
"found staticlib `{}` instead of rlib or dylib{}", locator_triple: locator.triple.to_string(),
crate_name, add_info,
add, found_crates,
); });
err.help("please recompile that crate using --crate-type lib"); } else if !locator.crate_rejections.via_kind.is_empty() {
let mismatches = locator.crate_rejections.via_kind.iter(); let mismatches = locator.crate_rejections.via_kind.iter();
for CrateMismatch { path, .. } in mismatches { for CrateMismatch { path, .. } in mismatches {
msg.push_str(&format!("\ncrate `{}`: {}", crate_name, path.display())); found_crates.push_str(&format!(
"\ncrate `{}`: {}",
crate_name,
path.display()
));
} }
err.note(&msg); sess.emit_err(FoundStaticlib {
err
} else if !locator.crate_rejections.via_version.is_empty() {
let mut err = struct_span_err!(
sess,
span, span,
E0514, crate_name: crate_name.to_string(),
"found crate `{}` compiled by an incompatible version of rustc{}", add_info,
crate_name, found_crates,
add, });
); } else if !locator.crate_rejections.via_version.is_empty() {
err.help(&format!(
"please recompile that crate using this compiler ({}) \
(consider running `cargo clean` first)",
rustc_version(),
));
let mismatches = locator.crate_rejections.via_version.iter(); let mismatches = locator.crate_rejections.via_version.iter();
for CrateMismatch { path, got } in mismatches { for CrateMismatch { path, got } in mismatches {
msg.push_str(&format!( found_crates.push_str(&format!(
"\ncrate `{}` compiled by {}: {}", "\ncrate `{}` compiled by {}: {}",
crate_name, crate_name,
got, got,
path.display(), path.display(),
)); ));
} }
err.note(&msg); sess.emit_err(IncompatibleRustc {
err span,
crate_name: crate_name.to_string(),
add_info,
found_crates,
rustc_version: rustc_version(),
});
} else if !locator.crate_rejections.via_invalid.is_empty() { } else if !locator.crate_rejections.via_invalid.is_empty() {
let mut err = struct_span_err!( let mut crate_rejections = Vec::new();
sess,
span,
E0786,
"found invalid metadata files for crate `{}`{}",
crate_name,
add,
);
for CrateMismatch { path: _, got } in locator.crate_rejections.via_invalid { for CrateMismatch { path: _, got } in locator.crate_rejections.via_invalid {
err.note(&got); crate_rejections.push(got);
} }
err sess.emit_err(InvalidMetadataFiles {
} else {
let mut err = struct_span_err!(
sess,
span, span,
E0463, crate_name: crate_name.to_string(),
"can't find crate for `{}`{}", add_info,
crate_name, crate_rejections,
add, });
); } else {
sess.emit_err(CannotFindCrate {
if (crate_name == sym::std || crate_name == sym::core) span,
&& locator.triple != TargetTriple::from_triple(config::host_triple()) crate_name: crate_name.to_string(),
{ crate_name_symbol: crate_name,
if missing_core { add_info,
err.note(&format!( missing_core,
"the `{}` target may not be installed", current_crate: sess
locator.triple .opts
)); .crate_name
} else { .clone()
err.note(&format!( .unwrap_or("<unknown>".to_string()),
"the `{}` target may not support the standard library", is_nightly_build: sess.is_nightly_build(),
locator.triple profiler_runtime: Symbol::intern(&sess.opts.unstable_opts.profiler_runtime),
)); locator_triple: 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.unstable_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.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: {}",
crate_name,
path.display(),
))
.help(&format!(
"file name should be lib*.rlib or {}*.{}",
locator.dll_prefix, locator.dll_suffix
));
}
} }
err
} }
CrateError::NonDylibPlugin(crate_name) => struct_span_err!( CrateError::NonDylibPlugin(crate_name) => {
sess, sess.emit_err(NoDylibPlugin { span, crate_name: crate_name.to_string() });
span, }
E0457, }
"plugin `{}` only found in rlib format, but must be available in dylib format",
crate_name,
),
};
diag.emit();
} }
} }

View File

@ -1,7 +1,6 @@
use rustc_ast::{NestedMetaItem, CRATE_NODE_ID}; use rustc_ast::{NestedMetaItem, CRATE_NODE_ID};
use rustc_attr as attr; use rustc_attr as attr;
use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::fx::FxHashSet;
use rustc_errors::struct_span_err;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def::DefKind; use rustc_hir::def::DefKind;
use rustc_middle::ty::{List, ParamEnv, ParamEnvAnd, Ty, TyCtxt}; use rustc_middle::ty::{List, ParamEnv, ParamEnvAnd, Ty, TyCtxt};