Rollup merge of #84450 - jyn514:missing-std, r=petrochenkov

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 https://github.com/rust-lang/rust/issues/84418.

r? `@petrochenkov`
This commit is contained in:
Dylan DPC 2021-04-25 23:15:11 +02:00 committed by GitHub
commit 379a55c64e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 5 deletions

View File

@ -511,8 +511,11 @@ fn resolve_crate<'b>(
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>(

View File

@ -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),
}
}
@ -883,7 +884,7 @@ struct CrateMismatch {
}
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,
@ -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");
}

View File

@ -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() {}

View File

@ -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`.