Skip to content

feat(ts): call a crate's own collect, eq, partial_cmp, by_ref, and is_sorted_by where std does - #513

Merged
kixelated merged 5 commits into
mainfrom
quest/m3/ts-iter-own-calls
Nov 3, 2026
Merged

kixelated merged 5 commits into
mainfrom
quest/m3/ts-iter-own-calls

Conversation

@kixelated

@kixelated kixelated commented Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

rust2-ts refused a crate's own iterator that overrides collect, eq, partial_cmp, by_ref, or is_sorted_by, as "overriding _, which std calls in place of another method". These are stable, provided methods a crate can override, and std calls each in place of another:

Finishes quest/m3/ts-iter-own-calls.md. Consumer: the ends fixture now, and any crate whose iterator overrides these.

Approach

  • eq and partial_cmp: Iterator's ne, lt, le, gt, and ge join library::Inherited, the path PartialEq::ne and PartialOrd::lt already take. Each calls the trait's eq or partial_cmp with the same generic arguments, which resolves to the crate's override where there is one, and else to std's mapping. rt.iterLt, iterLe, iterGt, and iterGe are deleted, and compare no longer handles ne.
  • collect: own_collect calls the crate's collect::<Vec<T>> instance where std's FromIterator of one of those six collections does, then collects from that Vec's into_iter. That is identical, since a Vec's own collect from its into_iter is in place. It applies only to the crate's type itself: &mut I and Box<I> collect by the default, which never calls the crate's.
  • by_ref: the refusal is lifted. std calls it only from Vec::splice, which has no translation, so only the crate's own calls reach it, and those resolve to its override.
  • is_sorted_by: joins rt.Own as all did in feat(ts): call a crate's own find, rfind, find_map, for_each, reduce, and all where std does #501. It is translated once for a Box<dyn FnMut(&T, &T) -> bool>, checked by erases, and rt.iterIsSortedBy calls it by value where std's is_sorted does. One with [u8; N] items stays refused, as find and rfind do.

Tests: the ends fixture's Overrides gains logged collect, eq, partial_cmp, by_ref, and is_sorted_by, each reading from the back so a missed call changes the result. A generic Pairs adaptor gains a logged collect, so BTreeMap is covered. A new collected runs every std caller by value, through &mut, and boxed (where neither forwards), and on the plain Run as a control. That is 4 new kinds, for 157,865 cases against native, with no differences. Mutations that skip own_collect or the isSortedBy dispatch produce 2434 and 128 differences.

Impact

  • Public API: the translator's output only. The runtime drops rt.iterLt, iterLe, iterGt, and iterGe, which only generated code called. Generated code for an iterator's lt and the others now tests rt.iterCmp inline, as a struct's lt already does.
  • Refusals: overriding an adaptor constructor such as map is still refused; the reject fixture's Collects case becomes Maps. The [u8; N] refusal now says "closure" rather than "predicate", since it covers is_sorted_by too.

Alternatives

  • A MIR walk for collect's callers instead of the list of six: it would have to follow Rc and Arc's ToRcSlice specialization two levels deep. The list is short, cited from std, and complete for stable std.
  • Keeping rt.iterLt and the others, and special-casing the crate's override: that adds a branch where Inherited already says what std does.

Follow-ups

None. With this, the only overrides std calls in place of another that stay refused are adaptor constructors, which can't return without diverging, and nightly-only ones such as try_fold.

Review

quest review, Codex gpt-6.1-sol at medium effort, round 3 on head a8c42b9 against base 3025d04: 0 findings.

Round 2 on aa7247f had 1 finding:

  • ts/src/lower/iter.rs (Rc and Arc slices from a TrustedLen iterator): fixed in a8c42b9. own_collect skips them, as std's ToRcSlice/ToArcSlice specialization reads the iterator itself. No differential case: TrustedLen is #[unstable(feature = "trusted_len")], so no stock crate's own iterator implements it.

just ci passed on a8c42b9.

Choices made without asking

  • is_sorted_by is in scope, though the quest named only collect, eq, partial_cmp, and by_ref. It is the same problem, the last stable instance of it, and AGENTS.md asks for every instance in one PR.

🤖 Generated with Claude Code

kixelated and others added 4 commits November 3, 2026 01:57
…_sorted_by where std does

std's FromIterator of Box<[T]>, Rc<[T]>, Arc<[T]>, BTreeMap, BTreeSet, and
BinaryHeap now calls the crate's own collect into a Vec first. Iterator's
ne, lt, le, gt, and ge join library::Inherited, so they call eq and
partial_cmp as resolved: the crate's override, or std's mapping, which
drops rt.iterLt, iterLe, iterGt, and iterGe. by_ref is lifted, as only
Vec::splice calls it in std. is_sorted_by joins rt.Own like all, so
is_sorted calls it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated kixelated changed the title A crate's own collect, eq, partial_cmp, and by_ref feat(ts): call a crate's own collect, eq, partial_cmp, by_ref, and is_sorted_by where std does Nov 3, 2026
…tself

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated
kixelated marked this pull request as ready for review November 3, 2026 10:27
@kixelated
kixelated added this pull request to the merge queue Nov 3, 2026
@coderabbitai

coderabbitai Bot commented Nov 3, 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 1 minute.

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: 7926be25-96e5-4365-9f29-a95560fb8bcf
Commits

Reviewing files that changed from the base of the PR and between 3025d04 and a8c42b9.

Files selected for processing: 21
  • quest/m3/README.md
  • quest/m3/ts-iter-own-calls.md
  • ts/README.md
  • ts/fixtures/lang/collections/expected/collections.ts
  • ts/fixtures/lang/ends/caller.ts
  • ts/fixtures/lang/ends/expected/core/option.ts
  • ts/fixtures/lang/ends/expected/ends.Overrides.ts
  • ts/fixtures/lang/ends/expected/ends.Pairs.ts
  • ts/fixtures/lang/ends/expected/ends.ts
  • ts/fixtures/lang/ends/src/ends.rs
  • ts/fixtures/lang/iters/expected/iters.ts
  • ts/fixtures/lang/refs/expected/refs.ts
  • ts/fixtures/reject/census.txt
  • ts/fixtures/reject/expected.txt
  • ts/fixtures/reject/src/iters.rs
  • ts/fixtures/reject/ts.toml
  • ts/fixtures/task/expected/drops/iters.ts
  • ts/runtime/rt/iter.ts
  • ts/src/lower/body.rs
  • ts/src/lower/iter.rs
  • ts/src/lower/library.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

CodeRabbit Verify

  • Run CodeRabbit Verify to test the changes in this pull request.

Merged via the queue into main with commit ce7a24a Nov 3, 2026
5 checks passed
@kixelated
kixelated deleted the quest/m3/ts-iter-own-calls branch November 3, 2026 10:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant