Skip to content

Commit

Permalink
Add help for potentially missing crate in Cargo.toml
Browse files Browse the repository at this point in the history
On resolve errors where there might be a missing crate, mention `cargo add foo`:

```
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `nope`
  --> $DIR/conflicting-impl-with-err.rs:4:11
   |
LL | impl From<nope::Thing> for Error {
   |           ^^^^ use of unresolved module or unlinked crate `nope`
   |
   = help: if you wanted to use a crate named `nope`, use `cargo add nope` to add it to your `Cargo.toml`
```
  • Loading branch information
estebank committed Nov 18, 2024
1 parent de01134 commit 3aefa86
Show file tree
Hide file tree
Showing 59 changed files with 124 additions and 44 deletions.
14 changes: 12 additions & 2 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
}
err.multipart_suggestion(msg, suggestions, applicability);
}

if let Some(ModuleOrUniformRoot::Module(module)) = module
&& let Some(module) = module.opt_def_id()
&& let Some(segment) = segment
Expand Down Expand Up @@ -2037,7 +2036,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
self.current_crate_outer_attr_insert_span,
format!("extern crate {ident};\n"),
)],
format!("consider importing the `{ident}` crate"),
format!(
"if you wanted to use a crate named `{ident}`, use `cargo add {ident}` \
to add it to your `Cargo.toml` and import it in your code",
),
Applicability::MaybeIncorrect,
)),
)
Expand Down Expand Up @@ -2216,6 +2218,14 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
let descr = binding.res().descr();
(format!("{descr} `{ident}` is not a crate or module"), suggestion)
} else {
let suggestion = suggestion.or(Some((
vec![],
format!(
"if you wanted to use a crate named `{ident}`, use `cargo add {ident}` to \
add it to your `Cargo.toml`",
),
Applicability::MaybeIncorrect,
)));
(format!("use of unresolved module or unlinked crate `{ident}`"), suggestion)
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/rustdoc-ui/ice-unresolved-import-100241.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0432]: unresolved import `inner`
LL | pub use inner::S;
| ^^^^^ use of unresolved module or unlinked crate `inner`
|
help: consider importing the `inner` crate
help: if you wanted to use a crate named `inner`, use `cargo add inner` to add it to your `Cargo.toml` and import it in your code
|
LL + extern crate inner;
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `unr
LL | use unresolved_crate::module::Name;
| ^^^^^^^^^^^^^^^^ use of unresolved module or unlinked crate `unresolved_crate`
|
help: consider importing the `unresolved_crate` crate
help: if you wanted to use a crate named `unresolved_crate`, use `cargo add unresolved_crate` to add it to your `Cargo.toml` and import it in your code
|
LL + extern crate unresolved_crate;
|
Expand Down
2 changes: 1 addition & 1 deletion tests/rustdoc-ui/issues/issue-61732.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `r#m
LL | pub(in crate::r#mod) fn main() {}
| ^^^^^ use of unresolved module or unlinked crate `r#mod`
|
help: consider importing the `r#mod` crate
help: if you wanted to use a crate named `r#mod`, use `cargo add r#mod` to add it to your `Cargo.toml` and import it in your code
|
LL + extern crate r#mod;
|
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/attributes/field-attributes-vis-unresolved.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `non
LL | pub(in nonexistent) field: u8
| ^^^^^^^^^^^ use of unresolved module or unlinked crate `nonexistent`
|
help: consider importing the `nonexistent` crate
help: if you wanted to use a crate named `nonexistent`, use `cargo add nonexistent` to add it to your `Cargo.toml` and import it in your code
|
LL + extern crate nonexistent;
|
Expand All @@ -15,7 +15,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `non
LL | pub(in nonexistent) u8
| ^^^^^^^^^^^ use of unresolved module or unlinked crate `nonexistent`
|
help: consider importing the `nonexistent` crate
help: if you wanted to use a crate named `nonexistent`, use `cargo add nonexistent` to add it to your `Cargo.toml` and import it in your code
|
LL + extern crate nonexistent;
|
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/coherence/conflicting-impl-with-err.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `nop
|
LL | impl From<nope::Thing> for Error {
| ^^^^ use of unresolved module or unlinked crate `nope`
|
= help: if you wanted to use a crate named `nope`, use `cargo add nope` to add it to your `Cargo.toml`

error[E0433]: failed to resolve: use of unresolved module or unlinked crate `nope`
--> $DIR/conflicting-impl-with-err.rs:5:16
|
LL | fn from(_: nope::Thing) -> Self {
| ^^^^ use of unresolved module or unlinked crate `nope`
|
= help: if you wanted to use a crate named `nope`, use `cargo add nope` to add it to your `Cargo.toml`

error: aborting due to 2 previous errors

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/delegation/bad-resolve.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `unr
|
LL | reuse unresolved_prefix::{a, b, c};
| ^^^^^^^^^^^^^^^^^ use of unresolved module or unlinked crate `unresolved_prefix`
|
= help: if you wanted to use a crate named `unresolved_prefix`, use `cargo add unresolved_prefix` to add it to your `Cargo.toml`

error[E0433]: failed to resolve: `crate` in paths can only be used in start position
--> $DIR/bad-resolve.rs:44:29
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/error-codes/E0432.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0432]: unresolved import `something`
LL | use something::Foo;
| ^^^^^^^^^ use of unresolved module or unlinked crate `something`
|
help: consider importing the `something` crate
help: if you wanted to use a crate named `something`, use `cargo add something` to add it to your `Cargo.toml` and import it in your code
|
LL + extern crate something;
|
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/extern-flag/multiple-opts.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `som
|
LL | somedep::somefun();
| ^^^^^^^ use of unresolved module or unlinked crate `somedep`
|
= help: if you wanted to use a crate named `somedep`, use `cargo add somedep` to add it to your `Cargo.toml`

error: aborting due to 1 previous error

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/extern-flag/noprelude.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `som
|
LL | somedep::somefun();
| ^^^^^^^ use of unresolved module or unlinked crate `somedep`
|
= help: if you wanted to use a crate named `somedep`, use `cargo add somedep` to add it to your `Cargo.toml`

