@@ -788,13 +788,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
788788 ResolutionError :: SelfImportOnlyInImportListWithNonEmptyPrefix => {
789789 self . dcx ( ) . create_err ( errs:: SelfImportOnlyInImportListWithNonEmptyPrefix { span } )
790790 }
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}" ) ;
798793 err. span_label ( span, label) ;
799794
800795 if let Some ( ( suggestions, msg, applicability) ) = suggestion {
@@ -1003,15 +998,15 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
1003998 VisResolutionError :: AncestorOnly ( span) => {
1004999 self . dcx ( ) . create_err ( errs:: AncestorOnly ( span) )
10051000 }
1006- VisResolutionError :: FailedToResolve ( span, segment, label, suggestion, scope ) => self
1001+ VisResolutionError :: FailedToResolve ( span, segment, label, suggestion, message ) => self
10071002 . into_struct_error (
10081003 span,
10091004 ResolutionError :: FailedToResolve {
10101005 segment,
10111006 label,
10121007 suggestion,
10131008 module : None ,
1014- scope ,
1009+ message ,
10151010 } ,
10161011 ) ,
10171012 VisResolutionError :: ExpectedFound ( span, path_str, res) => {
@@ -2292,13 +2287,25 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
22922287 module : Option < ModuleOrUniformRoot < ' ra > > ,
22932288 failed_segment_idx : usize ,
22942289 ident : Ident ,
2295- ) -> ( String , Option < Suggestion > ) {
2290+ ) -> ( String , String , Option < Suggestion > ) {
22962291 let is_last = failed_segment_idx == path. len ( ) - 1 ;
22972292 let ns = if is_last { opt_ns. unwrap_or ( TypeNS ) } else { TypeNS } ;
22982293 let module_res = match module {
22992294 Some ( ModuleOrUniformRoot :: Module ( module) ) => module. res ( ) ,
23002295 _ => None ,
23012296 } ;
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+
23022309 if module_res == self . graph_root . res ( ) {
23032310 let is_mod = |res| matches ! ( res, Res :: Def ( DefKind :: Mod , _) ) ;
23042311 let mut candidates = self . lookup_import_candidates ( ident, TypeNS , parent_scope, is_mod) ;
@@ -2316,6 +2323,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
23162323 Path { segments, span : Span :: default ( ) , tokens : None }
23172324 } ;
23182325 (
2326+ message,
23192327 String :: from ( "unresolved import" ) ,
23202328 Some ( (
23212329 vec ! [ ( ident. span, pprust:: path_to_string( & path) ) ] ,
@@ -2325,6 +2333,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
23252333 )
23262334 } else if ident. name == sym:: core {
23272335 (
2336+ message,
23282337 format ! ( "you might be missing crate `{ident}`" ) ,
23292338 Some ( (
23302339 vec ! [ ( ident. span, "std" . to_string( ) ) ] ,
@@ -2333,9 +2342,14 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
23332342 ) ) ,
23342343 )
23352344 } 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+ )
23372350 } else if self . tcx . sess . is_rust_2015 ( ) {
23382351 (
2352+ format ! ( "cannot find module or crate `{ident}` in {scope}" ) ,
23392353 format ! ( "use of unresolved module or unlinked crate `{ident}`" ) ,
23402354 Some ( (
23412355 vec ! [ (
@@ -2344,8 +2358,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
23442358 ) ] ,
23452359 if was_invoked_from_cargo ( ) {
23462360 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",
23492364 )
23502365 } else {
23512366 format ! (
@@ -2357,7 +2372,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
23572372 ) ) ,
23582373 )
23592374 } else {
2360- ( format ! ( "could not find `{ident}` in the crate root" ) , None )
2375+ ( message , format ! ( "could not find `{ident}` in the crate root" ) , None )
23612376 }
23622377 } else if failed_segment_idx > 0 {
23632378 let parent = path[ failed_segment_idx - 1 ] . ident . name ;
@@ -2423,15 +2438,16 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
24232438 ) ;
24242439 } ;
24252440 }
2426- ( msg, None )
2441+ ( message , msg, None )
24272442 } else if ident. name == kw:: SelfUpper {
24282443 // As mentioned above, `opt_ns` being `None` indicates a module path in import.
24292444 // We can use this to improve a confusing error for, e.g. `use Self::Variant` in an
24302445 // impl
24312446 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 )
24332448 } else {
24342449 (
2450+ message,
24352451 "`Self` is only available in impls, traits, and type definitions" . to_string ( ) ,
24362452 None ,
24372453 )
@@ -2485,7 +2501,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
24852501 )
24862502 } ) ;
24872503
2488- ( format ! ( "use of undeclared type `{ident}`" ) , suggestion)
2504+ ( message , format ! ( "use of undeclared type `{ident}`" ) , suggestion)
24892505 } else {
24902506 let mut suggestion = None ;
24912507 if ident. name == sym:: alloc {
@@ -2517,7 +2533,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
25172533 ignore_import,
25182534 ) {
25192535 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)
25212538 } else {
25222539 let suggestion = if suggestion. is_some ( ) {
25232540 suggestion
@@ -2539,7 +2556,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
25392556 Applicability :: MaybeIncorrect ,
25402557 ) )
25412558 } ;
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+ )
25432565 }
25442566 }
25452567 }
0 commit comments