Skip to content

Commit b5caa1e

Browse files
committed
fix false positive typoed crate or module suggestion
fix false positive typoed crate or module suggestion
1 parent acbe444 commit b5caa1e

File tree

4 files changed

+133
-37
lines changed

4 files changed

+133
-37
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+40-19
Original file line numberDiff line numberDiff line change
@@ -1331,28 +1331,49 @@ impl<'a> Resolver<'a> {
13311331

13321332
crate fn find_similarly_named_module_or_crate(
13331333
&mut self,
1334-
ident: Symbol,
1335-
current_module: &Module<'a>,
1334+
ns: Namespace,
1335+
parent_scope: &ParentScope<'a>,
1336+
ident: Ident,
13361337
) -> Option<Symbol> {
1337-
let mut candidates = self
1338-
.extern_prelude
1338+
let current_module = parent_scope.module;
1339+
let candidates = self
1340+
.resolutions(current_module)
1341+
.borrow()
13391342
.iter()
1340-
.map(|(ident, _)| ident.name)
1341-
.chain(
1342-
self.module_map
1343-
.iter()
1344-
.filter(|(_, module)| {
1345-
current_module.is_ancestor_of(module) && !ptr::eq(current_module, *module)
1346-
})
1347-
.map(|(_, module)| module.kind.name())
1348-
.flatten(),
1349-
)
1350-
.filter(|c| !c.to_string().is_empty())
1343+
.filter_map(|(key, res)| res.borrow().binding.map(|binding| (key, binding)))
1344+
.filter(|(_, binding)| {
1345+
matches!(
1346+
binding.res(),
1347+
Res::Def(DefKind::Mod, _) | Res::Def(DefKind::ExternCrate, _)
1348+
)
1349+
})
1350+
.map(|(key, binding)| (binding.is_extern_crate(), key.ident))
13511351
.collect::<Vec<_>>();
1352-
candidates.sort();
1353-
candidates.dedup();
1354-
match find_best_match_for_name(&candidates, ident, None) {
1355-
Some(sugg) if sugg == ident => None,
1352+
1353+
let candidates = candidates
1354+
.iter()
1355+
.filter_map(|(is_extern_crate, c)| {
1356+
if *is_extern_crate {
1357+
Some(c.name)
1358+
} else {
1359+
let mut ctxt = c.span.ctxt().normalize_to_macros_2_0();
1360+
let module = self.resolve_self(&mut ctxt, parent_scope.module);
1361+
self.resolve_ident_in_module(
1362+
ModuleOrUniformRoot::Module(module),
1363+
*c,
1364+
ns,
1365+
parent_scope,
1366+
false,
1367+
c.span,
1368+
)
1369+
.ok()
1370+
.map(|_| c.name)
1371+
}
1372+
})
1373+
.collect::<Vec<_>>();
1374+
1375+
match find_best_match_for_name(&candidates, ident.name, None) {
1376+
Some(sugg) if sugg == ident.name => None,
13561377
sugg => sugg,
13571378
}
13581379
}

compiler/rustc_resolve/src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2529,8 +2529,9 @@ impl<'a> Resolver<'a> {
25292529
))
25302530
} else {
25312531
self.find_similarly_named_module_or_crate(
2532-
ident.name,
2533-
&parent_scope.module,
2532+
ns,
2533+
parent_scope,
2534+
ident,
25342535
)
25352536
.map(|sugg| {
25362537
(

src/test/ui/suggestions/crate-or-module-typo.rs

+34-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,44 @@
11
// edition:2018
22

3-
use st::cell::Cell; //~ ERROR failed to resolve: use of undeclared crate or module `st`
3+
#![feature(decl_macro)]
4+
5+
macro a() {
6+
extern crate core as my_core;
7+
use my_cor::mem;
8+
mod a {
9+
pub fn bar() {}
10+
}
11+
}
412

5-
mod bar {
6-
pub fn bar() { bar::baz(); } //~ ERROR failed to resolve: use of undeclared crate or module `bar`
13+
macro_rules! b {
14+
() => {
15+
mod b {
16+
pub fn bar() {}
17+
}
18+
}
19+
}
20+
21+
mod foo {
22+
pub fn bar() { fooo::baz(); } //~ ERROR failed to resolve: use of undeclared crate or module `fooo`
723

824
fn baz() {}
925
}
1026

11-
use bas::bar; //~ ERROR unresolved import `bas`
27+
a!();
28+
29+
b!();
30+
31+
use my_cor::mem;
32+
33+
use my_core::mem;
34+
35+
use aa::bar; //~ ERROR unresolved import `aa`
36+
37+
use bb::bar; //~ ERROR unresolved import `bb`
38+
39+
use st::cell::Cell; //~ ERROR failed to resolve: use of undeclared crate or module `st`
40+
41+
use fooo::bar; //~ ERROR unresolved import `fooo`
1242

1343
struct Foo {
1444
bar: st::cell::Cell<bool> //~ ERROR failed to resolve: use of undeclared crate or module `st`
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
1+
error[E0432]: unresolved import `my_cor`
2+
--> $DIR/crate-or-module-typo.rs:31:5
3+
|
4+
LL | use my_cor::mem;
5+
| ^^^^^^ use of undeclared crate or module `my_cor`
6+
7+
error[E0432]: unresolved import `my_core`
8+
--> $DIR/crate-or-module-typo.rs:33:5
9+
|
10+
LL | use my_core::mem;
11+
| ^^^^^^^ use of undeclared crate or module `my_core`
12+
13+
error[E0432]: unresolved import `aa`
14+
--> $DIR/crate-or-module-typo.rs:35:5
15+
|
16+
LL | use aa::bar;
17+
| ^^ use of undeclared crate or module `aa`
18+
119
error[E0433]: failed to resolve: use of undeclared crate or module `st`
2-
--> $DIR/crate-or-module-typo.rs:3:5
20+
--> $DIR/crate-or-module-typo.rs:39:5
321
|
422
LL | use st::cell::Cell;
523
| ^^ use of undeclared crate or module `st`
@@ -9,25 +27,51 @@ help: there is a crate or module with a similar name
927
LL | use std::cell::Cell;
1028
| ~~~
1129

12-
error[E0432]: unresolved import `bas`
13-
--> $DIR/crate-or-module-typo.rs:11:5
30+
error[E0432]: unresolved import `bb`
31+
--> $DIR/crate-or-module-typo.rs:37:5
32+
|
33+
LL | use bb::bar;
34+
| ^^ use of undeclared crate or module `bb`
35+
|
36+
help: there is a crate or module with a similar name
37+
|
38+
LL | use b::bar;
39+
| ~
40+
41+
error[E0432]: unresolved import `fooo`
42+
--> $DIR/crate-or-module-typo.rs:41:5
1443
|
15-
LL | use bas::bar;
16-
| ^^^ use of undeclared crate or module `bas`
44+
LL | use fooo::bar;
45+
| ^^^^ use of undeclared crate or module `fooo`
1746
|
1847
help: there is a crate or module with a similar name
1948
|
20-
LL | use bar::bar;
49+
LL | use foo::bar;
2150
| ~~~
2251

23-
error[E0433]: failed to resolve: use of undeclared crate or module `bar`
24-
--> $DIR/crate-or-module-typo.rs:6:20
52+
error[E0432]: unresolved import `my_cor`
53+
--> $DIR/crate-or-module-typo.rs:7:9
54+
|
55+
LL | use my_cor::mem;
56+
| ^^^^^^ use of undeclared crate or module `my_cor`
57+
...
58+
LL | a!();
59+
| ----- in this macro invocation
60+
|
61+
= note: this error originates in the macro `a` (in Nightly builds, run with -Z macro-backtrace for more info)
62+
help: there is a crate or module with a similar name
63+
|
64+
LL | use my_core::mem;
65+
| ~~~~~~~
66+
67+
error[E0433]: failed to resolve: use of undeclared crate or module `fooo`
68+
--> $DIR/crate-or-module-typo.rs:22:20
2569
|
26-
LL | pub fn bar() { bar::baz(); }
27-
| ^^^ use of undeclared crate or module `bar`
70+
LL | pub fn bar() { fooo::baz(); }
71+
| ^^^^ use of undeclared crate or module `fooo`
2872

2973
error[E0433]: failed to resolve: use of undeclared crate or module `st`
30-
--> $DIR/crate-or-module-typo.rs:14:10
74+
--> $DIR/crate-or-module-typo.rs:44:10
3175
|
3276
LL | bar: st::cell::Cell<bool>
3377
| ^^ use of undeclared crate or module `st`
@@ -37,7 +81,7 @@ help: there is a crate or module with a similar name
3781
LL | bar: std::cell::Cell<bool>
3882
| ~~~
3983

40-
error: aborting due to 4 previous errors
84+
error: aborting due to 9 previous errors
4185

4286
Some errors have detailed explanations: E0432, E0433.
4387
For more information about an error, try `rustc --explain E0432`.

0 commit comments

Comments
 (0)