# PartialEq appears to be underspecified

**URL:** <https://internals.rust-lang.org/t/partialeq-appears-to-be-underspecified/24846>\
**Category:** libs\
**Created:** [November 27, 2026, 1:23pm UTC](https://internals.rust-lang.org/t/partialeq-appears-to-be-underspecified/24846 "2026-11-27T13:23:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kixunil](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kixunil/32/4110_2.png) [@Kixunil](https://internals.rust-lang.org/u/Kixunil)\
**Post date:** [November 27, 2026, 1:23pm UTC](https://internals.rust-lang.org/t/partialeq-appears-to-be-underspecified/24846/1 "2026-11-27T13:23:36Z")

</div>

I came across an interesting edge case in the `PartialEq` documentation: while it goes to great lengths to explain how it needs to be symmetric and transitive, it does not forbid this code:

```rust
pub struct Insane(AtomicU32);

impl PartialEq for Insane {
    fn eq(&self, rhs: &Self) -> bool {
        let left = self.0.fetch_add(1, Ordering::Relaxed);
        let right = rhs.0.load(Ordering::Relaxed);
        left == right
    }
}

```

This is symmetric and transitive because the original value is compared, so by documentation, this is a valid implementation but I'm pretty sure if you actually used this it'd break a shitload of algorithms that implicitly rely on this not happening.

And frankly, I don't think anyone has ever written anything this insane, so I think it should be desirable to add something like this to the documentation:

> The methods must be repeatable, that means, any number of calls to `eq` or `ne` must always produce the same result. This is similar to the function being pure and any pure function satisfies this requirement but the function does not have to be pure. It can mutate any state such as by logging the calls as long as this does not affect the result of the repeated call.

Same requirement for `Ord` and `PartialOrd`.

---

<div class="post-metadata">

**Author:** ![fbstj](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/fbstj/32/424_2.png) [@fbstj](https://internals.rust-lang.org/u/fbstj)\
**Post date:** [November 27, 2026, 1:47pm UTC](https://internals.rust-lang.org/t/partialeq-appears-to-be-underspecified/24846/2 "2026-11-27T13:47:24Z")

</div>

I believe another 'technical' name for this 'repeatable' property is idempotent, but I think that's also usually covered by it being 'pure'

---

<div class="post-metadata">

**Author:** ![mbrubeck](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mbrubeck/32/174_2.png) [@mbrubeck](https://internals.rust-lang.org/u/mbrubeck)\
**Post date:** [November 27, 2026, 11:31pm UTC](https://internals.rust-lang.org/t/partialeq-appears-to-be-underspecified/24846/3 "2026-11-27T23:31:45Z")

</div>

Arguably this is neither symmetric nor transitive. For example, both of these tests fail:

```rust
#[test]
fn test_symmetric() {
    let a = Insane(0.into());
    let b = Insane(0.into());

    if a == b {
        assert!(b == a);
    }
}

#[test]
fn test_transitive() {
    let a = Insane(0.into());
    let b = Insane(0.into());
    let c = Insane(0.into());
    
    if a == b && b == c {
        assert!(a == c);
    }
}

```

---

<div class="post-metadata">

**Author:** ![lordan](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/lordan/32/2107_2.png) [@lordan](https://internals.rust-lang.org/u/lordan)\
**Post date:** [November 28, 2026, 2:23am UTC](https://internals.rust-lang.org/t/partialeq-appears-to-be-underspecified/24846/4 "2026-11-28T02:23:11Z")

</div>

However, it could be made idempotent, and thus symmetric and transitive, with e.g., `fetch_or` or `fetch_and`:

```rust
impl PartialEq for Insane {
    fn eq(&self, rhs: &Self) -> bool {
        let left = self.0.fetch_or(1, Ordering::Relaxed);
        let right = rhs.0.fetch_or(1, Ordering::Relaxed);
        left == right
    }
}

```

I guess the question is whether `PartialEq::eq` (and `ne` for that matter) shouldn't be required to be pure, and mutation of the operands or other side effects (other than for debugging) be disallowed/discouraged.

---

<div class="post-metadata">

**Author:** ![SkiFire13](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/skifire13/32/7579_2.png) [@SkiFire13](https://internals.rust-lang.org/u/SkiFire13)\
**Post date:** [November 28, 2026, 8:33am UTC](https://internals.rust-lang.org/t/partialeq-appears-to-be-underspecified/24846/5 "2026-11-28T08:33:59Z")

</div>

That's still not idempotent. On the first execution the values of `self.0` and `rhs.0` are compared. On the second execution they became `self.0 | 1` and `rhs.0 | 1`
