@@ -49,7 +49,6 @@ use std::borrow::Cow;
49
49
use super :: probe:: { AutorefOrPtrAdjustment , IsSuggestion , Mode , ProbeScope } ;
50
50
use super :: { CandidateSource , MethodError , NoMatchData } ;
51
51
use rustc_hir:: intravisit:: Visitor ;
52
- use std:: cmp:: { self , Ordering } ;
53
52
use std:: iter;
54
53
55
54
impl < ' a , ' tcx > FnCtxt < ' a , ' tcx > {
@@ -1186,7 +1185,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1186
1185
} )
1187
1186
. collect :: < Vec < _ > > ( ) ;
1188
1187
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 ) ) ;
1190
1189
inherent_impls_candidate. dedup ( ) ;
1191
1190
1192
1191
// number of types to show at most
@@ -1567,7 +1566,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1567
1566
sources : & mut Vec < CandidateSource > ,
1568
1567
sugg_span : Option < Span > ,
1569
1568
) {
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
+ } ) ;
1571
1573
sources. dedup ( ) ;
1572
1574
// Dynamic limit to avoid hiding just one candidate, which is silly.
1573
1575
let limit = if sources. len ( ) == 5 { 5 } else { 4 } ;
@@ -2549,7 +2551,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2549
2551
_ => None ,
2550
2552
} )
2551
2553
. collect ( ) ;
2552
- preds. sort_by_key ( |pred| ( pred. def_id ( ) , pred . self_ty ( ) ) ) ;
2554
+ preds. sort_by_key ( |pred| pred. trait_ref . to_string ( ) ) ;
2553
2555
let def_ids = preds
2554
2556
. iter ( )
2555
2557
. filter_map ( |pred| match pred. self_ty ( ) . kind ( ) {
@@ -2663,7 +2665,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2663
2665
traits. push ( trait_pred. def_id ( ) ) ;
2664
2666
}
2665
2667
}
2666
- traits. sort ( ) ;
2668
+ traits. sort_by_key ( |id| self . tcx . def_path_str ( id ) ) ;
2667
2669
traits. dedup ( ) ;
2668
2670
2669
2671
let len = traits. len ( ) ;
@@ -2886,7 +2888,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2886
2888
) -> bool {
2887
2889
if !valid_out_of_scope_traits. is_empty ( ) {
2888
2890
let mut candidates = valid_out_of_scope_traits;
2889
- candidates. sort ( ) ;
2891
+ candidates. sort_by_key ( |id| self . tcx . def_path_str ( id ) ) ;
2890
2892
candidates. dedup ( ) ;
2891
2893
2892
2894
// `TryFrom` and `FromIterator` have no methods
@@ -3212,8 +3214,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
3212
3214
}
3213
3215
3214
3216
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 ) ) ) ;
3217
3220
candidates. dedup ( ) ;
3218
3221
3219
3222
let param_type = match rcvr_ty. kind ( ) {
@@ -3561,33 +3564,11 @@ pub enum SelfSource<'a> {
3561
3564
MethodCall ( & ' a hir:: Expr < ' a > /* rcvr */ ) ,
3562
3565
}
3563
3566
3564
- #[ derive( Copy , Clone ) ]
3567
+ #[ derive( Copy , Clone , PartialEq , Eq ) ]
3565
3568
pub struct TraitInfo {
3566
3569
pub def_id : DefId ,
3567
3570
}
3568
3571
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
-
3591
3572
/// Retrieves all traits in this crate and any dependent crates,
3592
3573
/// and wraps them into `TraitInfo` for custom sorting.
3593
3574
pub fn all_traits ( tcx : TyCtxt < ' _ > ) -> Vec < TraitInfo > {
0 commit comments