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

PartialEq between reference and non-reference type? #1332

Open
LukasKalbertodt opened this issue Oct 22, 2015 · 4 comments
Open

PartialEq between reference and non-reference type? #1332

LukasKalbertodt opened this issue Oct 22, 2015 · 4 comments
Labels
T-lang Relevant to the language team, which will review and decide on the RFC. T-libs-api Relevant to the library API team, which will review and decide on the RFC.

Comments

@LukasKalbertodt
Copy link
Member

I'm not sure, if that's intended, but it's "impossible" to compare a reference type with a non-reference type via PartialEq. Short example code:

let a = 3;
let b = 4;
&a == &b; // works
a == &b;   // doesn't work

This should work in my opinion. There already are these impls in std (I removed some noise -- original):

impl<A, B> PartialEq<&    B> for &    A where A: PartialEq<B>
impl<A, B> PartialEq<&mut B> for &mut A where A: PartialEq<B>
impl<A, B> PartialEq<&mut B> for &    A where A: PartialEq<B>
impl<A, B> PartialEq<&    B> for &mut A where A: PartialEq<B>

The third line in my example works because of this.

Why not something like this?

impl<A, B> PartialEq<&B> for  A where A: PartialEq<B>
impl<A, B> PartialEq< B> for &A where A: PartialEq<B>

Or do I misunderstand something?

@petrochenkov
Copy link
Contributor

Trait coherence prevents it :(

See rust-lang/rust#23521 for one (unsuccessful) attempt to fix it with help of OIBITs.
I'll try again when specialization is implemented.

@LukasKalbertodt
Copy link
Member Author

@petrochenkov It seems like specialization is mostly implemented (tracking issue, PR). I just came across this issue again and wanted to ask if there's anything new? :)

@nrc nrc added T-lang Relevant to the language team, which will review and decide on the RFC. T-libs-api Relevant to the library API team, which will review and decide on the RFC. labels Aug 25, 2016
@varkor
Copy link
Member

varkor commented Dec 22, 2017

I'm not sure specialisation helps in this situation. The problem as I see it is that if you something where: A: PartialEq<B> implies A: PartialEq<&B> and &A: PartialEq<B>, then there arise two ways to get to &A: PartialEq<&B> (depending on the order you apply the impls). These implementations are conflicting, even if you have a more specialised &A: PartialEq<&B> impl, because specialisation doesn't address this sort of case. What you want is some way for specialisation to "cover" the conflict-cases for multiple traits.

EDIT: Alternatively, if you could specify that a trait only applies to non-reference types, this should also solve the problem, along with the existing rules for referencing both types.

@nico-abram
Copy link

Would something like rust-lang/rust#42721 help there? (Referring to EDIT: Alternatively, if you could specify that a trait only applies to non-reference types)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-lang Relevant to the language team, which will review and decide on the RFC. T-libs-api Relevant to the library API team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests

5 participants