@@ -275,7 +275,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
275275 . span_if_local ( def_id)
276276 . unwrap_or_else ( || self . tcx . def_span ( def_id) ) ;
277277 err. span_label ( sp, format ! ( "private {kind} defined here" ) ) ;
278- self . suggest_valid_traits ( & mut err, out_of_scope_traits, true ) ;
278+ self . suggest_valid_traits ( & mut err, item_name , out_of_scope_traits, true ) ;
279279 err. emit ( ) ;
280280 }
281281
@@ -2890,6 +2890,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
28902890 fn suggest_valid_traits (
28912891 & self ,
28922892 err : & mut DiagnosticBuilder < ' _ > ,
2893+ item_name : Ident ,
28932894 valid_out_of_scope_traits : Vec < DefId > ,
28942895 explain : bool ,
28952896 ) -> bool {
@@ -2908,9 +2909,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
29082909 err. help ( "items from traits can only be used if the trait is in scope" ) ;
29092910 }
29102911 let msg = format ! (
2911- "the following {traits_are} implemented but not in scope; \
2912- perhaps add a `use` for {one_of_them}:",
2913- traits_are = if candidates. len( ) == 1 { "trait is" } else { "traits are" } ,
2912+ "{this_trait_is} implemented but not in scope; perhaps you want to import \
2913+ {one_of_them}",
2914+ this_trait_is = if candidates. len( ) == 1 {
2915+ format!(
2916+ "trait `{}` which provides `{item_name}` is" ,
2917+ self . tcx. item_name( candidates[ 0 ] ) ,
2918+ )
2919+ } else {
2920+ format!( "the following traits which provide `{item_name}` are" )
2921+ } ,
29142922 one_of_them = if candidates. len( ) == 1 { "it" } else { "one of them" } ,
29152923 ) ;
29162924
@@ -3118,7 +3126,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
31183126 }
31193127 }
31203128 }
3121- if self . suggest_valid_traits ( err, valid_out_of_scope_traits, true ) {
3129+ if self . suggest_valid_traits ( err, item_name , valid_out_of_scope_traits, true ) {
31223130 return ;
31233131 }
31243132
@@ -3404,7 +3412,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
34043412 [ ] => { }
34053413 [ trait_info] if trait_info. def_id . is_local ( ) => {
34063414 if impls_trait ( trait_info. def_id ) {
3407- self . suggest_valid_traits ( err, vec ! [ trait_info. def_id] , false ) ;
3415+ self . suggest_valid_traits ( err, item_name , vec ! [ trait_info. def_id] , false ) ;
34083416 } else {
34093417 err. subdiagnostic (
34103418 self . dcx ( ) ,
@@ -3431,7 +3439,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
34313439 ) ) ;
34323440 for ( i, trait_info) in trait_infos. iter ( ) . enumerate ( ) {
34333441 if impls_trait ( trait_info. def_id ) {
3434- self . suggest_valid_traits ( err, vec ! [ trait_info. def_id] , false ) ;
3442+ self . suggest_valid_traits (
3443+ err,
3444+ item_name,
3445+ vec ! [ trait_info. def_id] ,
3446+ false ,
3447+ ) ;
34353448 }
34363449 msg. push_str ( & format ! (
34373450 "\n candidate #{}: `{}`" ,
0 commit comments