Skip to content

Overly specific PartialEq impl for Option and Result #76483

Open
@bugaevc

Description

@bugaevc

I tried this code:

// This works:
let s1: String = "foo".to_owned();
let s2: &str = "foo";
s1 == s2;

// But these don't:
let o1: Option<String> = Some("foo".to_owned());
let o2: Option<&str> = Some("foo");
o1 == o2;

let r1: Result<String, ()> = Ok("foo".to_owned());
let r2: Result<&str, ()> = Ok("foo");
r1 == r2;

(playground)

This is because PartialEq impls are #[derive]d for Option and Result, which gives them conservative bounds.

Instead of current

impl<T> PartialEq<Option<T>> for Option<T>
where
    T: PartialEq<T>

I'd expect

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

and likewise, for Result

impl<T1, T2, E1, E2> PartialEq<Result<T2, E2>> for Result<T1, E1>
where
    T1: PartialEq<T2>,
    E1: PartialEq<E2>,

Note that this is similar to what Vec<T> already implements:

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

Meta

rustc --version --verbose:

rustc 1.45.2 (d3fb005a3 2020-07-31)
binary: rustc
commit-hash: d3fb005a39e62501b8b0b356166e515ae24e2e54
commit-date: 2020-07-31
host: x86_64-unknown-linux-gnu
release: 1.45.2
LLVM version: 10.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.T-libs-apiRelevant to the library API 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