diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index 3a4ff8fbb3b..33eb9b839bd 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -49,7 +49,6 @@ use super::probe::{AutorefOrPtrAdjustment, IsSuggestion, Mode, ProbeScope}; use super::{CandidateSource, MethodError, NoMatchData}; use rustc_hir::intravisit::Visitor; -use std::cmp::{self, Ordering}; use std::iter; impl<'a, 'tcx> FnCtxt<'a, 'tcx> { @@ -3215,8 +3214,9 @@ fn suggest_traits_to_import( } if !candidates.is_empty() { - // Sort from most relevant to least relevant. - candidates.sort_by_key(|&info| cmp::Reverse(info)); + // Sort local crate results before others + candidates + .sort_by_key(|&info| (!info.def_id.is_local(), self.tcx.def_path_str(info.def_id))); candidates.dedup(); let param_type = match rcvr_ty.kind() { @@ -3564,33 +3564,11 @@ pub enum SelfSource<'a> { MethodCall(&'a hir::Expr<'a> /* rcvr */), } -#[derive(Copy, Clone)] +#[derive(Copy, Clone, PartialEq, Eq)] pub struct TraitInfo { pub def_id: DefId, } -impl PartialEq for TraitInfo { - fn eq(&self, other: &TraitInfo) -> bool { - self.cmp(other) == Ordering::Equal - } -} -impl Eq for TraitInfo {} -impl PartialOrd for TraitInfo { - fn partial_cmp(&self, other: &TraitInfo) -> Option { - Some(self.cmp(other)) - } -} -impl Ord for TraitInfo { - fn cmp(&self, other: &TraitInfo) -> Ordering { - // Local crates are more important than remote ones (local: - // `cnum == 0`), and otherwise we throw in the defid for totality. - - let lhs = (other.def_id.krate, other.def_id); - let rhs = (self.def_id.krate, self.def_id); - lhs.cmp(&rhs) - } -} - /// Retrieves all traits in this crate and any dependent crates, /// and wraps them into `TraitInfo` for custom sorting. pub fn all_traits(tcx: TyCtxt<'_>) -> Vec { diff --git a/tests/ui/impl-trait/no-method-suggested-traits.stderr b/tests/ui/impl-trait/no-method-suggested-traits.stderr index 7a4dc366618..676247d1a42 100644 --- a/tests/ui/impl-trait/no-method-suggested-traits.stderr +++ b/tests/ui/impl-trait/no-method-suggested-traits.stderr @@ -127,8 +127,8 @@ LL | Foo.method(); = note: the following traits define an item `method`, perhaps you need to implement one of them: candidate #1: `foo::Bar` candidate #2: `PubPub` - candidate #3: `no_method_suggested_traits::qux::PrivPub` - candidate #4: `Reexported` + candidate #3: `Reexported` + candidate #4: `no_method_suggested_traits::qux::PrivPub` error[E0599]: no method named `method` found for struct `Rc<&mut Box<&Foo>>` in the current scope --> $DIR/no-method-suggested-traits.rs:42:43 @@ -140,8 +140,8 @@ LL | std::rc::Rc::new(&mut Box::new(&Foo)).method(); = note: the following traits define an item `method`, perhaps you need to implement one of them: candidate #1: `foo::Bar` candidate #2: `PubPub` - candidate #3: `no_method_suggested_traits::qux::PrivPub` - candidate #4: `Reexported` + candidate #3: `Reexported` + candidate #4: `no_method_suggested_traits::qux::PrivPub` error[E0599]: no method named `method2` found for type `u64` in the current scope --> $DIR/no-method-suggested-traits.rs:45:10 diff --git a/tests/ui/methods/method-call-err-msg.stderr b/tests/ui/methods/method-call-err-msg.stderr index 6df49e432a1..7d9b38fb29b 100644 --- a/tests/ui/methods/method-call-err-msg.stderr +++ b/tests/ui/methods/method-call-err-msg.stderr @@ -64,8 +64,8 @@ note: the trait `Iterator` must be implemented --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL = help: items from traits can only be used if the trait is implemented and in scope = note: the following traits define an item `take`, perhaps you need to implement one of them: - candidate #1: `std::io::Read` - candidate #2: `Iterator` + candidate #1: `Iterator` + candidate #2: `std::io::Read` error[E0061]: this method takes 3 arguments but 0 arguments were supplied --> $DIR/method-call-err-msg.rs:21:7 diff --git a/tests/ui/traits/method-on-unbounded-type-param.stderr b/tests/ui/traits/method-on-unbounded-type-param.stderr index 0d8bd8ee964..4d968e7bee1 100644 --- a/tests/ui/traits/method-on-unbounded-type-param.stderr +++ b/tests/ui/traits/method-on-unbounded-type-param.stderr @@ -9,10 +9,10 @@ LL | a.cmp(&b) = help: items from traits can only be used if the type parameter is bounded by the trait help: the following traits define an item `cmp`, perhaps you need to restrict type parameter `T` with one of them: | -LL | fn f(a: T, b: T) -> std::cmp::Ordering { - | +++++ LL | fn f(a: T, b: T) -> std::cmp::Ordering { | ++++++++++ +LL | fn f(a: T, b: T) -> std::cmp::Ordering { + | +++++ error[E0599]: the method `cmp` exists for reference `&T`, but its trait bounds were not satisfied --> $DIR/method-on-unbounded-type-param.rs:5:10 @@ -30,10 +30,10 @@ LL | (&a).cmp(&b) = help: items from traits can only be used if the type parameter is bounded by the trait help: the following traits define an item `cmp`, perhaps you need to restrict type parameter `T` with one of them: | -LL | fn g(a: T, b: T) -> std::cmp::Ordering { - | +++++ LL | fn g(a: T, b: T) -> std::cmp::Ordering { | ++++++++++ +LL | fn g(a: T, b: T) -> std::cmp::Ordering { + | +++++ error[E0599]: the method `cmp` exists for reference `&T`, but its trait bounds were not satisfied --> $DIR/method-on-unbounded-type-param.rs:8:7 @@ -51,10 +51,10 @@ LL | a.cmp(&b) = help: items from traits can only be used if the type parameter is bounded by the trait help: the following traits define an item `cmp`, perhaps you need to restrict type parameter `T` with one of them: | -LL | fn h(a: &T, b: T) -> std::cmp::Ordering { - | +++++ LL | fn h(a: &T, b: T) -> std::cmp::Ordering { | ++++++++++ +LL | fn h(a: &T, b: T) -> std::cmp::Ordering { + | +++++ error[E0599]: the method `cmp` exists for struct `Box`, but its trait bounds were not satisfied --> $DIR/method-on-unbounded-type-param.rs:14:7 @@ -76,8 +76,8 @@ LL | x.cmp(&x); which is required by `&mut dyn T: Iterator` = help: items from traits can only be used if the trait is implemented and in scope = note: the following traits define an item `cmp`, perhaps you need to implement one of them: - candidate #1: `Ord` - candidate #2: `Iterator` + candidate #1: `Iterator` + candidate #2: `Ord` error: aborting due to 4 previous errors