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

Suggest extern crate foo when failing to resolve use foo #97264

Merged
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
15 changes: 12 additions & 3 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -1836,9 +1836,18 @@ impl<'a> Resolver<'a> {
)),
)
} else if self.session.edition() == Edition::Edition2015 {
(format!("maybe a missing crate `{}`?", ident), None)
(
format!("maybe a missing crate `{ident}`?"),
Some((
vec![],
format!(
"consider adding `extern crate {ident}` to use the `{ident}` crate"
),
Applicability::MaybeIncorrect,
)),
)
} else {
(format!("could not find `{}` in the crate root", ident), None)
(format!("could not find `{ident}` in the crate root"), None)
}
} else if i > 0 {
let parent = path[i - 1].ident.name;
@@ -1849,7 +1858,7 @@ impl<'a> Resolver<'a> {
"the list of imported crates".to_owned()
}
kw::PathRoot | kw::Crate => "the crate root".to_owned(),
_ => format!("`{}`", parent),
_ => format!("`{parent}`"),
};

let mut msg = format!("could not find `{}` in {}", ident, parent);
4 changes: 4 additions & 0 deletions compiler/rustc_resolve/src/imports.rs
Original file line number Diff line number Diff line change
@@ -475,6 +475,10 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
}

if let Some((suggestions, msg, applicability)) = err.suggestion {
if suggestions.is_empty() {
diag.help(&msg);
continue;
}
diag.multipart_suggestion(&msg, suggestions, applicability);
}
}
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@ error[E0433]: failed to resolve: maybe a missing crate `unresolved_crate`?
|
LL | use unresolved_crate::module::Name;
| ^^^^^^^^^^^^^^^^ maybe a missing crate `unresolved_crate`?
|
= help: consider adding `extern crate unresolved_crate` to use the `unresolved_crate` crate

error: Compilation failed, aborting rustdoc

