@@ -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 > {
@@ -1189,7 +1188,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1189
1188
} )
1190
1189
. collect :: < Vec < _ > > ( ) ;
1191
1190
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 ) ) ;
1193
1192
inherent_impls_candidate. dedup ( ) ;
1194
1193
1195
1194
// number of types to show at most
@@ -1570,7 +1569,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1570
1569
sources : & mut Vec < CandidateSource > ,
1571
1570
sugg_span : Option < Span > ,
1572
1571
) {
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
+ } ) ;
1574
1576
sources. dedup ( ) ;
1575
1577
// Dynamic limit to avoid hiding just one candidate, which is silly.
1576
1578
let limit = if sources. len ( ) == 5 { 5 } else { 4 } ;
@@ -2552,7 +2554,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2552
2554
_ => None ,
2553
2555
} )
2554
2556
. collect ( ) ;
2555
- preds. sort_by_key ( |pred| ( pred. def_id ( ) , pred . self_ty ( ) ) ) ;
2557
+ preds. sort_by_key ( |pred| pred. trait_ref . to_string ( ) ) ;
2556
2558
let def_ids = preds
2557
2559
. iter ( )
2558
2560
. filter_map ( |pred| match pred. self_ty ( ) . kind ( ) {
@@ -2666,7 +2668,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2666
2668
traits. push ( trait_pred. def_id ( ) ) ;
2667
2669
}
2668
2670
}
2669
- traits. sort ( ) ;
2671
+ traits. sort_by_key ( |id| self . tcx . def_path_str ( id ) ) ;
2670
2672
traits. dedup ( ) ;
2671
2673
2672
2674
let len = traits. len ( ) ;
@@ -2889,7 +2891,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2889
2891
) -> bool {
2890
2892
if !valid_out_of_scope_traits. is_empty ( ) {
2891
2893
let mut candidates = valid_out_of_scope_traits;
2892
- candidates. sort ( ) ;
2894
+ candidates. sort_by_key ( |id| self . tcx . def_path_str ( id ) ) ;
2893
2895
candidates. dedup ( ) ;
2894
2896
2895
2897
// `TryFrom` and `FromIterator` have no methods
@@ -3215,8 +3217,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
3215
3217
}
3216
3218
3217
3219
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 ) ) ) ;
3220
3223
candidates. dedup ( ) ;
3221
3224
3222
3225
let param_type = match rcvr_ty. kind ( ) {
@@ -3564,33 +3567,11 @@ pub enum SelfSource<'a> {
3564
3567
MethodCall ( & ' a hir:: Expr < ' a > /* rcvr */ ) ,
3565
3568
}
3566
3569
3567
- #[ derive( Copy , Clone ) ]
3570
+ #[ derive( Copy , Clone , PartialEq , Eq ) ]
3568
3571
pub struct TraitInfo {
3569
3572
pub def_id : DefId ,
3570
3573
}
3571
3574
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
-
3594
3575
/// Retrieves all traits in this crate and any dependent crates,
3595
3576
/// and wraps them into `TraitInfo` for custom sorting.
3596
3577
pub fn all_traits ( tcx : TyCtxt < ' _ > ) -> Vec < TraitInfo > {
0 commit comments