-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix incorrect suggestion for trait bounds involving binary operators #94034
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Regression test for #93927: suggested trait bound for T should be Eq, not PartialEq | ||
struct MyType<T>(T); | ||
|
||
impl<T> PartialEq for MyType<T> | ||
where | ||
T: Eq, | ||
{ | ||
fn eq(&self, other: &Self) -> bool { | ||
true | ||
} | ||
} | ||
|
||
fn cond<T: PartialEq>(val: MyType<T>) -> bool { | ||
val == val | ||
//~^ ERROR binary operation `==` cannot be applied to type `MyType<T>` | ||
} | ||
|
||
fn main() { | ||
cond(MyType(0)); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
error[E0369]: binary operation `==` cannot be applied to type `MyType<T>` | ||
--> $DIR/issue-93927.rs:14:9 | ||
| | ||
LL | val == val | ||
| --- ^^ --- MyType<T> | ||
| | | ||
| MyType<T> | ||
| | ||
help: consider further restricting this bound | ||
| | ||
LL | fn cond<T: PartialEq + std::cmp::Eq>(val: MyType<T>) -> bool { | ||
| ++++++++++++++ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0369`. |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
// run-rustfix | ||
|
||
use std::ops::Add; | ||
|
||
struct A<B>(B); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
error: equality constraints are not yet supported in `where` clauses | ||
--> $DIR/missing-bounds.rs:37:33 | ||
--> $DIR/missing-bounds.rs:35:33 | ||
| | ||
LL | impl<B: Add> Add for E<B> where <B as Add>::Output = B { | ||
| ^^^^^^^^^^^^^^^^^^^^^^ not supported | ||
|
@@ -11,7 +11,7 @@ LL | impl<B: Add> Add for E<B> where B: Add<Output = B> { | |
| ~~~~~~~~~~~~~~~~~~ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/missing-bounds.rs:11:11 | ||
--> $DIR/missing-bounds.rs:9:11 | ||
| | ||
LL | impl<B> Add for A<B> where B: Add { | ||
| - this type parameter | ||
|
@@ -24,7 +24,7 @@ LL | A(self.0 + rhs.0) | |
= note: expected type parameter `B` | ||
found associated type `<B as Add>::Output` | ||
note: tuple struct defined here | ||
--> $DIR/missing-bounds.rs:5:8 | ||
--> $DIR/missing-bounds.rs:3:8 | ||
| | ||
LL | struct A<B>(B); | ||
| ^ | ||
|
@@ -34,7 +34,7 @@ LL | impl<B> Add for A<B> where B: Add + Add<Output = B> { | |
| +++++++++++++++++ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/missing-bounds.rs:21:14 | ||
--> $DIR/missing-bounds.rs:19:14 | ||
| | ||
LL | impl<B: Add> Add for C<B> { | ||
| - this type parameter | ||
|
@@ -47,7 +47,7 @@ LL | Self(self.0 + rhs.0) | |
= note: expected type parameter `B` | ||
found associated type `<B as Add>::Output` | ||
note: tuple struct defined here | ||
--> $DIR/missing-bounds.rs:15:8 | ||
--> $DIR/missing-bounds.rs:13:8 | ||
| | ||
LL | struct C<B>(B); | ||
| ^ | ||
|
@@ -57,7 +57,7 @@ LL | impl<B: Add + Add<Output = B>> Add for C<B> { | |
| +++++++++++++++++ | ||
|
||
error[E0369]: cannot add `B` to `B` | ||
--> $DIR/missing-bounds.rs:31:21 | ||
--> $DIR/missing-bounds.rs:29:21 | ||
| | ||
LL | Self(self.0 + rhs.0) | ||
| ------ ^ ----- B | ||
|
@@ -66,11 +66,11 @@ LL | Self(self.0 + rhs.0) | |
| | ||
help: consider restricting type parameter `B` | ||
| | ||
LL | impl<B: std::ops::Add<Output = B>> Add for D<B> { | ||
| +++++++++++++++++++++++++++ | ||
LL | impl<B: std::ops::Add> Add for D<B> { | ||
| +++++++++++++++ | ||
Comment on lines
-69
to
+70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note to self: check if we can get this back. |
||
|
||
error[E0308]: mismatched types | ||
--> $DIR/missing-bounds.rs:42:14 | ||
--> $DIR/missing-bounds.rs:40:14 | ||
| | ||
LL | impl<B: Add> Add for E<B> where <B as Add>::Output = B { | ||
| - this type parameter | ||
|
@@ -83,7 +83,7 @@ LL | Self(self.0 + rhs.0) | |
= note: expected type parameter `B` | ||
found associated type `<B as Add>::Output` | ||
note: tuple struct defined here | ||
--> $DIR/missing-bounds.rs:35:8 | ||
--> $DIR/missing-bounds.rs:33:8 | ||
| | ||
LL | struct E<B>(B); | ||
| ^ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,10 +6,10 @@ LL | a.iter().map(|a| a*a) | |
| | | ||
| &T | ||
| | ||
help: consider restricting type parameter `T` | ||
help: consider introducing a `where` bound, but there might be an alternative better way to express this requirement | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preexisting, but this message is too vague. We should probably check all occurrences in our ui tests and see if we can be more targetted (or at least change the wording) |
||
| | ||
LL | fn func<'a, T: std::ops::Mul<Output = &T>>(a: &'a [T]) -> impl Iterator<Item=&'a T> { | ||
| ++++++++++++++++++++++++++++ | ||
LL | fn func<'a, T>(a: &'a [T]) -> impl Iterator<Item=&'a T> where &T: Mul<&T> { | ||
| +++++++++++++++++ | ||
Comment on lines
+11
to
+12
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this suggestion change correct? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, albeit underconstrained on Edit: I got mixed up. resolution-in-overloaded-op.stderr is what I was referring to. This suggestion is still correct, but underconstrained by not suggesting |
||
|
||
error: aborting due to previous error | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should teach this function about associated type bounds. You could filter all projection predicates for one that is for the current trait and type and if there is such a projection predicate, extend the suggestion with the assoc bound
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One issue I'm encountering. I don't think there's a sound way to determine what the appropriate
Output
type should be. The functioncheck_overloaded_binop
attempts to do this, but it can only computereturn_ty
if the initiallookup_op_method
returns aMethodCallee
, which it does not in the case being addressed.Is there some possibility here that I'm missing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm hoping that the correct associated type bound is in the
errors
list. Try dumping the entire list and checking if it containsPredicateKind::Projection
that match thepred
in the loop here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I've checked, and a
Projection
predicate does not already exist. This is because the relevant predicates are generated by the functionobligation_for_method
. That function would have to be extended to produce aProjection
predicate, except it can only do so if it knows about the expected associated types of a method, which it cannot in this case.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I made some related changes in https://github.com/rust-lang/rust/pull/90006/files, which won't be landing until I manage to get rid of the perf regression.