Skip to content

Commit 379a55c

Browse files
authored
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 #84418. r? `@petrochenkov`
2 parents e7e22b4 + d326a4b commit 379a55c

File tree

4 files changed

+63
-5
lines changed

4 files changed

+63
-5
lines changed

compiler/rustc_metadata/src/creader.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,11 @@ impl<'a> CrateLoader<'a> {
511511
if dep.is_none() {
512512
self.used_extern_options.insert(name);
513513
}
514-
self.maybe_resolve_crate(name, dep_kind, dep)
515-
.unwrap_or_else(|err| err.report(self.sess, span))
514+
self.maybe_resolve_crate(name, dep_kind, dep).unwrap_or_else(|err| {
515+
let missing_core =
516+
self.maybe_resolve_crate(sym::core, CrateDepKind::Explicit, None).is_err();
517+
err.report(&self.sess, span, missing_core)
518+
})
516519
}
517520

518521
fn maybe_resolve_crate<'b>(

compiler/rustc_metadata/src/locator.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,8 @@ pub fn find_plugin_registrar(
790790
) -> (PathBuf, CrateDisambiguator) {
791791
match find_plugin_registrar_impl(sess, metadata_loader, name) {
792792
Ok(res) => res,
793-
Err(err) => err.report(sess, span),
793+
// `core` is always available if we got as far as loading plugins.
794+
Err(err) => err.report(sess, span, false),
794795
}
795796
}
796797

@@ -883,7 +884,7 @@ crate enum CrateError {
883884
}
884885

885886
impl CrateError {
886-
crate fn report(self, sess: &Session, span: Span) -> ! {
887+
crate fn report(self, sess: &Session, span: Span, missing_core: bool) -> ! {
887888
let mut err = match self {
888889
CrateError::NonAsciiName(crate_name) => sess.struct_span_err(
889890
span,
@@ -1068,7 +1069,37 @@ impl CrateError {
10681069
if (crate_name == sym::std || crate_name == sym::core)
10691070
&& locator.triple != TargetTriple::from_triple(config::host_triple())
10701071
{
1071-
err.note(&format!("the `{}` target may not be installed", locator.triple));
1072+
if missing_core {
1073+
err.note(&format!(
1074+
"the `{}` target may not be installed",
1075+
locator.triple
1076+
));
1077+
} else {
1078+
err.note(&format!(
1079+
"the `{}` target may not support the standard library",
1080+
locator.triple
1081+
));
1082+
}
1083+
if missing_core && std::env::var("RUSTUP_HOME").is_ok() {
1084+
err.help(&format!(
1085+
"consider downloading the target with `rustup target add {}`",
1086+
locator.triple
1087+
));
1088+
}
1089+
// Suggest using #![no_std]. #[no_core] is unstable and not really supported anyway.
1090+
// NOTE: this is a dummy span if `extern crate std` was injected by the compiler.
1091+
// If it's not a dummy, that means someone added `extern crate std` explicitly and `#![no_std]` won't help.
1092+
if !missing_core && span.is_dummy() {
1093+
let current_crate =
1094+
sess.opts.crate_name.as_deref().unwrap_or("<unknown>");
1095+
err.note(&format!(
1096+
"`std` is required by `{}` because it does not declare `#![no_std]`",
1097+
current_crate
1098+
));
1099+
}
1100+
if sess.is_nightly_build() && std::env::var("CARGO").is_ok() {
1101+
err.help("consider building the standard library from source with `cargo build -Zbuild-std`");
1102+
}
10721103
} else if crate_name == sym::profiler_builtins {
10731104
err.note(&"the compiler may have been built without the profiler runtime");
10741105
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// compile-flags: --target x86_64-unknown-uefi
2+
// rustc-env:CARGO=/usr/bin/cargo
3+
// rustc-env:RUSTUP_HOME=/home/bors/.rustup
4+
#![no_core]
5+
extern crate core;
6+
//~^ ERROR can't find crate for `core`
7+
//~| NOTE can't find crate
8+
//~| NOTE target may not be installed
9+
//~| HELP consider building the standard library from source with `cargo build -Zbuild-std`
10+
//~| HELP consider downloading the target with `rustup target add x86_64-unknown-uefi`
11+
fn main() {}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error[E0463]: can't find crate for `core`
2+
--> $DIR/missing-std.rs:5:1
3+
|
4+
LL | extern crate core;
5+
| ^^^^^^^^^^^^^^^^^^ can't find crate
6+
|
7+
= note: the `x86_64-unknown-uefi` target may not be installed
8+
= help: consider downloading the target with `rustup target add x86_64-unknown-uefi`
9+
= help: consider building the standard library from source with `cargo build -Zbuild-std`
10+
11+
error: aborting due to previous error
12+
13+
For more information about this error, try `rustc --explain E0463`.

0 commit comments

Comments
 (0)