Skip to content

Binary operators still have worse trait error information than their corrosponding operators. #135781

Open
@lolbinarycat

Description

@lolbinarycat

Code

struct Whatever;

fn main() {
    let _ = Some([1, 2].into_iter()) == &Some([1, 2].into_iter());
}

Current output

error[E0369]: binary operation `==` cannot be applied to type `Option<std::array::IntoIter<{integer}, 2>>`
   --> src/main.rs:4:38
    |
4   |     let _ = Some([1, 2].into_iter()) == &Some([1, 2].into_iter());
    |             ------------------------ ^^ ------------------------- &Option<std::array::IntoIter<{integer}, 2>>
    |             |
    |             Option<std::array::IntoIter<{integer}, 2>>
    |
note: the foreign item type `Option<std::array::IntoIter<{integer}, 2>>` doesn't implement `PartialEq<&Option<std::array::IntoIter<{integer}, 2>>>`
   --> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/option.rs:572:1
    |
572 | pub enum Option<T> {
    | ^^^^^^^^^^^^^^^^^^ not implement `PartialEq<&Option<std::array::IntoIter<{integer}, 2>>>`

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

Desired output

error[E0369]: binary operation `==` cannot be applied to type `Option<std::array::IntoIter<{integer}, 2>>`
   --> src/main.rs:4:38
    |
4   |     let _ = Some([1, 2].into_iter()) == &Some([1, 2].into_iter());
    |             ------------------------ ^^ ------------------------- &Option<std::array::IntoIter<{integer}, 2>>
    |             |
    |             Option<std::array::IntoIter<{integer}, 2>>
    |
    |
    = note: the following trait bounds were not satisfied:
             `std::array::IntoIter<{integer}, 2>: PartialEq`
             which is required by `Option<std::array::IntoIter<{integer}, 2>>: PartialEq`
             `Option<std::array::IntoIter<{integer}, 2>>: Iterator`
             which is required by `&mut Option<std::array::IntoIter<{integer}, 2>>: Iterator`

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

Rationale and extra context

compare to this code:

struct Whatever;

fn main() {
    let _ = Some([1, 2].into_iter()).eq(&Some([1, 2].into_iter()));
}

that gives the exact trait bounds that were not satisfied (in particular, iterators over arrays do not implement PartialEq).

this isn't the most realistic case, but i wanted to repro it without external dependencies, and there's a different diagnostic that triggers if the type is defined in the same crate as the error.

Other cases

this diagnostic should fire whenever there is a type whose ability to implement a trait is dependent on one of its type variables implementing a trait.

Rust Version

rustc 1.85.0-nightly (7442931d4 2024-11-30)

Anything else?

related to #125631

this time it would be nice if the logic could be unified somehow.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions