Skip to content

Commit

Permalink
Rollup merge of rust-lang#84450 - jyn514:missing-std, r=petrochenkov
Browse files Browse the repository at this point in the history
Give a better error when `std` or `core` are missing

- Suggest using `rustup target add` if `RUSTUP_HOME` is set. I don't know if there's any precedent for doing this, but it seems harmless enough and it will be a big help.
- On nightly, suggest using `cargo build -Z build-std` if `CARGO` is set
- Add a note about `#![no_std]` if `std` is missing but not core
- Add a note that std may be unsupported if `std` is missing but not core

Fixes rust-lang#84418.

r? `@petrochenkov`
  • Loading branch information
Dylan-DPC committed Apr 25, 2021
2 parents e7e22b4 + d326a4b commit 379a55c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
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() {
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`.

0 comments on commit 379a55c

Please sign in to comment.