Skip to content

feat(ts): translate collections of array keys, found by the slices they borrow as - #315

Merged
kixelated merged 8 commits into
mainfrom
quest/m3/ts-array-keys
Oct 31, 2026
Merged

kixelated merged 8 commits into
mainfrom
quest/m3/ts-array-keys

Conversation

@kixelated

@kixelated kixelated commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

A HashMap, HashSet, BTreeMap, or BTreeSet of [T; N] keys (T other than u8) translated to TypeScript tsc rejects: rt.btreeNew(rt.cmpSlice(rt.cmp)) as rt.BTreeMap<rt.u64[], rt.u8> fails with TS2352, since btreeNew and hashNew inferred K as the comparator's ArrayLike<T> rather than the asserted T[]. The lang fixture worked around it with a Big([u32; 275]) wrapper. Looking such a key up by the &[T] it borrows as (get, contains, remove, range, split_off) was refused outright.

Approach

  • rt.btreeNew and rt.hashNew take their comparator and hashing as NoInfer<K>, so the asserted collection type decides K, which then types the nested comparator. Their output is unchanged.
  • rt.cmpTuple gains an overload that types each field's function by the tuple's element, so a key such as (u8, [Option<u8>; 2]), whose field comparator rt.cmpSlice(rt.cmpOption(rt.cmp)) is generic, type-checks. The old signature remains as the fallback for calls with no asserted type.
  • A lookup's key is accepted when it is the same key as the collection's or alike it: arrays, slices, and Vecs of the same elements are all JS arrays, and std hashes, compares, and orders each as a slice. Bytes already worked this way. This also lets a Vec<T> key be found by &[T]. same_key is checked first, so a &Key key is still found by Key, which feat(ts): compare std sequences of unlike items, and VecDeque against any #313's reference guard in alike would otherwise refuse.
  • The Big wrapper in hints.rs is gone; its generic collection paths now hold [u32; 275] directly.

Tests: keys::arrays in the lang fixture covers keys of u16, u64, i32, char, bool, Option<u8>, and a struct, plus N = 0 and N = 275, in all four collections. It covers lookups by an equal but separately built array and by the borrowed slice; B-tree order, ranges by array and by slice, and split_off; the entry API; collect/from; set operations; tuple and Option keys holding arrays; Vec<T> keys found by &[T]; and &T keys found by T. The 50k-case differential against native wasm32 passes. As a mutation check, reversing cmpSlice's element order made it report 141 differences.

Impact

  • Translator output: the collections and lookups above now translate where they used to be refused or rejected by tsc. No snapshot outside the new cases and hints changed.
  • Runtime: rt.btreeNew, rt.hashNew, and rt.cmpTuple signatures are widened for inference only, so the change is not breaking.
  • ts/README.md: the key semantics paragraph covers arrays of any element.

Alternatives

  • Emitting explicit type arguments (rt.btreeNew<K, V>(...)) instead of the as assertion. The printer has no type arguments on calls, and this would churn every collection constructor in the snapshots.
  • A mapped-only cmpTuple signature. Calls with no asserted type, such as rt.cmpTuple([rt.cmp, rt.cmpStr])(a, b), then infer unknown for the generic rt.cmp, hence the overload.

Follow-ups

  • New quest ts-range-from-iter: iter.zip(0..) translates to rt.iterZip(..., { start: 0 }), which tsc rejects. I found it while writing the fixture, and it is unrelated to keys.
  • The [u8; N] refusals stay with ts-byte-refs-taken; they have a different root cause.

Review

Codex gpt-6.1-sol, medium effort, via quest review. Round 3 on head 4460cb2e9d3addf7d2d8c2994b6e476742b1fab6 reported no findings.

  • Round 1 (1add123a): "reference guard removed from alike". Refuted: that head predated merging feat(ts): compare std sequences of unlike items, and VecDeque against any #313, which added the guard, so the diff showed it reverted. Later heads keep it unchanged.
  • Round 2 (c05b7799): "&Key keys no longer found by Key". Fixed in 4460cb2e: same_key is checked before alike, with a differential case for &T keys.

just ci passed locally on 4460cb2e.

(written by Claude Opus 5.5)

🤖 Generated with Claude Code

kixelated and others added 8 commits October 30, 2026 16:03
Quest-Claim: 721692e74d14e1ba
…ey borrow as

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…d ranges as iterators

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated kixelated changed the title Array keys a collection holds feat(ts): translate collections of array keys, found by the slices they borrow as Oct 31, 2026
@kixelated
kixelated marked this pull request as ready for review October 31, 2026 00:28
@coderabbitai

coderabbitai Bot commented Oct 31, 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 30 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: 7f46985d-6596-4433-9353-c53a52e7cd5c
Commits

Reviewing files that changed from the base of the PR and between 40934cd and 4460cb2.

Files selected for processing: 16
  • quest/m3/README.md
  • quest/m3/ts-array-keys.md
  • quest/m3/ts-range-from-iter.md
  • ts/README.md
  • ts/fixtures/lang/expected/core/bool.ts
  • ts/fixtures/lang/expected/core/option.ts
  • ts/fixtures/lang/expected/harness.ts
  • ts/fixtures/lang/expected/hints.Big.ts
  • ts/fixtures/lang/expected/hints.ts
  • ts/fixtures/lang/expected/keys.ts
  • ts/fixtures/lang/expected/lib.ts
  • ts/fixtures/lang/src/harness.rs
  • ts/fixtures/lang/src/hints.rs
  • ts/fixtures/lang/src/keys.rs
  • ts/runtime/rt/collections.ts
  • ts/src/lower/collections.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 merged commit 4787e0a into main Oct 31, 2026
2 checks passed

@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: 4460cb2

No actionable bugs found. The direction is sound: contextual collection-key inference and reuse of existing sequence equivalence keep the change small without changing runtime algorithms. The same_key fast path preserves reference-key lookups, and the new differential fixture covers array/slice lookups, nested keys, ranges, entries and set operations.

Verification limits: static inspection of the implementation, relevant surrounding lowering code and fixtures only. I did not run the translator, TypeScript compiler, differential suite or tests, and have not independently verified the local test results reported in the PR.

(Written by OpenAI)

@kixelated
kixelated deleted the quest/m3/ts-array-keys branch October 31, 2026 00:29
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