Skip to content

Commit 3aefa86

Browse files
committed
Add help for potentially missing crate in Cargo.toml
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` ```
1 parent de01134 commit 3aefa86

File tree

59 files changed

+124
-44
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+124
-44
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
811811
}
812812
err.multipart_suggestion(msg, suggestions, applicability);
813813
}
814-
815814
if let Some(ModuleOrUniformRoot::Module(module)) = module
816815
&& let Some(module) = module.opt_def_id()
817816
&& let Some(segment) = segment
@@ -2037,7 +2036,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
20372036
self.current_crate_outer_attr_insert_span,
20382037
format!("extern crate {ident};\n"),
20392038
)],
2040-
format!("consider importing the `{ident}` crate"),
2039+
format!(
2040+
"if you wanted to use a crate named `{ident}`, use `cargo add {ident}` \
2041+
to add it to your `Cargo.toml` and import it in your code",
2042+
),
20412043
Applicability::MaybeIncorrect,
20422044
)),
20432045
)
@@ -2216,6 +2218,14 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
22162218
let descr = binding.res().descr();
22172219
(format!("{descr} `{ident}` is not a crate or module"), suggestion)
22182220
} else {
2221+
let suggestion = suggestion.or(Some((
2222+
vec![],
2223+
format!(
2224+
"if you wanted to use a crate named `{ident}`, use `cargo add {ident}` to \
2225+
add it to your `Cargo.toml`",
2226+
),
2227+
Applicability::MaybeIncorrect,
2228+
)));
22192229
(format!("use of unresolved module or unlinked crate `{ident}`"), suggestion)
22202230
}
22212231
}

tests/rustdoc-ui/ice-unresolved-import-100241.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0432]: unresolved import `inner`
44
LL | pub use inner::S;
55
| ^^^^^ use of unresolved module or unlinked crate `inner`
66
|
7-
help: consider importing the `inner` crate
7+
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
88
|
99
LL + extern crate inner;
1010
|

tests/rustdoc-ui/intra-doc/unresolved-import-recovery.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `unr
44
LL | use unresolved_crate::module::Name;
55
| ^^^^^^^^^^^^^^^^ use of unresolved module or unlinked crate `unresolved_crate`
66
|
7-
help: consider importing the `unresolved_crate` crate
7+
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
88
|
99
LL + extern crate unresolved_crate;
1010
|

tests/rustdoc-ui/issues/issue-61732.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `r#m
44
LL | pub(in crate::r#mod) fn main() {}
55
| ^^^^^ use of unresolved module or unlinked crate `r#mod`
66
|
7-
help: consider importing the `r#mod` crate
7+
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
88
|
99
LL + extern crate r#mod;
1010
|

tests/ui/attributes/field-attributes-vis-unresolved.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `non
44
LL | pub(in nonexistent) field: u8
55
| ^^^^^^^^^^^ use of unresolved module or unlinked crate `nonexistent`
66
|
7-
help: consider importing the `nonexistent` crate
7+
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
88
|
99
LL + extern crate nonexistent;
1010
|
@@ -15,7 +15,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `non
1515
LL | pub(in nonexistent) u8
1616
| ^^^^^^^^^^^ use of unresolved module or unlinked crate `nonexistent`
1717
|
18-
help: consider importing the `nonexistent` crate
18+
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
1919
|
2020
LL + extern crate nonexistent;
2121
|

tests/ui/coherence/conflicting-impl-with-err.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `nop
33
|
44
LL | impl From<nope::Thing> for Error {
55
| ^^^^ use of unresolved module or unlinked crate `nope`
6+
|
7+
= help: if you wanted to use a crate named `nope`, use `cargo add nope` to add it to your `Cargo.toml`
68

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

1317
error: aborting due to 2 previous errors
1418

tests/ui/delegation/bad-resolve.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `unr
8686
|
8787
LL | reuse unresolved_prefix::{a, b, c};
8888
| ^^^^^^^^^^^^^^^^^ use of unresolved module or unlinked crate `unresolved_prefix`
89+
|
90+
= help: if you wanted to use a crate named `unresolved_prefix`, use `cargo add unresolved_prefix` to add it to your `Cargo.toml`
8991

9092
error[E0433]: failed to resolve: `crate` in paths can only be used in start position
9193
--> $DIR/bad-resolve.rs:44:29

tests/ui/error-codes/E0432.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0432]: unresolved import `something`
44
LL | use something::Foo;
55
| ^^^^^^^^^ use of unresolved module or unlinked crate `something`
66
|
7-
help: consider importing the `something` crate
7+
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
88
|
99
LL + extern crate something;
1010
|

tests/ui/extern-flag/multiple-opts.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `som
33
|
44
LL | somedep::somefun();
55
| ^^^^^^^ use of unresolved module or unlinked crate `somedep`
6+
|
7+
= help: if you wanted to use a crate named `somedep`, use `cargo add somedep` to add it to your `Cargo.toml`
68

79
error: aborting due to 1 previous error
810

tests/ui/extern-flag/noprelude.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `som
33
|
44
LL | somedep::somefun();
55
| ^^^^^^^ use of unresolved module or unlinked crate `somedep`
6+
|
7+
= help: if you wanted to use a crate named `somedep`, use `cargo add somedep` to add it to your `Cargo.toml`
68

79
error: aborting due to 1 previous error
810

tests/ui/foreign/stashed-issue-121451.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `lib
33
|
44
LL | extern "C" fn _f() -> libc::uintptr_t {}
55
| ^^^^ use of unresolved module or unlinked crate `libc`
6+
|
7+
= help: if you wanted to use a crate named `libc`, use `cargo add libc` to add it to your `Cargo.toml`
68

79
error: aborting due to 1 previous error
810

tests/ui/hygiene/extern-prelude-from-opaque-fail-2018.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ LL | fn f() { my_core::mem::drop(0); }
2424
LL | a!();
2525
| ---- in this macro invocation
2626
|
27+
= help: if you wanted to use a crate named `my_core`, use `cargo add my_core` to add it to your `Cargo.toml`
2728
= help: consider importing this module:
2829
std::mem
2930
= note: this error originates in the macro `a` (in Nightly builds, run with -Z macro-backtrace for more info)
@@ -34,6 +35,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `my_
3435
LL | fn f() { my_core::mem::drop(0); }
3536
| ^^^^^^^ use of unresolved module or unlinked crate `my_core`
3637
|
38+
= help: if you wanted to use a crate named `my_core`, use `cargo add my_core` to add it to your `Cargo.toml`
3739
help: consider importing this module
3840
|
3941
LL + use std::mem;

