@@ -14,7 +14,7 @@ use rustc_data_structures::sorted_map::SortedMap;
1414use rustc_data_structures:: unord:: UnordSet ;
1515use rustc_errors:: codes:: * ;
1616use rustc_errors:: {
17- Applicability , Diag , MultiSpan , StashKey , listify, pluralize, struct_span_code_err,
17+ Applicability , Diag , MultiSpan , StashKey , StringPart , listify, pluralize, struct_span_code_err,
1818} ;
1919use rustc_hir:: attrs:: diagnostic:: OnUnimplementedNote ;
2020use rustc_hir:: def:: { CtorKind , DefKind , Res } ;
@@ -1413,33 +1413,45 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14131413 }
14141414 } )
14151415 . collect :: < Vec < _ > > ( ) ;
1416- if !inherent_impls_candidate. is_empty ( ) {
1417- inherent_impls_candidate. sort_by_key ( |& id| self . tcx . def_path_str ( id) ) ;
1418- inherent_impls_candidate. dedup ( ) ;
1419-
1420- // number of types to show at most
1421- let limit = if inherent_impls_candidate. len ( ) == 5 { 5 } else { 4 } ;
1422- let type_candidates = inherent_impls_candidate
1423- . iter ( )
1424- . take ( limit)
1425- . map ( |impl_item| {
1426- format ! (
1427- "- `{}`" ,
1428- self . tcx. at( span) . type_of( * impl_item) . instantiate_identity( )
1429- )
1430- } )
1431- . collect :: < Vec < _ > > ( )
1432- . join ( "\n " ) ;
1433- let additional_types = if inherent_impls_candidate. len ( ) > limit {
1434- format ! ( "\n and {} more types" , inherent_impls_candidate. len( ) - limit)
1435- } else {
1436- "" . to_string ( )
1437- } ;
1438- err. note ( format ! (
1439- "the {item_kind} was found for\n {type_candidates}{additional_types}"
1440- ) ) ;
1441- * find_candidate_for_method = mode == Mode :: MethodCall ;
1442- }
1416+ inherent_impls_candidate. sort_by_key ( |& id| self . tcx . def_path_str ( id) ) ;
1417+ inherent_impls_candidate. dedup ( ) ;
1418+ let msg = match & inherent_impls_candidate[ ..] {
1419+ [ ] => return ,
1420+ [ only] => {
1421+ vec ! [
1422+ StringPart :: normal( format!( "the {item_kind} was found for `" ) ) ,
1423+ StringPart :: highlighted(
1424+ self . tcx. at( span) . type_of( * only) . instantiate_identity( ) . to_string( ) ,
1425+ ) ,
1426+ StringPart :: normal( format!( "`" ) ) ,
1427+ ]
1428+ }
1429+ candidates => {
1430+ // number of types to show at most
1431+ let limit = if candidates. len ( ) == 5 { 5 } else { 4 } ;
1432+ let type_candidates = candidates
1433+ . iter ( )
1434+ . take ( limit)
1435+ . map ( |impl_item| {
1436+ format ! (
1437+ "- `{}`" ,
1438+ self . tcx. at( span) . type_of( * impl_item) . instantiate_identity( )
1439+ )
1440+ } )
1441+ . collect :: < Vec < _ > > ( )
1442+ . join ( "\n " ) ;
1443+ let additional_types = if candidates. len ( ) > limit {
1444+ format ! ( "\n and {} more types" , candidates. len( ) - limit)
1445+ } else {
1446+ "" . to_string ( )
1447+ } ;
1448+ vec ! [ StringPart :: normal( format!(
1449+ "the {item_kind} was found for\n {type_candidates}{additional_types}"
1450+ ) ) ]
1451+ }
1452+ } ;
1453+ err. highlighted_note ( msg) ;
1454+ * find_candidate_for_method = mode == Mode :: MethodCall ;
14431455 }
14441456 } else {
14451457 let ty_str = if ty_str. len ( ) > 50 { String :: new ( ) } else { format ! ( "on `{ty_str}` " ) } ;
0 commit comments