-
Notifications
You must be signed in to change notification settings - Fork 13k
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
rustc
suggests adding a PartialEq
bound where another trait is actually required
#93927
Comments
@rustbot label +D-incorrect +D-invalid-suggestion |
Reduced further: https://play.rust-lang.org/?version=stable&mode=release&edition=2021&gist=c6fde80b66900487eee5bd69cbde0970 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
}
fn main() {
cond(MyType(0));
}
|
rustc
suggests adding a PartialEq
bound where Eq
is actually requiredrustc
suggests adding a PartialEq
bound where another trait is actually required
This issue is more general than the original title suggested: if we switch the |
The relevant code is in op.rs, primarily added in this commit from @estebank: 8f40dae I believe the issue is that the high level if errors.len() == 1 {
if let Some(pred) =
errors[0].obligation.predicate.to_opt_poly_trait_pred()
{
self.infcx.suggest_restricting_param_bound(
&mut err,
pred,
self.body_id,
);
} else if *ty != lhs_ty {
// When we know that a missing bound is responsible, we don't show
// this note as it is redundant.
err.note(&format!(
"the trait `{}` is not implemented for `{}`",
missing_trait, lhs_ty
));
}
} The upside is that this fixes the issue at hand, and it consolidates related code. The downside is that the generic If y'all think this is an appropriate fix, I am happy to file a PR. This fixes at least these issues: Issue #93927Input: 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
}
fn main() {
cond(MyType(0));
} Output:
Issue #92347Input: fn rot<T : std::ops::Shl<Output = T>>(x:&T, k:u32) -> T {
let bit_count = std::mem::size_of::<T>() << 3;
(x << k as T) | (x >> (bit_count - k as T))
} Output:
Issue #93744Input: use core::ops::Add;
fn add<A, B, C>(a: A, b: B) -> C {
a + b
} Output:
|
…-binops, r=estebank Fix incorrect suggestion for trait bounds involving binary operators This PR fixes rust-lang#93927, rust-lang#92347, rust-lang#93744 by replacing the bespoke trait-suggestion logic in `op.rs` with a more common code path. The downside is that this fix causes some suggestions to not include an `Output=` type, reducing their usefulness. Note that this causes one case in the `missing-bounds.rs` test to fail rustfix. So I would need to move that code into a separate non-fix test if this PR is otherwise acceptable.
Given the following code (EDIT: see the comment below for a more concise snippet):
https://play.rust-lang.org/?version=stable&mode=release&edition=2021&gist=a69190abd7a2b258c588d8e994e69871
The current output is:
Ideally the output should look like:
!=
can't be used here becauseMyType<T>
only implementsPartialEq
whereT
implementsEq
, but instead of suggesting we add anEq
bound,rustc
suggests adding an additionalPartialEq
bound, despiteT
already beingPartialEq
.This bug is present on both stable and nightly.
The text was updated successfully, but these errors were encountered: