Skip to content

Commit 24559ce

Browse files
Prefer non-Self non-method types over Self, first
1 parent c874676 commit 24559ce

File tree

9 files changed

+72
-40
lines changed

9 files changed

+72
-40
lines changed

compiler/rustc_typeck/src/check/fn_ctxt/checks.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,6 +1669,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16691669
if let ty::GenericArgKind::Type(ty) = arg.unpack()
16701670
&& let ty::Param(param_ty) = ty.kind()
16711671
&& self.tcx.parent(generics.type_param(param_ty, self.tcx).def_id) != def_id
1672+
&& param_ty.name != rustc_span::symbol::kw::SelfUpper
1673+
{
1674+
Some(arg)
1675+
} else {
1676+
None
1677+
}
1678+
})
1679+
});
1680+
let mut self_param_to_point_at = predicate_substs.types().find_map(|ty| {
1681+
ty.walk().find_map(|arg| {
1682+
if let ty::GenericArgKind::Type(ty) = arg.unpack()
1683+
&& let ty::Param(param_ty) = ty.kind()
1684+
&& param_ty.name == rustc_span::symbol::kw::SelfUpper
16721685
{
16731686
Some(arg)
16741687
} else {
@@ -1681,6 +1694,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16811694
// to print a relevant error.
16821695
if let traits::FulfillmentErrorCode::CodeAmbiguity = error.code {
16831696
fallback_param_to_point_at = None;
1697+
self_param_to_point_at = None;
16841698
param_to_point_at =
16851699
self.find_ambiguous_parameter_in(def_id, error.root_obligation.predicate);
16861700
}
@@ -1704,6 +1718,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17041718
return;
17051719
}
17061720

1721+
if let Some(self_param_to_point_at) = self_param_to_point_at
1722+
&& self.point_at_args_if_possible(error, def_id, self_param_to_point_at, *call_hir_id, callee.span, args)
1723+
{
1724+
return;
1725+
}
1726+
17071727
if let hir::QPath::Resolved(_, path) = qpath
17081728
&& let Some(param_to_point_at) = param_to_point_at
17091729
&& let Some(segment) = path.segments.last()
@@ -1733,6 +1753,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17331753
return;
17341754
}
17351755

1756+
if let Some(self_param_to_point_at) = self_param_to_point_at
1757+
&& self.point_at_args_if_possible(error, def_id, self_param_to_point_at, hir_id, segment.ident.span, args)
1758+
{
1759+
return;
1760+
}
1761+
17361762
if let Some(param_to_point_at) = param_to_point_at
17371763
&& self.point_at_generics_if_possible(error, def_id, param_to_point_at, segment)
17381764
{

src/test/ui/did_you_mean/issue-21659-show-relevant-trait-impls-1.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
error[E0277]: the trait bound `Bar: Foo<usize>` is not satisfied
2-
--> $DIR/issue-21659-show-relevant-trait-impls-1.rs:24:5
2+
--> $DIR/issue-21659-show-relevant-trait-impls-1.rs:24:12
33
|
44
LL | f1.foo(1usize);
5-
| ^^ --- required by a bound introduced by this call
6-
| |
7-
| the trait `Foo<usize>` is not implemented for `Bar`
5+
| --- ^^^^^^ the trait `Foo<usize>` is not implemented for `Bar`
6+
| |
7+
| required by a bound introduced by this call
88
|
99
= help: the following other types implement trait `Foo<A>`:
1010
<Bar as Foo<i32>>

src/test/ui/did_you_mean/issue-21659-show-relevant-trait-impls-2.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
error[E0277]: the trait bound `Bar: Foo<usize>` is not satisfied
2-
--> $DIR/issue-21659-show-relevant-trait-impls-2.rs:28:5
2+
--> $DIR/issue-21659-show-relevant-trait-impls-2.rs:28:12
33
|
44
LL | f1.foo(1usize);
5-
| ^^ --- required by a bound introduced by this call
6-
| |
7-
| the trait `Foo<usize>` is not implemented for `Bar`
5+
| --- ^^^^^^ the trait `Foo<usize>` is not implemented for `Bar`
6+
| |
7+
| required by a bound introduced by this call
88
|
99
= help: the following other types implement trait `Foo<A>`:
1010
<Bar as Foo<i16>>

src/test/ui/never_type/never-value-fallback-issue-66757.nofallback.stderr

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
error[E0277]: the trait bound `E: From<()>` is not satisfied
2-
--> $DIR/never-value-fallback-issue-66757.rs:28:5
2+
--> $DIR/never-value-fallback-issue-66757.rs:28:26
33
|
44
LL | <E as From<_>>::from(never);
5-
| ^^^^^^^^^^^^^^^^^^^^ the trait `From<()>` is not implemented for `E`
5+
| -------------------- ^^^^^ the trait `From<()>` is not implemented for `E`
6+
| |
7+
| required by a bound introduced by this call
68
|
79
= help: the trait `From<!>` is implemented for `E`
810

src/test/ui/on-unimplemented/multiple-impls.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: the trait bound `[i32]: Index<u32>` is not satisfied
2-
--> $DIR/multiple-impls.rs:33:18
2+
--> $DIR/multiple-impls.rs:33:33
33
|
44
LL | Index::index(&[] as &[i32], 2u32);
5-
| ------------ ^^^^^^^^^^^^^ trait message
5+
| ------------ ^^^^ trait message
66
| |
77
| required by a bound introduced by this call
88
|
@@ -23,10 +23,10 @@ LL | Index::index(&[] as &[i32], 2u32);
2323
<[i32] as Index<Foo<usize>>>
2424

2525
error[E0277]: the trait bound `[i32]: Index<Foo<u32>>` is not satisfied
26-
--> $DIR/multiple-impls.rs:35:18
26+
--> $DIR/multiple-impls.rs:35:33
2727
|
2828
LL | Index::index(&[] as &[i32], Foo(2u32));
29-
| ------------ ^^^^^^^^^^^^^ on impl for Foo
29+
| ------------ ^^^^^^^^^ on impl for Foo
3030
| |
3131
| required by a bound introduced by this call
3232
|
@@ -47,10 +47,10 @@ LL | Index::index(&[] as &[i32], Foo(2u32));
4747
<[i32] as Index<Foo<usize>>>
4848

4949
error[E0277]: the trait bound `[i32]: Index<Bar<u32>>` is not satisfied
50-
--> $DIR/multiple-impls.rs:37:18
50+
--> $DIR/multiple-impls.rs:37:33
5151
|
5252
LL | Index::index(&[] as &[i32], Bar(2u32));
53-
| ------------ ^^^^^^^^^^^^^ on impl for Bar
53+
| ------------ ^^^^^^^^^ on impl for Bar
5454
| |
5555
| required by a bound introduced by this call
5656
|

src/test/ui/on-unimplemented/on-impl.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: the trait bound `[i32]: Index<u32>` is not satisfied
2-
--> $DIR/on-impl.rs:22:25
2+
--> $DIR/on-impl.rs:22:47
33
|
44
LL | Index::<u32>::index(&[1, 2, 3] as &[i32], 2u32);
5-
| ------------------- ^^^^^^^^^^^^^^^^^^^^ a usize is required to index into a slice
5+
| ------------------- ^^^^ a usize is required to index into a slice
66
| |
77
| required by a bound introduced by this call
88
|

src/test/ui/traits/inheritance/repeated-supertrait-ambig.stderr

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
error[E0277]: the trait bound `dyn CompareToInts: CompareTo<i32>` is not satisfied
2-
--> $DIR/repeated-supertrait-ambig.rs:26:5
2+
--> $DIR/repeated-supertrait-ambig.rs:26:15
33
|
44
LL | c.same_as(22)
5-
| ^ ------- required by a bound introduced by this call
6-
| |
7-
| the trait `CompareTo<i32>` is not implemented for `dyn CompareToInts`
5+
| ------- ^^ the trait `CompareTo<i32>` is not implemented for `dyn CompareToInts`
6+
| |
7+
| required by a bound introduced by this call
88
|
99
= help: the following other types implement trait `CompareTo<T>`:
1010
<i64 as CompareTo<i64>>
1111
<i64 as CompareTo<u64>>
1212

1313
error[E0277]: the trait bound `C: CompareTo<i32>` is not satisfied
14-
--> $DIR/repeated-supertrait-ambig.rs:30:5
14+
--> $DIR/repeated-supertrait-ambig.rs:30:15
1515
|
1616
LL | c.same_as(22)
17-
| ^ ------- required by a bound introduced by this call
18-
| |
19-
| the trait `CompareTo<i32>` is not implemented for `C`
17+
| ------- ^^ the trait `CompareTo<i32>` is not implemented for `C`
18+
| |
19+
| required by a bound introduced by this call
2020
|
2121
help: consider further restricting this bound
2222
|
2323
LL | fn with_trait<C:CompareToInts + CompareTo<i32>>(c: &C) -> bool {
2424
| ++++++++++++++++
2525

2626
error[E0277]: the trait bound `dyn CompareToInts: CompareTo<i32>` is not satisfied
27-
--> $DIR/repeated-supertrait-ambig.rs:34:34
27+
--> $DIR/repeated-supertrait-ambig.rs:34:37
2828
|
2929
LL | <dyn CompareToInts>::same_as(c, 22)
30-
| ---------------------------- ^ the trait `CompareTo<i32>` is not implemented for `dyn CompareToInts`
30+
| ---------------------------- ^^ the trait `CompareTo<i32>` is not implemented for `dyn CompareToInts`
3131
| |
3232
| required by a bound introduced by this call
3333
|
@@ -36,10 +36,10 @@ LL | <dyn CompareToInts>::same_as(c, 22)
3636
<i64 as CompareTo<u64>>
3737

3838
error[E0277]: the trait bound `C: CompareTo<i32>` is not satisfied
39-
--> $DIR/repeated-supertrait-ambig.rs:38:24
39+
--> $DIR/repeated-supertrait-ambig.rs:38:27
4040
|
4141
LL | CompareTo::same_as(c, 22)
42-
| ------------------ ^ the trait `CompareTo<i32>` is not implemented for `C`
42+
| ------------------ ^^ the trait `CompareTo<i32>` is not implemented for `C`
4343
| |
4444
| required by a bound introduced by this call
4545
|
@@ -49,12 +49,12 @@ LL | fn with_ufcs2<C:CompareToInts + CompareTo<i32>>(c: &C) -> bool {
4949
| ++++++++++++++++
5050

5151
error[E0277]: the trait bound `i64: CompareTo<i32>` is not satisfied
52-
--> $DIR/repeated-supertrait-ambig.rs:42:16
52+
--> $DIR/repeated-supertrait-ambig.rs:42:31
5353
|
5454
LL | assert_eq!(22_i64.same_as(22), true);
55-
| ^^^^^^ ------- required by a bound introduced by this call
56-
| |
57-
| the trait `CompareTo<i32>` is not implemented for `i64`
55+
| ------- ^^ the trait `CompareTo<i32>` is not implemented for `i64`
56+
| |
57+
| required by a bound introduced by this call
5858
|
5959
= help: the following other types implement trait `CompareTo<T>`:
6060
<i64 as CompareTo<i64>>

src/test/ui/type/type-params-in-different-spaces-2.stderr

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
error[E0277]: the trait bound `Self: Tr<U>` is not satisfied
2-
--> $DIR/type-params-in-different-spaces-2.rs:10:9
2+
--> $DIR/type-params-in-different-spaces-2.rs:10:16
33
|
44
LL | Tr::op(u)
5-
| ^^^^^^ the trait `Tr<U>` is not implemented for `Self`
5+
| ------ ^ the trait `Tr<U>` is not implemented for `Self`
6+
| |
7+
| required by a bound introduced by this call
68
|
79
help: consider further restricting `Self`
810
|
911
LL | fn test<U>(u: U) -> Self where Self: Tr<U> {
1012
| +++++++++++++++++
1113

1214
error[E0277]: the trait bound `Self: Tr<U>` is not satisfied
13-
--> $DIR/type-params-in-different-spaces-2.rs:16:9
15+
--> $DIR/type-params-in-different-spaces-2.rs:16:16
1416
|
1517
LL | Tr::op(u)
16-
| ^^^^^^ the trait `Tr<U>` is not implemented for `Self`
18+
| ------ ^ the trait `Tr<U>` is not implemented for `Self`
19+
| |
20+
| required by a bound introduced by this call
1721
|
1822
help: consider further restricting `Self`
1923
|

src/test/ui/ufcs/ufcs-qpath-self-mismatch.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: cannot add `u32` to `i32`
2-
--> $DIR/ufcs-qpath-self-mismatch.rs:4:28
2+
--> $DIR/ufcs-qpath-self-mismatch.rs:4:31
33
|
44
LL | <i32 as Add<u32>>::add(1, 2);
5-
| ---------------------- ^ no implementation for `i32 + u32`
5+
| ---------------------- ^ no implementation for `i32 + u32`
66
| |
77
| required by a bound introduced by this call
88
|

0 commit comments

Comments
 (0)