Repository navigation
feat(ts): lend the byte arrays a collection holds as slices - #295
Merged
Merged
Conversation
Quest-Claim: 8fb63f20dd30ede5
What a map, set, deque, or heap of `[u8; N]` keys or values lends out, by `keys`, `iter`, `get`, `get_key_value`, `range`, an entry's `key`, `Index`, `for` over a reference, or `retain`'s and `or_insert_with_key`'s closures, is an `rt.ByteSlice` of the bytes it holds, as a `&[u8; N]` is elsewhere. `HashMap::get_key_value` and `BTreeSet::get` translate, and `map[&key]` finds a byte-array key. Extending one by references to arrays, or holding a `&[u8; N]` beside a `[u8; N]`, is refused with a span. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…refs # Conflicts: # ts/fixtures/lang/expected/harness.ts # ts/fixtures/lang/expected/keys.ts # ts/fixtures/lang/src/harness.rs # ts/fixtures/reject/census.md # ts/fixtures/reject/expected.txt # ts/src/lower/collections.rs
kixelated
marked this pull request as ready for review
October 30, 2026 18:30
This was referenced Oct 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A collection of
[u8; N]keys holds each key as aUint8Array, where translated code reads a&[u8; N]as anrt.ByteSlice. So every call that lends a held key (keys,iter,get_key_value,Entry::key,HashSet::get, B-treefirst/range/get,retain's closure) was refused with a span (#276). Auditing them turned up silent mistranslations in the same class:for (k, v) in &map,for k in &set,(&map).into_iter(), andmap[&k]with a[u8; N]value handed out the heldUint8Arrayunconverted, becausebytes_acrossnever looked through the&of&HashMap<..>and waved everyIndexthrough.Approach
collections::lendconverts what a collection's mapping returns, by the call's output type: each&[u8; N]becomesrt.byteSliceFrom(held), through tuples ((&K, &V)),Options (newrt.optionMap), and iterator items (rt.iterMap). Closures std hands a held key or value (retain,or_insert_with_key,and_modify) are wrapped the same way.into_iterof&coll/&mut colllends its items; the heap's special case folds into this.HashMap,HashSet,BTreeMap,BTreeSet, entries,VecDeque, andBinaryHeap, so keys and values alike: a&shares the held bytes (nothing writes through it, and*kcopies), and a&mut(get_mut,front_mut,values_mut) writes where the collection holds them.collections::lends_callreplaces the lookup special case inbytes_across: a collection call is let through only if its output and closure parameters are lendable and no other input takes references to bytes. Everything else stays loud with a span.HashMap::get_key_value(rt.hashGetKeyValue) andBTreeSet::get(rt.btreeGetKey). Neither was mapped for any key type.lookup_tyforIndex<&Q>. It returned&Q, notQ, somap[&k]passed a byte-array key's slice unconverted.Vec<u8>and&[u8]keys didn't share the gap: they are held asrt.ByteVec/rt.ByteSlicealready. They are covered by the tests anyway.Tests:
keys::lent_keysandkeys::lent_valuesin thelangdifferential harness against native wasm32 cover:HashSet::getandBTreeSet::getby a borrowed&[u8], plusHashSet<Vec<u8>>::getinsertafter insertion&mutvalues, deque items, and heappeek/iter/retainA mutation check confirms the tests catch a broken lend. With
optionMaphanding out copies, thelangharness reported 162 differences from native.Impact
for-over-&collandmap[&k]cases now emit correct slices.extendof a[u8; N]collection by references to arrays (set.extend(&pairs)), which would store slices sharing the source's bytes.optionMap,hashGetKeyValue,btreeGetKey. No breaking change, noMIGRATE.mdentry.Decisions made without asking
VecDeque, andBinaryHeapas well as keys. It is the same mechanism and the same bug class ("fix every instance"); restricting it to keys would have meant extra code just to keep refusing values.&[u8; N]beside a[u8; N](e.g.HashMap<&[u8; 2], [u8; 2]>) is refused outright. Its held and lent&[u8; N]are the same type, and these calls were already refused before.Alternatives
[u8; N]keys asrt.ByteSlicein the collection. That would avoid conversion on lend but convert on every insert and owned yield, and change the collections' TS types. I rejected it as more invasive.lentKeys,lentEntry, ...). I rejected these as combinatorial; the type-driven conversion covers every shape with one function.Follow-ups
union/intersection/difference/symmetric_differenceare unsupported for every element type.extendby references, adaptors' closures over held arrays (filter_owned_bytes,map(|(k, v)| ..).collect()over a map), and a collection of both forms.Review
quest review), gpt-6.1-sol, medium effort.3fd141fb096722f349c4265cdaf124b7a3ce5aed: no findings.84262b7cebfb4cb7728114aadbc1149b81e8c9e3(merge oforigin/mainwith hand-resolved conflicts incollections.rsand the harness case list): no findings.84262b7cebfb4cb7728114aadbc1149b81e8c9e3.just cipassed locally on it.(written by Claude Opus 5.5)
🤖 Generated with Claude Code