File tree 5 files changed +109
-1
lines changed
compiler/rustc_resolve/src
5 files changed +109
-1
lines changed Original file line number Diff line number Diff line change @@ -1277,6 +1277,34 @@ impl<'a> Resolver<'a> {
1277
1277
1278
1278
err. emit ( ) ;
1279
1279
}
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
+ }
1280
1308
}
1281
1309
1282
1310
impl < ' a , ' b > ImportResolver < ' a , ' b > {
Original file line number Diff line number Diff line change @@ -2555,7 +2555,22 @@ impl<'a> Resolver<'a> {
2555
2555
2556
2556
( format ! ( "use of undeclared type `{}`" , ident) , suggestion)
2557
2557
} 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
+ )
2559
2574
}
2560
2575
} else {
2561
2576
let parent = path[ i - 1 ] . ident . name ;
Original file line number Diff line number Diff line change @@ -3,6 +3,11 @@ error[E0433]: failed to resolve: use of undeclared crate or module `a`
3
3
|
4
4
LL | a::bar();
5
5
| ^ 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
+ | ~
6
11
7
12
error: aborting due to previous error
8
13
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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`.
You can’t perform that action at this time.
0 commit comments