Skip to content

feat(ts): compare and hash byte arrays, slices, and vectors as std does - #276

Merged
kixelated merged 7 commits into
mainfrom
quest/m3/ts-byte-keys
Oct 30, 2026
Merged

kixelated merged 7 commits into
mainfrom
quest/m3/ts-byte-keys

Conversation

@kixelated

@kixelated kixelated commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Closes the quest quest/m3/ts-byte-keys.md.

Problem

rust2-ts refused == and != of byte arrays, Vec<u8> == Vec<u8>, == across byte forms (Vec<u8> == &[u8], [u8; N] == [u8], ...), and any [u8; N], &[u8], or Vec<u8> as a hash key, alone or in a tuple, Option, or Arc (44 refusals with spans in the new fixture cases, such as "hashing [u8; 2] as a key", "unsupported call to <[u8; 2] as std::cmp::PartialEq>::eq", "HashMap::get of HashMap<Vec<u8>, usize> by [u8]", and "HashMap::get of a reference to an array of bytes it is generic over").

Approach

  • rt.eqBytes and rt.hashBytes read each byte form at run time (typed array, rt.ByteSlice, rt.ByteVec), as rt.cmpBytes does for order (feat(ts): translate slice windows and chunks, Vec::drain, and the order of bytes #259). eq_fn and hash_fn use them for any byte type, so they compose with eqTuple, hashOption, hashArc, and the rest.
  • PartialEq::eq/ne between any two byte forms maps to rt.eqBytes. [u8] == [u8] keeps the library's rt.byteSliceEq.
  • Hashing: the pinned std's impl Hash for [T; N] calls Hash::hash(&self[..], state) (core/src/array/mod.rs:348), and [T]'s calls write_length_prefix and then u8::hash_slice, which writes the bytes (core/src/hash/mod.rs:931, :817). So all three forms write the length as a usize and then the bytes. The brief's first claim, that [u8; N] has no length prefix, was wrong and was later corrected. DefaultHasher::new digests of each form, compared with native wasm32, prove it. Dropping the prefix makes the differential report 414 differences.
  • Lookups: same_key treats any two byte forms as one key, so a Vec<u8> key is found by a &[u8] and a [u8; 2] key by a &[u8]. A lookup takes its key by reference, which for a [u8; N] key held as a typed array (through Boxes and Arcs) is an rt.ByteSlice, so rt.byteSliceView passes a view of its bytes. bytes_across lets PartialEq, Hash::hash, and collection lookups (get, get_mut, contains_key, contains, remove, take) take a &[u8; N], since they only read it. What a lookup returns is still checked, so HashSet<[u8; 2]>::get stays refused. A new reject case covers that.
  • Tests: hashes::bytes (digests of each form, empty arrays and slices, inside a tuple, Option, and Arc, and RandomState::hash_one equal across forms) and keys::bytes (==/!= across forms, hash maps keyed by [u8; 2], Vec<u8>, &[u8], (Option<[u8; 2]>, Vec<u8>), Arc<Vec<u8>>, Arc<[u8; 2]>, Box<[u8; 2]>, and HashSet<&[u8; 2]>, B-tree lookups by array and by slice, and lookups by a borrowed &[u8]), run differentially against native wasm32.

Impact

  • Translator output only: the constructs above now translate. New runtime exports are rt.eqBytes and rt.hashBytes. No breaking change.

Alternatives

  • Copying a lookup key into the stored form in place of a byteSliceView: this allocates, and the view already reads as a Uint8Array.
  • Hashing four bytes per Hasher.word call: this would probably be faster, but I didn't measure it, so I kept one byte per call. SipHash consumes a byte stream, so either gives the same values.

Choices made without asking

  • RandomState's keys are random on both sides, so hash_one values can't be printed. The fixture compares DefaultHasher::new digests with native directly, and checks that hash_one agrees across byte forms. BuildHasherDefault would allow a direct hash_one comparison, but it doesn't translate yet.
  • The reject fixture's byte_keys (HashMap<Vec<u8>, u8>) now translates, so it became set_keys over a BTreeSet<u8> key. That keeps the "hashing/comparing as a key" refusal covered.

Follow-ups

Review

Codex gpt-6.1-sol, medium effort, via quest review. The reviewed head is c8fe53bda1a867d6c71a86be5c89f9e7ebd6776b.

  • Round 1 (12f06002) had 2 findings, both fixed in 8ad812d4:
    • A lookup by &Box<[u8; 2]> was wrapped in byteSliceView, which throws on a Uint8Array. Now only a lookup whose Q is itself a byte array or slice gets the view. A differential case covers boxed.get(&Box::new(key)).
    • Widening same_key let the Borrow mapping return a ByteVec for Vec<u8>::borrow() -> &[u8]. same_key is back to main's definition, and the byte equivalence lives only in the lookup check. That borrow refuses with a span, as on main. Translating it is a separate std gap.
  • Round 2 (8ad812d4) had no findings.
  • Round 3 (c8fe53bd, a merge of origin/main with hand-resolved conflicts in collections.rs, the harness case list, and the reject roots) had no findings.

just ci passed locally on c8fe53bd.

Merge of main at c2f8edf9 (#275) into c8fe53bd, as 54766971: took main's reject census and regenerated it (this PR's BTreeSet key and HashSet::get refusal on top of #275's). Recorded skip, too trivial to review; just ci passed on 54766971.

(written by Claude Opus 5.5)

🤖 Generated with Claude Code

kixelated and others added 5 commits October 30, 2026 04:11
Quest-Claim: a6b626a4299b91c4
`==` and `!=` of bytes in any two forms, and a `[u8; N]`, `&[u8]`, or `Vec<u8>` as a
hash or B-tree key, alone or in a tuple, `Option`, or `Arc`, translate through
`rt.eqBytes` and `rt.hashBytes`, which read each form at run time as `rt.cmpBytes`
does. Every form hashes its length as a `usize` and then its bytes, as std's
`[T; N]` hashes as its slice, so a `Vec<u8>` key is found by a `&[u8]`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… the array

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	ts/fixtures/lang/expected/harness.ts
#	ts/fixtures/lang/src/harness.rs
#	ts/fixtures/lang/src/lib.rs
#	ts/fixtures/reject/census.md
#	ts/fixtures/reject/expected.txt
#	ts/fixtures/reject/ts.toml
#	ts/src/lower/collections.rs
@kixelated kixelated changed the title Byte arrays compared for equality and hashed feat(ts): compare and hash byte arrays, slices, and vectors as std does Oct 30, 2026
@kixelated
kixelated marked this pull request as ready for review October 30, 2026 12:03
@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 51 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: 01b8d10d-f095-4456-9d5a-19b46c3171e9
Commits

Reviewing files that changed from the base of the PR and between 87fc720 and c8fe53b.

Files selected for processing: 22
  • quest/m3/README.md
  • quest/m3/ts-array-as-slice.md
  • quest/m3/ts-byte-key-refs.md
  • quest/m3/ts-byte-keys.md
  • ts/README.md
  • ts/fixtures/lang/expected/core/bool.ts
  • ts/fixtures/lang/expected/harness.ts
  • ts/fixtures/lang/expected/hashes.ts
  • ts/fixtures/lang/expected/keys.ts
  • ts/fixtures/lang/expected/lib.ts
  • ts/fixtures/lang/src/harness.rs
  • ts/fixtures/lang/src/hashes.rs
  • ts/fixtures/lang/src/keys.rs
  • ts/fixtures/lang/src/lib.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/collections.ts
  • ts/runtime/rt/hash.ts
  • ts/src/lower/collections.rs
  • ts/src/lower/mir/call.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: c8fe53b.

No actionable bugs found. Centralizing byte equality/hash alongside cmpBytes is a sound direction: logical length and ByteSlice offsets are respected, and the length-prefixed byte stream is consistent across arrays, slices and vectors. The lookup adaptation preserves boxed queries and Arc wrapping, while byte-form equivalence remains limited to collection lookups rather than weakening Borrow lowering.

The fixtures cover cross-form comparisons/lookups, composite keys, offset slices and deterministic hash digests; unsupported returned array references remain explicitly separated into follow-ups. Reusing byte views avoids unnecessary lookup-key copies.

Verification limits: source/generated-fixture inspection and an independent static review only. I did not run the translator, differential tests or CI; reported test results were not independently verified.

Luke Curley and others added 2 commits October 30, 2026 05:06
# Conflicts:
#	ts/fixtures/reject/census.md
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated
kixelated merged commit 068673b into main Oct 30, 2026
@kixelated
kixelated deleted the quest/m3/ts-byte-keys branch October 30, 2026 12:23
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