Skip to content

Commit 6be79cb

Browse files
committed
Sort a diagnostic by DefPathStr instead of DefId
1 parent 57f68c3 commit 6be79cb

File tree

4 files changed

+18
-40
lines changed

4 files changed

+18
-40
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

+4-26
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ use std::borrow::Cow;
4949
use super::probe::{AutorefOrPtrAdjustment, IsSuggestion, Mode, ProbeScope};
5050
use super::{CandidateSource, MethodError, NoMatchData};
5151
use rustc_hir::intravisit::Visitor;
52-
use std::cmp::{self, Ordering};
5352
use std::iter;
5453

5554
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
@@ -3215,8 +3214,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
32153214
}
32163215

32173216
if !candidates.is_empty() {
3218-
// Sort from most relevant to least relevant.
3219-
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)));
32203220
candidates.dedup();
32213221

32223222
let param_type = match rcvr_ty.kind() {
@@ -3564,33 +3564,11 @@ pub enum SelfSource<'a> {
35643564
MethodCall(&'a hir::Expr<'a> /* rcvr */),
35653565
}
35663566

3567-
#[derive(Copy, Clone)]
3567+
#[derive(Copy, Clone, PartialEq, Eq)]
35683568
pub struct TraitInfo {
35693569
pub def_id: DefId,
35703570
}
35713571

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-
35943572
/// Retrieves all traits in this crate and any dependent crates,
35953573
/// and wraps them into `TraitInfo` for custom sorting.
35963574
pub fn all_traits(tcx: TyCtxt<'_>) -> Vec<TraitInfo> {

tests/ui/impl-trait/no-method-suggested-traits.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ LL | Foo.method();
127127
= note: the following traits define an item `method`, perhaps you need to implement one of them:
128128
candidate #1: `foo::Bar`
129129
candidate #2: `PubPub`
130-
candidate #3: `no_method_suggested_traits::qux::PrivPub`
131-
candidate #4: `Reexported`
130+
candidate #3: `Reexported`
131+
candidate #4: `no_method_suggested_traits::qux::PrivPub`
132132

133133
error[E0599]: no method named `method` found for struct `Rc<&mut Box<&Foo>>` in the current scope
134134
--> $DIR/no-method-suggested-traits.rs:42:43
@@ -140,8 +140,8 @@ LL | std::rc::Rc::new(&mut Box::new(&Foo)).method();
140140
= note: the following traits define an item `method`, perhaps you need to implement one of them:
141141
candidate #1: `foo::Bar`
142142
candidate #2: `PubPub`
143-
candidate #3: `no_method_suggested_traits::qux::PrivPub`
144-
candidate #4: `Reexported`
143+
candidate #3: `Reexported`
144+
candidate #4: `no_method_suggested_traits::qux::PrivPub`
145145

146146
error[E0599]: no method named `method2` found for type `u64` in the current scope
147147
--> $DIR/no-method-suggested-traits.rs:45:10

tests/ui/methods/method-call-err-msg.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ note: the trait `Iterator` must be implemented
6464
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
6565
= help: items from traits can only be used if the trait is implemented and in scope
6666
= note: the following traits define an item `take`, perhaps you need to implement one of them:
67-
candidate #1: `std::io::Read`
68-
candidate #2: `Iterator`
67+
candidate #1: `Iterator`
68+
candidate #2: `std::io::Read`
6969

7070
error[E0061]: this method takes 3 arguments but 0 arguments were supplied
7171
--> $DIR/method-call-err-msg.rs:21:7

tests/ui/traits/method-on-unbounded-type-param.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ LL | a.cmp(&b)
99
= help: items from traits can only be used if the type parameter is bounded by the trait
1010
help: the following traits define an item `cmp`, perhaps you need to restrict type parameter `T` with one of them:
1111
|
12-
LL | fn f<T: Ord>(a: T, b: T) -> std::cmp::Ordering {
13-
| +++++
1412
LL | fn f<T: Iterator>(a: T, b: T) -> std::cmp::Ordering {
1513
| ++++++++++
14+
LL | fn f<T: Ord>(a: T, b: T) -> std::cmp::Ordering {
15+
| +++++
1616

1717
error[E0599]: the method `cmp` exists for reference `&T`, but its trait bounds were not satisfied
1818
--> $DIR/method-on-unbounded-type-param.rs:5:10
@@ -30,10 +30,10 @@ LL | (&a).cmp(&b)
3030
= help: items from traits can only be used if the type parameter is bounded by the trait
3131
help: the following traits define an item `cmp`, perhaps you need to restrict type parameter `T` with one of them:
3232
|
33-
LL | fn g<T: Ord>(a: T, b: T) -> std::cmp::Ordering {
34-
| +++++
3533
LL | fn g<T: Iterator>(a: T, b: T) -> std::cmp::Ordering {
3634
| ++++++++++
35+
LL | fn g<T: Ord>(a: T, b: T) -> std::cmp::Ordering {
36+
| +++++
3737

3838
error[E0599]: the method `cmp` exists for reference `&T`, but its trait bounds were not satisfied
3939
--> $DIR/method-on-unbounded-type-param.rs:8:7
@@ -51,10 +51,10 @@ LL | a.cmp(&b)
5151
= help: items from traits can only be used if the type parameter is bounded by the trait
5252
help: the following traits define an item `cmp`, perhaps you need to restrict type parameter `T` with one of them:
5353
|
54-
LL | fn h<T: Ord>(a: &T, b: T) -> std::cmp::Ordering {
55-
| +++++
5654
LL | fn h<T: Iterator>(a: &T, b: T) -> std::cmp::Ordering {
5755
| ++++++++++
56+
LL | fn h<T: Ord>(a: &T, b: T) -> std::cmp::Ordering {
57+
| +++++
5858

5959
error[E0599]: the method `cmp` exists for struct `Box<dyn T>`, but its trait bounds were not satisfied
6060
--> $DIR/method-on-unbounded-type-param.rs:14:7
@@ -76,8 +76,8 @@ LL | x.cmp(&x);
7676
which is required by `&mut dyn T: Iterator`
7777
= help: items from traits can only be used if the trait is implemented and in scope
7878
= note: the following traits define an item `cmp`, perhaps you need to implement one of them:
79-
candidate #1: `Ord`
80-
candidate #2: `Iterator`
79+
candidate #1: `Iterator`
80+
candidate #2: `Ord`
8181

8282
error: aborting due to 4 previous errors
8383

0 commit comments

Comments
 (0)