-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Better error messages for &T == T #40660
Comments
Would the following output (as proposed in #40565) be enough to make the situation a bit more clear? error[E0277]: the trait bound `&T: std::cmp::PartialEq<T>>` is not satisfied
--> $DIR/file.rs:14:5
|
14 | &t == t;
| ^^^^^^^ can't compare `&T` with `T` |
that would certainly improve things, but in this case the error message could be even more specific and thereby helpful by providing the information about deref-ing, so i would keep it as a separate issue, i think |
Add better error message when == operator is badly used Part of #40660. With the following code: ```rust fn foo<T: PartialEq>(a: &T, b: T) { a == b; } fn main() { foo(&1, 1); } ``` It prints: ``` error[E0277]: the trait bound `&T: std::cmp::PartialEq<T>` is not satisfied --> test.rs:2:5 | 2 | a == b; | ^^^^^^ can't compare `&T` with `T` | = help: the trait `std::cmp::PartialEq<T>` is not implemented for `&T` = help: consider adding a `where &T: std::cmp::PartialEq<T>` bound error: aborting due to previous error ```
fn main() {
let x: usize = 3;
let y: &usize = &3;
if x == y {
println!("asdf");
}
if y == x {
println!("asdf");
}
if 3 == &3 {
println!("asdf");
}
if &3 == 3 {
println!("asdf");
}
}
There's room for improvement. |
Current output
|
Triage: current output is only slightly improved over previous output.
It's not clear what should improve to make this better. Should we try to detect if |
Output with #90006:
|
Another case that should be accounted for (from #67571): struct MyType<T>
where T: PartialEq,
{
data: T,
}
impl<T> MyType<T>
where T: PartialEq,
{
fn write(&mut self, data: T) {
self.data = data;
}
fn matches(&self, data: &T) -> bool {
data == self.data
}
}
fn main() {
let foo: MyType<String> = MyType { data: String::from("ABC") };
let bar = String::from("XYZ");
if foo.matches(&bar) {
println!("{:?}", bar);
}
} |
Triage, few changes:
|
I've gotten the output to look like this: fn main() {
let x: usize = 3;
let y: &usize = &3;
_ = y == x;
_ = &y == x;
_ = &&y == x;
_ = 3 == &3;
_ = &3 == 3;
}
I'll see if I can get Rhs suggestions working. @rustbot claim |
@sjwang05 if you make a new ObligationCauseCode you should be able to keep both lhs and rhs in it to do this. |
Storing the lhs and rhs |
Something along the lines of "you are trying to compare a refference to to Type, try dereferencing first" would be nice, right now it just has the usual PartialEq not implemented.
The text was updated successfully, but these errors were encountered: