Skip to content

feat(ts): compare Cells and RefCells by what they hold, and one-time cells by their Option - #324

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

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

Conversation

@kixelated

@kixelated kixelated commented Oct 31, 2026 •

Copy link
Copy Markdown
Collaborator

Closes the quest quest/m3/ts-eq-cells.md.

Problem

Every comparison of std's cells was refused as an unsupported call, including ==/!= of Cell and RefCell (the quest), their PartialOrd (partial_cmp, lt, le, gt, ge) and Ord::cmp, and PartialEq of OnceCell and OnceLock. Reproduced on a scratch crate: 18 refusals, one per operator, each with its span.

Approach

Pinned std (nightly-2026-10-15):

  • core/src/cell.rs:349-394: Cell compares self.get() by the held type's eq, partial_cmp, lt/le/gt/ge, and cmp.
  • core/src/cell.rs:1476-1540: RefCell does the same through *self.borrow() and *other.borrow(), so it panics with "RefCell already mutably borrowed" if either side is mutably borrowed.
  • core/src/cell/once.rs:413 and std/src/sync/once_lock.rs:728: one-time cells compare their get() as Options.
  • None of these impls overrides ne, so ne is !eq even where the held type overrides ne.

Changes:

  • cell_of in lower/collections.rs names what a cell compares by and the runtime wrapper that reads it. eq_fn, cmp_fn, test_fn, own_test, and std_key recurse through it, so cells compose with tuples, slices, Options, Arcs, and B-tree keys.
  • own_ne still stops at cells, so != of a cell never takes feat(ts): compare through a held type's own ne, as std's slices, tuples, and Arcs do #316's not_ne_fn path. A slice inside a cell still compares its items by their own ne, as std's does.
  • Runtime: rt.cellBy(f) reads v (Cell, OnceCell, OnceLock). rt.refCellBy(f) borrows a, then b, through refCellBorrow, so it panics with wasm32's message, and releases them in reverse order.
  • RwLock shares Std::RefCell but has no comparisons, so it is excluded by its diagnostic item.
  • Review round 1 finding: own_ord now looks through cells. Without that, collecting drop-valued entries keyed by a cell of an own Ord bypassed the existing refusal. This was reproduced (it lowered to rt.btreeCollect) and is now covered by the reject fixture's ranked_cells.

Tests: collections::cell_eqs in the lang differential fixture, against native wasm32. It covers:

  • Quirky, whose ne, lt/le/gt/ge, partial_cmp (sometimes None), and cmp each disagree with the others and log every call.
  • Every operator on Cell/RefCell of it, Cell<u8>, Cell<Option<u8>>, and RefCell<String>.
  • A RefCell compared with itself, including while shared-borrowed.
  • Nested cells (RefCell<RefCell<_>>, RefCell<Cell<_>>, Option<Cell<_>>, OnceCell<RefCell<_>>), tuples, slices of cells, and cells of slices.
  • Arc<RefCell<_>>, including its identity shortcut while mutably borrowed.
  • Ord::max/min/clamp, and OnceCell/OnceLock empty or set.
  • BTreeSet<Cell<u8>> and BTreeMap<RefCell<u8>, _>.
  • A check that compares released their borrows.
  • Picked by kind: a RefCell (either side, or only a nested inner one) compared while mutably borrowed, which panics with wasm32's exact message.

A mutation check (dropping refCellBy's second borrow) produced 44 differences.

Impact

  • Translator output: comparisons of Cell, RefCell, OnceCell, and OnceLock now translate where they were refused. Cells are now accepted as B-tree keys.
  • Runtime: new exports rt.cellBy and rt.refCellBy. Nothing is breaking.

Alternatives

  • Inline a.v === b.v for cells of primitives instead of rt.cellBy(rt.same). I skipped it because no consumer needs it yet, and the wrapper composes uniformly.

Follow-ups

  • Clone of Cell/RefCell and RefCell::into_inner are still refused (hit while writing the fixture). Recorded in quest/m2/ts-shared.md, which this PR also corrects: OnceCell's PartialEq is no longer refused.

Review

  • Reviewer: quest review, Codex gpt-6.1-sol, medium effort.
  • Round 1 on 0f06f665: one finding (own_ord did not look through cell keys). Fixed in 80b89d77, with a reject fixture case.
  • Round 2 on 27cf72d0 (the fix plus a conflict-resolved merge of origin/main): no findings.
  • just ci passed locally on 27cf72d0.

🤖 Generated with Claude Code

(written by Claude Opus 5.5)

kixelated and others added 4 commits October 30, 2026 19:36
Quest-Claim: 1638f4ac3335476f
…cells by their Option

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…own Ord

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	ts/fixtures/lang/expected/harness.ts
#	ts/fixtures/lang/src/harness.rs
@kixelated kixelated changed the title Equality of Cell and RefCell feat(ts): compare Cells and RefCells by what they hold, and one-time cells by their Option Oct 31, 2026
@kixelated
kixelated marked this pull request as ready for review October 31, 2026 03:30
@kixelated
kixelated merged commit f7125a0 into main Oct 31, 2026
1 of 2 checks passed
@kixelated
kixelated deleted the quest/m3/ts-eq-cells branch October 31, 2026 03:31
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