Skip to content

confusing type mismatch error when external crates introduce additional trait implementations #96972

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

Closed
nagisa opened this issue May 12, 2022 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked.

Comments

@nagisa
Copy link
Member

nagisa commented May 12, 2022

Consider the following code:

pub enum Error {
    Foo,
}

#[test]
fn it_works() {
    Err(Error::Foo) == Err(Error::Foo);
}

The error message output here is very nice and easy to understand:

error[E0369]: binary operation `==` cannot be applied to type `Result<_, Error>`
 --> src/lib.rs:7:21
  |
7 |     Err(Error::Foo) == Err(Error::Foo);
  |     --------------- ^^ --------------- Result<_, Error>
  |     |
  |     Result<_, Error>
  |
note: an implementation of `PartialEq` might be missing for `Error`
...

Now lets add a dependency on an external crate (inlined here) (playground):

// NB: in the original reproduction this was an external crate.
pub mod rkyv {
    pub enum ArchivedResult<T, E> {
        Ok(T),
        Err(E),
    }

    impl<T: PartialEq<U>, U, E: PartialEq<F>, F> PartialEq<ArchivedResult<T, E>>
    for Result<U, F> {
        fn eq(&self, _: &ArchivedResult<T, E>) -> bool { todo!() }
    }
}

pub enum Error {
    Foo,
}

#[test]
fn it_works() {
    Err(Error::Foo) == Err(Error::Foo);
}

Once that's done the diagnostic regresses significantly, to the point where it made me suspect an incremental or a similarly serious bug somewhere

error[E0308]: mismatched types
  --> src/lib.rs:20:24
   |
20 |     Err(Error::Foo) == Err(Error::Foo);
   |                        ^^^^^^^^^^^^^^^ expected enum `ArchivedResult`, found enum `Result`
   |
   = note: expected enum `ArchivedResult<_, _>`
              found enum `Result<_, Error>`
help: try wrapping the expression in a variant of `ArchivedResult`
   |
20 |     Err(Error::Foo) == rkyv::ArchivedResult::Ok(Err(Error::Foo));
   |                        +++++++++++++++++++++++++               +
20 |     Err(Error::Foo) == rkyv::ArchivedResult::Err(Err(Error::Foo));
   |                        ++++++++++++++++++++++++++               +

I think I would've preferred a plain old “trait is not implemented” error here.

@nagisa nagisa added A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. labels May 12, 2022
@SNCPlay42
Copy link
Contributor

This looks like #77304.

@nagisa
Copy link
Member Author

nagisa commented May 12, 2022

Looks like its probably that, yeah, although in my case I've seen the same problem present itself when calling methods directly too.

Duplicate of #77304

@nagisa nagisa closed this as completed May 12, 2022
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 D-confusing Diagnostics: Confusing error or lint that should be reworked.
Projects
None yet
Development

No branches or pull requests

2 participants