Skip to content

Commit efac68b

Browse files
authored
Rollup merge of #89347 - TaKO8Ki:crate-or-module-typo, r=estebank
suggestion for typoed crate or module Previously, the compiler didn't suggest similarly named crates or modules. This pull request adds a suggestion for typoed crates or modules. #76208 before: ``` error[E0433]: failed to resolve: use of undeclared type or module `chono` --> src/main.rs:2:5 | 2 | use chono::prelude::*; | ^^^^^ use of undeclared type or module `chono` ``` after: ``` error[E0433]: failed to resolve: use of undeclared type or module `chono` --> src/main.rs:2:5 | 2 | use chono::prelude::*; | ^^^^^ | | | use of undeclared crate or module `chono` | help: a similar crate or module exists: `chrono` ```
2 parents eeb16a2 + f819e6d commit efac68b

File tree

5 files changed

+109
-1
lines changed

5 files changed

+109
-1
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+28
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,34 @@ impl<'a> Resolver<'a> {
12771277

12781278
err.emit();
12791279
}
1280+
1281+
crate fn find_similarly_named_module_or_crate(
1282+
&mut self,
1283+
ident: Symbol,
1284+
current_module: &Module<'a>,
1285+
) -> Option<Symbol> {
1286+
let mut candidates = self
1287+
.extern_prelude
1288+
.iter()
1289+
.map(|(ident, _)| ident.name)
1290+
.chain(
1291+
self.module_map
1292+
.iter()
1293+
.filter(|(_, module)| {
1294+
current_module.is_ancestor_of(module) && !ptr::eq(current_module, *module)
1295+
})
1296+
.map(|(_, module)| module.kind.name())
1297+
.flatten(),
1298+
)
1299+
.filter(|c| !c.to_string().is_empty())
1300+
.collect::<Vec<_>>();
1301+
candidates.sort();
1302+
candidates.dedup();
1303+
match find_best_match_for_name(&candidates, ident, None) {
1304+
Some(sugg) if sugg == ident => None,
1305+
sugg => sugg,
1306+
}
1307+
}
12801308
}
12811309

12821310
impl<'a, 'b> ImportResolver<'a, 'b> {

compiler/rustc_resolve/src/lib.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -2555,7 +2555,22 @@ impl<'a> Resolver<'a> {
25552555

25562556
(format!("use of undeclared type `{}`", ident), suggestion)
25572557
} else {
2558-
(format!("use of undeclared crate or module `{}`", ident), None)
2558+
(
2559+
format!("use of undeclared crate or module `{}`", ident),
2560+
self.find_similarly_named_module_or_crate(
2561+
ident.name,
2562+
&parent_scope.module,
2563+
)
2564+
.map(|sugg| {
2565+
(
2566+
vec![(ident.span, sugg.to_string())],
2567+
String::from(
2568+
"there is a crate or module with a similar name",
2569+
),
2570+
Applicability::MaybeIncorrect,
2571+
)
2572+
}),
2573+
)
25592574
}
25602575
} else {
25612576
let parent = path[i - 1].ident.name;

src/test/ui/macros/macro-inner-attributes.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ error[E0433]: failed to resolve: use of undeclared crate or module `a`
33
|
44
LL | a::bar();
55
| ^ use of undeclared crate or module `a`
6+
|
7+
help: there is a crate or module with a similar name
8+
|
9+
LL | b::bar();
10+
| ~
611

712
error: aborting due to previous error
813

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// edition:2018
2+
3+
use st::cell::Cell; //~ ERROR failed to resolve: use of undeclared crate or module `st`
4+
5+
mod bar {
6+
pub fn bar() { bar::baz(); } //~ ERROR failed to resolve: use of undeclared crate or module `bar`
7+
8+
fn baz() {}
9+
}
10+
11+
use bas::bar; //~ ERROR unresolved import `bas`
12+
13+
struct Foo {
14+
bar: st::cell::Cell<bool> //~ ERROR failed to resolve: use of undeclared crate or module `st`
15+
}
16+
17+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
error[E0433]: failed to resolve: use of undeclared crate or module `st`
2+
--> $DIR/crate-or-module-typo.rs:3:5
3+
|
4+
LL | use st::cell::Cell;
5+
| ^^ use of undeclared crate or module `st`
6+
|
7+
help: there is a crate or module with a similar name
8+
|
9+
LL | use std::cell::Cell;
10+
| ~~~
11+
12+
error[E0432]: unresolved import `bas`
13+
--> $DIR/crate-or-module-typo.rs:11:5
14+
|
15+
LL | use bas::bar;
16+
| ^^^ use of undeclared crate or module `bas`
17+
|
18+
help: there is a crate or module with a similar name
19+
|
20+
LL | use bar::bar;
21+
| ~~~
22+
23+
error[E0433]: failed to resolve: use of undeclared crate or module `bar`
24+
--> $DIR/crate-or-module-typo.rs:6:20
25+
|
26+
LL | pub fn bar() { bar::baz(); }
27+
| ^^^ use of undeclared crate or module `bar`
28+
29+
error[E0433]: failed to resolve: use of undeclared crate or module `st`
30+
--> $DIR/crate-or-module-typo.rs:14:10
31+
|
32+
LL | bar: st::cell::Cell<bool>
33+
| ^^ use of undeclared crate or module `st`
34+
|
35+
help: there is a crate or module with a similar name
36+
|
37+
LL | bar: std::cell::Cell<bool>
38+
| ~~~
39+
40+
error: aborting due to 4 previous errors
41+
42+
Some errors have detailed explanations: E0432, E0433.
43+
For more information about an error, try `rustc --explain E0432`.

0 commit comments

Comments
 (0)