Sort a diagnostic by DefPathStr instead of DefId

This commit is contained in:
Oli Scherer 2024-03-21 17:00:11 +00:00
parent 57f68c3555
commit 6be79cb103
4 changed files with 18 additions and 40 deletions

View File

@ -49,7 +49,6 @@
use super::probe::{AutorefOrPtrAdjustment, IsSuggestion, Mode, ProbeScope}; use super::probe::{AutorefOrPtrAdjustment, IsSuggestion, Mode, ProbeScope};
use super::{CandidateSource, MethodError, NoMatchData}; use super::{CandidateSource, MethodError, NoMatchData};
use rustc_hir::intravisit::Visitor; use rustc_hir::intravisit::Visitor;
use std::cmp::{self, Ordering};
use std::iter; use std::iter;
impl<'a, 'tcx> FnCtxt<'a, 'tcx> { impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
@ -3215,8 +3214,9 @@ fn suggest_traits_to_import(
} }
if !candidates.is_empty() { if !candidates.is_empty() {
// Sort from most relevant to least relevant. // Sort local crate results before others
candidates.sort_by_key(|&info| cmp::Reverse(info)); candidates
.sort_by_key(|&info| (!info.def_id.is_local(), self.tcx.def_path_str(info.def_id)));
candidates.dedup(); candidates.dedup();
let param_type = match rcvr_ty.kind() { let param_type = match rcvr_ty.kind() {
@ -3564,33 +3564,11 @@ pub enum SelfSource<'a> {
MethodCall(&'a hir::Expr<'a> /* rcvr */), MethodCall(&'a hir::Expr<'a> /* rcvr */),
} }
#[derive(Copy, Clone)] #[derive(Copy, Clone, PartialEq, Eq)]
pub struct TraitInfo { pub struct TraitInfo {
pub def_id: DefId, 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<Ordering> {
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, /// Retrieves all traits in this crate and any dependent crates,
/// and wraps them into `TraitInfo` for custom sorting. /// and wraps them into `TraitInfo` for custom sorting.
pub fn all_traits(tcx: TyCtxt<'_>) -> Vec<TraitInfo> { pub fn all_traits(tcx: TyCtxt<'_>) -> Vec<TraitInfo> {

View File

@ -127,8 +127,8 @@ LL | Foo.method();
= note: the following traits define an item `method`, perhaps you need to implement one of them: = note: the following traits define an item `method`, perhaps you need to implement one of them:
candidate #1: `foo::Bar` candidate #1: `foo::Bar`
candidate #2: `PubPub` candidate #2: `PubPub`
candidate #3: `no_method_suggested_traits::qux::PrivPub` candidate #3: `Reexported`
candidate #4: `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 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 --> $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: = note: the following traits define an item `method`, perhaps you need to implement one of them:
candidate #1: `foo::Bar` candidate #1: `foo::Bar`
candidate #2: `PubPub` candidate #2: `PubPub`
candidate #3: `no_method_suggested_traits::qux::PrivPub` candidate #3: `Reexported`
candidate #4: `Reexported` candidate #4: `no_method_suggested_traits::qux::PrivPub`
error[E0599]: no method named `method2` found for type `u64` in the current scope error[E0599]: no method named `method2` found for type `u64` in the current scope
--> $DIR/no-method-suggested-traits.rs:45:10 --> $DIR/no-method-suggested-traits.rs:45:10

View File

@ -64,8 +64,8 @@ note: the trait `Iterator` must be implemented
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL --> $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 = 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: = note: the following traits define an item `take`, perhaps you need to implement one of them:
candidate #1: `std::io::Read` candidate #1: `Iterator`
candidate #2: `Iterator` candidate #2: `std::io::Read`
error[E0061]: this method takes 3 arguments but 0 arguments were supplied error[E0061]: this method takes 3 arguments but 0 arguments were supplied
--> $DIR/method-call-err-msg.rs:21:7 --> $DIR/method-call-err-msg.rs:21:7

View File

@ -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: 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: help: the following traits define an item `cmp`, perhaps you need to restrict type parameter `T` with one of them:
| |
LL | fn f<T: Ord>(a: T, b: T) -> std::cmp::Ordering {
| +++++
LL | fn f<T: Iterator>(a: T, b: T) -> std::cmp::Ordering { LL | fn f<T: Iterator>(a: T, b: T) -> std::cmp::Ordering {
| ++++++++++ | ++++++++++
LL | fn f<T: Ord>(a: T, b: T) -> std::cmp::Ordering {
| +++++
error[E0599]: the method `cmp` exists for reference `&T`, but its trait bounds were not satisfied 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 --> $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: 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: help: the following traits define an item `cmp`, perhaps you need to restrict type parameter `T` with one of them:
| |
LL | fn g<T: Ord>(a: T, b: T) -> std::cmp::Ordering {
| +++++
LL | fn g<T: Iterator>(a: T, b: T) -> std::cmp::Ordering { LL | fn g<T: Iterator>(a: T, b: T) -> std::cmp::Ordering {
| ++++++++++ | ++++++++++
LL | fn g<T: Ord>(a: T, b: T) -> std::cmp::Ordering {
| +++++
error[E0599]: the method `cmp` exists for reference `&T`, but its trait bounds were not satisfied 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 --> $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: 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: help: the following traits define an item `cmp`, perhaps you need to restrict type parameter `T` with one of them:
| |
LL | fn h<T: Ord>(a: &T, b: T) -> std::cmp::Ordering {
| +++++
LL | fn h<T: Iterator>(a: &T, b: T) -> std::cmp::Ordering { LL | fn h<T: Iterator>(a: &T, b: T) -> std::cmp::Ordering {
| ++++++++++ | ++++++++++
LL | fn h<T: Ord>(a: &T, b: T) -> std::cmp::Ordering {
| +++++
error[E0599]: the method `cmp` exists for struct `Box<dyn T>`, but its trait bounds were not satisfied error[E0599]: the method `cmp` exists for struct `Box<dyn T>`, but its trait bounds were not satisfied
--> $DIR/method-on-unbounded-type-param.rs:14:7 --> $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` which is required by `&mut dyn T: Iterator`
= help: items from traits can only be used if the trait is implemented and in scope = 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: = note: the following traits define an item `cmp`, perhaps you need to implement one of them:
candidate #1: `Ord` candidate #1: `Iterator`
candidate #2: `Iterator` candidate #2: `Ord`
error: aborting due to 4 previous errors error: aborting due to 4 previous errors