From 159b603a938b219efb6c27531f886528f63393a1 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 30 Oct 2026 04:11:20 -0700 Subject: [PATCH 1/5] chore(quest): claim quest/m3/ts-byte-keys.md Quest-Claim: a6b626a4299b91c4 From ff9c5158d9abaa56c4a68d014c14472c4110bfc4 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 30 Oct 2026 04:28:09 -0700 Subject: [PATCH 2/5] feat(ts): compare and hash byte arrays, slices, and vectors as std does `==` and `!=` of bytes in any two forms, and a `[u8; N]`, `&[u8]`, or `Vec` 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` key is found by a `&[u8]`. Co-Authored-By: Claude Opus 5.5 --- quest/m3/README.md | 3 +- quest/m3/ts-array-as-slice.md | 14 ++ quest/m3/ts-byte-key-refs.md | 17 +++ quest/m3/ts-byte-keys.md | 18 --- ts/README.md | 4 +- ts/fixtures/lang/expected/core/bool.ts | 7 + ts/fixtures/lang/expected/harness.ts | 10 +- ts/fixtures/lang/expected/hashes.ts | 148 +++++++++++++++++++++ ts/fixtures/lang/expected/keys.ts | 177 ++++++++++++++++++++++++- ts/fixtures/lang/expected/lib.ts | 4 + ts/fixtures/lang/src/harness.rs | 2 + ts/fixtures/lang/src/hashes.rs | 32 +++++ ts/fixtures/lang/src/keys.rs | 77 ++++++++++- ts/fixtures/lang/src/lib.rs | 4 +- ts/fixtures/reject/census.md | 38 +++--- ts/fixtures/reject/expected.txt | 5 +- ts/fixtures/reject/src/lib.rs | 9 +- ts/fixtures/reject/ts.toml | 3 +- ts/runtime/rt/collections.ts | 14 ++ ts/runtime/rt/hash.ts | 13 +- ts/src/lower/collections.rs | 32 ++++- ts/src/lower/mir/call.rs | 16 ++- 22 files changed, 591 insertions(+), 56 deletions(-) create mode 100644 quest/m3/ts-array-as-slice.md create mode 100644 quest/m3/ts-byte-key-refs.md delete mode 100644 quest/m3/ts-byte-keys.md diff --git a/quest/m3/README.md b/quest/m3/README.md index 0d7ddcdb0..5e9767ba8 100644 --- a/quest/m3/README.md +++ b/quest/m3/README.md @@ -28,7 +28,8 @@ generic std gap is planned here even when moq never calls it. - [Binding gaps](/quest/m3/ts-bind-gaps.md) - bound types as keys, unit returns checked, renamed dependencies - [A B-tree's drop order](/quest/m3/ts-btree-drop.md) - dropping a whole `BTreeMap` or `BTreeSet` drops its entries node by node, as std's does - [Drop order in std's folds and iterators](/quest/m3/ts-drop-order.md) - the values that drop still refused in std's folds, zips, flattens, and collects drop as std's do -- [Byte arrays compared for equality and hashed](/quest/m3/ts-byte-keys.md) - `==` of byte arrays, and byte arrays, slices, and vectors as hash keys, translate +- [References to byte-array keys a collection yields](/quest/m3/ts-byte-key-refs.md) - `keys`, `iter`, `get_key_value`, and `HashSet::get` of a `[u8; N]` key translate +- [An array as a slice](/quest/m3/ts-array-as-slice.md) - `<[T; N]>::as_slice` and `as_mut_slice` translate - [A slice's edits in place](/quest/m3/ts-slice-edits.md) - `fill`, `rotate_left`, and `copy_from_slice` of other values translate - [Mutable subslices of other values](/quest/m3/ts-mut-subslices.md) - `&mut v[a..b]` and `split_at_mut` of other than bytes translate, writing through - [Iterator consumers over items whose None is rt.None](/quest/m3/ts-option-items.md) - `nth`, `last`, and `find` of an iterator of `Option` keep `None` apart from `Some(None)` diff --git a/quest/m3/ts-array-as-slice.md b/quest/m3/ts-array-as-slice.md new file mode 100644 index 000000000..3c815701e --- /dev/null +++ b/quest/m3/ts-array-as-slice.md @@ -0,0 +1,14 @@ +# [XS] An array as a slice + +## Goal + +rust2-ts translates `<[T; N]>::as_slice` and `as_mut_slice`, for bytes and other values. +Proven by differential tests against native Rust. + +## Plan + +`as_slice` refuses with a span today, as "unsupported call to `std::array::::as_slice`". +A `&[u8; N]` is already an `rt.ByteSlice` and a reference to another array its JS array, +so each is the reference it is given. + +Public API: the translator's output. diff --git a/quest/m3/ts-byte-key-refs.md b/quest/m3/ts-byte-key-refs.md new file mode 100644 index 000000000..09e86562d --- /dev/null +++ b/quest/m3/ts-byte-key-refs.md @@ -0,0 +1,17 @@ +# [S] References to byte-array keys a collection yields + +## Goal + +rust2-ts translates what lends out a collection's `[u8; N]` key, such as `keys`, `iter`, +`get_key_value`, `Entry::key`, and `HashSet::get` and `iter`, each reference an +`rt.ByteSlice` of the key's bytes, as a `&[u8; N]` is elsewhere. Proven by differential +tests against native Rust. + +## Plan + +Each refuses with a span today, as "`HashMap::keys` of a reference to an array of bytes it +is generic over" (`bytes_across` in `src/lower/mir/call.rs`): the collection holds the key +as a typed array, which its mapping would hand back where the caller reads a slice. +Lookups by such a reference already translate, since they only read the key. + +Public API: the translator's output. diff --git a/quest/m3/ts-byte-keys.md b/quest/m3/ts-byte-keys.md deleted file mode 100644 index 352a302e5..000000000 --- a/quest/m3/ts-byte-keys.md +++ /dev/null @@ -1,18 +0,0 @@ -# [S] Byte arrays compared for equality and hashed - -## Goal - -rust2-ts translates `==` and `!=` of byte arrays such as `[u8; 2]`, and a `[u8; N]`, -`&[u8]`, or `Vec` as a hash key or inside a tuple, `Option`, or `Arc` that is one, -matching std's equality and finding each key as std's map does. Proven by differential -tests against native Rust. - -## Plan - -Each refuses with a span today: `==` of byte arrays as a call over a reference to an array -of bytes, and a key as "comparing" or "hashing `[u8; 2]` as a key". Their order translates -through `rt.cmpBytes`, which reads each byte form, a typed array, an `rt.ByteSlice`, or an -`rt.ByteVec`, at run time ([#259](https://github.com/moq-dev/rust2/pull/259)); equality and -hashing can take the same approach. rust2 aims to support all of std. - -Public API: the translator's output. diff --git a/ts/README.md b/ts/README.md index 420d372e8..d26d01fdf 100644 --- a/ts/README.md +++ b/ts/README.md @@ -259,7 +259,9 @@ finds any other key, a 64-bit one past 2^53 included, through its translated `Ha `Eq`; a B-tree orders every key by its translated `Ord`. std's impls are the runtime's: an `Option` orders `None` first, an `Arc` compares, orders, and hashes as what it holds, so a map of `Arc` finds a `&str`, and byte arrays, slices, and vectors order by their -bytes and then their length. A `&mut` to a value a map holds that JS copies, +bytes and then their length, and compare and hash alike whatever their form, so a map of +`Vec` finds a `&[u8]`. A collection's reference to a byte array it holds as a key, as +`keys` and `HashSet::get` return, is refused. A `&mut` to a value a map holds that JS copies, such as a number, from `get_mut` or the entry API, is an `rt.Place` whose `v` reads and writes the map's slot. A hash map iterates in an order of the runtime's, mostly insertion order, which std leaves unspecified, and hashes a key diff --git a/ts/fixtures/lang/expected/core/bool.ts b/ts/fixtures/lang/expected/core/bool.ts index 3945b369c..5c4233714 100644 --- a/ts/fixtures/lang/expected/core/bool.ts +++ b/ts/fixtures/lang/expected/core/bool.ts @@ -87,6 +87,13 @@ export function thenSome$0d5am30(self: boolean, t: rt.u8): rt.u8 | undefined { return t; } +export function thenSome$0rsp0eu(self: boolean, t: Uint8Array): Uint8Array | undefined { + if (!self) { + return undefined; + } + return t; +} + export function thenSome$1j8foe9(self: boolean, _: undefined): undefined | rt.None { if (!self) { return rt.None; diff --git a/ts/fixtures/lang/expected/harness.ts b/ts/fixtures/lang/expected/harness.ts index 34a80c25a..18f1fff59 100644 --- a/ts/fixtures/lang/expected/harness.ts +++ b/ts/fixtures/lang/expected/harness.ts @@ -21,11 +21,11 @@ import { edits, bytes } from "./edits.ts"; import { floats, formats, parses } from "./floats.ts"; import { ints, strings, derived, written, counts, rounding, panics, wrapped, writes } from "./fmt.ts"; import { methods } from "./generics.ts"; -import { values, random } from "./hashes.ts"; +import { values, random, bytes as hashes_bytes } from "./hashes.ts"; import { holds } from "./holds.ts"; import { u8Op, u16Op, u32Op, u64Op, u128Op, usizeOp, i8Op, i16Op, i32Op, i64Op, i128Op, isizeOp, i8Neg, i16Neg, i32Neg, i64Neg, i128Neg, isizeNeg, cast as ints_cast, convert } from "./ints.ts"; import { adaptors, consumers, skips, ranked, ranges, options, own, values as iters_values, returned } from "./iters.ts"; -import { keys, overrides } from "./keys.ts"; +import { keys, overrides, bytes as keys_bytes } from "./keys.ts"; import { locals } from "./locals.ts"; import { locks, relock, rwlocks } from "./locks.ts"; import { nested } from "./nested.ts"; @@ -90,7 +90,7 @@ export function cases(): [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) refs(data21, out21); }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::counts", (_2121: rt.u8, data22: rt.ByteSlice, out22: rt.ByteVec): void => { counts(data22, out22); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::rounding", (_2122: rt.u8, _339: rt.ByteSlice, out23: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::rounding", (_2122: rt.u8, _345: rt.ByteSlice, out23: rt.ByteVec): void => { rounding(out23); }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::skips", skips] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["wrappers::wrappers", wrappers] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["edits::edits", edits] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["utf8::decodes", decodes] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["floats::floats", floats] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["atomics::atomics", atomics] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["keys::overrides", overrides] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::dyns", (_2123: rt.u8, data23: rt.ByteSlice, out24: rt.ByteVec): void => { dyns(data23, out24); @@ -166,7 +166,9 @@ export function cases(): [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) onces(data58, out59); }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::writes", (_2159: rt.u8, data59: rt.ByteSlice, out60: rt.ByteVec): void => { writes(data59, out60); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["bounds::bounds", bounds] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void]]).map(copy$str_forabfnu8au8bmutstdvecVecu8); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["bounds::bounds", bounds] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["hashes::bytes", (_2160: rt.u8, data60: rt.ByteSlice, out61: rt.ByteVec): void => { + hashes_bytes(data60, out61); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["keys::bytes", keys_bytes] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void]]).map(copy$str_forabfnu8au8bmutstdvecVecu8); } /** Run one case. */ diff --git a/ts/fixtures/lang/expected/hashes.ts b/ts/fixtures/lang/expected/hashes.ts index 7d5125a1a..e08bbd85f 100644 --- a/ts/fixtures/lang/expected/hashes.ts +++ b/ts/fixtures/lang/expected/hashes.ts @@ -1,6 +1,7 @@ // Generated by rust2-ts. Do not edit. import * as rt from "./rt.ts"; +import { copy$u8s } from "./lib.ts"; import { thenSome$0n2gkz4 } from "./core/bool.ts"; import { unwrapOr$0k15iai, unwrapOr$0r2bmpr } from "./core/option.ts"; import { unwrapOr$01jdcm7 } from "./core/result.ts"; @@ -12,6 +13,13 @@ import * as Priorities from "./hashes.Priorities.ts"; import * as Shape from "./hashes.Shape.ts"; import * as Small from "./hashes.Small.ts"; +/** `DefaultHasher::new`'s hash of `value`, little-endian. */ +export function digest$Arc_Vec_u8(value: rt.Arc, out: rt.ByteVec): void { + const h: rt.Hasher = rt.hasher(); + rt.hashArc(rt.hashBytes)(value, h); + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.hashFinish(h)).reverse())); +} + /** `DefaultHasher::new`'s hash of `value`, little-endian. */ export function digest$Arc_str(value: rt.Arc, out: rt.ByteVec): void { const h: rt.Hasher = rt.hasher(); @@ -19,6 +27,13 @@ export function digest$Arc_str(value: rt.Arc, out: rt.ByteVec): void { rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.hashFinish(h)).reverse())); } +/** `DefaultHasher::new`'s hash of `value`, little-endian. */ +export function digest$Arc_u8s(value: rt.Arc, out: rt.ByteVec): void { + const h: rt.Hasher = rt.hasher(); + rt.hashArc(rt.hashBytes)(value, h); + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.hashFinish(h)).reverse())); +} + /** `DefaultHasher::new`'s hash of `value`, little-endian. */ export function digest$Dirs(value: Dir.Dir[], out: rt.ByteVec): void { const h: rt.Hasher = rt.hasher(); @@ -54,6 +69,13 @@ export function digest$Option_Option_u8(value: rt.u8 | undefined | rt.None, out: rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.hashFinish(h)).reverse())); } +/** `DefaultHasher::new`'s hash of `value`, little-endian. */ +export function digest$Option_Vec_u8(value: rt.ByteVec | undefined, out: rt.ByteVec): void { + const h: rt.Hasher = rt.hasher(); + rt.hashOption(rt.hashBytes)(value, h); + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.hashFinish(h)).reverse())); +} + /** `DefaultHasher::new`'s hash of `value`, little-endian. */ export function digest$Option_u32(value: rt.u32 | undefined, out: rt.ByteVec): void { const h: rt.Hasher = rt.hasher(); @@ -61,6 +83,13 @@ export function digest$Option_u32(value: rt.u32 | undefined, out: rt.ByteVec): v rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.hashFinish(h)).reverse())); } +/** `DefaultHasher::new`'s hash of `value`, little-endian. */ +export function digest$Option_u8s(value: Uint8Array | undefined, out: rt.ByteVec): void { + const h: rt.Hasher = rt.hasher(); + rt.hashOption(rt.hashBytes)(value, h); + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.hashFinish(h)).reverse())); +} + /** `DefaultHasher::new`'s hash of `value`, little-endian. */ export function digest$Ordering(value: rt.Ordering, out: rt.ByteVec): void { const h: rt.Hasher = rt.hasher(); @@ -110,6 +139,13 @@ export function digest$Vec_u16(value: rt.u16[], out: rt.ByteVec): void { rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.hashFinish(h)).reverse())); } +/** `DefaultHasher::new`'s hash of `value`, little-endian. */ +export function digest$Vec_u8(value: rt.ByteVec, out: rt.ByteVec): void { + const h: rt.Hasher = rt.hasher(); + rt.hashBytes(value, h); + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.hashFinish(h)).reverse())); +} + /** `DefaultHasher::new`'s hash of `value`, little-endian. */ export function digest$bool(value: boolean, out: rt.ByteVec): void { const h: rt.Hasher = rt.hasher(); @@ -236,6 +272,55 @@ export function digest$u8_str_u64(value: [rt.u8, string, rt.u64], out: rt.ByteVe rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.hashFinish(h)).reverse())); } +/** `DefaultHasher::new`'s hash of `value`, little-endian. */ +export function digest$u8s(value: rt.ByteSlice, out: rt.ByteVec): void { + const h: rt.Hasher = rt.hasher(); + rt.hashBytes(value, h); + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.hashFinish(h)).reverse())); +} + +/** `DefaultHasher::new`'s hash of `value`, little-endian. */ +export function digest$u8s$2(value: rt.ByteSlice, out: rt.ByteVec): void { + const h: rt.Hasher = rt.hasher(); + rt.hashBytes(value, h); + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.hashFinish(h)).reverse())); +} + +/** `DefaultHasher::new`'s hash of `value`, little-endian. */ +export function digest$u8s$3(value: rt.ByteSlice, out: rt.ByteVec): void { + const h: rt.Hasher = rt.hasher(); + rt.hashBytes(value, h); + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.hashFinish(h)).reverse())); +} + +/** `DefaultHasher::new`'s hash of `value`, little-endian. */ +export function digest$u8s$4(value: rt.ByteSlice, out: rt.ByteVec): void { + const h: rt.Hasher = rt.hasher(); + rt.hashBytes(value, h); + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.hashFinish(h)).reverse())); +} + +/** `DefaultHasher::new`'s hash of `value`, little-endian. */ +export function digest$u8s_Option_u8s(value: [Uint8Array, Uint8Array | undefined], out: rt.ByteVec): void { + const h: rt.Hasher = rt.hasher(); + rt.hashTuple([rt.hashBytes, rt.hashOption(rt.hashBytes)])(value, h); + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.hashFinish(h)).reverse())); +} + +/** `DefaultHasher::new`'s hash of `value`, little-endian. */ +export function digest$u8s_u8(value: [rt.ByteSlice, rt.u8], out: rt.ByteVec): void { + const h: rt.Hasher = rt.hasher(); + rt.hashTuple([rt.hashBytes, rt.hash8])(value, h); + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.hashFinish(h)).reverse())); +} + +/** `DefaultHasher::new`'s hash of `value`, little-endian. */ +export function digest$u8s_u8s(value: [Uint8Array, Uint8Array], out: rt.ByteVec): void { + const h: rt.Hasher = rt.hasher(); + rt.hashTuple([rt.hashBytes, rt.hashBytes])(value, h); + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.hashFinish(h)).reverse())); +} + /** `DefaultHasher::new`'s hash of `value`, little-endian. */ export function digest$usize(value: rt.usize, out: rt.ByteVec): void { const h: rt.Hasher = rt.hasher(); @@ -423,4 +508,67 @@ export function random(data: rt.ByteSlice, out: rt.ByteVec): void { } } +/** + * What `Hash` writes of bytes, which is alike in each form, a `[u8; N]`, a `&[u8]`, or a + * `Vec`: its length as a `usize`, then the bytes. Held in a tuple, an `Option`, and an + * `Arc` too, and through `RandomState`'s `hash_one`, which hashes each form alike. + */ +export function bytes(data: rt.ByteSlice, out: rt.ByteVec): void { + const iter: rt.RangeInclusive = new rt.RangeInclusive(0, rt.min(data.length, 5), rt.usizeStep); + while (true) { + const _8: rt.usize | rt.Done = iter.next(); + if (_8 === rt.DONE) { + break; + } + const i: rt.usize = _8; + const s: rt.ByteSlice = rt.byteSliceRange(data, 0, i); + digest$u8s$4(s, out); + digest$Vec_u8(rt.byteSliceToByteVec(s), out); + digest$u8s_u8([s, i & 0xff] as [rt.ByteSlice, rt.u8], out); + digest$Option_Vec_u8(rt.byteSliceToByteVec(s), out); + const _28: rt.Arc = new rt.Arc(rt.byteSliceToByteVec(s)); + digest$Arc_Vec_u8(_28, out); + _28[Symbol.dispose](); + } + digest$u8s(rt.byteSliceFrom(Uint8Array.of()), out); + digest$u8s_Option_u8s([Uint8Array.of(), Uint8Array.of()] as [Uint8Array, Uint8Array | undefined], out); + const state: rt.RandomState = rt.randomState(); + const iter2: rt.Iter = rt.byteSliceWindows(data, 3); + while (true) { + const _41: rt.ByteSlice | rt.Done = iter2.next(); + if (_41 === rt.DONE) { + break; + } + rt.idx(_41, 0); + const _46: rt.u8 = _41.data[_41.start]; + rt.idx(_41, 1); + const _50: rt.u8 = _41.data[_41.start + 1]; + rt.idx(_41, 2); + const a: Uint8Array = Uint8Array.of(_46, _50, _41.data[_41.start + 2]); + rt.idx(_41, 0); + const _59: rt.u8 = rt.u8Rem(_41.data[_41.start], 2); + rt.idx(_41, 1); + const pair: Uint8Array = Uint8Array.of(_59, rt.u8Rem(_41.data[_41.start + 1], 2)); + digest$u8s$3(rt.byteSliceFrom(a), out); + digest$u8s$2(rt.byteSliceFrom(pair), out); + digest$u8s_u8s([pair, a] as [Uint8Array, Uint8Array], out); + digest$Option_u8s(pair, out); + const _83: rt.Arc = new rt.Arc(copy$u8s(a)); + digest$Arc_u8s(_83, out); + _83[Symbol.dispose](); + const _87: rt.u64 = rt.hashOne(state, a, rt.hashBytes); + const _91: rt.ByteSlice = rt.byteSliceRange(_41, 0, 3); + rt.byteVecPush(out, rt.u64Eq(_87, rt.hashOne(state, _91, rt.hashBytes)) ? 1 : 0); + const _96: rt.u64 = rt.hashOne(state, rt.byteSliceFrom(a), rt.hashBytes); + const _101: rt.ByteVec = rt.byteSliceToByteVec(_41); + rt.byteVecPush(out, rt.u64Eq(_96, rt.hashOne(state, _101, rt.hashBytes)) ? 1 : 0); + const _105: rt.u64 = rt.hashOne(state, [pair, 1] as [Uint8Array, rt.u8], rt.hashTuple([rt.hashBytes, rt.hash8])); + const _110: [rt.ByteSlice, rt.u8] = [rt.byteSliceRange(_41, 0, 2), 1]; + rt.byteVecPush(out, rt.u64Eq(_105, rt.hashOne(state, _110, rt.hashTuple([rt.hashBytes, rt.hash8]))) ? 1 : 0); + } + const _116: rt.u64 = rt.hashOne(state, Uint8Array.of(), rt.hashBytes); + const _120: rt.ByteVec = new rt.ByteVec(); + rt.byteVecPush(out, rt.u64Eq(_116, rt.hashOne(state, _120, rt.hashBytes)) ? 1 : 0); +} + //# sourceMappingURL=hashes.ts.map diff --git a/ts/fixtures/lang/expected/keys.ts b/ts/fixtures/lang/expected/keys.ts index dc380e901..70f974a4f 100644 --- a/ts/fixtures/lang/expected/keys.ts +++ b/ts/fixtures/lang/expected/keys.ts @@ -1,8 +1,9 @@ // Generated by rust2-ts. Do not edit. import * as rt from "./rt.ts"; -import { copy$Hop, copy$Horizon, copy$u8_Horizon, drop$u8_Reverse_Arc_Lopsided } from "./lib.ts"; -import { unwrapOr$1tvry1i, unwrapOr$1j4op5y, unwrapOr$0r2bmpr, map$120lney, map$146awhd, map$0lcwx9x, map$0gn4nwv, mapOr$04khdaj, mapOr$1dgdmlk } from "./core/option.ts"; +import { copy$Hop, copy$Horizon, copy$Option_u8s, copy$u8_Horizon, copy$u8s, drop$u8_Reverse_Arc_Lopsided } from "./lib.ts"; +import { thenSome$0rsp0eu } from "./core/bool.ts"; +import { unwrapOr$1tvry1i, unwrapOr$1j4op5y, unwrapOr$0r2bmpr, map$120lney, map$146awhd, map$0lcwx9x, map$0gn4nwv, mapOr$04khdaj, mapOr$1dgdmlk, mapOr$1surhce, mapOr$02k4ito } from "./core/option.ts"; import * as Hop from "./keys.Hop.ts"; import * as Horizon from "./keys.Horizon.ts"; import * as Liar from "./keys.Liar.ts"; @@ -851,4 +852,176 @@ export function places(data: rt.ByteSlice, out: rt.ByteVec): void { } } +/** + * Bytes compared and found as keys: a `[u8; N]`, a `&[u8]`, or a `Vec`, equal to + * another form holding the same bytes, alone or in a tuple, an `Option`, or an `Arc`, + * and a key found by a `&[u8]` it borrows as, in hash and B-tree collections alike. + */ +export function bytes(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { + const cut: rt.usize = rt.min(data.length, 6); + const iter: rt.Iter = rt.byteSliceWindows(data, 2); + while (true) { + const _9: rt.ByteSlice | rt.Done = iter.next(); + if (_9 === rt.DONE) { + break; + } + rt.idx(_9, 0); + const _17: rt.u8 = rt.u8Rem(_9.data[_9.start], 3); + rt.idx(_9, 1); + const _16: Uint8Array = Uint8Array.of(_17, rt.u8Rem(_9.data[_9.start + 1], 3)); + rt.idx(_9, 1); + const _30: rt.u8 = rt.u8Rem(_9.data[_9.start + 1], 3); + rt.idx(_9, 0); + const _15: [Uint8Array, Uint8Array] = [_16, Uint8Array.of(_30, rt.u8Rem(_9.data[_9.start], 3))]; + const a: Uint8Array = _16; + const b: Uint8Array = _15[1]; + const _44: rt.u8 = rt.eqBytes(rt.byteSliceFrom(a), rt.byteSliceFrom(b)) ? 1 : 0; + const _48: rt.u8 = !rt.eqBytes(rt.byteSliceFrom(a), rt.byteSliceFrom(b)) ? 1 : 0; + const _52: rt.u8 = rt.eqBytes(rt.byteSliceFrom(a), rt.byteSliceFrom(b)) ? 1 : 0; + const _61: Uint8Array | undefined = a; + const _63: Uint8Array | undefined = b; + rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(_44, _48, _52, rt.eqOption(rt.eqBytes)(_61, _63) ? 1 : 0))); + rt.idx(_9, 0); + const _69: [Uint8Array, rt.u8] = [a, _9.data[_9.start]]; + rt.idx(_9, 1); + const _75: [Uint8Array, rt.u8] = [b, _9.data[_9.start + 1]]; + rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(rt.eqTuple([rt.eqBytes, rt.same])(_69, _75) ? 1 : 0, rt.eqBytes(rt.byteSliceToByteVec(rt.byteSliceFrom(a)), rt.byteSliceToByteVec(rt.byteSliceFrom(b))) ? 1 : 0))); + rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(rt.byteSliceEq(rt.byteSliceRange(rt.byteSliceFrom(a), 0), rt.byteSliceRange(rt.byteSliceFrom(b), 0)) ? 1 : 0, rt.eqBytes(rt.byteSliceToByteVec(rt.byteSliceFrom(a)), rt.byteSliceFrom(a)) ? 1 : 0, rt.eqBytes(rt.byteSliceRange(rt.byteSliceFrom(a), 0), rt.byteSliceToByteVec(rt.byteSliceFrom(b))) ? 1 : 0))); + const _117: rt.u8 = rt.eqBytes(rt.byteSliceToByteVec(rt.byteSliceFrom(a)), rt.byteSliceRange(rt.byteSliceFrom(b), 0)) ? 1 : 0; + const _127: rt.u8 = rt.eqBytes(rt.byteSliceFrom(a), rt.byteSliceRange(rt.byteSliceFrom(b), 0)) ? 1 : 0; + const _135: rt.Arc = new rt.Arc(copy$u8s(a)); + const _137: rt.Arc = new rt.Arc(copy$u8s(b)); + rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(_117, _127, rt.eqArc(rt.eqBytes, true)(_135, _137) ? 1 : 0))); + _137[Symbol.dispose](); + _135[Symbol.dispose](); + } + const arrays: rt.HashMap = rt.hashNew(rt.hashing(rt.hashBytes, rt.eqBytes)) as rt.HashMap; + const vecs: rt.HashMap = rt.hashNew(rt.hashing(rt.hashBytes, rt.eqBytes)) as rt.HashMap; + const slices: rt.HashMap = rt.hashNew(rt.hashing(rt.hashBytes, rt.eqBytes)) as rt.HashMap; + const nested: rt.HashMap<[Uint8Array | undefined, rt.ByteVec], rt.usize> = rt.hashNew(rt.hashing(rt.hashTuple([rt.hashOption(rt.hashBytes), rt.hashBytes]), rt.eqTuple([rt.eqOption(rt.eqBytes), rt.eqBytes]))) as rt.HashMap<[Uint8Array | undefined, rt.ByteVec], rt.usize>; + const shared: rt.HashMap, rt.usize> = rt.hashNew(rt.hashing(rt.hashArc(rt.hashBytes), rt.eqArc(rt.eqBytes, true))) as rt.HashMap, rt.usize>; + const arcs_: rt.HashMap, rt.usize> = rt.hashNew(rt.hashing(rt.hashArc(rt.hashBytes), rt.eqArc(rt.eqBytes, true))) as rt.HashMap, rt.usize>; + const tree: rt.BTreeMap = rt.btreeNew(rt.cmpBytes) as rt.BTreeMap; + const ordered: rt.BTreeSet = rt.btreeNew(rt.cmpBytes) as rt.BTreeSet; + const iter2: rt.Iter<[rt.usize, rt.ByteSlice]> = rt.iterEnumerate(rt.byteSliceWindows(data, 2)); + while (true) { + const _150: [rt.usize, rt.ByteSlice] | rt.Done = iter2.next(); + if (_150 === rt.DONE) { + break; + } + const i: rt.usize = _150[0]; + rt.idx(_150[1], 0); + const _156: rt.u8 = rt.u8Rem(_150[1].data[_150[1].start], 4); + rt.idx(_150[1], 1); + const key: Uint8Array = Uint8Array.of(_156, rt.u8Rem(_150[1].data[_150[1].start + 1], 4)); + const s: rt.ByteSlice = rt.byteSliceRange(data, i, rt.min(rt.usizeAdd(i, rt.usizeRem(kind as rt.usize, 3)), data.length)); + rt.byteVecPush(out, mapOr$02k4ito(rt.hashInsert(arrays, copy$u8s(key), i), 204, (old: rt.usize): rt.u8 => { + return old & 0xff; + })); + const _185: rt.ByteVec = rt.byteSliceToByteVec(s); + rt.byteVecPush(out, mapOr$02k4ito(rt.hashInsert(vecs, _185, i), 204, (old2: rt.usize): rt.u8 => { + return old2 & 0xff; + })); + rt.byteVecPush(out, mapOr$02k4ito(rt.hashInsert(slices, s, i), 204, (old3: rt.usize): rt.u8 => { + return old3 & 0xff; + })); + rt.idx(_150[1], 0); + const opt: Uint8Array | undefined = thenSome$0rsp0eu(rt.u8Rem(_150[1].data[_150[1].start], 3) !== 0, key); + const _203: rt.ByteVec = rt.byteSliceToByteVec(s); + const _202: [Uint8Array | undefined, rt.ByteVec] = [copy$Option_u8s(opt), _203]; + rt.byteVecPush(out, mapOr$02k4ito(rt.hashInsert(nested, _202, i), 204, (old4: rt.usize): rt.u8 => { + return old4 & 0xff; + })); + const _208: rt.Arc = new rt.Arc(rt.byteSliceToByteVec(s)); + rt.byteVecPush(out, mapOr$02k4ito(rt.hashInsert(shared, _208, i, rt.drop), 204, (old5: rt.usize): rt.u8 => { + return old5 & 0xff; + })); + const _214: rt.Arc = new rt.Arc(copy$u8s(key)); + rt.byteVecPush(out, mapOr$02k4ito(rt.hashInsert(arcs_, _214, i, rt.drop), 204, (old6: rt.usize): rt.u8 => { + return old6 & 0xff; + })); + rt.byteVecPush(out, mapOr$02k4ito(rt.btreeInsert(tree, copy$u8s(key), i), 204, (old7: rt.usize): rt.u8 => { + return old7 & 0xff; + })); + const _223: rt.ByteVec = rt.byteSliceToByteVec(s); + rt.byteVecPush(out, rt.btreeAdd(ordered, _223) ? 1 : 0); + } + const iter3: rt.RangeInclusive = new rt.RangeInclusive(0, cut, rt.usizeStep); + l5: while (true) { + const _227: rt.usize | rt.Done = iter3.next(); + if (_227 === rt.DONE) { + break; + } + const i2: rt.usize = _227; + const iter4: rt.RangeInclusive = new rt.RangeInclusive(i2, cut, rt.usizeStep); + while (true) { + const _234: rt.usize | rt.Done = iter4.next(); + if (_234 === rt.DONE) { + continue l5; + } + const j: rt.usize = _234; + const s2: rt.ByteSlice = rt.byteSliceRange(data, i2, j); + rt.byteVecPush(out, mapOr$02k4ito(rt.hashGet(vecs, s2), 221, (v: rt.usize): rt.u8 => { + return v & 0xff; + })); + rt.byteVecPush(out, mapOr$02k4ito(rt.hashGet(slices, s2), 221, (v2: rt.usize): rt.u8 => { + return v2 & 0xff; + })); + const _253: rt.ByteVec = rt.byteSliceToByteVec(s2); + rt.byteVecPush(out, rt.hashContains(vecs, _253) ? 1 : 0); + const _259: rt.Arc = new rt.Arc(rt.byteSliceToByteVec(s2)); + rt.byteVecPush(out, mapOr$02k4ito(rt.hashGet(shared, _259), 221, (v3: rt.usize): rt.u8 => { + return v3 & 0xff; + })); + _259[Symbol.dispose](); + const key2: Uint8Array = Uint8Array.of(mapOr$1surhce(rt.byteSliceGet(data, i2), 0, (b2: rt.u8): rt.u8 => { + return rt.u8Rem(b2, 4); + }), mapOr$1surhce(rt.byteSliceGet(data, j), 0, (b3: rt.u8): rt.u8 => { + return rt.u8Rem(b3, 4); + })); + rt.byteVecPush(out, mapOr$02k4ito(rt.hashGet(arrays, rt.byteSliceView(rt.byteSliceFrom(key2))), 221, (v4: rt.usize): rt.u8 => { + return v4 & 0xff; + })); + rt.byteVecPush(out, mapOr$02k4ito(rt.hashGet(arcs_, new rt.Arc(rt.byteSliceView(rt.byteSliceFrom(key2)))), 221, (v5: rt.usize): rt.u8 => { + return v5 & 0xff; + })); + rt.byteVecPush(out, mapOr$02k4ito(rt.btreeGet(tree, rt.byteSliceView(rt.byteSliceFrom(key2))), 221, (v6: rt.usize): rt.u8 => { + return v6 & 0xff; + })); + rt.byteVecPush(out, rt.btreeContains(ordered, s2) ? 1 : 0); + if (s2.length >= 2) { + const two: rt.ByteSlice = rt.byteSliceRange(s2, 0, 2); + rt.byteVecPush(out, mapOr$02k4ito(rt.hashGet(arrays, rt.byteSliceView(two)), 221, (v7: rt.usize): rt.u8 => { + return v7 & 0xff; + })); + rt.byteVecPush(out, mapOr$02k4ito(rt.btreeGet(tree, rt.byteSliceView(two)), 221, (v8: rt.usize): rt.u8 => { + return v8 & 0xff; + })); + } + const _302: [Uint8Array | undefined, rt.ByteVec] = [key2, rt.byteSliceToByteVec(s2)]; + rt.byteVecPush(out, mapOr$02k4ito(rt.hashGet(nested, _302), 221, (v9: rt.usize): rt.u8 => { + return v9 & 0xff; + })); + const _310: [Uint8Array | undefined, rt.ByteVec] = [undefined, rt.byteSliceToByteVec(s2)]; + rt.byteVecPush(out, mapOr$02k4ito(rt.hashGet(nested, _310), 221, (v10: rt.usize): rt.u8 => { + return v10 & 0xff; + })); + } + } + const _317: rt.ByteSlice = rt.byteSliceRange(rt.byteSliceFrom(Uint8Array.of()), 0); + rt.byteVecPush(out, mapOr$02k4ito(rt.hashRemove(vecs, _317), 238, (v11: rt.usize): rt.u8 => { + return v11 & 0xff; + })); + rt.byteVecPush(out, rt.hashLen(vecs) & 0xff); + const _324: rt.Iter = rt.iterMap(rt.byteSliceChunks(data, 2), (a0: rt.ByteSlice): rt.ByteVec => { + return rt.byteSliceToByteVec(a0); + }); + const set: rt.HashSet = rt.hashExtendKeys(rt.hashNew(rt.hashing(rt.hashBytes, rt.eqBytes)) as rt.HashSet, _324); + rt.byteVecPush(out, rt.hashLen(set) & 0xff); + const _334: rt.ByteSlice = rt.byteSliceRange(data, 0, rt.min(data.length, 2)); + rt.byteVecPush(out, rt.hashContains(set, _334) ? 1 : 0); + rt.hashDrop(arcs_, rt.drop); + rt.hashDrop(shared, rt.drop); +} + //# sourceMappingURL=keys.ts.map diff --git a/ts/fixtures/lang/expected/lib.ts b/ts/fixtures/lang/expected/lib.ts index 5d40e4d79..a5acccf56 100644 --- a/ts/fixtures/lang/expected/lib.ts +++ b/ts/fixtures/lang/expected/lib.ts @@ -157,6 +157,10 @@ export function copy$Option_u8_u8(v: [rt.u8, rt.u8] | undefined): [rt.u8, rt.u8] return v === undefined ? v : copy$u8_u8(v); } +export function copy$Option_u8s(v: Uint8Array | undefined): Uint8Array | undefined { + return v === undefined ? v : copy$u8s(v); +} + export function copy$Option_usize_u8(v: [rt.usize, rt.u8] | undefined): [rt.usize, rt.u8] | undefined { return v === undefined ? v : copy$usize_u8(v); } diff --git a/ts/fixtures/lang/src/harness.rs b/ts/fixtures/lang/src/harness.rs index 2e77577f8..99ffc9c24 100644 --- a/ts/fixtures/lang/src/harness.rs +++ b/ts/fixtures/lang/src/harness.rs @@ -135,6 +135,8 @@ fn cases() -> Vec<(&'static str, Case)> { ("statics::onces", |_, data, out| statics::onces(data, out)), ("fmt::writes", |_, data, out| fmt::writes(data, out)), ("bounds::bounds", bounds::bounds), + ("hashes::bytes", |_, data, out| hashes::bytes(data, out)), + ("keys::bytes", keys::bytes), ] } diff --git a/ts/fixtures/lang/src/hashes.rs b/ts/fixtures/lang/src/hashes.rs index e850a647f..a31f6c305 100644 --- a/ts/fixtures/lang/src/hashes.rs +++ b/ts/fixtures/lang/src/hashes.rs @@ -219,3 +219,35 @@ pub fn random(data: &[u8], out: &mut Vec) { out.extend_from_slice(&k.to_le_bytes()); } } + +/// What `Hash` writes of bytes, which is alike in each form, a `[u8; N]`, a `&[u8]`, or a +/// `Vec`: its length as a `usize`, then the bytes. Held in a tuple, an `Option`, and an +/// `Arc` too, and through `RandomState`'s `hash_one`, which hashes each form alike. +pub fn bytes(data: &[u8], out: &mut Vec) { + let cut = data.len().min(5); + for i in 0..=cut { + let s = &data[..i]; + digest(s, out); + digest(&s.to_vec(), out); + digest(&(s, i as u8), out); + digest(&Some(s.to_vec()), out); + digest(&Arc::new(s.to_vec()), out); + } + let empty: [u8; 0] = []; + digest(&empty, out); + digest(&(empty, Some(empty)), out); + let state = RandomState::new(); + for w in data.windows(3) { + let a = [w[0], w[1], w[2]]; + let pair = [w[0] % 2, w[1] % 2]; + digest(&a, out); + digest(&pair, out); + digest(&(pair, a), out); + digest(&Some(pair), out); + digest(&Arc::new(a), out); + out.push((state.hash_one(a) == state.hash_one(&w[..3])) as u8); + out.push((state.hash_one(&a) == state.hash_one(w.to_vec())) as u8); + out.push((state.hash_one((pair, 1u8)) == state.hash_one((&w[..2], 1u8))) as u8); + } + out.push((state.hash_one(empty) == state.hash_one(Vec::::new())) as u8); +} diff --git a/ts/fixtures/lang/src/keys.rs b/ts/fixtures/lang/src/keys.rs index e4f82af5d..8545104e7 100644 --- a/ts/fixtures/lang/src/keys.rs +++ b/ts/fixtures/lang/src/keys.rs @@ -2,10 +2,11 @@ //! `None` first, and `Arc`s, which compare as what they hold; lookups through `Borrow`; //! the references to values a map holds that JS copies, changed in place; and the `!=`, //! `<`, `<=`, `>`, and `>=` a type's `PartialEq` and `PartialOrd` inherit or override. +//! Then bytes of each form, equal to one another and found by one another. use std::borrow::Borrow; use std::cmp::{Ordering, Reverse}; -use std::collections::{BTreeMap, HashMap, btree_map, hash_map}; +use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, btree_map, hash_map}; use std::hash::{DefaultHasher, Hash, Hasher}; use std::sync::Arc; @@ -610,3 +611,77 @@ fn places(data: &[u8], out: &mut Vec) { out.push(*b); } } + +/// Bytes compared and found as keys: a `[u8; N]`, a `&[u8]`, or a `Vec`, equal to +/// another form holding the same bytes, alone or in a tuple, an `Option`, or an `Arc`, +/// and a key found by a `&[u8]` it borrows as, in hash and B-tree collections alike. +pub fn bytes(kind: u8, data: &[u8], out: &mut Vec) { + let cut = data.len().min(6); + for w in data.windows(2) { + let (a, b) = ([w[0] % 3, w[1] % 3], [w[1] % 3, w[0] % 3]); + out.extend([ + (a == b) as u8, + (a != b) as u8, + (&a == &b) as u8, + (Some(a) == Some(b)) as u8, + ]); + out.extend([((a, w[0]) == (b, w[1])) as u8, (a.to_vec() == b.to_vec()) as u8]); + out.extend([ + (a[..] == b[..]) as u8, + (a.to_vec() == a) as u8, + (&a[..] == b.to_vec()) as u8, + ]); + out.extend([ + (a.to_vec() == &b[..]) as u8, + (a == b[..]) as u8, + (Arc::new(a) == Arc::new(b)) as u8, + ]); + } + let mut arrays: HashMap<[u8; 2], usize> = HashMap::new(); + let mut vecs: HashMap, usize> = HashMap::new(); + let mut slices: HashMap<&[u8], usize> = HashMap::new(); + let mut nested: HashMap<(Option<[u8; 2]>, Vec), usize> = HashMap::new(); + let mut shared: HashMap>, usize> = HashMap::new(); + let mut arcs: HashMap, usize> = HashMap::new(); + let mut tree: BTreeMap<[u8; 2], usize> = BTreeMap::new(); + let mut ordered: BTreeSet> = BTreeSet::new(); + for (i, w) in data.windows(2).enumerate() { + let key = [w[0] % 4, w[1] % 4]; + let s = &data[i..(i + kind as usize % 3).min(data.len())]; + out.push(arrays.insert(key, i).map_or(0xcc, |old| old as u8)); + out.push(vecs.insert(s.to_vec(), i).map_or(0xcc, |old| old as u8)); + out.push(slices.insert(s, i).map_or(0xcc, |old| old as u8)); + let opt = (w[0] % 3 != 0).then_some(key); + out.push(nested.insert((opt, s.to_vec()), i).map_or(0xcc, |old| old as u8)); + out.push(shared.insert(Arc::new(s.to_vec()), i).map_or(0xcc, |old| old as u8)); + out.push(arcs.insert(Arc::new(key), i).map_or(0xcc, |old| old as u8)); + out.push(tree.insert(key, i).map_or(0xcc, |old| old as u8)); + out.push(ordered.insert(s.to_vec()) as u8); + } + for i in 0..=cut { + for j in i..=cut { + let s = &data[i..j]; + out.push(vecs.get(s).map_or(0xdd, |v| *v as u8)); + out.push(slices.get(s).map_or(0xdd, |v| *v as u8)); + out.push(vecs.contains_key(&s.to_vec()) as u8); + out.push(shared.get(&Arc::new(s.to_vec())).map_or(0xdd, |v| *v as u8)); + let key = [data.get(i).map_or(0, |b| b % 4), data.get(j).map_or(0, |b| b % 4)]; + out.push(arrays.get(&key).map_or(0xdd, |v| *v as u8)); + out.push(arcs.get(&key).map_or(0xdd, |v| *v as u8)); + out.push(tree.get(&key).map_or(0xdd, |v| *v as u8)); + out.push(ordered.contains(s) as u8); + if s.len() >= 2 { + let two = &s[..2]; + out.push(arrays.get(two).map_or(0xdd, |v| *v as u8)); + out.push(tree.get(two).map_or(0xdd, |v| *v as u8)); + } + out.push(nested.get(&(Some(key), s.to_vec())).map_or(0xdd, |v| *v as u8)); + out.push(nested.get(&(None, s.to_vec())).map_or(0xdd, |v| *v as u8)); + } + } + out.push(vecs.remove(&[][..]).map_or(0xee, |v| v as u8)); + out.push(vecs.len() as u8); + let set: HashSet> = data.chunks(2).map(<[u8]>::to_vec).collect(); + out.push(set.len() as u8); + out.push(set.contains(&data[..data.len().min(2)]) as u8); +} diff --git a/ts/fixtures/lang/src/lib.rs b/ts/fixtures/lang/src/lib.rs index 178b1dcbc..5db39dfc0 100644 --- a/ts/fixtures/lang/src/lib.rs +++ b/ts/fixtures/lang/src/lib.rs @@ -10,8 +10,8 @@ //! the counts of shared pointers, arrays `array::from_fn` makes, maps of `Option`s, //! values held back past a write of a variable they name, what `Hash` writes into //! `DefaultHasher` and `RandomState`, slices' `windows` and chunks, mutable ones written -//! through, `Vec::drain`, the order of bytes, and ranges of `Bound`s and of the crate's -//! own `RangeBounds`. +//! through, `Vec::drain`, the order, equality, and hashes of bytes, and ranges of `Bound`s +//! and of the crate's own `RangeBounds`. //! //! `harness::run` is the entry point both sides of the differential test run, and //! `fmt::Fault` an error only `caller.ts` prints. diff --git a/ts/fixtures/reject/census.md b/ts/fixtures/reject/census.md index 6b8fa6e59..54ac427d4 100644 --- a/ts/fixtures/reject/census.md +++ b/ts/fixtures/reject/census.md @@ -1,7 +1,7 @@ # Census of `reject-fixture` -- Refusals: 106 -- Function instances: 124 +- Refusals: 107 +- Function instances: 126 - Generic functions no instance reached, lowered over their type parameters: 10 ## By construct @@ -12,12 +12,12 @@ | 4 | a variant of `_`, which is not supported: either is the same JS string | fixtures/reject/src/lib.rs:385:2 | | 4 | passing `_`, which drops, to `_`, which is not supported yet | fixtures/reject/src/lib.rs:380:4 | | 3 | `_` of `_`, whose `None` is `rt.None`, which is not supported yet | fixtures/reject/src/lib.rs:41:42 | +| 3 | `_` 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 | fixtures/reject/src/lib.rs:250:16 | | 3 | a copy of a closure that changes what it captured | fixtures/reject/src/lib.rs:260:14 | | 3 | an assignment through a reference to `_` | fixtures/reject/src/lib.rs:102:2 | | 3 | replacing the value behind a reference to `_` | fixtures/reject/src/lib.rs:110:2 | | 3 | unsupported type `_` | fixtures/reject/src/lib.rs:7:14 | | 2 | `_` is not supported: its `None` and `Some` would both be `rt.None` | fixtures/reject/src/lib.rs:16:15 | -| 2 | `_` 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 | fixtures/reject/src/lib.rs:250:16 | | 2 | `_`, a trait object of another crate's trait, which is not supported yet | fixtures/reject/src/lib.rs:11:16 | | 2 | `_`, a trait object of the generic trait `_`, which is not supported yet | fixtures/reject/src/lib.rs:518:23 | | 2 | a coercion to `_`, which is not supported yet | fixtures/reject/src/lib.rs:509:40 | @@ -65,12 +65,12 @@ | 1 | an upcast from `_` to `_`, which is not supported yet | fixtures/reject/src/lib.rs:593:32 | | 1 | arithmetic on `_` | fixtures/reject/src/lib.rs:8:2 | | 1 | collecting `_`, whose `None` is `rt.None`, which is not supported yet | fixtures/reject/src/lib.rs:36:43 | -| 1 | comparing `_` as a key, which is not supported yet | fixtures/reject/src/lib.rs:271:52 | +| 1 | comparing `_` as a key, which is not supported yet | fixtures/reject/src/lib.rs:271:75 | | 1 | comparing pointers to `()`, which has no bytes and so no address of its own | fixtures/reject/src/lib.rs:692:2 | | 1 | comparing pointers to `_` in an atomic, which JS has no address of | fixtures/reject/src/lib.rs:746:15 | | 1 | comparing pointers to `_`, each a place JS makes of its own | fixtures/reject/src/lib.rs:657:2 | | 1 | constructing `_` | fixtures/reject/src/lib.rs:403:53 | -| 1 | hashing `_` as a key, which is not supported yet | fixtures/reject/src/lib.rs:271:52 | +| 1 | hashing `_` as a key, which is not supported yet | fixtures/reject/src/lib.rs:271:75 | | 1 | hashing `_` through `_`'s own `Hash::hash_slice`, which is not supported yet | fixtures/reject/src/lib.rs:363:16 | | 1 | union `_`, whose fields overlap in `_` and `f64`, which JS keeps apart | fixtures/reject/src/lib.rs:643:1 | | 1 | unsupported type `_` from another crate | fixtures/reject/src/lib.rs:492:31 | @@ -87,7 +87,6 @@ The type or module each refused type or path belongs to. | 6 | `u32` | fixtures/reject/src/lib.rs:236:13 | | 4 | `Iterator` | fixtures/reject/src/lib.rs:41:42 | | 4 | `std::pin::Pin` | fixtures/reject/src/lib.rs:110:2 | -| 4 | `std::vec::Vec` | fixtures/reject/src/lib.rs:271:52 | | 3 | `(u32, u32)` | fixtures/reject/src/lib.rs:669:10 | | 3 | `Spent` | fixtures/reject/src/lib.rs:772:4 | | 3 | `std::sync::Arc` | fixtures/reject/src/lib.rs:321:2 | @@ -99,6 +98,7 @@ The type or module each refused type or path belongs to. | 2 | `dyn std::any::Any` | fixtures/reject/src/lib.rs:593:32 | | 2 | `dyn std::iter::Iterator` | fixtures/reject/src/lib.rs:11:16 | | 2 | `std::any::TypeId` | fixtures/reject/src/lib.rs:599:2 | +| 2 | `std::collections::BTreeSet` | fixtures/reject/src/lib.rs:271:75 | | 2 | `std::collections::HashMap` | fixtures/reject/src/lib.rs:53:13 | | 2 | `std::iter::Iterator` | fixtures/reject/src/lib.rs:250:16 | | 2 | `std::iter::Map` | fixtures/reject/src/lib.rs:155:23 | @@ -107,6 +107,7 @@ The type or module each refused type or path belongs to. | 2 | `std::std` | fixtures/reject/src/lib.rs:628:11 | | 2 | `std::sync::MutexGuard` | fixtures/reject/src/lib.rs:102:2 | | 2 | `std::vec::IntoIter` | fixtures/reject/src/lib.rs:483:17 | +| 2 | `std::vec::Vec` | fixtures/reject/src/lib.rs:363:16 | | 1 | `BTreeMap` | fixtures/reject/src/lib.rs:772:56 | | 1 | `Batched` | fixtures/reject/src/lib.rs:363:16 | | 1 | `BinaryHeap` | fixtures/reject/src/lib.rs:730:7 | @@ -136,6 +137,7 @@ The type or module each refused type or path belongs to. | 1 | `poll` | fixtures/reject/src/lib.rs:556:2 | | 1 | `rt.Poll` | fixtures/reject/src/lib.rs:556:2 | | 1 | `std::any::Any` | fixtures/reject/src/lib.rs:599:4 | +| 1 | `std::collections::HashSet` | fixtures/reject/src/lib.rs:804:6 | | 1 | `std::iter::Chain` | fixtures/reject/src/lib.rs:182:51 | | 1 | `std::iter::Rev` | fixtures/reject/src/lib.rs:176:14 | | 1 | `std::num::ParseIntError` | fixtures/reject/src/lib.rs:492:31 | @@ -177,6 +179,7 @@ The type or module each refused type or path belongs to. | 1 | `_` of `_`, whose `None` is `rt.None`, which is not supported yet | `BinaryHeap::pop`, `Option` | fixtures/reject/src/lib.rs:730:7 | | 1 | `_` of `_`, whose `None` is `rt.None`, which is not supported yet | `HashSet::get`, `Option` | fixtures/reject/src/lib.rs:266:6 | | 1 | `_` of `_`, whose `None` is `rt.None`, which is not supported yet | `Iterator::find`, `Option` | fixtures/reject/src/lib.rs:41:42 | +| 1 | `_` 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 | `std::collections::HashSet::get` | fixtures/reject/src/lib.rs:804:6 | | 1 | `_` 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 | `std::iter::Iterator::count` | fixtures/reject/src/lib.rs:250:38 | | 1 | `_` 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 | `std::iter::Iterator::filter` | fixtures/reject/src/lib.rs:250:16 | | 1 | `_` through a held type's own, which is not supported yet | `::ne` | fixtures/reject/src/lib.rs:321:2 | @@ -208,13 +211,13 @@ The type or module each refused type or path belongs to. | 1 | an upcast from `_` to `_`, which is not supported yet | `dyn Keyed`, `dyn std::any::Any` | fixtures/reject/src/lib.rs:593:32 | | 1 | arithmetic on `_` | `f32` | fixtures/reject/src/lib.rs:8:2 | | 1 | collecting `_`, whose `None` is `rt.None`, which is not supported yet | `Option` | fixtures/reject/src/lib.rs:36:43 | -| 1 | comparing `_` as a key, which is not supported yet | `std::vec::Vec` | fixtures/reject/src/lib.rs:271:52 | +| 1 | comparing `_` as a key, which is not supported yet | `std::collections::BTreeSet` | fixtures/reject/src/lib.rs:271:75 | | 1 | comparing pointers to `_` in an atomic, which JS has no address of | `u32` | fixtures/reject/src/lib.rs:746:15 | | 1 | comparing pointers to `_`, each a place JS makes of its own | `u32` | fixtures/reject/src/lib.rs:657:2 | | 1 | constructing `_` | `std::borrow::Cow` | fixtures/reject/src/lib.rs:403:53 | | 1 | constructing `_` from another crate | `std::iter::Map` | fixtures/reject/src/lib.rs:155:23 | | 1 | constructing `_` from another crate | `std::iter::Rev` | fixtures/reject/src/lib.rs:176:14 | -| 1 | hashing `_` as a key, which is not supported yet | `std::vec::Vec` | fixtures/reject/src/lib.rs:271:52 | +| 1 | hashing `_` as a key, which is not supported yet | `std::collections::BTreeSet` | fixtures/reject/src/lib.rs:271:75 | | 1 | hashing `_` through `_`'s own `Hash::hash_slice`, which is not supported yet | `std::vec::Vec`, `Batched` | fixtures/reject/src/lib.rs:363:16 | | 1 | overriding `_`, which std's adaptors call in place of `next` or beside it | `Iterator::fold` | fixtures/reject/src/lib.rs:149:2 | | 1 | overriding `_`, which std's adaptors call in place of `next` or beside it | `Iterator::size_hint` | fixtures/reject/src/lib.rs:203:2 | @@ -291,6 +294,14 @@ The type or module each refused type or path belongs to. +
`_` 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 (3) + +- fixtures/reject/src/lib.rs:250:16: `std::iter::Iterator::filter` 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 +- fixtures/reject/src/lib.rs:250:38: `std::iter::Iterator::count` 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 +- fixtures/reject/src/lib.rs:804:6: `std::collections::HashSet::::get` 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 + +
+
a copy of a closure that changes what it captured (3) - fixtures/reject/src/lib.rs:260:14: a copy of a closure that changes what it captured @@ -330,13 +341,6 @@ The type or module each refused type or path belongs to.
-
`_` 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 (2) - -- fixtures/reject/src/lib.rs:250:16: `std::iter::Iterator::filter` 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 -- fixtures/reject/src/lib.rs:250:38: `std::iter::Iterator::count` 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 - -
-
`_`, a trait object of another crate's trait, which is not supported yet (2) - fixtures/reject/src/lib.rs:11:16: `dyn std::iter::Iterator`, a trait object of another crate's trait, which is not supported yet @@ -628,7 +632,7 @@ The type or module each refused type or path belongs to.
comparing `_` as a key, which is not supported yet (1) -- fixtures/reject/src/lib.rs:271:52: comparing `std::vec::Vec` as a key, which is not supported yet +- fixtures/reject/src/lib.rs:271:75: comparing `std::collections::BTreeSet` as a key, which is not supported yet
@@ -658,7 +662,7 @@ The type or module each refused type or path belongs to.
hashing `_` as a key, which is not supported yet (1) -- fixtures/reject/src/lib.rs:271:52: hashing `std::vec::Vec` as a key, which is not supported yet +- fixtures/reject/src/lib.rs:271:75: hashing `std::collections::BTreeSet` as a key, which is not supported yet
diff --git a/ts/fixtures/reject/expected.txt b/ts/fixtures/reject/expected.txt index 6bb0e2b1f..048261b0b 100644 --- a/ts/fixtures/reject/expected.txt +++ b/ts/fixtures/reject/expected.txt @@ -38,8 +38,8 @@ an assignment through a reference to `std::pin::Pin<&mut Noisy>` at fixtures/rej `std::iter::Iterator::count` 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 at fixtures/reject/src/lib.rs:250:38 a copy of a closure that changes what it captured at fixtures/reject/src/lib.rs:260:14 `HashSet::get` of `Option<&std::option::Option>`, whose `None` is `rt.None`, which is not supported yet at fixtures/reject/src/lib.rs:266:6 -hashing `std::vec::Vec` as a key, which is not supported yet at fixtures/reject/src/lib.rs:271:52 -comparing `std::vec::Vec` as a key, which is not supported yet at fixtures/reject/src/lib.rs:271:52 +hashing `std::collections::BTreeSet` as a key, which is not supported yet at fixtures/reject/src/lib.rs:271:75 +comparing `std::collections::BTreeSet` as a key, which is not supported yet at fixtures/reject/src/lib.rs:271:75 `HashMap::contains_key` of `std::collections::HashMap` by `u8`, which is not supported yet at fixtures/reject/src/lib.rs:289:6 unsupported call to `core::slice::::copy_within::>` at fixtures/reject/src/lib.rs:339:4 a coercion to `std::sync::Arc<[u8]>` at fixtures/reject/src/lib.rs:344:2 @@ -98,6 +98,7 @@ a constant of type `&u32`, a pointer into memory translation can't name at fixtu passing `Spent`, which drops, to `Vec::drain`, which is not supported yet at fixtures/reject/src/lib.rs:772:4 passing `Spent`, which drops, to `VecDeque::drain`, which is not supported yet at fixtures/reject/src/lib.rs:772:29 passing `Spent`, which drops, to `BTreeMap::range`, which is not supported yet at fixtures/reject/src/lib.rs:772:56 +`std::collections::HashSet::::get` 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 at fixtures/reject/src/lib.rs:804:6 overriding `Iterator::fold`, which std's adaptors call in place of `next` or beside it at fixtures/reject/src/lib.rs:149:2 an iterator of the crate implementing `DoubleEndedIterator`, which is not supported yet at fixtures/reject/src/lib.rs:169:1 overriding `Iterator::size_hint`, which std's adaptors call in place of `next` or beside it at fixtures/reject/src/lib.rs:203:2 diff --git a/ts/fixtures/reject/src/lib.rs b/ts/fixtures/reject/src/lib.rs index eea1ecfa4..a2efb2320 100644 --- a/ts/fixtures/reject/src/lib.rs +++ b/ts/fixtures/reject/src/lib.rs @@ -267,8 +267,8 @@ pub fn optional(set: &std::collections::HashSet>) -> bool { } /// A key the runtime has no `Hash` and `Eq` of. -pub fn byte_keys() -> usize { - let map: std::collections::HashMap, u8> = std::collections::HashMap::new(); +pub fn set_keys() -> usize { + let map: std::collections::HashMap, u8> = std::collections::HashMap::new(); map.len() } @@ -798,3 +798,8 @@ impl std::fmt::Write for Sink { Ok(()) } } + +/// A lookup by a byte array translates, but this one hands back the set's own key. +pub fn byte_key(set: &std::collections::HashSet<[u8; 2]>, key: [u8; 2]) -> u8 { + set.get(&key).map_or(0, |k| k[0]) +} diff --git a/ts/fixtures/reject/ts.toml b/ts/fixtures/reject/ts.toml index 63e568078..5ef02d3af 100644 --- a/ts/fixtures/reject/ts.toml +++ b/ts/fixtures/reject/ts.toml @@ -34,7 +34,7 @@ roots = [ "filter_owned_bytes", "copied_closure", "optional", - "byte_keys", + "set_keys", "borrowed", "raw_keys", "shifted", @@ -81,5 +81,6 @@ roots = [ "places_sorted", "pointer_statics", "spent_range", + "byte_key", ] printable = ["Meters"] diff --git a/ts/runtime/rt/collections.ts b/ts/runtime/rt/collections.ts index 5bb59c58b..29c7530bd 100644 --- a/ts/runtime/rt/collections.ts +++ b/ts/runtime/rt/collections.ts @@ -161,6 +161,20 @@ export function cmpBytes(a: Uint8Array | ByteSlice | ByteVec, b: Uint8Array | By return a.length < b.length ? -1 : a.length > b.length ? 1 : 0; } +/** + * `PartialEq::eq` of byte arrays, slices, or vectors: their lengths', then their bytes in + * order, of whichever form each is, as `cmpBytes` reads them. + */ +export function eqBytes(a: Uint8Array | ByteSlice | ByteVec, b: Uint8Array | ByteSlice | ByteVec): boolean { + if (a.length !== b.length) return false; + const x = a instanceof Uint8Array ? a : a.data; + const y = b instanceof Uint8Array ? b : b.data; + const i = a instanceof ByteSlice ? a.start : 0; + const j = b instanceof ByteSlice ? b.start : 0; + for (let k = 0; k < a.length; k++) if (x[i + k] !== y[j + k]) return false; + return true; +} + /** `Ord::cmp` of `Arc`s, or `PartialOrd::partial_cmp` or one of its tests: their values'. */ export function cmpArc(cmp: (a: T, b: T) => O): (a: Arc, b: Arc) => O { return (a, b) => cmp(a.value, b.value); diff --git a/ts/runtime/rt/hash.ts b/ts/runtime/rt/hash.ts index cd4ea7da1..bfbeaf27f 100644 --- a/ts/runtime/rt/hash.ts +++ b/ts/runtime/rt/hash.ts @@ -5,7 +5,7 @@ // A hash collection hashes a key that is a JS object with a faster hasher of its own, // whose values nothing sees, as std keys a map's hasher at random. -import type { None } from "../rt.ts"; +import { ByteSlice, type ByteVec, type None } from "../rt.ts"; import { type I64Limbs, type I128Limbs, type U64Limbs, type U128Limbs, type u64, u64FromHalves } from "./int.ts"; import type { Arc } from "./task.ts"; @@ -314,6 +314,17 @@ export function hashSlice(hash: (value: T, s: Hasher) => void): (value: Array }; } +/** + * `Hash::hash` of a byte array, slice, or vector, which is alike in each form: its length + * as a `usize`, then its bytes. + */ +export function hashBytes(value: Uint8Array | ByteSlice | ByteVec, s: Hasher): void { + const data = value instanceof Uint8Array ? value : value.data; + const start = value instanceof ByteSlice ? value.start : 0; + s.word(value.length, 4); + for (let i = 0; i < value.length; i++) s.word(data[start + i], 1); +} + /** `Hash::hash` of an `Option`: its discriminant as an `isize`, then its value's. */ export function hashOption(hash: (value: T, s: Hasher) => void): (value: T | undefined, s: Hasher) => void; export function hashOption(hash: (value: T, s: Hasher) => void, none: None): (value: T | None, s: Hasher) => void; diff --git a/ts/src/lower/collections.rs b/ts/src/lower/collections.rs index 75927ee44..fac41c361 100644 --- a/ts/src/lower/collections.rs +++ b/ts/src/lower/collections.rs @@ -101,7 +101,17 @@ pub fn call<'tcx>( } ("PartialEq", "eq" | "ne", ..) if let Some(t) = self_ty => { // The library maps the integers', `bool`'s, and byte slices'. + let rhs = library::targ(call.args, 1); + let slice = |t: Ty<'tcx>| matches!(t.kind(), ty::Slice(_)) && bytes(b, t); + if slice(t) && slice(rhs) { + return Err(args); + } let t = peel(b, t); + // Bytes of any form against any other, as the runtime reads each. + if bytes(b, t) && bytes(b, peel(b, rhs)) { + let eq = Expr::rt("eqBytes", args); + return Ok(Some(if name == "eq" { eq } else { eq.not() })); + } if matches!(t.kind(), ty::Bool | ty::Int(_) | ty::Uint(_)) || bytes(b, t) || !std_key(b, t) { return Err(args); } @@ -145,7 +155,20 @@ pub fn call<'tcx>( format!("`{coll}` holds `{elem}`, which is `undefined` as `None` is, which is not supported"), ))); } - if let Some(by) = lookup_ty(b, std, call, owner, name) + let by = lookup_ty(b, std, call, owner, name); + // A `[u8; N]` key is a typed array, but the reference a lookup takes is an `rt.ByteSlice`, + // so a view of its bytes stands in. A range holds the keys themselves. + if let Some(by) = by + && !matches!(name, "range" | "range_mut") + && bytes(b, peel(b, by)) + { + let key = peel(b, arg(coll, 0)); + let key = arc_of(b, key).map_or(key, |inner| peel(b, inner)); + if let (true, Some(at)) = (super::byte_array(key), args.get_mut(1)) { + *at = Expr::rt("byteSliceView", vec![std::mem::replace(at, Expr::undefined())]); + } + } + if let Some(by) = by && !same_key(b, arg(coll, 0), by) { // An `Arc`'s `Hash`, `Eq`, and `Ord` are its value's, so the value it is looked up by @@ -267,12 +290,14 @@ fn lookup_ty<'tcx>(b: &Body<'_, '_, 'tcx>, std: Std, call: &Call<'tcx>, owner: & } /// Whether a key of type `by` is a JS value that hashes, compares, and orders as one of -/// type `key` does: the same type through references and boxes, or a string either way. +/// type `key` does: the same type through references and boxes, a string either way, or +/// bytes of any form, which the runtime reads alike. fn same_key<'tcx>(b: &Body<'_, '_, 'tcx>, key: Ty<'tcx>, by: Ty<'tcx>) -> bool { let tcx = b.t.tcx; let (key, by) = (peel(b, key), peel(b, by)); tcx.erase_and_anonymize_regions(key) == tcx.erase_and_anonymize_regions(by) || (library::string(tcx, key) && library::string(tcx, by)) + || (bytes(b, key) && bytes(b, by)) } /// A key or item type of `coll` that can be `undefined` in JS, so that an `Option` of it, @@ -1413,6 +1438,7 @@ pub fn eq_fn<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, span: Span) -> Expr ) } _ if let Some(elem) = elements_of(b, ty) => Expr::rt("eqSlice", vec![eq_fn(b, elem, span)]), + _ if bytes(b, ty) => Expr::Rt("eqBytes".into()), ty::Adt(..) if let Some(inner) = arc_of(b, ty) => { let mut values = vec![eq_fn(b, inner, span)]; // std's `Arc` finds itself equal before comparing a value that is `Eq`. @@ -1463,6 +1489,8 @@ fn hash_fn<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, hasher: Ty<'tcx>, spa } Expr::rt("hashSlice", vec![hash_fn(b, elem, hasher, span)]) } + // Its length, then its bytes, whichever form it is: a `[u8; N]` hashes as a slice. + _ if bytes(b, ty) => Expr::Rt("hashBytes".into()), ty::Adt(adt, _) if b.t.own(adt.did()) => { let hash = trait_fn(tcx, "Hash", "hash"); b.fn_value(hash, tcx.mk_args(&[ty.into(), hasher.into()]), span) diff --git a/ts/src/lower/mir/call.rs b/ts/src/lower/mir/call.rs index 5ab467fa7..fedae6f51 100644 --- a/ts/src/lower/mir/call.rs +++ b/ts/src/lower/mir/call.rs @@ -164,12 +164,21 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { return false; } let self_ty = gargs.types().next(); + // A lookup only reads the key it is given, so only what it returns can hold a slice. + let mut lookup = false; // Mappings that make the slice, or take it. if let Some((owner, name)) = library::key(tcx, def_id) { let handled = match (owner.as_str(), name.as_str()) { ("Index" | "IndexMut", "index" | "index_mut") | ("Clone", "clone") | ("Argument", _) => true, - // The runtime compares bytes in whichever form it is given them. - ("PartialOrd" | "Ord", _) => true, + // The runtime compares and hashes bytes in whichever form it is given them. + ("PartialOrd" | "Ord" | "PartialEq", _) | ("Hash", "hash") => true, + ( + "HashMap" | "HashSet" | "BTreeMap" | "BTreeSet", + "get" | "get_mut" | "contains_key" | "contains" | "remove" | "take", + ) => { + lookup = true; + false + } ("slice", "iter" | "iter_mut") => true, ("IntoIterator", "into_iter") => self_ty.is_some_and(|s| elem(tcx, s).is_some()), _ => false, @@ -205,6 +214,9 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { item => std::iter::once(ty).chain(item).any(holds_bytes_ref), } }; + if lookup { + return refs(sig.output()); + } sig.inputs_and_output.iter().any(refs) || gargs.types().any(refs) } From 12f06002c569f07371552c73081cacca405ec216 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 30 Oct 2026 04:34:59 -0700 Subject: [PATCH 3/5] fix(ts): view a byte-array key's lookup only where the key is held as the array Co-Authored-By: Claude Opus 5.5 --- ts/fixtures/lang/expected/keys.ts | 116 +++++++++++++++++------------- ts/fixtures/lang/src/keys.rs | 6 ++ ts/src/lower/collections.rs | 7 +- 3 files changed, 77 insertions(+), 52 deletions(-) diff --git a/ts/fixtures/lang/expected/keys.ts b/ts/fixtures/lang/expected/keys.ts index 70f974a4f..851681a27 100644 --- a/ts/fixtures/lang/expected/keys.ts +++ b/ts/fixtures/lang/expected/keys.ts @@ -901,65 +901,77 @@ export function bytes(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { const nested: rt.HashMap<[Uint8Array | undefined, rt.ByteVec], rt.usize> = rt.hashNew(rt.hashing(rt.hashTuple([rt.hashOption(rt.hashBytes), rt.hashBytes]), rt.eqTuple([rt.eqOption(rt.eqBytes), rt.eqBytes]))) as rt.HashMap<[Uint8Array | undefined, rt.ByteVec], rt.usize>; const shared: rt.HashMap, rt.usize> = rt.hashNew(rt.hashing(rt.hashArc(rt.hashBytes), rt.eqArc(rt.eqBytes, true))) as rt.HashMap, rt.usize>; const arcs_: rt.HashMap, rt.usize> = rt.hashNew(rt.hashing(rt.hashArc(rt.hashBytes), rt.eqArc(rt.eqBytes, true))) as rt.HashMap, rt.usize>; + const boxed: rt.HashMap = rt.hashNew(rt.hashing(rt.hashBytes, rt.eqBytes)) as rt.HashMap; const tree: rt.BTreeMap = rt.btreeNew(rt.cmpBytes) as rt.BTreeMap; const ordered: rt.BTreeSet = rt.btreeNew(rt.cmpBytes) as rt.BTreeSet; + const _151: rt.Iter = rt.iterMap(rt.iterSlice(rt.iterToArray(rt.iterMap(rt.byteSliceWindows(data, 2), (w: rt.ByteSlice): Uint8Array => { + rt.idx(w, 0); + const _3: rt.u8 = rt.u8Rem(w.data[w.start], 4); + rt.idx(w, 1); + return Uint8Array.of(_3, rt.u8Rem(w.data[w.start + 1], 4)); + }))), rt.byteSliceFrom); + const borrowed: rt.HashSet = rt.hashExtendKeys(rt.hashNew(rt.hashing(rt.hashBytes, rt.eqBytes)) as rt.HashSet, _151); const iter2: rt.Iter<[rt.usize, rt.ByteSlice]> = rt.iterEnumerate(rt.byteSliceWindows(data, 2)); while (true) { - const _150: [rt.usize, rt.ByteSlice] | rt.Done = iter2.next(); - if (_150 === rt.DONE) { + const _158: [rt.usize, rt.ByteSlice] | rt.Done = iter2.next(); + if (_158 === rt.DONE) { break; } - const i: rt.usize = _150[0]; - rt.idx(_150[1], 0); - const _156: rt.u8 = rt.u8Rem(_150[1].data[_150[1].start], 4); - rt.idx(_150[1], 1); - const key: Uint8Array = Uint8Array.of(_156, rt.u8Rem(_150[1].data[_150[1].start + 1], 4)); + const i: rt.usize = _158[0]; + rt.idx(_158[1], 0); + const _164: rt.u8 = rt.u8Rem(_158[1].data[_158[1].start], 4); + rt.idx(_158[1], 1); + const key: Uint8Array = Uint8Array.of(_164, rt.u8Rem(_158[1].data[_158[1].start + 1], 4)); const s: rt.ByteSlice = rt.byteSliceRange(data, i, rt.min(rt.usizeAdd(i, rt.usizeRem(kind as rt.usize, 3)), data.length)); rt.byteVecPush(out, mapOr$02k4ito(rt.hashInsert(arrays, copy$u8s(key), i), 204, (old: rt.usize): rt.u8 => { return old & 0xff; })); - const _185: rt.ByteVec = rt.byteSliceToByteVec(s); - rt.byteVecPush(out, mapOr$02k4ito(rt.hashInsert(vecs, _185, i), 204, (old2: rt.usize): rt.u8 => { + const _193: rt.ByteVec = rt.byteSliceToByteVec(s); + rt.byteVecPush(out, mapOr$02k4ito(rt.hashInsert(vecs, _193, i), 204, (old2: rt.usize): rt.u8 => { return old2 & 0xff; })); rt.byteVecPush(out, mapOr$02k4ito(rt.hashInsert(slices, s, i), 204, (old3: rt.usize): rt.u8 => { return old3 & 0xff; })); - rt.idx(_150[1], 0); - const opt: Uint8Array | undefined = thenSome$0rsp0eu(rt.u8Rem(_150[1].data[_150[1].start], 3) !== 0, key); - const _203: rt.ByteVec = rt.byteSliceToByteVec(s); - const _202: [Uint8Array | undefined, rt.ByteVec] = [copy$Option_u8s(opt), _203]; - rt.byteVecPush(out, mapOr$02k4ito(rt.hashInsert(nested, _202, i), 204, (old4: rt.usize): rt.u8 => { + rt.idx(_158[1], 0); + const opt: Uint8Array | undefined = thenSome$0rsp0eu(rt.u8Rem(_158[1].data[_158[1].start], 3) !== 0, key); + const _211: rt.ByteVec = rt.byteSliceToByteVec(s); + const _210: [Uint8Array | undefined, rt.ByteVec] = [copy$Option_u8s(opt), _211]; + rt.byteVecPush(out, mapOr$02k4ito(rt.hashInsert(nested, _210, i), 204, (old4: rt.usize): rt.u8 => { return old4 & 0xff; })); - const _208: rt.Arc = new rt.Arc(rt.byteSliceToByteVec(s)); - rt.byteVecPush(out, mapOr$02k4ito(rt.hashInsert(shared, _208, i, rt.drop), 204, (old5: rt.usize): rt.u8 => { + const _216: rt.Arc = new rt.Arc(rt.byteSliceToByteVec(s)); + rt.byteVecPush(out, mapOr$02k4ito(rt.hashInsert(shared, _216, i, rt.drop), 204, (old5: rt.usize): rt.u8 => { return old5 & 0xff; })); - const _214: rt.Arc = new rt.Arc(copy$u8s(key)); - rt.byteVecPush(out, mapOr$02k4ito(rt.hashInsert(arcs_, _214, i, rt.drop), 204, (old6: rt.usize): rt.u8 => { + const _222: rt.Arc = new rt.Arc(copy$u8s(key)); + rt.byteVecPush(out, mapOr$02k4ito(rt.hashInsert(arcs_, _222, i, rt.drop), 204, (old6: rt.usize): rt.u8 => { return old6 & 0xff; })); - rt.byteVecPush(out, mapOr$02k4ito(rt.btreeInsert(tree, copy$u8s(key), i), 204, (old7: rt.usize): rt.u8 => { + const _227: Uint8Array = copy$u8s(key); + rt.byteVecPush(out, mapOr$02k4ito(rt.hashInsert(boxed, _227, i), 204, (old7: rt.usize): rt.u8 => { return old7 & 0xff; })); - const _223: rt.ByteVec = rt.byteSliceToByteVec(s); - rt.byteVecPush(out, rt.btreeAdd(ordered, _223) ? 1 : 0); + rt.byteVecPush(out, mapOr$02k4ito(rt.btreeInsert(tree, copy$u8s(key), i), 204, (old8: rt.usize): rt.u8 => { + return old8 & 0xff; + })); + const _236: rt.ByteVec = rt.byteSliceToByteVec(s); + rt.byteVecPush(out, rt.btreeAdd(ordered, _236) ? 1 : 0); } const iter3: rt.RangeInclusive = new rt.RangeInclusive(0, cut, rt.usizeStep); l5: while (true) { - const _227: rt.usize | rt.Done = iter3.next(); - if (_227 === rt.DONE) { + const _240: rt.usize | rt.Done = iter3.next(); + if (_240 === rt.DONE) { break; } - const i2: rt.usize = _227; + const i2: rt.usize = _240; const iter4: rt.RangeInclusive = new rt.RangeInclusive(i2, cut, rt.usizeStep); while (true) { - const _234: rt.usize | rt.Done = iter4.next(); - if (_234 === rt.DONE) { + const _247: rt.usize | rt.Done = iter4.next(); + if (_247 === rt.DONE) { continue l5; } - const j: rt.usize = _234; + const j: rt.usize = _247; const s2: rt.ByteSlice = rt.byteSliceRange(data, i2, j); rt.byteVecPush(out, mapOr$02k4ito(rt.hashGet(vecs, s2), 221, (v: rt.usize): rt.u8 => { return v & 0xff; @@ -967,13 +979,13 @@ export function bytes(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { rt.byteVecPush(out, mapOr$02k4ito(rt.hashGet(slices, s2), 221, (v2: rt.usize): rt.u8 => { return v2 & 0xff; })); - const _253: rt.ByteVec = rt.byteSliceToByteVec(s2); - rt.byteVecPush(out, rt.hashContains(vecs, _253) ? 1 : 0); - const _259: rt.Arc = new rt.Arc(rt.byteSliceToByteVec(s2)); - rt.byteVecPush(out, mapOr$02k4ito(rt.hashGet(shared, _259), 221, (v3: rt.usize): rt.u8 => { + const _266: rt.ByteVec = rt.byteSliceToByteVec(s2); + rt.byteVecPush(out, rt.hashContains(vecs, _266) ? 1 : 0); + const _272: rt.Arc = new rt.Arc(rt.byteSliceToByteVec(s2)); + rt.byteVecPush(out, mapOr$02k4ito(rt.hashGet(shared, _272), 221, (v3: rt.usize): rt.u8 => { return v3 & 0xff; })); - _259[Symbol.dispose](); + _272[Symbol.dispose](); const key2: Uint8Array = Uint8Array.of(mapOr$1surhce(rt.byteSliceGet(data, i2), 0, (b2: rt.u8): rt.u8 => { return rt.u8Rem(b2, 4); }), mapOr$1surhce(rt.byteSliceGet(data, j), 0, (b3: rt.u8): rt.u8 => { @@ -985,41 +997,45 @@ export function bytes(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { rt.byteVecPush(out, mapOr$02k4ito(rt.hashGet(arcs_, new rt.Arc(rt.byteSliceView(rt.byteSliceFrom(key2)))), 221, (v5: rt.usize): rt.u8 => { return v5 & 0xff; })); - rt.byteVecPush(out, mapOr$02k4ito(rt.btreeGet(tree, rt.byteSliceView(rt.byteSliceFrom(key2))), 221, (v6: rt.usize): rt.u8 => { + rt.byteVecPush(out, mapOr$02k4ito(rt.hashGet(boxed, rt.byteSliceView(rt.byteSliceFrom(key2))), 221, (v6: rt.usize): rt.u8 => { return v6 & 0xff; })); + rt.byteVecPush(out, mapOr$02k4ito(rt.btreeGet(tree, rt.byteSliceView(rt.byteSliceFrom(key2))), 221, (v7: rt.usize): rt.u8 => { + return v7 & 0xff; + })); + rt.byteVecPush(out, rt.hashContains(borrowed, rt.byteSliceFrom(key2)) ? 1 : 0); rt.byteVecPush(out, rt.btreeContains(ordered, s2) ? 1 : 0); if (s2.length >= 2) { const two: rt.ByteSlice = rt.byteSliceRange(s2, 0, 2); - rt.byteVecPush(out, mapOr$02k4ito(rt.hashGet(arrays, rt.byteSliceView(two)), 221, (v7: rt.usize): rt.u8 => { - return v7 & 0xff; - })); - rt.byteVecPush(out, mapOr$02k4ito(rt.btreeGet(tree, rt.byteSliceView(two)), 221, (v8: rt.usize): rt.u8 => { + rt.byteVecPush(out, mapOr$02k4ito(rt.hashGet(arrays, rt.byteSliceView(two)), 221, (v8: rt.usize): rt.u8 => { return v8 & 0xff; })); + rt.byteVecPush(out, mapOr$02k4ito(rt.btreeGet(tree, rt.byteSliceView(two)), 221, (v9: rt.usize): rt.u8 => { + return v9 & 0xff; + })); } - const _302: [Uint8Array | undefined, rt.ByteVec] = [key2, rt.byteSliceToByteVec(s2)]; - rt.byteVecPush(out, mapOr$02k4ito(rt.hashGet(nested, _302), 221, (v9: rt.usize): rt.u8 => { - return v9 & 0xff; - })); - const _310: [Uint8Array | undefined, rt.ByteVec] = [undefined, rt.byteSliceToByteVec(s2)]; - rt.byteVecPush(out, mapOr$02k4ito(rt.hashGet(nested, _310), 221, (v10: rt.usize): rt.u8 => { + const _326: [Uint8Array | undefined, rt.ByteVec] = [key2, rt.byteSliceToByteVec(s2)]; + rt.byteVecPush(out, mapOr$02k4ito(rt.hashGet(nested, _326), 221, (v10: rt.usize): rt.u8 => { return v10 & 0xff; })); + const _334: [Uint8Array | undefined, rt.ByteVec] = [undefined, rt.byteSliceToByteVec(s2)]; + rt.byteVecPush(out, mapOr$02k4ito(rt.hashGet(nested, _334), 221, (v11: rt.usize): rt.u8 => { + return v11 & 0xff; + })); } } - const _317: rt.ByteSlice = rt.byteSliceRange(rt.byteSliceFrom(Uint8Array.of()), 0); - rt.byteVecPush(out, mapOr$02k4ito(rt.hashRemove(vecs, _317), 238, (v11: rt.usize): rt.u8 => { - return v11 & 0xff; + const _341: rt.ByteSlice = rt.byteSliceRange(rt.byteSliceFrom(Uint8Array.of()), 0); + rt.byteVecPush(out, mapOr$02k4ito(rt.hashRemove(vecs, _341), 238, (v12: rt.usize): rt.u8 => { + return v12 & 0xff; })); rt.byteVecPush(out, rt.hashLen(vecs) & 0xff); - const _324: rt.Iter = rt.iterMap(rt.byteSliceChunks(data, 2), (a0: rt.ByteSlice): rt.ByteVec => { + const _348: rt.Iter = rt.iterMap(rt.byteSliceChunks(data, 2), (a0: rt.ByteSlice): rt.ByteVec => { return rt.byteSliceToByteVec(a0); }); - const set: rt.HashSet = rt.hashExtendKeys(rt.hashNew(rt.hashing(rt.hashBytes, rt.eqBytes)) as rt.HashSet, _324); + const set: rt.HashSet = rt.hashExtendKeys(rt.hashNew(rt.hashing(rt.hashBytes, rt.eqBytes)) as rt.HashSet, _348); rt.byteVecPush(out, rt.hashLen(set) & 0xff); - const _334: rt.ByteSlice = rt.byteSliceRange(data, 0, rt.min(data.length, 2)); - rt.byteVecPush(out, rt.hashContains(set, _334) ? 1 : 0); + const _358: rt.ByteSlice = rt.byteSliceRange(data, 0, rt.min(data.length, 2)); + rt.byteVecPush(out, rt.hashContains(set, _358) ? 1 : 0); rt.hashDrop(arcs_, rt.drop); rt.hashDrop(shared, rt.drop); } diff --git a/ts/fixtures/lang/src/keys.rs b/ts/fixtures/lang/src/keys.rs index 8545104e7..d883a49a8 100644 --- a/ts/fixtures/lang/src/keys.rs +++ b/ts/fixtures/lang/src/keys.rs @@ -643,8 +643,11 @@ pub fn bytes(kind: u8, data: &[u8], out: &mut Vec) { let mut nested: HashMap<(Option<[u8; 2]>, Vec), usize> = HashMap::new(); let mut shared: HashMap>, usize> = HashMap::new(); let mut arcs: HashMap, usize> = HashMap::new(); + let mut boxed: HashMap, usize> = HashMap::new(); let mut tree: BTreeMap<[u8; 2], usize> = BTreeMap::new(); let mut ordered: BTreeSet> = BTreeSet::new(); + let pairs: Vec<[u8; 2]> = data.windows(2).map(|w| [w[0] % 4, w[1] % 4]).collect(); + let borrowed: HashSet<&[u8; 2]> = pairs.iter().collect(); for (i, w) in data.windows(2).enumerate() { let key = [w[0] % 4, w[1] % 4]; let s = &data[i..(i + kind as usize % 3).min(data.len())]; @@ -655,6 +658,7 @@ pub fn bytes(kind: u8, data: &[u8], out: &mut Vec) { out.push(nested.insert((opt, s.to_vec()), i).map_or(0xcc, |old| old as u8)); out.push(shared.insert(Arc::new(s.to_vec()), i).map_or(0xcc, |old| old as u8)); out.push(arcs.insert(Arc::new(key), i).map_or(0xcc, |old| old as u8)); + out.push(boxed.insert(Box::new(key), i).map_or(0xcc, |old| old as u8)); out.push(tree.insert(key, i).map_or(0xcc, |old| old as u8)); out.push(ordered.insert(s.to_vec()) as u8); } @@ -668,7 +672,9 @@ pub fn bytes(kind: u8, data: &[u8], out: &mut Vec) { let key = [data.get(i).map_or(0, |b| b % 4), data.get(j).map_or(0, |b| b % 4)]; out.push(arrays.get(&key).map_or(0xdd, |v| *v as u8)); out.push(arcs.get(&key).map_or(0xdd, |v| *v as u8)); + out.push(boxed.get(&key).map_or(0xdd, |v| *v as u8)); out.push(tree.get(&key).map_or(0xdd, |v| *v as u8)); + out.push(borrowed.contains(&&key) as u8); out.push(ordered.contains(s) as u8); if s.len() >= 2 { let two = &s[..2]; diff --git a/ts/src/lower/collections.rs b/ts/src/lower/collections.rs index fac41c361..2d3eb360a 100644 --- a/ts/src/lower/collections.rs +++ b/ts/src/lower/collections.rs @@ -162,8 +162,11 @@ pub fn call<'tcx>( && !matches!(name, "range" | "range_mut") && bytes(b, peel(b, by)) { - let key = peel(b, arg(coll, 0)); - let key = arc_of(b, key).map_or(key, |inner| peel(b, inner)); + // The key as held, through boxes and `Arc`s: a reference to an array is a slice already. + let mut key = arg(coll, 0); + while let Some(Std::Box | Std::Arc) = b.t.std_of(key) { + key = arg(key, 0); + } if let (true, Some(at)) = (super::byte_array(key), args.get_mut(1)) { *at = Expr::rt("byteSliceView", vec![std::mem::replace(at, Expr::undefined())]); } From 8ad812d4b17ff51a13ed108d1ad4459725434d17 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 30 Oct 2026 04:53:33 -0700 Subject: [PATCH 4/5] fix(ts): view only a lookup's slice, and keep Borrow to the same key Co-Authored-By: Claude Opus 5.5 --- ts/fixtures/lang/expected/keys.ts | 40 +++++++++++++++++-------------- ts/fixtures/lang/src/keys.rs | 1 + ts/src/lower/collections.rs | 16 +++++++------ 3 files changed, 32 insertions(+), 25 deletions(-) diff --git a/ts/fixtures/lang/expected/keys.ts b/ts/fixtures/lang/expected/keys.ts index 851681a27..0533ac71e 100644 --- a/ts/fixtures/lang/expected/keys.ts +++ b/ts/fixtures/lang/expected/keys.ts @@ -1000,42 +1000,46 @@ export function bytes(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { rt.byteVecPush(out, mapOr$02k4ito(rt.hashGet(boxed, rt.byteSliceView(rt.byteSliceFrom(key2))), 221, (v6: rt.usize): rt.u8 => { return v6 & 0xff; })); - rt.byteVecPush(out, mapOr$02k4ito(rt.btreeGet(tree, rt.byteSliceView(rt.byteSliceFrom(key2))), 221, (v7: rt.usize): rt.u8 => { + const _299: Uint8Array = copy$u8s(key2); + rt.byteVecPush(out, mapOr$02k4ito(rt.hashGet(boxed, _299), 221, (v7: rt.usize): rt.u8 => { return v7 & 0xff; })); + rt.byteVecPush(out, mapOr$02k4ito(rt.btreeGet(tree, rt.byteSliceView(rt.byteSliceFrom(key2))), 221, (v8: rt.usize): rt.u8 => { + return v8 & 0xff; + })); rt.byteVecPush(out, rt.hashContains(borrowed, rt.byteSliceFrom(key2)) ? 1 : 0); rt.byteVecPush(out, rt.btreeContains(ordered, s2) ? 1 : 0); if (s2.length >= 2) { const two: rt.ByteSlice = rt.byteSliceRange(s2, 0, 2); - rt.byteVecPush(out, mapOr$02k4ito(rt.hashGet(arrays, rt.byteSliceView(two)), 221, (v8: rt.usize): rt.u8 => { - return v8 & 0xff; - })); - rt.byteVecPush(out, mapOr$02k4ito(rt.btreeGet(tree, rt.byteSliceView(two)), 221, (v9: rt.usize): rt.u8 => { + rt.byteVecPush(out, mapOr$02k4ito(rt.hashGet(arrays, rt.byteSliceView(two)), 221, (v9: rt.usize): rt.u8 => { return v9 & 0xff; })); + rt.byteVecPush(out, mapOr$02k4ito(rt.btreeGet(tree, rt.byteSliceView(two)), 221, (v10: rt.usize): rt.u8 => { + return v10 & 0xff; + })); } - const _326: [Uint8Array | undefined, rt.ByteVec] = [key2, rt.byteSliceToByteVec(s2)]; - rt.byteVecPush(out, mapOr$02k4ito(rt.hashGet(nested, _326), 221, (v10: rt.usize): rt.u8 => { - return v10 & 0xff; - })); - const _334: [Uint8Array | undefined, rt.ByteVec] = [undefined, rt.byteSliceToByteVec(s2)]; - rt.byteVecPush(out, mapOr$02k4ito(rt.hashGet(nested, _334), 221, (v11: rt.usize): rt.u8 => { + const _332: [Uint8Array | undefined, rt.ByteVec] = [key2, rt.byteSliceToByteVec(s2)]; + rt.byteVecPush(out, mapOr$02k4ito(rt.hashGet(nested, _332), 221, (v11: rt.usize): rt.u8 => { return v11 & 0xff; })); + const _340: [Uint8Array | undefined, rt.ByteVec] = [undefined, rt.byteSliceToByteVec(s2)]; + rt.byteVecPush(out, mapOr$02k4ito(rt.hashGet(nested, _340), 221, (v12: rt.usize): rt.u8 => { + return v12 & 0xff; + })); } } - const _341: rt.ByteSlice = rt.byteSliceRange(rt.byteSliceFrom(Uint8Array.of()), 0); - rt.byteVecPush(out, mapOr$02k4ito(rt.hashRemove(vecs, _341), 238, (v12: rt.usize): rt.u8 => { - return v12 & 0xff; + const _347: rt.ByteSlice = rt.byteSliceRange(rt.byteSliceFrom(Uint8Array.of()), 0); + rt.byteVecPush(out, mapOr$02k4ito(rt.hashRemove(vecs, _347), 238, (v13: rt.usize): rt.u8 => { + return v13 & 0xff; })); rt.byteVecPush(out, rt.hashLen(vecs) & 0xff); - const _348: rt.Iter = rt.iterMap(rt.byteSliceChunks(data, 2), (a0: rt.ByteSlice): rt.ByteVec => { + const _354: rt.Iter = rt.iterMap(rt.byteSliceChunks(data, 2), (a0: rt.ByteSlice): rt.ByteVec => { return rt.byteSliceToByteVec(a0); }); - const set: rt.HashSet = rt.hashExtendKeys(rt.hashNew(rt.hashing(rt.hashBytes, rt.eqBytes)) as rt.HashSet, _348); + const set: rt.HashSet = rt.hashExtendKeys(rt.hashNew(rt.hashing(rt.hashBytes, rt.eqBytes)) as rt.HashSet, _354); rt.byteVecPush(out, rt.hashLen(set) & 0xff); - const _358: rt.ByteSlice = rt.byteSliceRange(data, 0, rt.min(data.length, 2)); - rt.byteVecPush(out, rt.hashContains(set, _358) ? 1 : 0); + const _364: rt.ByteSlice = rt.byteSliceRange(data, 0, rt.min(data.length, 2)); + rt.byteVecPush(out, rt.hashContains(set, _364) ? 1 : 0); rt.hashDrop(arcs_, rt.drop); rt.hashDrop(shared, rt.drop); } diff --git a/ts/fixtures/lang/src/keys.rs b/ts/fixtures/lang/src/keys.rs index d883a49a8..f8f13b2a0 100644 --- a/ts/fixtures/lang/src/keys.rs +++ b/ts/fixtures/lang/src/keys.rs @@ -673,6 +673,7 @@ pub fn bytes(kind: u8, data: &[u8], out: &mut Vec) { out.push(arrays.get(&key).map_or(0xdd, |v| *v as u8)); out.push(arcs.get(&key).map_or(0xdd, |v| *v as u8)); out.push(boxed.get(&key).map_or(0xdd, |v| *v as u8)); + out.push(boxed.get(&Box::new(key)).map_or(0xdd, |v| *v as u8)); out.push(tree.get(&key).map_or(0xdd, |v| *v as u8)); out.push(borrowed.contains(&&key) as u8); out.push(ordered.contains(s) as u8); diff --git a/ts/src/lower/collections.rs b/ts/src/lower/collections.rs index 2d3eb360a..a13f3c465 100644 --- a/ts/src/lower/collections.rs +++ b/ts/src/lower/collections.rs @@ -156,11 +156,13 @@ pub fn call<'tcx>( ))); } let by = lookup_ty(b, std, call, owner, name); - // A `[u8; N]` key is a typed array, but the reference a lookup takes is an `rt.ByteSlice`, - // so a view of its bytes stands in. A range holds the keys themselves. + // A `[u8; N]` key is a typed array, but a reference to an array or a slice, as a lookup + // takes one, is an `rt.ByteSlice`, so a view of its bytes stands in. A range holds the + // keys themselves. if let Some(by) = by && !matches!(name, "range" | "range_mut") - && bytes(b, peel(b, by)) + && matches!(by.kind(), ty::Array(..) | ty::Slice(_)) + && bytes(b, by) { // The key as held, through boxes and `Arc`s: a reference to an array is a slice already. let mut key = arg(coll, 0); @@ -171,8 +173,10 @@ pub fn call<'tcx>( *at = Expr::rt("byteSliceView", vec![std::mem::replace(at, Expr::undefined())]); } } + // Bytes of any form hash and compare alike, so each finds the others. + let alike = |key: Ty<'tcx>, by: Ty<'tcx>| same_key(b, key, by) || (bytes(b, peel(b, key)) && bytes(b, peel(b, by))); if let Some(by) = by - && !same_key(b, arg(coll, 0), by) + && !alike(arg(coll, 0), by) { // An `Arc`'s `Hash`, `Eq`, and `Ord` are its value's, so the value it is looked up by // stands in as an `Arc` of it, which the collection only compares. @@ -293,14 +297,12 @@ fn lookup_ty<'tcx>(b: &Body<'_, '_, 'tcx>, std: Std, call: &Call<'tcx>, owner: & } /// Whether a key of type `by` is a JS value that hashes, compares, and orders as one of -/// type `key` does: the same type through references and boxes, a string either way, or -/// bytes of any form, which the runtime reads alike. +/// type `key` does: the same type through references and boxes, or a string either way. fn same_key<'tcx>(b: &Body<'_, '_, 'tcx>, key: Ty<'tcx>, by: Ty<'tcx>) -> bool { let tcx = b.t.tcx; let (key, by) = (peel(b, key), peel(b, by)); tcx.erase_and_anonymize_regions(key) == tcx.erase_and_anonymize_regions(by) || (library::string(tcx, key) && library::string(tcx, by)) - || (bytes(b, key) && bytes(b, by)) } /// A key or item type of `coll` that can be `undefined` in JS, so that an `Option` of it, From 547669714c4d487a5b2cfa58710cb0f0e7ad935c Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 05:08:13 -0700 Subject: [PATCH 5/5] test(ts): regenerate the reject census after merging main Co-Authored-By: Claude Opus 5.5 --- ts/fixtures/reject/census.md | 38 ++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/ts/fixtures/reject/census.md b/ts/fixtures/reject/census.md index 022ab2901..6363d9ecd 100644 --- a/ts/fixtures/reject/census.md +++ b/ts/fixtures/reject/census.md @@ -1,7 +1,7 @@ # Census of `reject-fixture` -- Refusals: 109 -- Function instances: 126 +- Refusals: 110 +- Function instances: 128 - Generic functions no instance reached, lowered over their type parameters: 10 ## By construct @@ -12,13 +12,13 @@ | 5 | passing `_`, which drops, to `_`, which is not supported yet | fixtures/reject/src/lib.rs:372:4 | | 4 | a variant of `_`, which is not supported: either is the same JS string | fixtures/reject/src/lib.rs:377:2 | | 3 | `_` of `_`, whose `None` is `rt.None`, which is not supported yet | fixtures/reject/src/lib.rs:41:11 | +| 3 | `_` 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 | fixtures/reject/src/lib.rs:242:16 | | 3 | a copy of a closure that changes what it captured | fixtures/reject/src/lib.rs:252:14 | | 3 | a raw pointer to `_` in `_`, which the crate may put another object in place of | fixtures/reject/src/lib.rs:661:10 | | 3 | an assignment through a reference to `_` | fixtures/reject/src/lib.rs:102:2 | | 3 | replacing the value behind a reference to `_` | fixtures/reject/src/lib.rs:110:2 | | 3 | unsupported type `_` | fixtures/reject/src/lib.rs:7:14 | | 2 | `_` is not supported: its `None` and `Some` would both be `rt.None` | fixtures/reject/src/lib.rs:16:15 | -| 2 | `_` 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 | fixtures/reject/src/lib.rs:242:16 | | 2 | `_`, a trait object of another crate's trait, which is not supported yet | fixtures/reject/src/lib.rs:11:16 | | 2 | `_`, a trait object of the generic trait `_`, which is not supported yet | fixtures/reject/src/lib.rs:510:23 | | 2 | a coercion to `_`, which is not supported yet | fixtures/reject/src/lib.rs:501:40 | @@ -64,12 +64,12 @@ | 1 | an upcast from `_` to `_`, which is not supported yet | fixtures/reject/src/lib.rs:585:32 | | 1 | arithmetic on `_` | fixtures/reject/src/lib.rs:8:2 | | 1 | collecting `_`, whose `None` is `rt.None`, which is not supported yet | fixtures/reject/src/lib.rs:36:43 | -| 1 | comparing `_` as a key, which is not supported yet | fixtures/reject/src/lib.rs:263:52 | +| 1 | comparing `_` as a key, which is not supported yet | fixtures/reject/src/lib.rs:263:75 | | 1 | comparing pointers to `()`, which has no bytes and so no address of its own | fixtures/reject/src/lib.rs:717:2 | | 1 | comparing pointers to `_` in an atomic, which JS has no address of | fixtures/reject/src/lib.rs:771:15 | | 1 | comparing pointers to `_`, each a place JS makes of its own | fixtures/reject/src/lib.rs:649:2 | | 1 | constructing `_` | fixtures/reject/src/lib.rs:395:53 | -| 1 | hashing `_` as a key, which is not supported yet | fixtures/reject/src/lib.rs:263:52 | +| 1 | hashing `_` as a key, which is not supported yet | fixtures/reject/src/lib.rs:263:75 | | 1 | hashing `_` through `_`'s own `Hash::hash_slice`, which is not supported yet | fixtures/reject/src/lib.rs:355:16 | | 1 | union `_`, whose fields overlap in `_` and `f64`, which JS keeps apart | fixtures/reject/src/lib.rs:635:1 | | 1 | unsupported type `_` from another crate | fixtures/reject/src/lib.rs:484:32 | @@ -87,7 +87,6 @@ The type or module each refused type or path belongs to. | 4 | `(u32, u32)` | fixtures/reject/src/lib.rs:661:10 | | 4 | `Iterator` | fixtures/reject/src/lib.rs:41:11 | | 4 | `std::pin::Pin` | fixtures/reject/src/lib.rs:110:2 | -| 4 | `std::vec::Vec` | fixtures/reject/src/lib.rs:263:52 | | 4 | `{closure}` | fixtures/reject/src/lib.rs:364:13 | | 3 | `Spent` | fixtures/reject/src/lib.rs:797:4 | | 3 | `std::cell::UnsafeCell` | fixtures/reject/src/lib.rs:685:15 | @@ -99,6 +98,7 @@ The type or module each refused type or path belongs to. | 2 | `dyn std::any::Any` | fixtures/reject/src/lib.rs:585:32 | | 2 | `dyn std::iter::Iterator` | fixtures/reject/src/lib.rs:11:16 | | 2 | `std::any::TypeId` | fixtures/reject/src/lib.rs:591:2 | +| 2 | `std::collections::BTreeSet` | fixtures/reject/src/lib.rs:263:75 | | 2 | `std::collections::HashMap` | fixtures/reject/src/lib.rs:53:13 | | 2 | `std::iter::Iterator` | fixtures/reject/src/lib.rs:242:16 | | 2 | `std::iter::Map` | fixtures/reject/src/lib.rs:155:23 | @@ -107,6 +107,7 @@ The type or module each refused type or path belongs to. | 2 | `std::std` | fixtures/reject/src/lib.rs:620:11 | | 2 | `std::sync::MutexGuard` | fixtures/reject/src/lib.rs:102:2 | | 2 | `std::vec::IntoIter` | fixtures/reject/src/lib.rs:475:17 | +| 2 | `std::vec::Vec` | fixtures/reject/src/lib.rs:355:16 | | 1 | `BTreeMap` | fixtures/reject/src/lib.rs:797:56 | | 1 | `Batched` | fixtures/reject/src/lib.rs:355:16 | | 1 | `BinaryHeap` | fixtures/reject/src/lib.rs:755:7 | @@ -139,6 +140,7 @@ The type or module each refused type or path belongs to. | 1 | `rt.Poll` | fixtures/reject/src/lib.rs:548:2 | | 1 | `std::any::Any` | fixtures/reject/src/lib.rs:591:4 | | 1 | `std::char::ParseCharError` | fixtures/reject/src/lib.rs:484:32 | +| 1 | `std::collections::HashSet` | fixtures/reject/src/lib.rs:835:6 | | 1 | `std::iter::Chain` | fixtures/reject/src/lib.rs:182:51 | | 1 | `std::iter::Rev` | fixtures/reject/src/lib.rs:176:14 | | 1 | `std::option::Option` | fixtures/reject/src/lib.rs:415:2 | @@ -179,6 +181,7 @@ The type or module each refused type or path belongs to. | 1 | `_` of `_`, whose `None` is `rt.None`, which is not supported yet | `BinaryHeap::pop`, `Option` | fixtures/reject/src/lib.rs:755:7 | | 1 | `_` of `_`, whose `None` is `rt.None`, which is not supported yet | `HashSet::get`, `Option` | fixtures/reject/src/lib.rs:258:6 | | 1 | `_` of `_`, whose `None` is `rt.None`, which is not supported yet | `Iterator::find_map`, `Option` | fixtures/reject/src/lib.rs:41:11 | +| 1 | `_` 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 | `std::collections::HashSet::get` | fixtures/reject/src/lib.rs:835:6 | | 1 | `_` 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 | `std::iter::Iterator::count` | fixtures/reject/src/lib.rs:242:38 | | 1 | `_` 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 | `std::iter::Iterator::filter` | fixtures/reject/src/lib.rs:242:16 | | 1 | `_` through a held type's own, which is not supported yet | `::ne` | fixtures/reject/src/lib.rs:313:2 | @@ -212,13 +215,13 @@ The type or module each refused type or path belongs to. | 1 | an upcast from `_` to `_`, which is not supported yet | `dyn Keyed`, `dyn std::any::Any` | fixtures/reject/src/lib.rs:585:32 | | 1 | arithmetic on `_` | `f32` | fixtures/reject/src/lib.rs:8:2 | | 1 | collecting `_`, whose `None` is `rt.None`, which is not supported yet | `Option` | fixtures/reject/src/lib.rs:36:43 | -| 1 | comparing `_` as a key, which is not supported yet | `std::vec::Vec` | fixtures/reject/src/lib.rs:263:52 | +| 1 | comparing `_` as a key, which is not supported yet | `std::collections::BTreeSet` | fixtures/reject/src/lib.rs:263:75 | | 1 | comparing pointers to `_` in an atomic, which JS has no address of | `u32` | fixtures/reject/src/lib.rs:771:15 | | 1 | comparing pointers to `_`, each a place JS makes of its own | `u32` | fixtures/reject/src/lib.rs:649:2 | | 1 | constructing `_` | `std::borrow::Cow` | fixtures/reject/src/lib.rs:395:53 | | 1 | constructing `_` from another crate | `std::iter::Map` | fixtures/reject/src/lib.rs:155:23 | | 1 | constructing `_` from another crate | `std::iter::Rev` | fixtures/reject/src/lib.rs:176:14 | -| 1 | hashing `_` as a key, which is not supported yet | `std::vec::Vec` | fixtures/reject/src/lib.rs:263:52 | +| 1 | hashing `_` as a key, which is not supported yet | `std::collections::BTreeSet` | fixtures/reject/src/lib.rs:263:75 | | 1 | hashing `_` through `_`'s own `Hash::hash_slice`, which is not supported yet | `std::vec::Vec`, `Batched` | fixtures/reject/src/lib.rs:355:16 | | 1 | overriding `_`, which std's adaptors call in place of `next` or beside it | `Iterator::fold` | fixtures/reject/src/lib.rs:149:2 | | 1 | overriding `_`, which std's adaptors call in place of `next` or beside it | `Iterator::size_hint` | fixtures/reject/src/lib.rs:203:2 | @@ -297,6 +300,14 @@ The type or module each refused type or path belongs to.
+
`_` 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 (3) + +- fixtures/reject/src/lib.rs:242:16: `std::iter::Iterator::filter` 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 +- fixtures/reject/src/lib.rs:242:38: `std::iter::Iterator::count` 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 +- fixtures/reject/src/lib.rs:835:6: `std::collections::HashSet::::get` 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 + +
+
a copy of a closure that changes what it captured (3) - fixtures/reject/src/lib.rs:252:14: a copy of a closure that changes what it captured @@ -344,13 +355,6 @@ The type or module each refused type or path belongs to.
-
`_` 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 (2) - -- fixtures/reject/src/lib.rs:242:16: `std::iter::Iterator::filter` 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 -- fixtures/reject/src/lib.rs:242:38: `std::iter::Iterator::count` 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 - -
-
`_`, a trait object of another crate's trait, which is not supported yet (2) - fixtures/reject/src/lib.rs:11:16: `dyn std::iter::Iterator`, a trait object of another crate's trait, which is not supported yet @@ -631,7 +635,7 @@ The type or module each refused type or path belongs to.
comparing `_` as a key, which is not supported yet (1) -- fixtures/reject/src/lib.rs:263:52: comparing `std::vec::Vec` as a key, which is not supported yet +- fixtures/reject/src/lib.rs:263:75: comparing `std::collections::BTreeSet` as a key, which is not supported yet
@@ -661,7 +665,7 @@ The type or module each refused type or path belongs to.
hashing `_` as a key, which is not supported yet (1) -- fixtures/reject/src/lib.rs:263:52: hashing `std::vec::Vec` as a key, which is not supported yet +- fixtures/reject/src/lib.rs:263:75: hashing `std::collections::BTreeSet` as a key, which is not supported yet