@@ -275,7 +275,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
275
275
. span_if_local ( def_id)
276
276
. unwrap_or_else ( || self . tcx . def_span ( def_id) ) ;
277
277
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 ) ;
279
279
err. emit ( ) ;
280
280
}
281
281
@@ -2890,6 +2890,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2890
2890
fn suggest_valid_traits (
2891
2891
& self ,
2892
2892
err : & mut DiagnosticBuilder < ' _ > ,
2893
+ item_name : Ident ,
2893
2894
valid_out_of_scope_traits : Vec < DefId > ,
2894
2895
explain : bool ,
2895
2896
) -> bool {
@@ -2908,9 +2909,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2908
2909
err. help ( "items from traits can only be used if the trait is in scope" ) ;
2909
2910
}
2910
2911
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
+ } ,
2914
2922
one_of_them = if candidates. len( ) == 1 { "it" } else { "one of them" } ,
2915
2923
) ;
2916
2924
@@ -3118,7 +3126,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
3118
3126
}
3119
3127
}
3120
3128
}
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 ) {
3122
3130
return ;
3123
3131
}
3124
3132
@@ -3404,7 +3412,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
3404
3412
[ ] => { }
3405
3413
[ trait_info] if trait_info. def_id . is_local ( ) => {
3406
3414
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 ) ;
3408
3416
} else {
3409
3417
err. subdiagnostic (
3410
3418
self . dcx ( ) ,
@@ -3431,7 +3439,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
3431
3439
) ) ;
3432
3440
for ( i, trait_info) in trait_infos. iter ( ) . enumerate ( ) {
3433
3441
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
+ ) ;
3435
3448
}
3436
3449
msg. push_str ( & format ! (
3437
3450
"\n candidate #{}: `{}`" ,
0 commit comments