Repository navigation
feat(ts): translate collections of array keys, found by the slices they borrow as - #315
Conversation
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>
|
Warning Review limit reachedYou'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. View limit details
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. Comment |
kixelated
left a comment
There was a problem hiding this comment.
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)
Problem
A
HashMap,HashSet,BTreeMap, orBTreeSetof[T; N]keys (Tother thanu8) translated to TypeScripttscrejects:rt.btreeNew(rt.cmpSlice(rt.cmp)) as rt.BTreeMap<rt.u64[], rt.u8>fails with TS2352, sincebtreeNewandhashNewinferredKas the comparator'sArrayLike<T>rather than the assertedT[]. The lang fixture worked around it with aBig([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.btreeNewandrt.hashNewtake their comparator and hashing asNoInfer<K>, so the asserted collection type decidesK, which then types the nested comparator. Their output is unchanged.rt.cmpTuplegains an overload that types each field's function by the tuple's element, so a key such as(u8, [Option<u8>; 2]), whose field comparatorrt.cmpSlice(rt.cmpOption(rt.cmp))is generic, type-checks. The old signature remains as the fallback for calls with no asserted type.alikeit: arrays, slices, andVecs 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 aVec<T>key be found by&[T].same_keyis checked first, so a&Keykey is still found byKey, which feat(ts): compare std sequences of unlike items, and VecDeque against any #313's reference guard inalikewould otherwise refuse.Bigwrapper inhints.rsis gone; its generic collection paths now hold[u32; 275]directly.Tests:
keys::arraysin the lang fixture covers keys ofu16,u64,i32,char,bool,Option<u8>, and a struct, plusN = 0andN = 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, andsplit_off; the entry API;collect/from; set operations; tuple andOptionkeys holding arrays;Vec<T>keys found by&[T]; and&Tkeys found byT. The 50k-case differential against native wasm32 passes. As a mutation check, reversingcmpSlice's element order made it report 141 differences.Impact
hintschanged.rt.btreeNew,rt.hashNew, andrt.cmpTuplesignatures are widened for inference only, so the change is not breaking.ts/README.md: the key semantics paragraph covers arrays of any element.Alternatives
rt.btreeNew<K, V>(...)) instead of theasassertion. The printer has no type arguments on calls, and this would churn every collection constructor in the snapshots.cmpTuplesignature. Calls with no asserted type, such asrt.cmpTuple([rt.cmp, rt.cmpStr])(a, b), then inferunknownfor the genericrt.cmp, hence the overload.Follow-ups
iter.zip(0..)translates tort.iterZip(..., { start: 0 }), which tsc rejects. I found it while writing the fixture, and it is unrelated to keys.[u8; N]refusals stay with ts-byte-refs-taken; they have a different root cause.Review
Codex
gpt-6.1-sol, medium effort, viaquest review. Round 3 on head4460cb2e9d3addf7d2d8c2994b6e476742b1fab6reported no findings.1add123a): "reference guard removed fromalike". 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.c05b7799): "&Keykeys no longer found byKey". Fixed in4460cb2e:same_keyis checked beforealike, with a differential case for&Tkeys.just cipassed locally on4460cb2e.(written by Claude Opus 5.5)
🤖 Generated with Claude Code