@@ -188,7 +188,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
188188 . span_if_local ( def_id)
189189 . unwrap_or_else ( || self . tcx . def_span ( def_id) ) ;
190190 err. span_label ( sp, format ! ( "private {kind} defined here" ) ) ;
191- self . suggest_valid_traits ( & mut err, out_of_scope_traits, true ) ;
191+ self . suggest_valid_traits ( & mut err, item_name , out_of_scope_traits, true ) ;
192192 err. emit ( ) ;
193193 }
194194
@@ -2779,6 +2779,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
27792779 fn suggest_valid_traits (
27802780 & self ,
27812781 err : & mut Diagnostic ,
2782+ item_name : Ident ,
27822783 valid_out_of_scope_traits : Vec < DefId > ,
27832784 explain : bool ,
27842785 ) -> bool {
@@ -2797,9 +2798,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
27972798 err. help ( "items from traits can only be used if the trait is in scope" ) ;
27982799 }
27992800 let msg = format ! (
2800- "the following {traits_are} implemented but not in scope; \
2801- perhaps add a `use` for {one_of_them}:",
2802- traits_are = if candidates. len( ) == 1 { "trait is" } else { "traits are" } ,
2801+ "{this_trait_is} implemented but not in scope; perhaps you want to import \
2802+ {one_of_them}",
2803+ this_trait_is = if candidates. len( ) == 1 {
2804+ format!(
2805+ "trait `{}` which provides `{item_name}` is" ,
2806+ self . tcx. item_name( candidates[ 0 ] ) ,
2807+ )
2808+ } else {
2809+ format!( "the following traits which provide `{item_name}` are" )
2810+ } ,
28032811 one_of_them = if candidates. len( ) == 1 { "it" } else { "one of them" } ,
28042812 ) ;
28052813
@@ -3007,7 +3015,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
30073015 }
30083016 }
30093017 }
3010- if self . suggest_valid_traits ( err, valid_out_of_scope_traits, true ) {
3018+ if self . suggest_valid_traits ( err, item_name , valid_out_of_scope_traits, true ) {
30113019 return ;
30123020 }
30133021
@@ -3293,7 +3301,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
32933301 [ ] => { }
32943302 [ trait_info] if trait_info. def_id . is_local ( ) => {
32953303 if impls_trait ( trait_info. def_id ) {
3296- self . suggest_valid_traits ( err, vec ! [ trait_info. def_id] , false ) ;
3304+ self . suggest_valid_traits ( err, item_name , vec ! [ trait_info. def_id] , false ) ;
32973305 } else {
32983306 err. subdiagnostic ( CandidateTraitNote {
32993307 span : self . tcx . def_span ( trait_info. def_id ) ,
@@ -3317,7 +3325,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
33173325 ) ) ;
33183326 for ( i, trait_info) in trait_infos. iter ( ) . enumerate ( ) {
33193327 if impls_trait ( trait_info. def_id ) {
3320- self . suggest_valid_traits ( err, vec ! [ trait_info. def_id] , false ) ;
3328+ self . suggest_valid_traits (
3329+ err,
3330+ item_name,
3331+ vec ! [ trait_info. def_id] ,
3332+ false ,
3333+ ) ;
33213334 }
33223335 msg. push_str ( & format ! (
33233336 "\n candidate #{}: `{}`" ,
0 commit comments