Skip to content

feat(ts): translate set operations of hash and B-tree sets - #302

Merged
kixelated merged 6 commits into
mainfrom
quest/m3/ts-set-ops
Oct 30, 2026
Merged

kixelated merged 6 commits into
mainfrom
quest/m3/ts-set-ops

Conversation

@kixelated

@kixelated kixelated commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

union, intersection, difference, and symmetric_difference of a HashSet and a BTreeSet were refused for every element type. So were the operator forms (&a | &b, &a & &b, &a - &b, &a ^ &b) and is_subset, is_superset, and is_disjoint. Reproduced on 955ac19f with the new collections::set_ops fixture: 120 refusals, each at its span. Plain elements got "unsupported call to std::collections::BTreeSet::<u8>::union", and so on. [u8; 2] elements got "std::collections::HashSet::union of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it".

Approach

  • Runtime (ts/runtime/rt/collections.ts): hashUnion, hashIntersection, hashDifference, hashSymmetricDifference, hashIsSubset, hashIsSuperset, hashIsDisjoint, the btree* equivalents, and btreeFromSorted. Each follows std's algorithm, which also decides which set's key of two equal ones is yielded:
    • B-tree: std's MergeIterInner merge. Where the sizes differ by ITER_PERFORMANCE_TIPPING_SIZE_DIFF (16) or more, it looks the smaller set's keys up in the larger instead. It takes std's shortcuts when the sets' ends meet.
    • Hash: union walks the larger set first, and intersection and is_disjoint walk the smaller. On a tie, self wins.
    • Every iterator is lazy, and gives std's size_hint (feat(ts): translate size_hint and reserve by it in collect and extend #294 made size_hint translate). Iterators that stitch keep a peeked key apart from "not yet peeked", so a cached None key isn't skipped.
  • Translator (ts/src/lower/collections.rs): maps the seven methods and the four operators. An operator builds a new set from the op's keys, each through the element's Clone, as std's does:
    • Hash: hashExtendKeys into a fresh set, checking hashbrown's capacity limit as collect does.
    • B-tree: btreeFromSorted, which keeps std's root even when empty, so range still checks its bounds.
  • lends_call accepts the other set of a set op or operator as an argument when its keys are [u8; N]. It accepts nothing else: set.extend(&other) is still refused, as feat(ts): lend the byte arrays a collection holds as slices #295 intended.
  • Differential test: collections::set_ops runs against native wasm32. It covers element types u8, [u8; 2], Option<u8>, Reverse<Option<u8>> (where None is the greatest key), and Nibble, whose equal keys differ in their high bits, so the output shows which set's key std picks.
    • Shapes: overlapping, identical, one empty, disjoint, one key against 30 (std's lookup path), and an empty operator-built set's range panic.
    • Each op both ways, only its first item (laziness), size_hint before and after one item (B-tree) or before any (hash), and the operator-built sets read with iter() and iter().rev().
    • Named cases in caller.ts reach the lookup path for every element type, and hash sets of equal and unequal size.
  • Mutation check: I broke the runtime 10 ways (wrong side searched, union's pick of equal keys, hash union's and intersection's tie-breaks, the built set's root, a peeked None taken as unpeeked, and the size hints). The differential failed on every one.

Choices made without asking:

  • std's set-op iterators aren't DoubleEndedIterator, so rev() is tested on the sets the operators build. The ops' own order is checked forward.
  • Hash forms match by element set, as the quest allows (hashbrown isn't ported). Laziness of the hash forms is checked by membership of the first item, which is the runtime's pick.
  • Clone and Debug of the set-op iterators stay refused with a span, as other std iterators' are.

Impact

  • Public API: the translator's output. New rt exports: the 14 set functions above and btreeFromSorted. Nothing breaks.

Alternatives

  • Translating std's MIR for the operators: the B-tree ones call std's private from_sorted_iter, and std's iterator structs hold std's iterators, which the runtime models itself.
  • Eager arrays in the runtime: simpler, but not lazy.

Follow-ups

  • An array's map: [bool; 3].map(u8::from) was refused while writing the fixture.

Review

Codex gpt-6.1-sol, medium effort, via quest review. Round 2 reviewed head c7d13638de063686d850d1ec57906aa371d9bab5 against origin/main 4b965ce1 and found nothing.

Round 1 (head 2c5f77b4) found two issues:

🤖 Generated with Claude Code

(written by Claude Opus 5.5)

kixelated and others added 6 commits October 30, 2026 12:11
Quest-Claim: 6d74af3846513f70
`union`, `intersection`, `difference`, and `symmetric_difference` of a `HashSet` and a
`BTreeSet`, their operators `|`, `&`, `-`, and `^`, and `is_subset`, `is_superset`, and
`is_disjoint` map to runtime functions that follow std's algorithms: a B-tree's merge or
lookup by relative size, which picks which set's key of two equal ones is yielded, and a
hash set's walk of the larger or smaller set. Each is lazy, and lends `[u8; N]` keys as
slices. A set an operator builds has its root, so even empty it checks a range's bounds.

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/runtime/rt/collections.ts
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	ts/fixtures/lang/expected/harness.ts
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated kixelated changed the title Set operations feat(ts): translate set operations of hash and B-tree sets Oct 30, 2026
@kixelated
kixelated marked this pull request as ready for review October 30, 2026 21:11
@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 54 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: 3333f3f9-722f-49b2-b3cd-3b47f234ede6
Commits

Reviewing files that changed from the base of the PR and between 4b965ce and c7d1363.

Files selected for processing: 11
  • quest/m3/README.md
  • quest/m3/ts-array-map.md
  • quest/m3/ts-set-ops.md
  • ts/fixtures/lang/caller.ts
  • ts/fixtures/lang/expected/collections.Nibble.ts
  • ts/fixtures/lang/expected/collections.ts
  • ts/fixtures/lang/expected/harness.ts
  • ts/fixtures/lang/src/collections.rs
  • ts/fixtures/lang/src/harness.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 5cbb480 into main Oct 30, 2026
2 checks passed
@kixelated
kixelated deleted the quest/m3/ts-set-ops branch October 30, 2026 21:12
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