Skip to content

feat(ts): compare through a held type's own ne, as std's slices, tuples, and Arcs do - #316

Merged
kixelated merged 4 commits into
mainfrom
quest/m3/ts-eq-own-ne
Oct 31, 2026
Merged

kixelated merged 4 commits into
mainfrom
quest/m3/ts-eq-own-ne

Conversation

@kixelated

@kixelated kixelated commented Oct 31, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

== of slices, arrays, Vecs, and VecDeques of a crate's type whose PartialEq overrides ne refused with "through a held type's own", as did != of a tuple, an Arc, or an Rc holding one. The runtime's eqSlice, eqTuple, and eqArc were handed each item's eq, where std calls its ne. A tuple or Vec of Reverse<T> refused too, though std's derived Reverse::ne is !eq.

Approach

One lowering function, not_ne_fn, gives !<T as PartialEq>::ne. It is eq_fn unless T's ne reaches the crate's own:

  • tuple: eqTuple of each field's !ne, so it stops at the first field whose ne is true, as std's || chain does
  • Arc/Rc: eqArc of the inner !ne, keeping the pointer shortcut when the inner type is Eq
  • the crate's type: !T::ne

eq_fn's slices, item_eq (which #313's eqSeq uses for VecDeques and unlike items), and a direct != all use it. own_ne no longer recurses into slice elements or Reverse, since std's ne for both is !eq. The runtime is unchanged.

Audit of std's ne (nightly-2026-10-15 library/)

Impl ne Source
[T] == [U] !eq; eq checks lengths, then each *lhs != *rhs in order core/src/slice/cmp.rs:14-29, 125-136
[T; N] spec_ne → a[..] != b[..]; [T; N] != [U] → *self != *b, else true core/src/array/equality.rs:5-37, 141-149
Vec (and Cow<[T]> against a Vec) self[..] != other[..] alloc/src/vec/partial_eq.rs:17
VecDeque !eq; eq compares the slices by == alloc/src/collections/vec_deque/mod.rs:3862, macros.rs
tuples self.i != other.i joined by || core/src/tuple.rs:30-36
Arc, Rc **self != **other; for T: Eq, !ptr_eq && **self != **other alloc/src/rcs/arc.rs:3681-3730, rcs/rc.rs:2758-2815
Box PartialEq::ne(&**self, &**other) alloc/src/boxed.rs:2312-2321
&A, &mut A PartialEq::ne(*self, *other) core/src/cmp.rs:2445-2460, 2520-2625
Option !eq; eq is *l == *r core/src/option.rs:2439-2450
Result, Reverse, Wrapping derived, so !eq core/src/result.rs:554, core/src/cmp.rs:696, core/src/num/wrapping.rs:40
Cell, RefCell !eq core/src/cell.rs:349, 1476
Cow !eq; eq is PartialEq::eq(&**self, &**other) alloc/src/borrow.rs:385-394

peel sees through &, Box, and NonZero, all of which forward ne. Result lowers from core's MIR. Cell, RefCell, Cow<[T]>, and Wrapping refuse outright before they reach this code (probed separately), so they produce no wrong output; the follow-ups below cover them.

Proof

collections::own_nes runs differentially against native wasm32 on 640 inputs: 128 choices of the positions where a Picky's eq (remainder by 4) and ne (remainder by 3) disagree, times lengths 0, 1, 2, 4, and 9, with the right side optionally one shorter. Each eq/ne call adds to an ordered log. The case covers slices, arrays, Vecs, empty and unequal lengths, nested Vec<Vec<_>>, tuples (including short-circuiting at the first field and nesting), Option/Reverse/Box/& inside tuples and Vecs, Arc/Rc with and without Eq (an Arc<(Picky, f32)> compares even against itself), Vec<Arc<_>>, and wrapped VecDeques against each other, a Vec, and a slice.

Mutation check: lowering a tuple's != by its fields' eq makes the differential test fail on 827 inputs.

Cost

The runtime didn't change, and neither did any existing generated code: every snapshot change is new output for the new case. A type with no ne of its own still lowers through eq_fn as before, so I didn't run a benchmark.

Impact

  • Translator output only: these comparisons now translate where they used to refuse. No public API change.

Alternatives

  • A runtime neTuple/neArc taking each field's ne. I didn't add them: != is the negation of eqTuple/eqArc over each part's !ne, with the same short-circuit order, so the existing functions cover it.

Follow-ups

Review

quest review with Codex (gpt-6.1-sol, medium effort), reviewed head 356590f7e96f464526f73ff6f24e54b6f9e31372:

  • Round 1, head d251a247, found one issue: a zip overread in ts/src/lower/iter.rs:877. Refuted: that head predated origin/main's feat(ts): fold a zip of TrustedLen iterators by its size hint #314, so the diff made it look reverted. This PR doesn't touch iter.rs, and after merging origin/main, git diff origin/main -- ts/src/lower/iter.rs is empty.
  • Round 2, head 356590f7, found nothing.

Local just ci passes on 356590f7.

(written by Claude Opus 5.5)

🤖 Generated with Claude Code

kixelated and others added 4 commits October 30, 2026 17:23
Quest-Claim: 540569525a39f09e
…es, and Arcs do

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ixture's lines

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated kixelated changed the title Equality through a held type's own ne feat(ts): compare through a held type's own ne, as std's slices, tuples, and Arcs do Oct 31, 2026
@kixelated
kixelated marked this pull request as ready for review October 31, 2026 01:21
@kixelated
kixelated merged commit d1a076e into main Oct 31, 2026
1 of 2 checks passed
@kixelated
kixelated deleted the quest/m3/ts-eq-own-ne branch October 31, 2026 01:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant