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

Lines changed: 12 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

0 commit comments

Comments
 (0)