@@ -188,7 +188,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
188
188
. span_if_local ( def_id)
189
189
. unwrap_or_else ( || self . tcx . def_span ( def_id) ) ;
190
190
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 ) ;
192
192
err. emit ( ) ;
193
193
}
194
194
@@ -2779,6 +2779,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2779
2779
fn suggest_valid_traits (
2780
2780
& self ,
2781
2781
err : & mut Diagnostic ,
2782
+ item_name : Ident ,
2782
2783
valid_out_of_scope_traits : Vec < DefId > ,
2783
2784
explain : bool ,
2784
2785
) -> bool {
@@ -2797,9 +2798,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2797
2798
err. help ( "items from traits can only be used if the trait is in scope" ) ;
2798
2799
}
2799
2800
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
+ } ,
2803
2811
one_of_them = if candidates. len( ) == 1 { "it" } else { "one of them" } ,
2804
2812
) ;
2805
2813
@@ -3007,7 +3015,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
3007
3015
}
3008
3016
}
3009
3017
}
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 ) {
3011
3019
return ;
3012
3020
}
3013
3021
@@ -3293,7 +3301,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
3293
3301
[ ] => { }
3294
3302
[ trait_info] if trait_info. def_id . is_local ( ) => {
3295
3303
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 ) ;
3297
3305
} else {
3298
3306
err. subdiagnostic ( CandidateTraitNote {
3299
3307
span : self . tcx . def_span ( trait_info. def_id ) ,
@@ -3317,7 +3325,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
3317
3325
) ) ;
3318
3326
for ( i, trait_info) in trait_infos. iter ( ) . enumerate ( ) {
3319
3327
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
+ ) ;
3321
3334
}
3322
3335
msg. push_str ( & format ! (
3323
3336
"\n candidate #{}: `{}`" ,
0 commit comments