Skip to content

feat(ts): translate comparisons through references and &mut slice index writes - #98

Merged
kixelated merged 3 commits into
mainfrom
quest/m2/ts-ref-shapes
Oct 27, 2026
Merged

kixelated merged 3 commits into
mainfrom
quest/m2/ts-ref-shapes

Conversation

@kixelated

@kixelated kixelated commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

Two shapes #83's tests hit were refused with a span:

  • <&T as PartialEq>::eq, which a derived == on a type holding references reaches, was an unsupported call. std's impls for references were mapped only for std key types, not the crate's own.
  • live[0] = x on a &mut [u8] was refused as a raw pointer. MIR's bounds check of a &mut slice reads its length through &raw const (fake) *live, a fake borrow that only PtrMetadata reads.

Approach

  • library::referent: a method that std's PartialEq, PartialOrd, Ord, or Hash impl for references defines calls the referent's with the operands dereferenced. call_def calls the referent's impl with the same operands, since a reference is its referent in JS. This is recursive (&&T), covers &mut and mixed &/&mut operands, and leaves methods the impl inherits from its trait (Ord::max) alone.
  • The fake raw pointer reads the slice it points at. A bounds check through it becomes rt.idx(live, i), the same as a &[u8] read, and the pointer is dropped as unread. Any other raw pointer is still refused.
  • A derived == on an enum whose variants all hold data reads the other operand's fields with no switch on it, because MIR removed the unreachable arms. tsc then rejected other._0. A downcast of a path no emitted switch tests is now cast to its variant: (other as { tag: "Hop"; _0: Hop.Hop | undefined })._0. The cast is type-only.

Differential tests against native Rust, in the lang fixture:

  • keys::borrows: an enum deriving PartialEq, Eq, PartialOrd, Ord, and Hash, holding &Option<Hop>, &&Horizon, &str, &Name, and &mut Hop. Every pair is compared, ordered, and hashed. Also &a == &b, &&g == &&h, &mut x == &mut y, the mixed &/&mut forms, and Hash of &mut. A struct deriving only PartialOrd over &Skew reaches Skew's own partial_cmp, which disagrees with its cmp.
  • shapes::write_at and moved_at: index writes and compound assignments on &mut [u8] and &mut [Point], an index past the end that panics, and a reassigned reborrow whose bounds checks go the general way rather than through rt.idx.

A mutation check that reversed the forwarded operands gave 951 differences from native Rust.

moq-net census

