You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
use std::cmp::PartialEq;#[derive(Debug)]enumMyOption<T>{Some(T),None,}impl<T,U:PartialEq<T>>PartialEq<Option<T>>forMyOption<U>{fneq(&self,other:&Option<T>) -> bool{match(self, other){(MyOption::Some(a),Some(b)) => a == b,(MyOption::None,None) => true,
_ => false}}}// This impl causes a worse error message. Comment out the impl to see the ideal oneimpl<T:PartialEq<U>,U>PartialEq<MyOption<T>>forOption<U>{#[inline]fneq(&self,other:&MyOption<T>) -> bool{
other.eq(self)}}#[derive(Debug)]structS;fnmain(){// Want: note: an implementation of `PartialEq` might be missing for `S`// Actual: = note: expected enum `MyOption<_>`// found enum `Option<S>`assert_eq!(Some(S), Some(S));}
error[E0369]: binary operation `==` cannot be applied to type`Option<S>`
--> src/main.rs:34:5
|
34 | assert_eq!(Some(S), Some(S));| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
||| Option<S>| Option<S>|
note: an implementation of `PartialEq` might be missing for`S`
--> src/main.rs:28:1
|
28 | struct S;| ^^^^^^^^ must implement `PartialEq`
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider annotating `S` with `#[derive(PartialEq)]`|
28 + #[derive(PartialEq)]
29 | struct S;|
Rationale and extra context
The actual issue in the code is that S does not implement PartialEq, and ideally the error message would tell me to implement PartialEq for S. rustc looks for an implementation of PartialEq on Option<S> and due to the blanket impls for comparing Option and MyOption, it thinks that wrapping in MyOption will resolve it (it won't) and suggests to use MyOption, even though the type MyOption is not used at all on the relevant line.
The MyOption type in the repro is inspired by rkyv::option::ArchivedOption which has this issue in the real world. I have helped several beginners with this exact compile error due to ArchivedOption
The text was updated successfully, but these errors were encountered:
kuecks
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
Jul 2, 2024
Code
Current output
Desired output
Rationale and extra context
The actual issue in the code is that S does not implement PartialEq, and ideally the error message would tell me to implement PartialEq for S. rustc looks for an implementation of PartialEq on Option<S> and due to the blanket impls for comparing Option and MyOption, it thinks that wrapping in MyOption will resolve it (it won't) and suggests to use MyOption, even though the type MyOption is not used at all on the relevant line.
The MyOption type in the repro is inspired by rkyv::option::ArchivedOption which has this issue in the real world. I have helped several beginners with this exact compile error due to ArchivedOption
Other cases
No response
Rust Version
Anything else?
Playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=7a349cc3cb9f841f52b4f07e58501226
The text was updated successfully, but these errors were encountered: