@@ -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 > {
@@ -1186,7 +1185,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11861185 } )
11871186 . collect :: < Vec < _ > > ( ) ;
11881187 if !inherent_impls_candidate. is_empty ( ) {
1189- inherent_impls_candidate. sort ( ) ;
1188+ inherent_impls_candidate. sort_by_key ( |id| self . tcx . def_path_str ( id ) ) ;
11901189 inherent_impls_candidate. dedup ( ) ;
11911190
11921191 // number of types to show at most
@@ -1567,7 +1566,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15671566 sources : & mut Vec < CandidateSource > ,
15681567 sugg_span : Option < Span > ,
15691568 ) {
1570- sources. sort ( ) ;
1569+ sources. sort_by_key ( |source| match source {
1570+ CandidateSource :: Trait ( id) => ( 0 , self . tcx . def_path_str ( id) ) ,
1571+ CandidateSource :: Impl ( id) => ( 1 , self . tcx . def_path_str ( id) ) ,
1572+ } ) ;
15711573 sources. dedup ( ) ;
15721574 // Dynamic limit to avoid hiding just one candidate, which is silly.
15731575 let limit = if sources. len ( ) == 5 { 5 } else { 4 } ;
@@ -2549,7 +2551,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
25492551 _ => None ,
25502552 } )
25512553 . collect ( ) ;
2552- preds. sort_by_key ( |pred| ( pred. def_id ( ) , pred . self_ty ( ) ) ) ;
2554+ preds. sort_by_key ( |pred| pred. trait_ref . to_string ( ) ) ;
25532555 let def_ids = preds
25542556 . iter ( )
25552557 . filter_map ( |pred| match pred. self_ty ( ) . kind ( ) {
@@ -2663,7 +2665,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
26632665 traits. push ( trait_pred. def_id ( ) ) ;
26642666 }
26652667 }
2666- traits. sort ( ) ;
2668+ traits. sort_by_key ( |id| self . tcx . def_path_str ( id ) ) ;
26672669 traits. dedup ( ) ;
26682670
26692671 let len = traits. len ( ) ;
@@ -2886,7 +2888,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
28862888 ) -> bool {
28872889 if !valid_out_of_scope_traits. is_empty ( ) {
28882890 let mut candidates = valid_out_of_scope_traits;
2889- candidates. sort ( ) ;
2891+ candidates. sort_by_key ( |id| self . tcx . def_path_str ( id ) ) ;
28902892 candidates. dedup ( ) ;
28912893
28922894 // `TryFrom` and `FromIterator` have no methods
@@ -3212,8 +3214,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
32123214 }
32133215
32143216 if !candidates. is_empty ( ) {
3215- // Sort from most relevant to least relevant.
3216- candidates. sort_by_key ( |& info| cmp:: Reverse ( info) ) ;
3217+ // Sort local crate results before others
3218+ candidates
3219+ . sort_by_key ( |& info| ( !info. def_id . is_local ( ) , self . tcx . def_path_str ( info. def_id ) ) ) ;
32173220 candidates. dedup ( ) ;
32183221
32193222 let param_type = match rcvr_ty. kind ( ) {
@@ -3561,33 +3564,11 @@ pub enum SelfSource<'a> {
35613564 MethodCall ( & ' a hir:: Expr < ' a > /* rcvr */ ) ,
35623565}
35633566
3564- #[ derive( Copy , Clone ) ]
3567+ #[ derive( Copy , Clone , PartialEq , Eq ) ]
35653568pub struct TraitInfo {
35663569 pub def_id : DefId ,
35673570}
35683571
3569- impl PartialEq for TraitInfo {
3570- fn eq ( & self , other : & TraitInfo ) -> bool {
3571- self . cmp ( other) == Ordering :: Equal
3572- }
3573- }
3574- impl Eq for TraitInfo { }
3575- impl PartialOrd for TraitInfo {
3576- fn partial_cmp ( & self , other : & TraitInfo ) -> Option < Ordering > {
3577- Some ( self . cmp ( other) )
3578- }
3579- }
3580- impl Ord for TraitInfo {
3581- fn cmp ( & self , other : & TraitInfo ) -> Ordering {
3582- // Local crates are more important than remote ones (local:
3583- // `cnum == 0`), and otherwise we throw in the defid for totality.
3584-
3585- let lhs = ( other. def_id . krate , other. def_id ) ;
3586- let rhs = ( self . def_id . krate , self . def_id ) ;
3587- lhs. cmp ( & rhs)
3588- }
3589- }
3590-
35913572/// Retrieves all traits in this crate and any dependent crates,
35923573/// and wraps them into `TraitInfo` for custom sorting.
35933574pub fn all_traits ( tcx : TyCtxt < ' _ > ) -> Vec < TraitInfo > {
0 commit comments