@@ -788,13 +788,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
788
788
ResolutionError :: SelfImportOnlyInImportListWithNonEmptyPrefix => {
789
789
self . dcx ( ) . create_err ( errs:: SelfImportOnlyInImportListWithNonEmptyPrefix { span } )
790
790
}
791
- ResolutionError :: FailedToResolve { segment, label, suggestion, module, scope } => {
792
- let mut err = struct_span_code_err ! (
793
- self . dcx( ) ,
794
- span,
795
- E0433 ,
796
- "cannot find `{segment}` in {scope}"
797
- ) ;
791
+ ResolutionError :: FailedToResolve { segment, label, suggestion, module, message } => {
792
+ let mut err = struct_span_code_err ! ( self . dcx( ) , span, E0433 , "{message}" ) ;
798
793
err. span_label ( span, label) ;
799
794
800
795
if let Some ( ( suggestions, msg, applicability) ) = suggestion {
@@ -1003,15 +998,15 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
1003
998
VisResolutionError :: AncestorOnly ( span) => {
1004
999
self . dcx ( ) . create_err ( errs:: AncestorOnly ( span) )
1005
1000
}
1006
- VisResolutionError :: FailedToResolve ( span, segment, label, suggestion, scope ) => self
1001
+ VisResolutionError :: FailedToResolve ( span, segment, label, suggestion, message ) => self
1007
1002
. into_struct_error (
1008
1003
span,
1009
1004
ResolutionError :: FailedToResolve {
1010
1005
segment,
1011
1006
label,
1012
1007
suggestion,
1013
1008
module : None ,
1014
- scope ,
1009
+ message ,
1015
1010
} ,
1016
1011
) ,
1017
1012
VisResolutionError :: ExpectedFound ( span, path_str, res) => {
@@ -2292,13 +2287,25 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
2292
2287
module : Option < ModuleOrUniformRoot < ' ra > > ,
2293
2288
failed_segment_idx : usize ,
2294
2289
ident : Ident ,
2295
- ) -> ( String , Option < Suggestion > ) {
2290
+ ) -> ( String , String , Option < Suggestion > ) {
2296
2291
let is_last = failed_segment_idx == path. len ( ) - 1 ;
2297
2292
let ns = if is_last { opt_ns. unwrap_or ( TypeNS ) } else { TypeNS } ;
2298
2293
let module_res = match module {
2299
2294
Some ( ModuleOrUniformRoot :: Module ( module) ) => module. res ( ) ,
2300
2295
_ => None ,
2301
2296
} ;
2297
+ let scope = match & path[ ..failed_segment_idx] {
2298
+ [ .., prev] => {
2299
+ if prev. ident . name == kw:: PathRoot {
2300
+ format ! ( "the crate root" )
2301
+ } else {
2302
+ format ! ( "`{}`" , prev. ident)
2303
+ }
2304
+ }
2305
+ _ => format ! ( "this scope" ) ,
2306
+ } ;
2307
+ let message = format ! ( "cannot find `{ident}` in {scope}" ) ;
2308
+
2302
2309
if module_res == self . graph_root . res ( ) {
2303
2310
let is_mod = |res| matches ! ( res, Res :: Def ( DefKind :: Mod , _) ) ;
2304
2311
let mut candidates = self . lookup_import_candidates ( ident, TypeNS , parent_scope, is_mod) ;
@@ -2316,6 +2323,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
2316
2323
Path { segments, span : Span :: default ( ) , tokens : None }
2317
2324
} ;
2318
2325
(
2326
+ message,
2319
2327
String :: from ( "unresolved import" ) ,
2320
2328
Some ( (
2321
2329
vec ! [ ( ident. span, pprust:: path_to_string( & path) ) ] ,
@@ -2325,6 +2333,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
2325
2333
)
2326
2334
} else if ident. name == sym:: core {
2327
2335
(
2336
+ message,
2328
2337
format ! ( "you might be missing crate `{ident}`" ) ,
2329
2338
Some ( (
2330
2339
vec ! [ ( ident. span, "std" . to_string( ) ) ] ,
@@ -2333,9 +2342,14 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
2333
2342
) ) ,
2334
2343
)
2335
2344
} else if ident. name == kw:: Underscore {
2336
- ( format ! ( "`_` is not a valid crate or module name" ) , None )
2345
+ (
2346
+ "invalid crate or module name `_`" . to_string ( ) ,
2347
+ "`_` is not a valid crate or module name" . to_string ( ) ,
2348
+ None ,
2349
+ )
2337
2350
} else if self . tcx . sess . is_rust_2015 ( ) {
2338
2351
(
2352
+ format ! ( "cannot find module or crate `{ident}` in {scope}" ) ,
2339
2353
format ! ( "use of unresolved module or unlinked crate `{ident}`" ) ,
2340
2354
Some ( (
2341
2355
vec ! [ (
@@ -2344,8 +2358,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
2344
2358
) ] ,
2345
2359
if was_invoked_from_cargo ( ) {
2346
2360
format ! (
2347
- "if you wanted to use a crate named `{ident}`, use `cargo add {ident}` \
2348
- to add it to your `Cargo.toml` and import it in your code",
2361
+ "if you wanted to use a crate named `{ident}`, use `cargo add \
2362
+ {ident}` to add it to your `Cargo.toml` and import it in your \
2363
+ code",
2349
2364
)
2350
2365
} else {
2351
2366
format ! (
@@ -2357,7 +2372,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
2357
2372
) ) ,
2358
2373
)
2359
2374
} else {
2360
- ( format ! ( "could not find `{ident}` in the crate root" ) , None )
2375
+ ( message , format ! ( "could not find `{ident}` in the crate root" ) , None )
2361
2376
}
2362
2377
} else if failed_segment_idx > 0 {
2363
2378
let parent = path[ failed_segment_idx - 1 ] . ident . name ;
@@ -2423,15 +2438,16 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
2423
2438
) ;
2424
2439
} ;
2425
2440
}
2426
- ( msg, None )
2441
+ ( message , msg, None )
2427
2442
} else if ident. name == kw:: SelfUpper {
2428
2443
// As mentioned above, `opt_ns` being `None` indicates a module path in import.
2429
2444
// We can use this to improve a confusing error for, e.g. `use Self::Variant` in an
2430
2445
// impl
2431
2446
if opt_ns. is_none ( ) {
2432
- ( "`Self` cannot be used in imports" . to_string ( ) , None )
2447
+ ( message , "`Self` cannot be used in imports" . to_string ( ) , None )
2433
2448
} else {
2434
2449
(
2450
+ message,
2435
2451
"`Self` is only available in impls, traits, and type definitions" . to_string ( ) ,
2436
2452
None ,
2437
2453
)
@@ -2485,7 +2501,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
2485
2501
)
2486
2502
} ) ;
2487
2503
2488
- ( format ! ( "use of undeclared type `{ident}`" ) , suggestion)
2504
+ ( message , format ! ( "use of undeclared type `{ident}`" ) , suggestion)
2489
2505
} else {
2490
2506
let mut suggestion = None ;
2491
2507
if ident. name == sym:: alloc {
@@ -2517,7 +2533,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
2517
2533
ignore_import,
2518
2534
) {
2519
2535
let descr = binding. res ( ) . descr ( ) ;
2520
- ( format ! ( "{descr} `{ident}` is not a crate or module" ) , suggestion)
2536
+ let message = format ! ( "cannot find module or crate `{ident}` in {scope}" ) ;
2537
+ ( message, format ! ( "{descr} `{ident}` is not a crate or module" ) , suggestion)
2521
2538
} else {
2522
2539
let suggestion = if suggestion. is_some ( ) {
2523
2540
suggestion
@@ -2539,7 +2556,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
2539
2556
Applicability :: MaybeIncorrect ,
2540
2557
) )
2541
2558
} ;
2542
- ( format ! ( "use of unresolved module or unlinked crate `{ident}`" ) , suggestion)
2559
+ let message = format ! ( "cannot find module or crate `{ident}` in {scope}" ) ;
2560
+ (
2561
+ message,
2562
+ format ! ( "use of unresolved module or unlinked crate `{ident}`" ) ,
2563
+ suggestion,
2564
+ )
2543
2565
}
2544
2566
}
2545
2567
}
0 commit comments