Prefer non-Self non-method types over Self, first

This commit is contained in:
Michael Goulet 2022-08-17 01:28:26 +00:00
parent c8746766cb
commit 24559ce2fe
9 changed files with 72 additions and 40 deletions

View File

@ -1669,6 +1669,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if let ty::GenericArgKind::Type(ty) = arg.unpack()
&& let ty::Param(param_ty) = ty.kind()
&& self.tcx.parent(generics.type_param(param_ty, self.tcx).def_id) != def_id
&& param_ty.name != rustc_span::symbol::kw::SelfUpper
{
Some(arg)
} else {
None
}
})
});
let mut self_param_to_point_at = predicate_substs.types().find_map(|ty| {
ty.walk().find_map(|arg| {
if let ty::GenericArgKind::Type(ty) = arg.unpack()
&& let ty::Param(param_ty) = ty.kind()
&& param_ty.name == rustc_span::symbol::kw::SelfUpper
{
Some(arg)
} else {
@ -1681,6 +1694,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// to print a relevant error.
if let traits::FulfillmentErrorCode::CodeAmbiguity = error.code {
fallback_param_to_point_at = None;
self_param_to_point_at = None;
param_to_point_at =
self.find_ambiguous_parameter_in(def_id, error.root_obligation.predicate);
}
@ -1704,6 +1718,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
return;
}
if let Some(self_param_to_point_at) = self_param_to_point_at
&& self.point_at_args_if_possible(error, def_id, self_param_to_point_at, *call_hir_id, callee.span, args)
{
return;
}
if let hir::QPath::Resolved(_, path) = qpath
&& let Some(param_to_point_at) = param_to_point_at
&& let Some(segment) = path.segments.last()
@ -1733,6 +1753,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
return;
}
if let Some(self_param_to_point_at) = self_param_to_point_at
&& self.point_at_args_if_possible(error, def_id, self_param_to_point_at, hir_id, segment.ident.span, args)
{
return;
}
if let Some(param_to_point_at) = param_to_point_at
&& self.point_at_generics_if_possible(error, def_id, param_to_point_at, segment)
{

View File

@ -1,10 +1,10 @@
error[E0277]: the trait bound `Bar: Foo<usize>` is not satisfied
--> $DIR/issue-21659-show-relevant-trait-impls-1.rs:24:5
--> $DIR/issue-21659-show-relevant-trait-impls-1.rs:24:12
|
LL | f1.foo(1usize);
| ^^ --- required by a bound introduced by this call
| |
| the trait `Foo<usize>` is not implemented for `Bar`
| --- ^^^^^^ the trait `Foo<usize>` is not implemented for `Bar`
| |
| required by a bound introduced by this call
|
= help: the following other types implement trait `Foo<A>`:
<Bar as Foo<i32>>

View File

@ -1,10 +1,10 @@
error[E0277]: the trait bound `Bar: Foo<usize>` is not satisfied
--> $DIR/issue-21659-show-relevant-trait-impls-2.rs:28:5
--> $DIR/issue-21659-show-relevant-trait-impls-2.rs:28:12
|
LL | f1.foo(1usize);
| ^^ --- required by a bound introduced by this call
| |
| the trait `Foo<usize>` is not implemented for `Bar`
| --- ^^^^^^ the trait `Foo<usize>` is not implemented for `Bar`
| |
| required by a bound introduced by this call
|
= help: the following other types implement trait `Foo<A>`:
<Bar as Foo<i16>>

View File

@ -1,8 +1,10 @@
error[E0277]: the trait bound `E: From<()>` is not satisfied
--> $DIR/never-value-fallback-issue-66757.rs:28:5
--> $DIR/never-value-fallback-issue-66757.rs:28:26
|
LL | <E as From<_>>::from(never);
| ^^^^^^^^^^^^^^^^^^^^ the trait `From<()>` is not implemented for `E`
| -------------------- ^^^^^ the trait `From<()>` is not implemented for `E`
| |
| required by a bound introduced by this call
|
= help: the trait `From<!>` is implemented for `E`

View File

@ -1,8 +1,8 @@
error[E0277]: the trait bound `[i32]: Index<u32>` is not satisfied
--> $DIR/multiple-impls.rs:33:18
--> $DIR/multiple-impls.rs:33:33
|
LL | Index::index(&[] as &[i32], 2u32);
| ------------ ^^^^^^^^^^^^^ trait message
| ------------ ^^^^ trait message
| |
| required by a bound introduced by this call
|
@ -23,10 +23,10 @@ LL | Index::index(&[] as &[i32], 2u32);
<[i32] as Index<Foo<usize>>>
error[E0277]: the trait bound `[i32]: Index<Foo<u32>>` is not satisfied
--> $DIR/multiple-impls.rs:35:18
--> $DIR/multiple-impls.rs:35:33
|
LL | Index::index(&[] as &[i32], Foo(2u32));
| ------------ ^^^^^^^^^^^^^ on impl for Foo
| ------------ ^^^^^^^^^ on impl for Foo
| |
| required by a bound introduced by this call
|
@ -47,10 +47,10 @@ LL | Index::index(&[] as &[i32], Foo(2u32));
<[i32] as Index<Foo<usize>>>
error[E0277]: the trait bound `[i32]: Index<Bar<u32>>` is not satisfied
--> $DIR/multiple-impls.rs:37:18
--> $DIR/multiple-impls.rs:37:33
|
LL | Index::index(&[] as &[i32], Bar(2u32));
| ------------ ^^^^^^^^^^^^^ on impl for Bar
| ------------ ^^^^^^^^^ on impl for Bar
| |
| required by a bound introduced by this call
|

View File

@ -1,8 +1,8 @@
error[E0277]: the trait bound `[i32]: Index<u32>` is not satisfied
--> $DIR/on-impl.rs:22:25
--> $DIR/on-impl.rs:22:47
|
LL | Index::<u32>::index(&[1, 2, 3] as &[i32], 2u32);
| ------------------- ^^^^^^^^^^^^^^^^^^^^ a usize is required to index into a slice
| ------------------- ^^^^ a usize is required to index into a slice
| |
| required by a bound introduced by this call
|

View File

@ -1,22 +1,22 @@
error[E0277]: the trait bound `dyn CompareToInts: CompareTo<i32>` is not satisfied
--> $DIR/repeated-supertrait-ambig.rs:26:5
--> $DIR/repeated-supertrait-ambig.rs:26:15
|
LL | c.same_as(22)
| ^ ------- required by a bound introduced by this call
| |
| the trait `CompareTo<i32>` is not implemented for `dyn CompareToInts`
| ------- ^^ the trait `CompareTo<i32>` is not implemented for `dyn CompareToInts`
| |
| required by a bound introduced by this call
|
= help: the following other types implement trait `CompareTo<T>`:
<i64 as CompareTo<i64>>
<i64 as CompareTo<u64>>
error[E0277]: the trait bound `C: CompareTo<i32>` is not satisfied
--> $DIR/repeated-supertrait-ambig.rs:30:5
--> $DIR/repeated-supertrait-ambig.rs:30:15
|
LL | c.same_as(22)
| ^ ------- required by a bound introduced by this call
| |
| the trait `CompareTo<i32>` is not implemented for `C`
| ------- ^^ the trait `CompareTo<i32>` is not implemented for `C`
| |
| required by a bound introduced by this call
|
help: consider further restricting this bound
|
@ -24,10 +24,10 @@ LL | fn with_trait<C:CompareToInts + CompareTo<i32>>(c: &C) -> bool {
| ++++++++++++++++
error[E0277]: the trait bound `dyn CompareToInts: CompareTo<i32>` is not satisfied
--> $DIR/repeated-supertrait-ambig.rs:34:34
--> $DIR/repeated-supertrait-ambig.rs:34:37
|
LL | <dyn CompareToInts>::same_as(c, 22)
| ---------------------------- ^ the trait `CompareTo<i32>` is not implemented for `dyn CompareToInts`
| ---------------------------- ^^ the trait `CompareTo<i32>` is not implemented for `dyn CompareToInts`
| |
| required by a bound introduced by this call
|
@ -36,10 +36,10 @@ LL | <dyn CompareToInts>::same_as(c, 22)
<i64 as CompareTo<u64>>
error[E0277]: the trait bound `C: CompareTo<i32>` is not satisfied
--> $DIR/repeated-supertrait-ambig.rs:38:24
--> $DIR/repeated-supertrait-ambig.rs:38:27
|
LL | CompareTo::same_as(c, 22)
| ------------------ ^ the trait `CompareTo<i32>` is not implemented for `C`
| ------------------ ^^ the trait `CompareTo<i32>` is not implemented for `C`
| |
| required by a bound introduced by this call
|
@ -49,12 +49,12 @@ LL | fn with_ufcs2<C:CompareToInts + CompareTo<i32>>(c: &C) -> bool {
| ++++++++++++++++
error[E0277]: the trait bound `i64: CompareTo<i32>` is not satisfied
--> $DIR/repeated-supertrait-ambig.rs:42:16
--> $DIR/repeated-supertrait-ambig.rs:42:31
|
LL | assert_eq!(22_i64.same_as(22), true);
| ^^^^^^ ------- required by a bound introduced by this call
| |
| the trait `CompareTo<i32>` is not implemented for `i64`
| ------- ^^ the trait `CompareTo<i32>` is not implemented for `i64`
| |
| required by a bound introduced by this call
|
= help: the following other types implement trait `CompareTo<T>`:
<i64 as CompareTo<i64>>

View File

@ -1,8 +1,10 @@
error[E0277]: the trait bound `Self: Tr<U>` is not satisfied
--> $DIR/type-params-in-different-spaces-2.rs:10:9
--> $DIR/type-params-in-different-spaces-2.rs:10:16
|
LL | Tr::op(u)
| ^^^^^^ the trait `Tr<U>` is not implemented for `Self`
| ------ ^ the trait `Tr<U>` is not implemented for `Self`
| |
| required by a bound introduced by this call
|
help: consider further restricting `Self`
|
@ -10,10 +12,12 @@ LL | fn test<U>(u: U) -> Self where Self: Tr<U> {
| +++++++++++++++++
error[E0277]: the trait bound `Self: Tr<U>` is not satisfied
--> $DIR/type-params-in-different-spaces-2.rs:16:9
--> $DIR/type-params-in-different-spaces-2.rs:16:16
|
LL | Tr::op(u)
| ^^^^^^ the trait `Tr<U>` is not implemented for `Self`
| ------ ^ the trait `Tr<U>` is not implemented for `Self`
| |
| required by a bound introduced by this call
|
help: consider further restricting `Self`
|

View File

@ -1,8 +1,8 @@
error[E0277]: cannot add `u32` to `i32`
--> $DIR/ufcs-qpath-self-mismatch.rs:4:28
--> $DIR/ufcs-qpath-self-mismatch.rs:4:31
|
LL | <i32 as Add<u32>>::add(1, 2);
| ---------------------- ^ no implementation for `i32 + u32`
| ---------------------- ^ no implementation for `i32 + u32`
| |
| required by a bound introduced by this call
|