Skip to content

Commit 203e6c1

Browse files
committedJan 25, 2025
Auto merge of #133154 - estebank:issue-133137, r=wesleywiser
Reword resolve errors caused by likely missing crate in dep tree Reword label and add `help`: ``` error[E0432]: unresolved import `some_novel_crate` --> f704.rs:1:5 | 1 | use some_novel_crate::Type; | ^^^^^^^^^^^^^^^^ use of unresolved module or unlinked crate `some_novel_crate` | = help: if you wanted to use a crate named `some_novel_crate`, use `cargo add some_novel_crate` to add it to your `Cargo.toml` ``` Fix #133137.
2 parents f940188 + dd52bfc commit 203e6c1

File tree

108 files changed

+414
-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

+414
-294
lines changed
 

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

+1-1
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
2323
```
2424

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

‎compiler/rustc_resolve/src/diagnostics.rs

+32-4
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;
@@ -800,7 +801,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
800801
}
801802
err.multipart_suggestion(msg, suggestions, applicability);
802803
}
803-
804804
if let Some(ModuleOrUniformRoot::Module(module)) = module
805805
&& let Some(module) = module.opt_def_id()
806806
&& let Some(segment) = segment
@@ -2034,13 +2034,23 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
20342034
(format!("`_` is not a valid crate or module name"), None)
20352035
} else if self.tcx.sess.is_rust_2015() {
20362036
(
2037-
format!("you might be missing crate `{ident}`"),
2037+
format!("use of unresolved module or unlinked crate `{ident}`"),
20382038
Some((
20392039
vec![(
20402040
self.current_crate_outer_attr_insert_span,
20412041
format!("extern crate {ident};\n"),
20422042
)],
2043-
format!("consider importing the `{ident}` crate"),
2043+
if was_invoked_from_cargo() {
2044+
format!(
2045+
"if you wanted to use a crate named `{ident}`, use `cargo add {ident}` \
2046+
to add it to your `Cargo.toml` and import it in your code",
2047+
)
2048+
} else {
2049+
format!(
2050+
"you might be missing a crate named `{ident}`, add it to your \
2051+
project and import it in your code",
2052+
)
2053+
},
20442054
Applicability::MaybeIncorrect,
20452055
)),
20462056
)
@@ -2219,7 +2229,25 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
22192229
let descr = binding.res().descr();
22202230
(format!("{descr} `{ident}` is not a crate or module"), suggestion)
22212231
} else {
2222-
(format!("use of undeclared crate or module `{ident}`"), suggestion)
2232+
let suggestion = if suggestion.is_some() {
2233+
suggestion
2234+
} else if was_invoked_from_cargo() {
2235+
Some((
2236+
vec![],
2237+
format!(
2238+
"if you wanted to use a crate named `{ident}`, use `cargo add {ident}` \
2239+
to add it to your `Cargo.toml`",
2240+
),
2241+
Applicability::MaybeIncorrect,
2242+
))
2243+
} else {
2244+
Some((
2245+
vec![],
2246+
format!("you might be missing a crate named `{ident}`",),
2247+
Applicability::MaybeIncorrect,
2248+
))
2249+
};
2250+
(format!("use of unresolved module or unlinked crate `{ident}`"), suggestion)
22232251
}
22242252
}
22252253
}

0 commit comments

Comments
 (0)
Please sign in to comment.