7

Github Remove requirement that forces symmetric and transitive PartialEq impls t...

 3 years ago
source link: https://github.com/rust-lang/rust/pull/81198
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Counterexample of symmetry:

If you have an impl like:

impl<T> PartialEq<T> for Ident
where
    T: ?Sized + AsRef<str>

then Rust will not even allow the symmetric impl to exist.

error[E0210]: type parameter `T` must be covered by another type when it appears before the first local type (`Ident`)
 --> src/main.rs:9:6
  |
9 | impl<T> PartialEq<Ident> for T where T: ?Sized + AsRef<str> {
  |      ^ type parameter `T` must be covered by another type when it appears before the first local type (`Ident`)
  |
  = note: implementing a foreign trait is only possible if at least one of the types for which it is implemented is local, and no uncovered type parameters appear before that first local type
  = note: in this case, 'before' refers to the following order: `impl<..> ForeignTrait<T1, ..., Tn> for T0`, where `T0` is the first and `Tn` is the last

Counterexample of transitivity:

Consider these two existing impls from regex and clap:

// regex

/// An inline representation of `Option<char>`.
pub struct Char(u32);

impl PartialEq<char> for Char {
    fn eq(&self, other: &char) -> bool {
        self.0 == *other as u32
    }
}
// clap

pub(crate) enum KeyType {
    Short(char),
    Long(OsString),
    Position(u64),
}

impl PartialEq<char> for KeyType {
    fn eq(&self, rhs: &char) -> bool {
        match self {
            KeyType::Short(c) => c == rhs,
            _ => false,
        }
    }
}

It's nice to be able to add PartialEq<proc_macro::Punct> for char in libproc_macro (#80595), but it makes no sense to force an impl PartialEq<Punct> for Char and impl PartialEq<Punct> for KeyType in regex and clap in code that otherwise has nothing to do with proc macros.

@rust-lang/libs


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK