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 adaab66

Browse files
committedJan 24, 2025·
Reword "crate not found" resolve message
``` error[E0432]: unresolved import `some_novel_crate` --> file.rs:1:5 | 1 | use some_novel_crate::Type; | ^^^^^^^^^^^^^^^^ use of unresolved module or unlinked crate `some_novel_crate` ``` 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 dee7d0e commit adaab66

File tree

108 files changed

+413
-294
lines changed

Some content is hidden

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

108 files changed

+413
-294
lines changed
 

‎compiler/rustc_error_codes/src/error_codes/E0433.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ If you've expected to use a crate name:
1919

2020
```compile_fail
2121
use ferris_wheel::BigO;
22-
// error: failed to resolve: use of undeclared crate or module `ferris_wheel`
22+
// error: failed to resolve: use of undeclared module or unlinked crate `ferris_wheel`
2323
```
2424

2525
Make sure the crate has been added as a dependency in `Cargo.toml`.

‎compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use rustc_session::lint::builtin::{
2424
MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS,
2525
};
2626
use rustc_session::lint::{AmbiguityErrorDiag, BuiltinLintDiag};
27+
use rustc_session::utils::was_invoked_from_cargo;
2728
use rustc_span::edit_distance::find_best_match_for_name;
2829
use rustc_span::edition::Edition;
2930
use rustc_span::hygiene::MacroKind;
@@ -809,7 +810,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
809810
}
810811
err.multipart_suggestion(msg, suggestions, applicability);
811812
}
812-
813813
if let Some(ModuleOrUniformRoot::Module(module)) = module
814814
&& let Some(module) = module.opt_def_id()
815815
&& let Some(segment) = segment
@@ -2044,13 +2044,23 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
20442044
(format!("`_` is not a valid crate or module name"), None)
20452045
} else if self.tcx.sess.is_rust_2015() {
20462046
(
2047-
format!("you might be missing crate `{ident}`"),
2047+
format!("use of unresolved module or unlinked crate `{ident}`"),
20482048
Some((
20492049
vec![(
20502050
self.current_crate_outer_attr_insert_span,
20512051
format!("extern crate {ident};\n"),
20522052
)],
2053-
format!("consider importing the `{ident}` crate"),
2053+
if was_invoked_from_cargo() {
2054+
format!(
2055+
"if you wanted to use a crate named `{ident}`, use `cargo add {ident}` \
2056+
to add it to your `Cargo.toml` and import it in your code",
2057+
)
2058+
} else {
2059+
format!(
2060+
"you might be missing a crate named `{ident}`, add it to your \
2061+
project and import it in your code",
2062+
)
2063+
},
20542064
Applicability::MaybeIncorrect,
20552065
)),
20562066
)
@@ -2229,7 +2239,25 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
22292239
let descr = binding.res().descr();
22302240
(format!("{descr} `{ident}` is not a crate or module"), suggestion)
22312241
} else {
2232-
(format!("use of undeclared crate or module `{ident}`"), suggestion)
2242+
let suggestion = if suggestion.is_some() {
2243+
suggestion
2244+
} else if was_invoked_from_cargo() {
2245+
Some((
2246+
vec![],
2247+
format!(
2248+
"if you wanted to use a crate named `{ident}`, use `cargo add {ident}` \
2249+
to add it to your `Cargo.toml`",
2250+
),
2251+
Applicability::MaybeIncorrect,
2252+
))
2253+
} else {
2254+
Some((
2255+
vec![],
2256+
format!("you might be missing a crate named `{ident}`",),
2257+
Applicability::MaybeIncorrect,
2258+
))
2259+
};
2260+
(format!("use of unresolved module or unlinked crate `{ident}`"), suggestion)
22332261
}
22342262
}
22352263
}

0 commit comments

Comments
 (0)
Please sign in to comment.