tests/ui/hygiene/extern-prelude-from-opaque-fail.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ LL | fn f() { my_core::mem::drop(0); }
2424
LL | a!();
2525
| ---- in this macro invocation
2626
|
27+
= help: if you wanted to use a crate named `my_core`, use `cargo add my_core` to add it to your `Cargo.toml`
2728
= help: consider importing this module:
2829
my_core::mem
2930
= note: this error originates in the macro `a` (in Nightly builds, run with -Z macro-backtrace for more info)
@@ -34,6 +35,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `my_
3435
LL | fn f() { my_core::mem::drop(0); }
3536
| ^^^^^^^ use of unresolved module or unlinked crate `my_core`
3637
|
38+
= help: if you wanted to use a crate named `my_core`, use `cargo add my_core` to add it to your `Cargo.toml`
3739
help: consider importing this module
3840
|
3941
LL + use my_core::mem;

tests/ui/impl-trait/issue-72911.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `foo
33
|
44
LL | fn gather_from_file(dir_entry: &foo::MissingItem) -> impl Iterator<Item = Lint> {
55
| ^^^ use of unresolved module or unlinked crate `foo`
6+
|
7+
= help: if you wanted to use a crate named `foo`, use `cargo add foo` to add it to your `Cargo.toml`
68

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

1317
error: aborting due to 2 previous errors
1418

tests/ui/impl-trait/stashed-diag-issue-121504.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `xyz
33
|
44
LL | impl MyTrait for xyz::T {
55
| ^^^ use of unresolved module or unlinked crate `xyz`
6+
|
7+
= help: if you wanted to use a crate named `xyz`, use `cargo add xyz` to add it to your `Cargo.toml`
68

79
error: aborting due to 1 previous error
810

tests/ui/imports/import-from-missing-star-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0432]: unresolved import `spam`
44
LL | use spam::*;
55
| ^^^^ use of unresolved module or unlinked crate `spam`
66
|
7-
help: consider importing the `spam` crate
7+
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
88
|
99
LL + extern crate spam;
1010
|

tests/ui/imports/import-from-missing-star-3.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0432]: unresolved import `spam`
44
LL | use spam::*;
55
| ^^^^ use of unresolved module or unlinked crate `spam`
66
|
7-
help: consider importing the `spam` crate
7+
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
88
|
99
LL + extern crate spam;
1010
|
@@ -15,7 +15,7 @@ error[E0432]: unresolved import `spam`
1515
LL | use spam::*;
1616
| ^^^^ use of unresolved module or unlinked crate `spam`
1717
|
18-
help: consider importing the `spam` crate
18+
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
1919
|
2020
LL + extern crate spam;
2121
|

tests/ui/imports/import-from-missing-star.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0432]: unresolved import `spam`
44
LL | use spam::*;
55
| ^^^^ use of unresolved module or unlinked crate `spam`
66
|
7-
help: consider importing the `spam` crate
7+
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
88
|
99
LL + extern crate spam;
1010
|

tests/ui/imports/import3.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0432]: unresolved import `main`
44
LL | use main::bar;
55
| ^^^^ use of unresolved module or unlinked crate `main`
66
|
7-
help: consider importing the `main` crate
7+
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
88
|
99
LL + extern crate main;
1010
|

tests/ui/imports/issue-109343.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0432]: unresolved import `unresolved`
44
LL | pub use unresolved::f;
55
| ^^^^^^^^^^ use of unresolved module or unlinked crate `unresolved`
66
|
7-
help: consider importing the `unresolved` crate
7+
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
88
|
99
LL + extern crate unresolved;
1010
|

tests/ui/imports/issue-1697.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
use unresolved::*;
44
//~^ ERROR unresolved import `unresolved` [E0432]
55
//~| NOTE use of unresolved module or unlinked crate `unresolved`
6-
//~| HELP consider importing the `unresolved` crate
6+
//~| HELP if you wanted to use a crate named `unresolved`, use `cargo add unresolved` to add it to your `Cargo.toml`
77

88
fn main() {}

tests/ui/imports/issue-1697.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0432]: unresolved import `unresolved`
44
LL | use unresolved::*;
55
| ^^^^^^^^^^ use of unresolved module or unlinked crate `unresolved`
66
|
7-
help: consider importing the `unresolved` crate
7+
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
88
|
99
LL + extern crate unresolved;
1010
|

tests/ui/imports/issue-33464.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0432]: unresolved import `abc`
44
LL | use abc::one_el;
55
| ^^^ use of unresolved module or unlinked crate `abc`
66
|
7-
help: consider importing the `abc` crate
7+
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
88
|
99
LL + extern crate abc;
1010
|
@@ -15,7 +15,7 @@ error[E0432]: unresolved import `abc`
1515
LL | use abc::{a, bbb, cccccc};
1616
| ^^^ use of unresolved module or unlinked crate `abc`
1717
|
18-
help: consider importing the `abc` crate
18+
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
1919
|
2020
LL + extern crate abc;
2121
|
@@ -26,7 +26,7 @@ error[E0432]: unresolved import `a_very_long_name`
2626
LL | use a_very_long_name::{el, el2};
2727
| ^^^^^^^^^^^^^^^^ use of unresolved module or unlinked crate `a_very_long_name`
2828
|
29-
help: consider importing the `a_very_long_name` crate
29+
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
3030
|
3131
LL + extern crate a_very_long_name;
3232
|

tests/ui/imports/issue-36881.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0432]: unresolved import `issue_36881_aux`
44
LL | use issue_36881_aux::Foo;
55
| ^^^^^^^^^^^^^^^ use of unresolved module or unlinked crate `issue_36881_aux`
66
|
7-
help: consider importing the `issue_36881_aux` crate
7+
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
88
|
99
LL + extern crate issue_36881_aux;
1010
|

tests/ui/imports/issue-37887.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0432]: unresolved import `test`
44
LL | use test::*;
55
| ^^^^ use of unresolved module or unlinked crate `test`
66
|
7-
help: consider importing the `test` crate
7+
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
88
|
99
LL + extern crate test;
1010
|

tests/ui/imports/issue-53269.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0432]: unresolved import `nonexistent_module`
44
LL | use nonexistent_module::mac;
55
| ^^^^^^^^^^^^^^^^^^ use of unresolved module or unlinked crate `nonexistent_module`
66
|
7-
help: consider importing the `nonexistent_module` crate
7+
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
88
|
99
LL + extern crate nonexistent_module;
1010
|

tests/ui/imports/issue-55457.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ error[E0432]: unresolved import `non_existent`
1313
LL | use non_existent::non_existent;
1414
| ^^^^^^^^^^^^ use of unresolved module or unlinked crate `non_existent`
1515
|
16-
help: consider importing the `non_existent` crate
16+
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
1717
|
1818
LL + extern crate non_existent;
1919
|

tests/ui/imports/issue-81413.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0432]: unresolved import `doesnt_exist`
44
LL | pub use doesnt_exist::*;
55
| ^^^^^^^^^^^^ use of unresolved module or unlinked crate `doesnt_exist`
66
|
7-
help: consider importing the `doesnt_exist` crate
7+
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
88
|
99
LL + extern crate doesnt_exist;
1010
|

tests/ui/imports/tool-mod-child.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `cli
44
LL | use clippy::a::b;
55
| ^^^^^^ use of unresolved module or unlinked crate `clippy`
66
|
7-
help: consider importing the `clippy` crate
7+
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
88
|
99
LL + extern crate clippy;
1010
|
@@ -15,7 +15,7 @@ error[E0432]: unresolved import `clippy`
1515
LL | use clippy::a;
1616
| ^^^^^^ use of unresolved module or unlinked crate `clippy`
1717
|
18-
help: consider importing the `clippy` crate
18+
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
1919
|
2020
LL + extern crate clippy;
2121
|
@@ -26,7 +26,7 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `rus
2626
LL | use rustdoc::a::b;
2727
| ^^^^^^^ use of unresolved module or unlinked crate `rustdoc`
2828
|
29-
help: consider importing the `rustdoc` crate
29+
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
3030
|
3131
LL + extern crate rustdoc;
3232
|
@@ -37,7 +37,7 @@ error[E0432]: unresolved import `rustdoc`
3737
LL | use rustdoc::a;
3838
| ^^^^^^^ use of unresolved module or unlinked crate `rustdoc`
3939
|
40-
help: consider importing the `rustdoc` crate
40+
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
4141
|
4242
LL + extern crate rustdoc;
4343
|

0 commit comments

Comments
 (0)