2 changes: 2 additions & 0 deletions src/test/rustdoc-ui/issue-61732.stderr
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@ error[E0433]: failed to resolve: maybe a missing crate `r#mod`?
|
LL | pub(in crate::r#mod) fn main() {}
| ^^^^^ maybe a missing crate `r#mod`?
|
= help: consider adding `extern crate r#mod` to use the `r#mod` crate

error: Compilation failed, aborting rustdoc

4 changes: 4 additions & 0 deletions src/test/ui/attributes/field-attributes-vis-unresolved.stderr
Original file line number Diff line number Diff line change
@@ -3,12 +3,16 @@ error[E0433]: failed to resolve: maybe a missing crate `nonexistent`?
|
LL | pub(in nonexistent) field: u8
| ^^^^^^^^^^^ maybe a missing crate `nonexistent`?
|
= help: consider adding `extern crate nonexistent` to use the `nonexistent` crate

error[E0433]: failed to resolve: maybe a missing crate `nonexistent`?
--> $DIR/field-attributes-vis-unresolved.rs:22:12
|
LL | pub(in nonexistent) u8
| ^^^^^^^^^^^ maybe a missing crate `nonexistent`?
|
= help: consider adding `extern crate nonexistent` to use the `nonexistent` crate

error: aborting due to 2 previous errors

2 changes: 2 additions & 0 deletions src/test/ui/error-codes/E0432.stderr
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@ error[E0432]: unresolved import `something`
|
LL | use something::Foo;
| ^^^^^^^^^ maybe a missing crate `something`?
|
= help: consider adding `extern crate something` to use the `something` crate

error: aborting due to previous error

Original file line number Diff line number Diff line change
@@ -3,12 +3,16 @@ error[E0432]: unresolved import `core`
|
LL | use core::default;
| ^^^^ maybe a missing crate `core`?
|
= help: consider adding `extern crate core` to use the `core` crate

error[E0433]: failed to resolve: maybe a missing crate `core`?
--> $DIR/feature-gate-extern_absolute_paths.rs:4:19
|
LL | let _: u8 = ::core::default::Default();
| ^^^^ maybe a missing crate `core`?
|
= help: consider adding `extern crate core` to use the `core` crate

error: aborting due to 2 previous errors

2 changes: 2 additions & 0 deletions src/test/ui/imports/import3.stderr
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@ error[E0432]: unresolved import `main`
|
LL | use main::bar;
| ^^^^ maybe a missing crate `main`?
|
= help: consider adding `extern crate main` to use the `main` crate

error: aborting due to previous error

2 changes: 2 additions & 0 deletions src/test/ui/imports/issue-1697.stderr
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@ error[E0432]: unresolved import `unresolved`
|
LL | use unresolved::*;
| ^^^^^^^^^^ maybe a missing crate `unresolved`?
|
= help: consider adding `extern crate unresolved` to use the `unresolved` crate

error: aborting due to previous error

6 changes: 6 additions & 0 deletions src/test/ui/imports/issue-33464.stderr
Original file line number Diff line number Diff line change
@@ -3,18 +3,24 @@ error[E0432]: unresolved import `abc`
|
LL | use abc::one_el;
| ^^^ maybe a missing crate `abc`?
|
= help: consider adding `extern crate abc` to use the `abc` crate

error[E0432]: unresolved import `abc`
--> $DIR/issue-33464.rs:5:5
|
LL | use abc::{a, bbb, cccccc};
| ^^^ maybe a missing crate `abc`?
|
= help: consider adding `extern crate abc` to use the `abc` crate

error[E0432]: unresolved import `a_very_long_name`
--> $DIR/issue-33464.rs:7:5
|
LL | use a_very_long_name::{el, el2};
| ^^^^^^^^^^^^^^^^ maybe a missing crate `a_very_long_name`?
|
= help: consider adding `extern crate a_very_long_name` to use the `a_very_long_name` crate

error: aborting due to 3 previous errors

2 changes: 2 additions & 0 deletions src/test/ui/imports/issue-36881.stderr
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@ error[E0432]: unresolved import `issue_36881_aux`
|
LL | use issue_36881_aux::Foo;
| ^^^^^^^^^^^^^^^ maybe a missing crate `issue_36881_aux`?
|
= help: consider adding `extern crate issue_36881_aux` to use the `issue_36881_aux` crate

error: aborting due to previous error

2 changes: 2 additions & 0 deletions src/test/ui/imports/issue-37887.stderr
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@ error[E0432]: unresolved import `libc`
|
LL | use libc::*;
| ^^^^ maybe a missing crate `libc`?
|
= help: consider adding `extern crate libc` to use the `libc` crate

error[E0658]: use of unstable library feature 'rustc_private': this crate is being loaded from the sysroot, an unstable location; did you mean to load this crate from crates.io via `Cargo.toml` instead?
--> $DIR/issue-37887.rs:2:5
2 changes: 2 additions & 0 deletions src/test/ui/imports/issue-53269.stderr
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@ error[E0432]: unresolved import `nonexistent_module`
|
LL | use nonexistent_module::mac;
| ^^^^^^^^^^^^^^^^^^ maybe a missing crate `nonexistent_module`?
|
= help: consider adding `extern crate nonexistent_module` to use the `nonexistent_module` crate

error[E0659]: `mac` is ambiguous
--> $DIR/issue-53269.rs:8:5
2 changes: 2 additions & 0 deletions src/test/ui/imports/issue-55457.stderr
Original file line number Diff line number Diff line change
@@ -12,6 +12,8 @@ error[E0432]: unresolved import `non_existent`
|
LL | use non_existent::non_existent;
| ^^^^^^^^^^^^ maybe a missing crate `non_existent`?
|
= help: consider adding `extern crate non_existent` to use the `non_existent` crate

error: cannot determine resolution for the derive macro `NonExistent`
--> $DIR/issue-55457.rs:5:10
8 changes: 8 additions & 0 deletions src/test/ui/imports/tool-mod-child.stderr
Original file line number Diff line number Diff line change
@@ -3,24 +3,32 @@ error[E0433]: failed to resolve: maybe a missing crate `clippy`?
|
LL | use clippy::a::b;
| ^^^^^^ maybe a missing crate `clippy`?
|
= help: consider adding `extern crate clippy` to use the `clippy` crate

error[E0432]: unresolved import `clippy`
--> $DIR/tool-mod-child.rs:1:5
|
LL | use clippy::a;
| ^^^^^^ maybe a missing crate `clippy`?
|
= help: consider adding `extern crate clippy` to use the `clippy` crate

error[E0433]: failed to resolve: maybe a missing crate `rustdoc`?
--> $DIR/tool-mod-child.rs:5:5
|
LL | use rustdoc::a::b;
| ^^^^^^^ maybe a missing crate `rustdoc`?
|
= help: consider adding `extern crate rustdoc` to use the `rustdoc` crate

error[E0432]: unresolved import `rustdoc`
--> $DIR/tool-mod-child.rs:4:5
|
LL | use rustdoc::a;
| ^^^^^^^ maybe a missing crate `rustdoc`?
|
= help: consider adding `extern crate rustdoc` to use the `rustdoc` crate

error: aborting due to 4 previous errors

8 changes: 8 additions & 0 deletions src/test/ui/imports/unresolved-imports-used.stderr
Original file line number Diff line number Diff line change
@@ -15,24 +15,32 @@ error[E0432]: unresolved import `foo`
|
LL | use foo::bar;
| ^^^ maybe a missing crate `foo`?
|
= help: consider adding `extern crate foo` to use the `foo` crate

error[E0432]: unresolved import `baz`
--> $DIR/unresolved-imports-used.rs:12:5
|
LL | use baz::*;
| ^^^ maybe a missing crate `baz`?
|
= help: consider adding `extern crate baz` to use the `baz` crate

error[E0432]: unresolved import `foo2`
--> $DIR/unresolved-imports-used.rs:14:5
|
LL | use foo2::bar2;
| ^^^^ maybe a missing crate `foo2`?
|
= help: consider adding `extern crate foo2` to use the `foo2` crate

error[E0432]: unresolved import `baz2`
--> $DIR/unresolved-imports-used.rs:15:5
|
LL | use baz2::*;
| ^^^^ maybe a missing crate `baz2`?
|
= help: consider adding `extern crate baz2` to use the `baz2` crate

error[E0603]: function `quz` is private
--> $DIR/unresolved-imports-used.rs:9:10
Original file line number Diff line number Diff line change
@@ -14,6 +14,8 @@ error[E0432]: unresolved import `r#extern`
|
LL | use extern::foo;
| ^^^^^^ maybe a missing crate `r#extern`?
|
= help: consider adding `extern crate r#extern` to use the `r#extern` crate

error: aborting due to 2 previous errors

2 changes: 2 additions & 0 deletions src/test/ui/privacy/restricted/test.stderr
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@ error[E0433]: failed to resolve: maybe a missing crate `bad`?
|
LL | pub(in bad::path) mod m1 {}
| ^^^ maybe a missing crate `bad`?
|
= help: consider adding `extern crate bad` to use the `bad` crate

error[E0742]: visibilities can only be restricted to ancestor modules
--> $DIR/test.rs:51:12
4 changes: 4 additions & 0 deletions src/test/ui/resolve/editions-crate-root-2015.stderr
Original file line number Diff line number Diff line change
@@ -3,12 +3,16 @@ error[E0433]: failed to resolve: maybe a missing crate `nonexistant`?
|
LL | fn global_inner(_: ::nonexistant::Foo) {
| ^^^^^^^^^^^ maybe a missing crate `nonexistant`?
|
= help: consider adding `extern crate nonexistant` to use the `nonexistant` crate

error[E0433]: failed to resolve: maybe a missing crate `nonexistant`?
--> $DIR/editions-crate-root-2015.rs:7:30
|
LL | fn crate_inner(_: crate::nonexistant::Foo) {
| ^^^^^^^^^^^ maybe a missing crate `nonexistant`?
|
= help: consider adding `extern crate nonexistant` to use the `nonexistant` crate

error[E0412]: cannot find type `nonexistant` in the crate root
--> $DIR/editions-crate-root-2015.rs:11:25
4 changes: 4 additions & 0 deletions src/test/ui/resolve/extern-prelude-fail.stderr
Original file line number Diff line number Diff line change
@@ -3,12 +3,16 @@ error[E0432]: unresolved import `extern_prelude`
|
LL | use extern_prelude::S;
| ^^^^^^^^^^^^^^ maybe a missing crate `extern_prelude`?
|
= help: consider adding `extern crate extern_prelude` to use the `extern_prelude` crate

error[E0433]: failed to resolve: maybe a missing crate `extern_prelude`?
--> $DIR/extern-prelude-fail.rs:8:15
|
LL | let s = ::extern_prelude::S;
| ^^^^^^^^^^^^^^ maybe a missing crate `extern_prelude`?
|
= help: consider adding `extern crate extern_prelude` to use the `extern_prelude` crate

error: aborting due to 2 previous errors

2 changes: 2 additions & 0 deletions src/test/ui/resolve/issue-82865.stderr
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@ error[E0433]: failed to resolve: maybe a missing crate `x`?
|
LL | use x::y::z;
| ^ maybe a missing crate `x`?
|
= help: consider adding `extern crate x` to use the `x` crate

error[E0599]: no function or associated item named `z` found for struct `Box<_, _>` in the current scope
--> $DIR/issue-82865.rs:8:10
4 changes: 4 additions & 0 deletions src/test/ui/resolve/resolve-bad-visibility.stderr
Original file line number Diff line number Diff line change
@@ -21,12 +21,16 @@ error[E0433]: failed to resolve: maybe a missing crate `nonexistent`?
|
LL | pub(in nonexistent) struct G;
| ^^^^^^^^^^^ maybe a missing crate `nonexistent`?
|
= help: consider adding `extern crate nonexistent` to use the `nonexistent` crate

error[E0433]: failed to resolve: maybe a missing crate `too_soon`?
--> $DIR/resolve-bad-visibility.rs:8:8
|
LL | pub(in too_soon) struct H;
| ^^^^^^^^ maybe a missing crate `too_soon`?
|
= help: consider adding `extern crate too_soon` to use the `too_soon` crate

error: aborting due to 5 previous errors

2 changes: 2 additions & 0 deletions src/test/ui/simd/portable-intrinsics-arent-exposed.stderr
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@ error[E0433]: failed to resolve: maybe a missing crate `core`?
|
LL | use core::simd::intrinsics;
| ^^^^ maybe a missing crate `core`?
|
= help: consider adding `extern crate core` to use the `core` crate

error[E0432]: unresolved import `std::simd::intrinsics`
--> $DIR/portable-intrinsics-arent-exposed.rs:5:5
2 changes: 2 additions & 0 deletions src/test/ui/unresolved/unresolved-asterisk-imports.stderr
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@ error[E0432]: unresolved import `not_existing_crate`
|
LL | use not_existing_crate::*;
| ^^^^^^^^^^^^^^^^^^ maybe a missing crate `not_existing_crate`?
|
= help: consider adding `extern crate not_existing_crate` to use the `not_existing_crate` crate

error: aborting due to previous error

1 change: 1 addition & 0 deletions src/test/ui/unresolved/unresolved-import.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use foo::bar; //~ ERROR unresolved import `foo` [E0432]
//~^ maybe a missing crate `foo`?
//~| HELP consider adding `extern crate foo` to use the `foo` crate

use bar::Baz as x; //~ ERROR unresolved import `bar::Baz` [E0432]
//~| no `Baz` in `bar`
12 changes: 7 additions & 5 deletions src/test/ui/unresolved/unresolved-import.stderr
Original file line number Diff line number Diff line change
@@ -3,9 +3,11 @@ error[E0432]: unresolved import `foo`
|
LL | use foo::bar;
| ^^^ maybe a missing crate `foo`?
|
= help: consider adding `extern crate foo` to use the `foo` crate

error[E0432]: unresolved import `bar::Baz`
--> $DIR/unresolved-import.rs:4:5
--> $DIR/unresolved-import.rs:5:5
|
LL | use bar::Baz as x;
| ^^^^^---^^^^^
@@ -14,7 +16,7 @@ LL | use bar::Baz as x;
| no `Baz` in `bar`

error[E0432]: unresolved import `food::baz`
--> $DIR/unresolved-import.rs:9:5
--> $DIR/unresolved-import.rs:10:5
|
LL | use food::baz;
| ^^^^^^---
@@ -23,7 +25,7 @@ LL | use food::baz;
| no `baz` in `food`

error[E0432]: unresolved import `food::beens`
--> $DIR/unresolved-import.rs:14:12
--> $DIR/unresolved-import.rs:15:12
|
LL | use food::{beens as Foo};
| -----^^^^^^^
@@ -32,13 +34,13 @@ LL | use food::{beens as Foo};
| help: a similar name exists in the module: `beans`

error[E0432]: unresolved import `MyEnum`
--> $DIR/unresolved-import.rs:38:9
--> $DIR/unresolved-import.rs:39:9
|
LL | use MyEnum::*;
| ^^^^^^ help: a similar path exists: `self::MyEnum`

error[E0432]: unresolved import `Enum`
--> $DIR/unresolved-import.rs:48:9
--> $DIR/unresolved-import.rs:49:9
|
LL | use Enum::*;
| ^^^^ help: a similar path exists: `self::Enum`