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 PartialEq bound for binary operation == error #73416

Closed
tesuji opened this issue Jun 16, 2020 · 4 comments · Fixed by #73674
Closed

Suggest PartialEq bound for binary operation == error #73416

tesuji opened this issue Jun 16, 2020 · 4 comments · Fixed by #73674
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@tesuji
Copy link
Contributor

tesuji commented Jun 16, 2020

Try this snippet: playground

pub fn strip_prefix<'a, T>(s: &'a [T], prefix: &[T]) -> Option<&'a [T]>
// where 
//     T: PartialEq
{
    let n = prefix.len();
    if n <= s.len() {
        let (head, tail) = s.split_at(n);
        if head == prefix {
            return Some(tail);
        }
    }
    None
}

The compiler errors out:

error[E0369]: binary operation `==` cannot be applied to type `&[T]`
 --> src/lib.rs:8:17
  |
8 |         if head == prefix {
  |            ---- ^^ ------ &[T]
  |            |
  |            &[T]

error: aborting due to previous error

For more information about this error, try `rustc --explain E0369`.

It would be nice if the compiler could suggest adding PartialEq bound for T.

@tesuji tesuji changed the title Suggest PartialEq bounds for "==" error Suggest PartialEq bound for "==" error Jun 16, 2020
@tesuji tesuji changed the title Suggest PartialEq bound for "==" error Suggest PartialEq bound for binary operation == error Jun 16, 2020
@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 16, 2020
@estebank estebank added the A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` label Jun 16, 2020
@estebank
Copy link
Contributor

related: #73261

@tesuji
Copy link
Contributor Author

tesuji commented Jun 17, 2020

Note: The stderr for Rust 1.42 and below:

error[E0369]: binary operation `==` cannot be applied to type `&[T]`
 --> <source>:8:17
  |
8 |         if head == prefix {
  |            ---- ^^ ------ &[T]
  |            |
  |            &[T]
  |
  = note: an implementation of `std::cmp::PartialEq` might be missing for `&[T]`

@estebank
Copy link
Contributor

Yep. The ideal output would be something along the lines of

error[E0369]: binary operation `==` cannot be applied to type `&[T]`
 --> src/lib.rs:8:17
  |
8 |         if head == prefix {
  |            ---- ^^ ------ &[T]
  |            |
  |            &[T]
help: consider restricting `T` to require it implements `std::cmp::PartialEq`
  |
1 | pub fn strip_prefix<'a, T: std::cmp::PartialEq>(s: &'a [T], prefix: &[T]) -> Option<&'a [T]>
  |                          ^^^^^^^^^^^^^^^^^^^^^

@estebank
Copy link
Contributor

How does this look like?

Screen Shot 2020-06-23 at 4 54 50 PM

Manishearth added a commit to Manishearth/rust that referenced this issue Jun 26, 2020
… r=davidtwco

Tweak binop errors

* Suggest potentially missing binop trait bound (fix rust-lang#73416)
* Use structured suggestion for dereference in binop
Manishearth added a commit to Manishearth/rust that referenced this issue Jun 26, 2020
… r=davidtwco

Tweak binop errors

* Suggest potentially missing binop trait bound (fix rust-lang#73416)
* Use structured suggestion for dereference in binop
@bors bors closed this as completed in 5aab1a9 Jun 26, 2020
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` C-enhancement Category: An issue proposing an enhancement or a PR with one. 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.

3 participants