Skip to content
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

Suggest swapping the equality when the message can't compare occurs #132695

Closed
makai410 opened this issue Nov 6, 2024 · 1 comment · Fixed by #137171
Closed

Suggest swapping the equality when the message can't compare occurs #132695

makai410 opened this issue Nov 6, 2024 · 1 comment · Fixed by #137171
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@makai410
Copy link
Contributor

makai410 commented Nov 6, 2024

Code

// inspired by https://github.com/rust-lang/rust/issues/130495
struct T(String);

impl PartialEq<String> for T {
    fn eq(&self, other: &String) -> bool {
        &self.0 == other
    }
}

fn main() {
    String::from("123") == T(String::from("123"));
}

Current output

error[E0277]: can't compare `String` with `T`
  --> src/main.rs:11:25
   |
11 |     String::from("123") == T(String::from("123"));
   |                         ^^ no implementation for `String == T`
   |
   = help: the trait `PartialEq<T>` is not implemented for `String`
   = help: the following other types implement trait `PartialEq<Rhs>`:
             `String` implements `PartialEq<&str>`
             `String` implements `PartialEq<Cow<'_, str>>`
             `String` implements `PartialEq<str>`
             `String` implements `PartialEq`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (bin "playground") due to 1 previous error

Desired output

error[E0277]: can't compare `String` with `T`
  --> src/main.rs:11:25
   |
11 |     String::from("123") == T(String::from("123"));
   |                         ^^ no implementation for `String == T`
   |
   = help: the trait `PartialEq<T>` is not implemented for `String`
   = help: the following other types implement trait `PartialEq<Rhs>`:
             `String` implements `PartialEq<&str>`
             `String` implements `PartialEq<Cow<'_, str>>`
             `String` implements `PartialEq<str>`
             `String` implements `PartialEq`
   = note: `T` implements `PartialEq<String>`
help: consider swapping the equality
   |
11 |     T(String::from("123")) == String::from("123");
   |     ~~~~~~~~~~~~~~~~~~~~~~    ~~~~~~~~~~~~~~~~~~~


For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (bin "playground") due to 1 previous error

Rationale and extra context

In this case where T implements PartialEq<String>, swapping the equality is a good way to fix it.
Although #132404 has implemented this, it only covers the case where error[0308]: mismatched types occurs.

Other cases

No response

Rust Version

1.84.0-nightly (2024-10-15 e7c0d27) (playground)

Anything else?

No response

@makai410 makai410 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 6, 2024
@jieyouxu jieyouxu added the A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` label Nov 7, 2024
@makai410
Copy link
Contributor Author

@rustbot claim

@makai410 makai410 reopened this Feb 17, 2025
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 28, 2025
…r-errors

Suggest swapping equality on E0277

Closes: rust-lang#132695 .
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 28, 2025
…r-errors

Suggest swapping equality on E0277

Closes: rust-lang#132695 .
@bors bors closed this as completed in 51e0976 Mar 1, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Mar 1, 2025
Rollup merge of rust-lang#137171 - makai410:swapping-e0277, r=compiler-errors

Suggest swapping equality on E0277

Closes: rust-lang#132695 .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants