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

Give a better error when std or core are missing #84450

Merged
merged 1 commit into from
Apr 26, 2021
Merged
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
7 changes: 5 additions & 2 deletions compiler/rustc_metadata/src/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,11 @@ impl<'a> CrateLoader<'a> {
if dep.is_none() {
self.used_extern_options.insert(name);
}
self.maybe_resolve_crate(name, dep_kind, dep)
.unwrap_or_else(|err| err.report(self.sess, span))
self.maybe_resolve_crate(name, dep_kind, dep).unwrap_or_else(|err| {
let missing_core =
self.maybe_resolve_crate(sym::core, CrateDepKind::Explicit, None).is_err();
err.report(&self.sess, span, missing_core)
})
}

fn maybe_resolve_crate<'b>(
Expand Down
37 changes: 34 additions & 3 deletions compiler/rustc_metadata/src/locator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,8 @@ pub fn find_plugin_registrar(
) -> (PathBuf, CrateDisambiguator) {
match find_plugin_registrar_impl(sess, metadata_loader, name) {
Ok(res) => res,
Err(err) => err.report(sess, span),
// `core` is always available if we got as far as loading plugins.
Err(err) => err.report(sess, span, false),
}
}

Expand Down Expand Up @@ -883,7 +884,7 @@ crate enum CrateError {
}

impl CrateError {
crate fn report(self, sess: &Session, span: Span) -> ! {
crate fn report(self, sess: &Session, span: Span, missing_core: bool) -> ! {
let mut err = match self {
CrateError::NonAsciiName(crate_name) => sess.struct_span_err(
span,
Expand Down Expand Up @@ -1068,7 +1069,37 @@ impl CrateError {
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));
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
));
}
if missing_core && std::env::var("RUSTUP_HOME").is_ok() {
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() && std::env::var("CARGO").is_ok() {
petrochenkov marked this conversation as resolved.
Show resolved Hide resolved
err.help("consider building the standard library from source with `cargo build -Zbuild-std`");
}
} else if crate_name == sym::profiler_builtins {
err.note(&"the compiler may have been built without the profiler runtime");
}
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/crate-loading/missing-std.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// compile-flags: --target x86_64-unknown-uefi
// rustc-env:CARGO=/usr/bin/cargo
// rustc-env:RUSTUP_HOME=/home/bors/.rustup
#![no_core]
extern crate core;
//~^ ERROR can't find crate for `core`
//~| NOTE can't find crate
//~| NOTE target may not be installed
//~| HELP consider building the standard library from source with `cargo build -Zbuild-std`
//~| HELP consider downloading the target with `rustup target add x86_64-unknown-uefi`
fn main() {}
13 changes: 13 additions & 0 deletions src/test/ui/crate-loading/missing-std.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error[E0463]: can't find crate for `core`
--> $DIR/missing-std.rs:5:1
|
LL | extern crate core;
| ^^^^^^^^^^^^^^^^^^ can't find crate
|
= note: the `x86_64-unknown-uefi` target may not be installed
= help: consider downloading the target with `rustup target add x86_64-unknown-uefi`
= help: consider building the standard library from source with `cargo build -Zbuild-std`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0463`.