error: aborting due to 1 previous error

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/foreign/stashed-issue-121451.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `lib
|
LL | extern "C" fn _f() -> libc::uintptr_t {}
| ^^^^ use of unresolved module or unlinked crate `libc`
|
= help: if you wanted to use a crate named `libc`, use `cargo add libc` to add it to your `Cargo.toml`

error: aborting due to 1 previous error

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/hygiene/extern-prelude-from-opaque-fail-2018.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ LL | fn f() { my_core::mem::drop(0); }
LL | a!();
| ---- in this macro invocation
|
= help: if you wanted to use a crate named `my_core`, use `cargo add my_core` to add it to your `Cargo.toml`
= help: consider importing this module:
std::mem
= note: this error originates in the macro `a` (in Nightly builds, run with -Z macro-backtrace for more info)
Expand All @@ -34,6 +35,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `my_
LL | fn f() { my_core::mem::drop(0); }
| ^^^^^^^ use of unresolved module or unlinked crate `my_core`
|
= help: if you wanted to use a crate named `my_core`, use `cargo add my_core` to add it to your `Cargo.toml`
help: consider importing this module
|
LL + use std::mem;
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/hygiene/extern-prelude-from-opaque-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ LL | fn f() { my_core::mem::drop(0); }
LL | a!();
| ---- in this macro invocation
|
= help: if you wanted to use a crate named `my_core`, use `cargo add my_core` to add it to your `Cargo.toml`
= help: consider importing this module:
my_core::mem
= note: this error originates in the macro `a` (in Nightly builds, run with -Z macro-backtrace for more info)
Expand All @@ -34,6 +35,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `my_
LL | fn f() { my_core::mem::drop(0); }
| ^^^^^^^ use of unresolved module or unlinked crate `my_core`
|
= help: if you wanted to use a crate named `my_core`, use `cargo add my_core` to add it to your `Cargo.toml`
help: consider importing this module
|
LL + use my_core::mem;
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/impl-trait/issue-72911.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `foo
|
LL | fn gather_from_file(dir_entry: &foo::MissingItem) -> impl Iterator<Item = Lint> {
| ^^^ use of unresolved module or unlinked crate `foo`
|
= help: if you wanted to use a crate named `foo`, use `cargo add foo` to add it to your `Cargo.toml`

error[E0433]: failed to resolve: use of unresolved module or unlinked crate `foo`
--> $DIR/issue-72911.rs:16:41
|
LL | fn lint_files() -> impl Iterator<Item = foo::MissingItem> {
| ^^^ use of unresolved module or unlinked crate `foo`
|
= help: if you wanted to use a crate named `foo`, use `cargo add foo` to add it to your `Cargo.toml`

error: aborting due to 2 previous errors

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/impl-trait/stashed-diag-issue-121504.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `xyz
|
LL | impl MyTrait for xyz::T {
| ^^^ use of unresolved module or unlinked crate `xyz`
|
= help: if you wanted to use a crate named `xyz`, use `cargo add xyz` to add it to your `Cargo.toml`

error: aborting due to 1 previous error

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/import-from-missing-star-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0432]: unresolved import `spam`
LL | use spam::*;
| ^^^^ use of unresolved module or unlinked crate `spam`
|
help: consider importing the `spam` crate
help: if you wanted to use a crate named `spam`, use `cargo add spam` to add it to your `Cargo.toml` and import it in your code
|
LL + extern crate spam;
|
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/imports/import-from-missing-star-3.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0432]: unresolved import `spam`
LL | use spam::*;
| ^^^^ use of unresolved module or unlinked crate `spam`
|
help: consider importing the `spam` crate
help: if you wanted to use a crate named `spam`, use `cargo add spam` to add it to your `Cargo.toml` and import it in your code
|
LL + extern crate spam;
|
Expand All @@ -15,7 +15,7 @@ error[E0432]: unresolved import `spam`
LL | use spam::*;
| ^^^^ use of unresolved module or unlinked crate `spam`
|
help: consider importing the `spam` crate
help: if you wanted to use a crate named `spam`, use `cargo add spam` to add it to your `Cargo.toml` and import it in your code
|
LL + extern crate spam;
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/import-from-missing-star.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0432]: unresolved import `spam`
LL | use spam::*;
| ^^^^ use of unresolved module or unlinked crate `spam`
|
help: consider importing the `spam` crate
help: if you wanted to use a crate named `spam`, use `cargo add spam` to add it to your `Cargo.toml` and import it in your code
|
LL + extern crate spam;
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/import3.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0432]: unresolved import `main`
LL | use main::bar;
| ^^^^ use of unresolved module or unlinked crate `main`
|
help: consider importing the `main` crate
help: if you wanted to use a crate named `main`, use `cargo add main` to add it to your `Cargo.toml` and import it in your code
|
LL + extern crate main;
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/issue-109343.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0432]: unresolved import `unresolved`
LL | pub use unresolved::f;
| ^^^^^^^^^^ use of unresolved module or unlinked crate `unresolved`
|
help: consider importing the `unresolved` crate
help: if you wanted to use a crate named `unresolved`, use `cargo add unresolved` to add it to your `Cargo.toml` and import it in your code
|
LL + extern crate unresolved;
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/issue-1697.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
use unresolved::*;
//~^ ERROR unresolved import `unresolved` [E0432]
//~| NOTE use of unresolved module or unlinked crate `unresolved`
//~| HELP consider importing the `unresolved` crate
//~| HELP if you wanted to use a crate named `unresolved`, use `cargo add unresolved` to add it to your `Cargo.toml`

fn main() {}
2 changes: 1 addition & 1 deletion tests/ui/imports/issue-1697.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0432]: unresolved import `unresolved`
LL | use unresolved::*;
| ^^^^^^^^^^ use of unresolved module or unlinked crate `unresolved`
|
help: consider importing the `unresolved` crate
help: if you wanted to use a crate named `unresolved`, use `cargo add unresolved` to add it to your `Cargo.toml` and import it in your code
|
LL + extern crate unresolved;
|
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/imports/issue-33464.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0432]: unresolved import `abc`
LL | use abc::one_el;
| ^^^ use of unresolved module or unlinked crate `abc`
|
help: consider importing the `abc` crate
help: if you wanted to use a crate named `abc`, use `cargo add abc` to add it to your `Cargo.toml` and import it in your code
|
LL + extern crate abc;
|
Expand All @@ -15,7 +15,7 @@ error[E0432]: unresolved import `abc`
LL | use abc::{a, bbb, cccccc};
| ^^^ use of unresolved module or unlinked crate `abc`
|
help: consider importing the `abc` crate
help: if you wanted to use a crate named `abc`, use `cargo add abc` to add it to your `Cargo.toml` and import it in your code
|
LL + extern crate abc;
|
Expand All @@ -26,7 +26,7 @@ error[E0432]: unresolved import `a_very_long_name`
LL | use a_very_long_name::{el, el2};
| ^^^^^^^^^^^^^^^^ use of unresolved module or unlinked crate `a_very_long_name`
|
help: consider importing the `a_very_long_name` crate
help: if you wanted to use a crate named `a_very_long_name`, use `cargo add a_very_long_name` to add it to your `Cargo.toml` and import it in your code
|
LL + extern crate a_very_long_name;
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/issue-36881.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0432]: unresolved import `issue_36881_aux`
LL | use issue_36881_aux::Foo;
| ^^^^^^^^^^^^^^^ use of unresolved module or unlinked crate `issue_36881_aux`
|
help: consider importing the `issue_36881_aux` crate
help: if you wanted to use a crate named `issue_36881_aux`, use `cargo add issue_36881_aux` to add it to your `Cargo.toml` and import it in your code
|
LL + extern crate issue_36881_aux;
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/issue-37887.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0432]: unresolved import `test`
LL | use test::*;
| ^^^^ use of unresolved module or unlinked crate `test`
|
help: consider importing the `test` crate
help: if you wanted to use a crate named `test`, use `cargo add test` to add it to your `Cargo.toml` and import it in your code
|
LL + extern crate test;
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/issue-53269.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0432]: unresolved import `nonexistent_module`
LL | use nonexistent_module::mac;
| ^^^^^^^^^^^^^^^^^^ use of unresolved module or unlinked crate `nonexistent_module`
|
help: consider importing the `nonexistent_module` crate
help: if you wanted to use a crate named `nonexistent_module`, use `cargo add nonexistent_module` to add it to your `Cargo.toml` and import it in your code
|
LL + extern crate nonexistent_module;
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/issue-55457.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ error[E0432]: unresolved import `non_existent`
LL | use non_existent::non_existent;
| ^^^^^^^^^^^^ use of unresolved module or unlinked crate `non_existent`
|
help: consider importing the `non_existent` crate
help: if you wanted to use a crate named `non_existent`, use `cargo add non_existent` to add it to your `Cargo.toml` and import it in your code
|
LL + extern crate non_existent;
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/issue-81413.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0432]: unresolved import `doesnt_exist`
LL | pub use doesnt_exist::*;
| ^^^^^^^^^^^^ use of unresolved module or unlinked crate `doesnt_exist`
|
help: consider importing the `doesnt_exist` crate
help: if you wanted to use a crate named `doesnt_exist`, use `cargo add doesnt_exist` to add it to your `Cargo.toml` and import it in your code
|
LL + extern crate doesnt_exist;
|
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/imports/tool-mod-child.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `cli
LL | use clippy::a::b;
| ^^^^^^ use of unresolved module or unlinked crate `clippy`
|
help: consider importing the `clippy` crate
help: if you wanted to use a crate named `clippy`, use `cargo add clippy` to add it to your `Cargo.toml` and import it in your code
|
LL + extern crate clippy;
|
Expand All @@ -15,7 +15,7 @@ error[E0432]: unresolved import `clippy`
LL | use clippy::a;
| ^^^^^^ use of unresolved module or unlinked crate `clippy`
|
help: consider importing the `clippy` crate
help: if you wanted to use a crate named `clippy`, use `cargo add clippy` to add it to your `Cargo.toml` and import it in your code
|
LL + extern crate clippy;
|
Expand All @@ -26,7 +26,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `rus
LL | use rustdoc::a::b;
| ^^^^^^^ use of unresolved module or unlinked crate `rustdoc`
|
help: consider importing the `rustdoc` crate
help: if you wanted to use a crate named `rustdoc`, use `cargo add rustdoc` to add it to your `Cargo.toml` and import it in your code
|
LL + extern crate rustdoc;
|
Expand All @@ -37,7 +37,7 @@ error[E0432]: unresolved import `rustdoc`
LL | use rustdoc::a;
| ^^^^^^^ use of unresolved module or unlinked crate `rustdoc`
|
help: consider importing the `rustdoc` crate
help: if you wanted to use a crate named `rustdoc`, use `cargo add rustdoc` to add it to your `Cargo.toml` and import it in your code
|
LL + extern crate rustdoc;
|
Expand Down
Loading

0 comments on commit 3aefa86

Please sign in to comment.