Skip to content

feat(ts): translate next_if_eq against another type than the item - #297

Merged
kixelated merged 7 commits into
mainfrom
quest/m3/ts-next-if-eq-other
Oct 30, 2026
Merged

kixelated merged 7 commits into
mainfrom
quest/m3/ts-next-if-eq-other

Conversation

@kixelated

@kixelated kixelated commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Completes quest/m3/ts-next-if-eq-other.md.

Problem

Peekable::next_if_eq refused an expected value of another type than the item, such as next_if_eq("a") of String items, because it compared by eq_fn of the item's own type. Review also found that the collections mapping for std PartialEq<U> compared by the left type's eq_fn even when U differed. So a crate's impl PartialEq<Local> for String compared as two strings, and a Vec<Left> against a &[Right] compared as two Vec<Left>s, in == as well.

Approach

  • next_if_eq of another type compares by <Item as PartialEq<T>>::eq through fn_value, so the comparison lowers as a call to that impl does: std's mapping or the crate's own impl. A [u8; N] item is lent to it as the rt.ByteSlice that &[u8; N] is.
  • rt.peekableNextIfEq takes expected as its own type parameter.
  • The collections PartialEq mapping applies only to types that compare alike: the same key type, strings, bytes, or element containers of alike elements. Anything else goes to its impl's own call: the crate's impl lowers, and an unmapped std impl refuses at its span.
  • Lang differential iters::peeked_others against native wasm32 covers:
    • String/&String/&str/Cow<str> against str, &str, String, and Cow.
    • Vec<String> against Vec<&str>.
    • Vec<u16> and Vec<u8> against slices, arrays, and &[_].
    • [u8; 2] items against a slice.
    • &mut u8 items written through after a match.
    • Crate impls (Quarter: PartialEq<u8>, u8: PartialEq<Quarter>, [u8; 2]: PartialEq<Quarter>, String: PartialEq<Quarter>), including ==.
    • Edge cases: a match, a mismatch that keeps the item for the next next, an empty iterator, and call counts through a Cell.
  • Checked by mutation: making the runtime call eq twice fails the differential with 167 differences.
  • Reject fixture: peeked_str (now translated) is replaced by peeked_deque (VecDeque against Vec) and unlike_elements (Vec<Left> against &[Right]), both refused at their spans.

Option has no cross-type PartialEq in std, so no Option pair reaches here.

Impact

  • Translator output: next_if_eq against another type translates. == of a std type against a type that doesn't compare alike now runs the impl's own eq: the crate's, or a refusal at the span for an unmapped std impl. No Rust public API change.
  • Runtime: peekableNextIfEq<T, U> takes expected: U (widened, not breaking).

Alternatives

  • Lowering unlike elements element-wise, through each pair's PartialEq<U>, within this PR. Left out because it needs ne handling for elements whose crate overrides ne, and the same lend for byte-array elements on both sides. Recorded as a quest.
  • Using fn_value for the same-type case too. Dropped because it would wrap the runtime's same and eqSlice in an arrow for no gain.

Follow-ups

Review

  • Reviewer: quest review, Codex gpt-6.1-sol, medium effort.
  • Reviewed head: eceeb9654799932125d9c41cde0fc4ffc0888829.
  • Round 1 (head 59b7836b):
    • collections.rs:544 Reverse hashing: refuted. That diff came from main's feat(ts): translate accessors of elements whose None is rt.None #292 because the base was ahead of the branch, and merging origin/main in 0628bb0b removed it.
    • Heterogeneous element comparisons: fixed at the source with alike in ts/src/lower/collections.rs. Covered by the reject fixture unlike_elements and the lang String: PartialEq<Quarter> case.
  • Round 2 (head 0628bb0b): no findings.
  • Round 3 (head eceeb965, after merging origin/main cecbf0d7 with a hand-resolved conflict in the lang harness case list): no findings.
  • Local checks: just ci passes on eceeb965.

Merge of main at f7f0e722 (#296, #300) into eceeb965, as a35bd2ec: ordered main's singles::* cases before iters::peeked_others in harness.rs and caller.ts, and regenerated harness.ts and the reject census (this PR's refusal changes, shifted lines). Recorded skip, too trivial to review; just ci passed on a35bd2ec.

🤖 Generated with Claude Code

(written by Claude Opus 5.5)

kixelated and others added 4 commits October 30, 2026 10:52
`Peekable::next_if_eq` compares by the item's `PartialEq<T>`: of another type than the item,
by that impl's `eq` as a call to it lowers, std's or the crate's, with a `[u8; N]` item lent
as the `rt.ByteSlice` its reference is. A pair the call lowering refuses fails at the span.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Merges origin/main, regenerating the lang and reject snapshots.

The collections mapping compared a std `PartialEq<U>` by `eq_fn` of the left type alone,
so a `String` against a crate type, or a `Vec<Left>` against a `&[Right]`, compared as two
`String`s or two `Vec<Left>`s. It now maps only types that compare alike, and another goes
to its impl's own call: the crate's lowers, and std's unmapped one refuses at its span.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated kixelated changed the title Peekable::next_if_eq against another type feat(ts): translate next_if_eq against another type than the item Oct 30, 2026
@kixelated
kixelated marked this pull request as ready for review October 30, 2026 18:38
@coderabbitai

coderabbitai Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 24 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: dc693296-725c-4d2d-834a-87cad828e963
Commits

Reviewing files that changed from the base of the PR and between f7f0e72 and a35bd2e.

Files selected for processing: 18
  • quest/m3/README.md
  • quest/m3/ts-eq-unlike.md
  • quest/m3/ts-next-if-eq-other.md
  • ts/fixtures/lang/caller.ts
  • ts/fixtures/lang/expected/core/option.ts
  • ts/fixtures/lang/expected/harness.ts
  • ts/fixtures/lang/expected/iters.Quarter.ts
  • ts/fixtures/lang/expected/iters.ts
  • ts/fixtures/lang/src/harness.rs
  • ts/fixtures/lang/src/iters.rs
  • ts/fixtures/reject/census.md
  • ts/fixtures/reject/expected.txt
  • ts/fixtures/reject/src/lib.rs
  • ts/fixtures/reject/ts.toml
  • ts/runtime/rt/iter.ts
  • ts/src/census.rs
  • ts/src/lower/collections.rs
  • ts/src/lower/iter.rs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@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: 0628bb0

No actionable bugs found. Routing heterogeneous next_if_eq through the resolved PartialEq<T> implementation is a sound direction, and the alike guard avoids silently substituting same-type equality for unsupported element comparisons. Keeping the same-type fast path and refusing unsupported heterogeneous containers is a reasonable scope boundary; the byte-array lending adapter matches the generated fixtures.

The added fixtures cover mismatch retention, empty iterators, comparison side effects, custom implementations, and mutable references. Consider a nested-container heterogeneous comparison regression as the follow-up expands support.

Verification: static review of the lowering changes, runtime signature, fixtures and surrounding equality dispatch only. I did not run tests, builds, or the reported differential/mutation checks.

kixelated and others added 3 commits October 30, 2026 11:42
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	ts/fixtures/lang/caller.ts
#	ts/fixtures/lang/expected/harness.ts
#	ts/fixtures/lang/src/harness.rs
#	ts/fixtures/reject/census.md
… main

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated
kixelated merged commit 955ac19 into main Oct 30, 2026
1 check passed
@kixelated
kixelated deleted the quest/m3/ts-next-if-eq-other branch October 30, 2026 19:10
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