@@ -2423,6 +2423,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
2423
2423
} else {
2424
2424
let suggestion = if suggestion. is_some ( ) {
2425
2425
suggestion
2426
+ } else if let Some ( m) = self . undeclared_module_exists ( ident) {
2427
+ self . undeclared_module_suggest_declare ( ident, m)
2426
2428
} else if was_invoked_from_cargo ( ) {
2427
2429
Some ( (
2428
2430
vec ! [ ] ,
@@ -2444,6 +2446,55 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
2444
2446
}
2445
2447
}
2446
2448
2449
+ fn undeclared_module_suggest_declare (
2450
+ & mut self ,
2451
+ ident : Ident ,
2452
+ path : std:: path:: PathBuf ,
2453
+ ) -> Option < ( Vec < ( Span , String ) > , String , Applicability ) > {
2454
+ Some ( (
2455
+ vec ! [ ( self . current_crate_outer_attr_insert_span, format!( "mod {ident};\n " ) ) ] ,
2456
+ format ! (
2457
+ "to make use of source file {}, use `mod {ident}` \
2458
+ in this file to declare the module",
2459
+ path. display( )
2460
+ ) ,
2461
+ Applicability :: MaybeIncorrect ,
2462
+ ) )
2463
+ }
2464
+
2465
+ fn undeclared_module_exists ( & mut self , ident : Ident ) -> Option < std:: path:: PathBuf > {
2466
+ let map = self . tcx . sess . source_map ( ) ;
2467
+
2468
+ let src = map. span_to_filename ( ident. span ) . into_local_path ( ) ?;
2469
+ let i = ident. as_str ( ) ;
2470
+ // FIXME: add case where non parent using undeclared module (hard?)
2471
+ let dir = src. parent ( ) ?;
2472
+ let src = src. file_stem ( ) ?. to_str ( ) ?;
2473
+ for file in [
2474
+ // …/x.rs
2475
+ dir. join ( i) . with_extension ( "rs" ) ,
2476
+ // …/x/mod.rs
2477
+ dir. join ( i) . join ( "mod.rs" ) ,
2478
+ ] {
2479
+ if file. exists ( ) {
2480
+ return Some ( file) ;
2481
+ }
2482
+ }
2483
+ if !matches ! ( src, "main" | "lib" | "mod" ) {
2484
+ for file in [
2485
+ // …/x/y.rs
2486
+ dir. join ( src) . join ( i) . with_extension ( "rs" ) ,
2487
+ // …/x/y/mod.rs
2488
+ dir. join ( src) . join ( i) . join ( "mod.rs" ) ,
2489
+ ] {
2490
+ if file. exists ( ) {
2491
+ return Some ( file) ;
2492
+ }
2493
+ }
2494
+ }
2495
+ None
2496
+ }
2497
+
2447
2498
/// Adds suggestions for a path that cannot be resolved.
2448
2499
#[ instrument( level = "debug" , skip( self , parent_scope) ) ]
2449
2500
pub ( crate ) fn make_path_suggestion (
0 commit comments