Skip to content

Commit 57f68c3

Browse files
committed
Sort method suggestions by DefPath instead of DefId
1 parent 7278072 commit 57f68c3

8 files changed

+54
-51
lines changed

compiler/rustc_hir_typeck/src/method/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub struct NoMatchData<'tcx> {
8181

8282
// A pared down enum describing just the places from which a method
8383
// candidate can arise. Used for error reporting only.
84-
#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
84+
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
8585
pub enum CandidateSource {
8686
Impl(DefId),
8787
Trait(DefId /* trait id */),

compiler/rustc_hir_typeck/src/method/suggest.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1567,7 +1567,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15671567
sources: &mut Vec<CandidateSource>,
15681568
sugg_span: Option<Span>,
15691569
) {
1570-
sources.sort();
1570+
sources.sort_by_key(|source| match source {
1571+
CandidateSource::Trait(id) => (0, self.tcx.def_path_str(id)),
1572+
CandidateSource::Impl(id) => (1, self.tcx.def_path_str(id)),
1573+
});
15711574
sources.dedup();
15721575
// Dynamic limit to avoid hiding just one candidate, which is silly.
15731576
let limit = if sources.len() == 5 { 5 } else { 4 };

tests/ui/associated-consts/associated-const-ambiguity-report.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ error[E0034]: multiple applicable items in scope
44
LL | const X: i32 = <i32>::ID;
55
| ^^ multiple `ID` found
66
|
7-
note: candidate #1 is defined in an impl of the trait `Foo` for the type `i32`
8-
--> $DIR/associated-const-ambiguity-report.rs:10:5
9-
|
10-
LL | const ID: i32 = 1;
11-
| ^^^^^^^^^^^^^
12-
note: candidate #2 is defined in an impl of the trait `Bar` for the type `i32`
7+
note: candidate #1 is defined in an impl of the trait `Bar` for the type `i32`
138
--> $DIR/associated-const-ambiguity-report.rs:14:5
149
|
1510
LL | const ID: i32 = 3;
1611
| ^^^^^^^^^^^^^
12+
note: candidate #2 is defined in an impl of the trait `Foo` for the type `i32`
13+
--> $DIR/associated-const-ambiguity-report.rs:10:5
14+
|
15+
LL | const ID: i32 = 1;
16+
| ^^^^^^^^^^^^^
1717
help: use fully-qualified syntax to disambiguate
1818
|
1919
LL | const X: i32 = <i32 as Bar>::ID;

tests/ui/issues/issue-18446.stderr

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ error[E0034]: multiple applicable items in scope
44
LL | x.foo();
55
| ^^^ multiple `foo` found
66
|
7-
note: candidate #1 is defined in an impl for the type `(dyn T + 'a)`
8-
--> $DIR/issue-18446.rs:9:5
9-
|
10-
LL | fn foo(&self) {}
11-
| ^^^^^^^^^^^^^
12-
note: candidate #2 is defined in the trait `T`
7+
note: candidate #1 is defined in the trait `T`
138
--> $DIR/issue-18446.rs:5:5
149
|
1510
LL | fn foo(&self);
1611
| ^^^^^^^^^^^^^^
17-
help: disambiguate the method for candidate #2
12+
note: candidate #2 is defined in an impl for the type `(dyn T + 'a)`
13+
--> $DIR/issue-18446.rs:9:5
14+
|
15+
LL | fn foo(&self) {}
16+
| ^^^^^^^^^^^^^
17+
help: disambiguate the method for candidate #1
1818
|
1919
LL | T::foo(&x);
2020
| ~~~~~~~~~~

tests/ui/issues/issue-3702-2.stderr

+10-10
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@ error[E0034]: multiple applicable items in scope
44
LL | self.to_int() + other.to_int()
55
| ^^^^^^ multiple `to_int` found
66
|
7-
note: candidate #1 is defined in an impl of the trait `ToPrimitive` for the type `isize`
8-
--> $DIR/issue-3702-2.rs:2:5
9-
|
10-
LL | fn to_int(&self) -> isize { 0 }
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
12-
note: candidate #2 is defined in an impl of the trait `Add` for the type `isize`
7+
note: candidate #1 is defined in an impl of the trait `Add` for the type `isize`
138
--> $DIR/issue-3702-2.rs:14:5
149
|
1510
LL | fn to_int(&self) -> isize { *self }
1611
| ^^^^^^^^^^^^^^^^^^^^^^^^^
17-
help: disambiguate the method for candidate #1
12+
note: candidate #2 is defined in an impl of the trait `ToPrimitive` for the type `isize`
13+
--> $DIR/issue-3702-2.rs:2:5
1814
|
19-
LL | ToPrimitive::to_int(&self) + other.to_int()
20-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
21-
help: disambiguate the method for candidate #2
15+
LL | fn to_int(&self) -> isize { 0 }
16+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
17+
help: disambiguate the method for candidate #1
2218
|
2319
LL | Add::to_int(&self) + other.to_int()
2420
| ~~~~~~~~~~~~~~~~~~
21+
help: disambiguate the method for candidate #2
22+
|
23+
LL | ToPrimitive::to_int(&self) + other.to_int()
24+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
2525

2626
error: aborting due to 1 previous error
2727

tests/ui/methods/method-ambig-two-traits-cross-crate.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ error[E0034]: multiple applicable items in scope
44
LL | fn main() { 1_usize.me(); }
55
| ^^ multiple `me` found
66
|
7-
= note: candidate #1 is defined in an impl of the trait `Me` for the type `usize`
8-
note: candidate #2 is defined in an impl of the trait `Me2` for the type `usize`
7+
note: candidate #1 is defined in an impl of the trait `Me2` for the type `usize`
98
--> $DIR/method-ambig-two-traits-cross-crate.rs:10:22
109
|
1110
LL | impl Me2 for usize { fn me(&self) -> usize { *self } }
1211
| ^^^^^^^^^^^^^^^^^^^^^
12+
= note: candidate #2 is defined in an impl of the trait `Me` for the type `usize`
1313
help: disambiguate the method for candidate #1
1414
|
15-
LL | fn main() { Me::me(&1_usize); }
16-
| ~~~~~~~~~~~~~~~~
17-
help: disambiguate the method for candidate #2
18-
|
1915
LL | fn main() { Me2::me(&1_usize); }
2016
| ~~~~~~~~~~~~~~~~~
17+
help: disambiguate the method for candidate #2
18+
|
19+
LL | fn main() { Me::me(&1_usize); }
20+
| ~~~~~~~~~~~~~~~~
2121

2222
error: aborting due to 1 previous error
2323

tests/ui/methods/method-ambig-two-traits-with-default-method.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@ error[E0034]: multiple applicable items in scope
44
LL | 1_usize.method();
55
| ^^^^^^ multiple `method` found
66
|
7-
note: candidate #1 is defined in an impl of the trait `Foo` for the type `usize`
8-
--> $DIR/method-ambig-two-traits-with-default-method.rs:5:13
9-
|
10-
LL | trait Foo { fn method(&self) {} }
11-
| ^^^^^^^^^^^^^^^^
12-
note: candidate #2 is defined in an impl of the trait `Bar` for the type `usize`
7+
note: candidate #1 is defined in an impl of the trait `Bar` for the type `usize`
138
--> $DIR/method-ambig-two-traits-with-default-method.rs:6:13
149
|
1510
LL | trait Bar { fn method(&self) {} }
1611
| ^^^^^^^^^^^^^^^^
12+
note: candidate #2 is defined in an impl of the trait `Foo` for the type `usize`
13+
--> $DIR/method-ambig-two-traits-with-default-method.rs:5:13
14+
|
15+
LL | trait Foo { fn method(&self) {} }
16+
| ^^^^^^^^^^^^^^^^
1717
help: disambiguate the method for candidate #1
1818
|
19-
LL | Foo::method(&1_usize);
19+
LL | Bar::method(&1_usize);
2020
| ~~~~~~~~~~~~~~~~~~~~~
2121
help: disambiguate the method for candidate #2
2222
|
23-
LL | Bar::method(&1_usize);
23+
LL | Foo::method(&1_usize);
2424
| ~~~~~~~~~~~~~~~~~~~~~
2525

2626
error: aborting due to 1 previous error

tests/ui/methods/method-deref-to-same-trait-object-with-separate-params.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,33 @@ error[E0034]: multiple applicable items in scope
2929
LL | let z = x.foo();
3030
| ^^^ multiple `foo` found
3131
|
32-
note: candidate #1 is defined in an impl of the trait `X` for the type `T`
33-
--> $DIR/method-deref-to-same-trait-object-with-separate-params.rs:45:9
32+
note: candidate #1 is defined in the trait `FinalFoo`
33+
--> $DIR/method-deref-to-same-trait-object-with-separate-params.rs:59:5
3434
|
35-
LL | fn foo(self: Smaht<Self, u64>) -> u64 {
36-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
35+
LL | fn foo(&self) -> u8;
36+
| ^^^^^^^^^^^^^^^^^^^^
3737
note: candidate #2 is defined in an impl of the trait `NuisanceFoo` for the type `T`
3838
--> $DIR/method-deref-to-same-trait-object-with-separate-params.rs:72:9
3939
|
4040
LL | fn foo(self) {}
4141
| ^^^^^^^^^^^^
42-
note: candidate #3 is defined in the trait `FinalFoo`
43-
--> $DIR/method-deref-to-same-trait-object-with-separate-params.rs:59:5
42+
note: candidate #3 is defined in an impl of the trait `X` for the type `T`
43+
--> $DIR/method-deref-to-same-trait-object-with-separate-params.rs:45:9
4444
|
45-
LL | fn foo(&self) -> u8;
46-
| ^^^^^^^^^^^^^^^^^^^^
45+
LL | fn foo(self: Smaht<Self, u64>) -> u64 {
46+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4747
help: disambiguate the method for candidate #1
4848
|
49-
LL | let z = X::foo(x);
50-
| ~~~~~~~~~
49+
LL | let z = FinalFoo::foo(&x);
50+
| ~~~~~~~~~~~~~~~~~
5151
help: disambiguate the method for candidate #2
5252
|
5353
LL | let z = NuisanceFoo::foo(x);
5454
| ~~~~~~~~~~~~~~~~~~~
5555
help: disambiguate the method for candidate #3
5656
|
57-
LL | let z = FinalFoo::foo(&x);
58-
| ~~~~~~~~~~~~~~~~~
57+
LL | let z = X::foo(x);
58+
| ~~~~~~~~~
5959

6060
error[E0308]: mismatched types
6161
--> $DIR/method-deref-to-same-trait-object-with-separate-params.rs:139:24

0 commit comments

Comments
 (0)