before after
unsupported call to <&T as PartialEq>::eq or ::ne 14 0
raw pointers are not supported 6 2
refusals 11879 11872
function instances 4892 4895
  • Of the 14, 7 now translate. The other 7 now report the referent's own refusal: <path::Path as PartialEq>::ne (4, the trait's default ne on a crate type), <moq_clock::Instant as PartialEq>::eq (2, another crate's type), and <Cow<str> as PartialEq<str>>::eq (1).
  • Of the raw pointer refusals, the 4 &mut slice index sites translate. The 2 left are in num_enum's derive in ietf/parameters.rs.
  • Code that is now reached surfaces 4 new refusals: kio::Producer (2), kio::Pending (1), and moq_clock::Instant (1).

Impact

  • Translator output: comparisons and hashes through references, and index writes through &mut slices, now translate. A variant read with no switch on it gets a type-only cast.
  • No runtime or config changes.

Choices made without asking (for review)

  1. PartialOrd, Ord, and Hash through references are included with PartialEq. They are the same shape, std's impls for references, and the same code handles them. Recommendation: keep.
  2. The variant cast is in this PR. Without it, the quest's own shape, a derived == on an enum holding references, fails tsc whenever every variant holds data. The check does not follow control flow: it casts unless a switch emitted earlier in the function tested that path. An extra cast costs nothing. A missing one fails tsc loudly. Recommendation: keep.
  3. The trait's default ne, lt, le, gt, and ge on a crate type stay refused. a != b and a < b on a derived type are separate gaps. They can land on their own, and moq-net's census has about 60 <T as PartialEq>::ne refusals. Recommendation: plan a follow-up quest.

Alternatives

  • Map each reference impl in collections.rs as a key comparison. That covers only std key types and gets no recursion for free.
  • Cast every variant read of a crate enum. That is simpler, but it adds a cast to every match arm in every snapshot.
  • Read MIR from before the unreachable arms are removed. That changes the MIR the whole translator reads.

Follow-ups

  • Translate the default ne, lt, le, gt, and ge of PartialEq and PartialOrd on the crate's types as !eq and partial_cmp tests (about 60 moq-net refusals).
  • <[u8] as PartialEq>::eq (slice equality) is unsupported. It came up while writing the tests.

Checks: just check, just ts test (all fixtures, differential tests included), just ts moq, just ts census, and quest check --base origin/main. Not run: just ffi test, just driver test, and just uniffi test, which this change does not touch. #82 (rt.ByteSlice) is still open. Its byte slices have a length too, so rt.idx and the fallback work unchanged after a merge.

(written by Opus 5.5)

🤖 Generated with Claude Code

https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn

kixelated and others added 2 commits October 26, 2026 18:14
Quest-Claim: 11a6aeffc6455635
…ex writes

std's PartialEq, PartialOrd, Ord, and Hash impls for references call their
referents', so a derived impl on a type holding references now reaches the
referent's. A bounds check of a `&mut` slice reads its length through a fake
raw pointer, which now reads the slice's, so `live[0] = x` translates. A
variant MIR reads without a switch, as a derived `==` reads the other operand
of an enum whose variants all hold data, is cast to it for tsc.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
@kixelated kixelated changed the title Translate derived == through references and slice index writes feat(ts): translate comparisons through references and mut slice index writes Oct 27, 2026
@kixelated kixelated changed the title feat(ts): translate comparisons through references and mut slice index writes feat(ts): translate comparisons through references and &mut slice index writes Oct 27, 2026
@kixelated
kixelated marked this pull request as ready for review October 27, 2026 01:48
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-27T02:10:17.168703Z — New commits
🔒 Security Review 🔄 Running since 2026-10-27T02:05:51.940571Z 6601e55 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@kixelated kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review by review (OpenAI)
Reviewed commit: 45f606e

No actionable bugs found in the changed code. Recursive forwarding through the resolved std reference impl preserves the referent's custom comparison/hash behavior without conflating PartialOrd with Ord; restricting raw-pointer support to FakeForPtrMetadata keeps the slice-write change narrow. The mixed-reference, disagreeing PartialOrd, reassigned-reborrow, and bounds-panic fixtures target the important distinctions.

Direction: these are coherent extensions of the existing lowering, with no new runtime abstraction. Always casting enum downcasts would be simpler than the function-wide switched-path heuristic, but I found no concrete regression requiring that change here; the heuristic is not control-flow proof. Documented default-method and slice-equality refusals remain separate gaps, not newly fixed behavior.

Verification: source and generated-snapshot review only; I did not run the translator, native differential tests, or the test suite.

(Written by OpenAI)

@kixelated
kixelated merged commit f4cbe53 into main Oct 27, 2026
1 check passed
kixelated added a commit that referenced this pull request Oct 27, 2026
Regenerate the lang snapshots for #98's cases over ByteSlice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
kixelated added a commit that referenced this pull request Oct 27, 2026
* chore(quest): default comparison methods and generic instances

Follow-ups from #98 and #94.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn

* chore(quest): Cow<str> and str slicing from #96

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn

* chore(quest): share Copy types the crate never changes in place

Replaces ts-shared-read's narrow fix, as #99 suggested.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn

* chore(quest): an A/A control that measures noise

From #87's and #97's bench findings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn

---------

Co-authored-by: Luke Curley <432854+kixelated@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated
kixelated deleted the quest/m2/ts-ref-shapes branch October 27, 2026 20:32
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