@@ -49,7 +49,6 @@ use std::borrow::Cow;
4949use super :: probe:: { AutorefOrPtrAdjustment , IsSuggestion , Mode , ProbeScope } ;
5050use super :: { CandidateSource , MethodError , NoMatchData } ;
5151use rustc_hir:: intravisit:: Visitor ;
52- use std:: cmp:: { self , Ordering } ;
5352use std:: iter;
5453
5554impl < ' a , ' tcx > FnCtxt < ' a , ' tcx > {
@@ -1189,7 +1188,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11891188 } )
11901189 . collect :: < Vec < _ > > ( ) ;
11911190 if !inherent_impls_candidate. is_empty ( ) {
1192- inherent_impls_candidate. sort ( ) ;
1191+ inherent_impls_candidate. sort_by_key ( |id| self . tcx . def_path_str ( id ) ) ;
11931192 inherent_impls_candidate. dedup ( ) ;
11941193
11951194 // number of types to show at most
@@ -1570,7 +1569,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15701569 sources : & mut Vec < CandidateSource > ,
15711570 sugg_span : Option < Span > ,
15721571 ) {
1573- sources. sort ( ) ;
1572+ sources. sort_by_key ( |source| match source {
1573+ CandidateSource :: Trait ( id) => ( 0 , self . tcx . def_path_str ( id) ) ,
1574+ CandidateSource :: Impl ( id) => ( 1 , self . tcx . def_path_str ( id) ) ,
1575+ } ) ;
15741576 sources. dedup ( ) ;
15751577 // Dynamic limit to avoid hiding just one candidate, which is silly.
15761578 let limit = if sources. len ( ) == 5 { 5 } else { 4 } ;
@@ -2552,7 +2554,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
25522554 _ => None ,
25532555 } )
25542556 . collect ( ) ;
2555- preds. sort_by_key ( |pred| ( pred. def_id ( ) , pred . self_ty ( ) ) ) ;
2557+ preds. sort_by_key ( |pred| pred. trait_ref . to_string ( ) ) ;
25562558 let def_ids = preds
25572559 . iter ( )
25582560 . filter_map ( |pred| match pred. self_ty ( ) . kind ( ) {
@@ -2666,7 +2668,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
26662668 traits. push ( trait_pred. def_id ( ) ) ;
26672669 }
26682670 }
2669- traits. sort ( ) ;
2671+ traits. sort_by_key ( |id| self . tcx . def_path_str ( id ) ) ;
26702672 traits. dedup ( ) ;
26712673
26722674 let len = traits. len ( ) ;
@@ -2889,7 +2891,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
28892891 ) -> bool {
28902892 if !valid_out_of_scope_traits. is_empty ( ) {
28912893 let mut candidates = valid_out_of_scope_traits;
2892- candidates. sort ( ) ;
2894+ candidates. sort_by_key ( |id| self . tcx . def_path_str ( id ) ) ;
28932895 candidates. dedup ( ) ;
28942896
28952897 // `TryFrom` and `FromIterator` have no methods
@@ -3215,8 +3217,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
32153217 }
32163218
32173219 if !candidates. is_empty ( ) {
3218- // Sort from most relevant to least relevant.
3219- candidates. sort_by_key ( |& info| cmp:: Reverse ( info) ) ;
3220+ // Sort local crate results before others
3221+ candidates
3222+ . sort_by_key ( |& info| ( !info. def_id . is_local ( ) , self . tcx . def_path_str ( info. def_id ) ) ) ;
32203223 candidates. dedup ( ) ;
32213224
32223225 let param_type = match rcvr_ty. kind ( ) {
@@ -3564,33 +3567,11 @@ pub enum SelfSource<'a> {
35643567 MethodCall ( & ' a hir:: Expr < ' a > /* rcvr */ ) ,
35653568}
35663569
3567- #[ derive( Copy , Clone ) ]
3570+ #[ derive( Copy , Clone , PartialEq , Eq ) ]
35683571pub struct TraitInfo {
35693572 pub def_id : DefId ,
35703573}
35713574
3572- impl PartialEq for TraitInfo {
3573- fn eq ( & self , other : & TraitInfo ) -> bool {
3574- self . cmp ( other) == Ordering :: Equal
3575- }
3576- }
3577- impl Eq for TraitInfo { }
3578- impl PartialOrd for TraitInfo {
3579- fn partial_cmp ( & self , other : & TraitInfo ) -> Option < Ordering > {
3580- Some ( self . cmp ( other) )
3581- }
3582- }
3583- impl Ord for TraitInfo {
3584- fn cmp ( & self , other : & TraitInfo ) -> Ordering {
3585- // Local crates are more important than remote ones (local:
3586- // `cnum == 0`), and otherwise we throw in the defid for totality.
3587-
3588- let lhs = ( other. def_id . krate , other. def_id ) ;
3589- let rhs = ( self . def_id . krate , self . def_id ) ;
3590- lhs. cmp ( & rhs)
3591- }
3592- }
3593-
35943575/// Retrieves all traits in this crate and any dependent crates,
35953576/// and wraps them into `TraitInfo` for custom sorting.
35963577pub fn all_traits ( tcx : TyCtxt < ' _ > ) -> Vec < TraitInfo > {
0 commit comments