Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3aefa86

Browse files
committedNov 18, 2024·
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
|

0 commit comments

Comments
 (0)
Please sign in to comment.