From 10b07c0768d056251ba853c3032f139221e39370 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 30 Oct 2026 01:56:13 -0700 Subject: [PATCH 1/8] chore(quest): claim quest/m3/ts-bound-ranges.md Quest-Claim: 75591e93be84a4c9 From 96cfd8dd4c790b5007c5caa28b06e3578f851552 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 30 Oct 2026 02:43:50 -0700 Subject: [PATCH 2/8] chore(quest): move Ranges of Bounds under ts-model, which the model's census needs Co-Authored-By: Claude Opus 5.5 --- quest/m2/ts-model/README.md | 1 + quest/{m3/ts-bound-ranges.md => m2/ts-model/bound-ranges.md} | 3 +++ quest/m3/README.md | 1 - 3 files changed, 4 insertions(+), 1 deletion(-) rename quest/{m3/ts-bound-ranges.md => m2/ts-model/bound-ranges.md} (82%) diff --git a/quest/m2/ts-model/README.md b/quest/m2/ts-model/README.md index 67daeb410..231ff6e1f 100644 --- a/quest/m2/ts-model/README.md +++ b/quest/m2/ts-model/README.md @@ -51,3 +51,4 @@ Public API: the translator's output for moq-net's model. - [The rest of std the model reaches](/quest/m2/ts-model/std-rest.md) - `Vec` and `VecDeque` capacity - [hashbrown as the runtime's map](/quest/m2/ts-model/hashbrown.md) - hashlink's `hashbrown` tables translate as `rt.HashMap`, with std's capacity - [Async in the TypeScript translator](/quest/m2/ts-async.md) - kio's producers and consumers are futures +- [Ranges of Bounds](/quest/m2/ts-model/bound-ranges.md) - the model's, moq-net's, and rangemap's B-tree ranges of a `(Bound, Bound)` translate diff --git a/quest/m3/ts-bound-ranges.md b/quest/m2/ts-model/bound-ranges.md similarity index 82% rename from quest/m3/ts-bound-ranges.md rename to quest/m2/ts-model/bound-ranges.md index 0a5a506bf..b7bdf4260 100644 --- a/quest/m3/ts-bound-ranges.md +++ b/quest/m2/ts-model/bound-ranges.md @@ -8,6 +8,9 @@ items and panics. Proven by differential tests against native Rust. ## Plan +Moved from m3 on 2026-10-30: the model's census refuses 15 of these, in moq-net's +`ledger.rs`, `model/track.rs`, and `model/expiry.rs` and in rangemap. + Each refuses with a span since [#259](https://github.com/moq-dev/rust2/pull/259): the runtime's `bounds` reads std's range types alone, and read a tuple as unbounded. Reading the tuple's `rt.RangeBound`s there, and an excluded start where `slice::range` and the diff --git a/quest/m3/README.md b/quest/m3/README.md index f7c4e5ef2..6cbd63a9b 100644 --- a/quest/m3/README.md +++ b/quest/m3/README.md @@ -28,7 +28,6 @@ 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 - [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 -- [Ranges of Bounds](/quest/m3/ts-bound-ranges.md) - drains and B-tree ranges of a `(Bound, Bound)` translate - [The rest of a slice's chunks](/quest/m3/ts-slice-chunks.md) - `chunks_exact`, `rchunks`, and the mutable chunks translate - [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)` - [Port the workflow guard to kixelated/quest](/quest/m3/quest-workflow-guard.md) - quest's environment guard parses YAML as this repository's does From 4cd98d6d76b91c48ef8086bb815015247297070f Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 30 Oct 2026 02:47:12 -0700 Subject: [PATCH 3/8] feat(ts): translate drains and B-tree ranges of Bound pairs and the crate's own RangeBounds The runtime's Bounds reads a (Bound, Bound) as the pair it is, and any other RangeBounds through rt.boundsOf, which calls its start_bound and then its end_bound where std's does: not at all for an empty B-tree. B-tree ranges panic on equal excluded ends, and slice::range checks an excluded start as std's does, with std's messages. Co-Authored-By: Claude Opus 5.5 --- quest/m2/ts-model/README.md | 1 - quest/m2/ts-model/bound-ranges.md | 20 -- quest/m3/README.md | 1 + quest/m3/ts-btree-drop.md | 22 ++ quest/m3/ts-drop-order.md | 2 + ts/README.md | 1 + ts/fixtures/lang/caller.ts | 13 +- ts/fixtures/lang/expected/bounds.Asked.ts | 37 +++ ts/fixtures/lang/expected/bounds.Item.ts | 23 ++ ts/fixtures/lang/expected/bounds.Key.ts | 14 ++ ts/fixtures/lang/expected/bounds.ts | 294 ++++++++++++++++++++++ ts/fixtures/lang/expected/core/option.ts | 21 +- ts/fixtures/lang/expected/harness.ts | 5 +- ts/fixtures/lang/expected/lib.ts | 8 + ts/fixtures/lang/expected/slices.ts | 4 +- ts/fixtures/lang/src/bounds.rs | 202 +++++++++++++++ ts/fixtures/lang/src/harness.rs | 3 +- ts/fixtures/lang/src/lib.rs | 5 +- ts/fixtures/reject/census.md | 32 +-- ts/fixtures/reject/expected.txt | 3 +- ts/fixtures/reject/src/lib.rs | 23 +- ts/fixtures/reject/ts.toml | 2 +- ts/runtime/rt.ts | 2 +- ts/runtime/rt/collections.ts | 72 ++++-- ts/src/lower/collections.rs | 65 ++++- ts/src/lower/library.rs | 13 +- 26 files changed, 792 insertions(+), 96 deletions(-) delete mode 100644 quest/m2/ts-model/bound-ranges.md create mode 100644 quest/m3/ts-btree-drop.md create mode 100644 ts/fixtures/lang/expected/bounds.Asked.ts create mode 100644 ts/fixtures/lang/expected/bounds.Item.ts create mode 100644 ts/fixtures/lang/expected/bounds.Key.ts create mode 100644 ts/fixtures/lang/expected/bounds.ts create mode 100644 ts/fixtures/lang/src/bounds.rs diff --git a/quest/m2/ts-model/README.md b/quest/m2/ts-model/README.md index 231ff6e1f..67daeb410 100644 --- a/quest/m2/ts-model/README.md +++ b/quest/m2/ts-model/README.md @@ -51,4 +51,3 @@ Public API: the translator's output for moq-net's model. - [The rest of std the model reaches](/quest/m2/ts-model/std-rest.md) - `Vec` and `VecDeque` capacity - [hashbrown as the runtime's map](/quest/m2/ts-model/hashbrown.md) - hashlink's `hashbrown` tables translate as `rt.HashMap`, with std's capacity - [Async in the TypeScript translator](/quest/m2/ts-async.md) - kio's producers and consumers are futures -- [Ranges of Bounds](/quest/m2/ts-model/bound-ranges.md) - the model's, moq-net's, and rangemap's B-tree ranges of a `(Bound, Bound)` translate diff --git a/quest/m2/ts-model/bound-ranges.md b/quest/m2/ts-model/bound-ranges.md deleted file mode 100644 index b7bdf4260..000000000 --- a/quest/m2/ts-model/bound-ranges.md +++ /dev/null @@ -1,20 +0,0 @@ -# [XS] Ranges of Bounds - -## Goal - -rust2-ts translates `Vec::drain`, `VecDeque::drain`, and a B-tree's `range` and -`range_mut` of a `(Bound, Bound)`, an excluded start included, matching std's -items and panics. Proven by differential tests against native Rust. - -## Plan - -Moved from m3 on 2026-10-30: the model's census refuses 15 of these, in moq-net's -`ledger.rs`, `model/track.rs`, and `model/expiry.rs` and in rangemap. - -Each refuses with a span since [#259](https://github.com/moq-dev/rust2/pull/259): the -runtime's `bounds` reads std's range types alone, and read a tuple as unbounded. Reading -the tuple's `rt.RangeBound`s there, and an excluded start where `slice::range` and the -B-tree's search take one, covers all four. A crate's own `RangeBounds` stays refused -until it has a consumer. rust2 aims to support all of std. - -Public API: the translator's output. diff --git a/quest/m3/README.md b/quest/m3/README.md index 6cbd63a9b..ab1266772 100644 --- a/quest/m3/README.md +++ b/quest/m3/README.md @@ -26,6 +26,7 @@ generic std gap is planned here even when moq never calls it. - [Waiting: type-check in process with tsrs](/quest/m3/ts-tsrs.md) - rust2-ts checks bindings through tsrs instead of `tsc` once it is a stable library - [An inclusive range folded through an adaptor](/quest/m3/ts-range-fold.md) - a `RangeInclusive` folded through `map`, `filter`, or `rev` ends in core's state, and `by_ref` of one type-checks - [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 - [The rest of a slice's chunks](/quest/m3/ts-slice-chunks.md) - `chunks_exact`, `rchunks`, and the mutable chunks translate diff --git a/quest/m3/ts-btree-drop.md b/quest/m3/ts-btree-drop.md new file mode 100644 index 000000000..d54bc86d4 --- /dev/null +++ b/quest/m3/ts-btree-drop.md @@ -0,0 +1,22 @@ +# [S] A B-tree's drop order + +## Goal + +[rust2-ts](/ts/README.md) drops a whole `BTreeMap` or `BTreeSet` whose entries drop in the +order std's does, proven by the lang fixture's differential tests against native Rust. + +## Plan + +Found on 2026-10-30 while testing ranges of `Bound`s: at the pinned nightly, std's +`Drop for BTreeMap` calls `drop_tree` (`btree/navigate.rs`), which drops each node's +entries and then moves on in postorder, so a parent's entries drop after its children's. +The runtime drops them in key order, so a map of 12 or more entries that log as they drop +logs a separator key last natively and in place translated. Std's node size is 11 keys and +the runtime's 64, so the order depends on std's node layout, which insertion order decides: +porting it means modelling std's splits and merges for maps whose entries drop, or +refusing with a span the drop of a map whose entries drop and that may hold more than one +node. + +`IntoIter`'s drop, through `dying_next`, stays in key order. + +Public API: the translator's output for B-trees of values that drop. diff --git a/quest/m3/ts-drop-order.md b/quest/m3/ts-drop-order.md index 27d2ce6af..8b06f9b71 100644 --- a/quest/m3/ts-drop-order.md +++ b/quest/m3/ts-drop-order.md @@ -26,6 +26,8 @@ with a span where std's order is one JS's can't follow yet: - `RangeInclusive::new` of bounds that drop, such as `Arc::new(a)..=Arc::new(b)`, refused where the range is built, found by [#247](https://github.com/moq-dev/rust2/pull/247) on 2026-10-29. +- A range that drops given to a drain or a B-tree's `range`, which std drops once it has + read the range's bounds, or at once for an empty B-tree. Each `rt.Iter` has a `fold` and `rfold` that drop what it holds where std's do, and the by-value consumers std folds go through it: build on those rather than `rt.drop`. diff --git a/ts/README.md b/ts/README.md index 0afaa8f3e..d21f2523f 100644 --- a/ts/README.md +++ b/ts/README.md @@ -95,6 +95,7 @@ crate translation reaches is refused, and so are two versions of one crate it re | an iterator | `rt.Iter`, whose adaptors are runtime functions: `rt.iterMap(rt.iterSlice(v), f)` | | `a..b`, `a..=b` | `rt.Range`, `rt.RangeInclusive`, which are iterators too | | `a..`, `..b`, `..=b`, `Bound` | `{ start }`, `{ end }`, `{ end, inclusive: true }`; `rt.RangeBound`, a union tagged by variant | +| a range a drain or a B-tree's `range` takes | itself, or of a type other than std's ranges and a pair of `Bound`s, `rt.boundsOf` of it and its `start_bound` and `end_bound`, which the runtime calls where std's does | | `HashMap`, `HashSet`, `BTreeMap`, `BTreeSet`, `VecDeque`, `BinaryHeap` | `rt.HashMap` and so on, each a plain object whose functions are the runtime's: `rt.hashGet(map, key)` | | `PeekMut` | `rt.PeekMut`, the `rt.Place` of a heap's greatest element, which sifts it down as it drops once `DerefMut` lent it | | `Ordering`, `DefaultHasher`, `RandomState` | `rt.Ordering`, which is `-1`, `0`, or `1`; `rt.Hasher`; `rt.RandomState`, the keys of the `DefaultHasher` it builds, never changed once made | diff --git a/ts/fixtures/lang/caller.ts b/ts/fixtures/lang/caller.ts index 1a1e2ce21..38651ecb4 100644 --- a/ts/fixtures/lang/caller.ts +++ b/ts/fixtures/lang/caller.ts @@ -292,4 +292,15 @@ const locks = [[0, 1, 5, 6, 2, 7, 3, 8, 3, 0, 1], [4, 0, 4, 1], [2, 9, 2, 10, 4, (bytes) => Uint8Array.of(op("locks::locks"), 0, ...bytes), ); -export const cases: Uint8Array[] = [...loops, ...durations, ...ints(), ...utf8, ...floats, ...formats, ...parsed, ...dyns, ...cells, ...shared, ...locks]; +/** + * `bounds` with each kind of bound at each end, over every pair of values around and past + * four items, so ranges are empty, backwards, equal, and out of bounds, and of no items. + */ +const boundsOp = op("bounds::bounds"); +const bounds = Array.from({ length: 6 * 9 * 36 }, (_, n) => { + const [kind, ends, at] = [n % 6, Math.floor(n / 6) % 9, Math.floor(n / 54)]; + const [a, b] = [at % 6, Math.floor(at / 6)]; + return [Uint8Array.of(boundsOp, kind, ends % 3, Math.floor(ends / 3), a, b, 1, 3, 2, 5), Uint8Array.of(boundsOp, kind, ends % 3, Math.floor(ends / 3), a, b)]; +}).flat(); + +export const cases: Uint8Array[] = [...loops, ...durations, ...ints(), ...utf8, ...floats, ...formats, ...parsed, ...dyns, ...cells, ...shared, ...locks, ...bounds]; diff --git a/ts/fixtures/lang/expected/bounds.Asked.ts b/ts/fixtures/lang/expected/bounds.Asked.ts new file mode 100644 index 000000000..b2642ec90 --- /dev/null +++ b/ts/fixtures/lang/expected/bounds.Asked.ts @@ -0,0 +1,37 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; +import { log } from "./bounds.ts"; + +/** The crate's own range, which logs each bound std asks it for: 0xa5 its start, 0xae its end. */ +export class Asked { + start: rt.RangeBound; + end: rt.RangeBound; + + constructor(start: rt.RangeBound, end: rt.RangeBound) { + this.start = start; + this.end = end; + } +} + +export function startBound$RangeBounds_u8_u8(self: Asked): rt.RangeBound { + log(165); + return self.start; +} + +export function startBound$RangeBounds_usize_usize(self: Asked): rt.RangeBound { + log(165); + return self.start; +} + +export function endBound$RangeBounds_u8_u8(self: Asked): rt.RangeBound { + log(174); + return self.end; +} + +export function endBound$RangeBounds_usize_usize(self: Asked): rt.RangeBound { + log(174); + return self.end; +} + +//# sourceMappingURL=bounds.Asked.ts.map diff --git a/ts/fixtures/lang/expected/bounds.Item.ts b/ts/fixtures/lang/expected/bounds.Item.ts new file mode 100644 index 000000000..6d2d6f36c --- /dev/null +++ b/ts/fixtures/lang/expected/bounds.Item.ts @@ -0,0 +1,23 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; +import { log } from "./bounds.ts"; + +/** Logs its tag as it drops. */ +export class Item { + tag: rt.u8; + + constructor(tag: rt.u8) { + this.tag = tag; + } + + [Symbol.dispose](): void { + drop$Drop(this); + } +} + +export function drop$Drop(self: Item): void { + log(self.tag); +} + +//# sourceMappingURL=bounds.Item.ts.map diff --git a/ts/fixtures/lang/expected/bounds.Key.ts b/ts/fixtures/lang/expected/bounds.Key.ts new file mode 100644 index 000000000..74657687f --- /dev/null +++ b/ts/fixtures/lang/expected/bounds.Key.ts @@ -0,0 +1,14 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; + +/** A key the crate orders by its own `Ord`, high to low, as rangemap's keys are by theirs. */ +export interface Key { + _0: rt.u8; +} + +export function cmp$Ord(self: Key, other: Key): rt.Ordering { + return rt.cmp(other._0, self._0); +} + +//# sourceMappingURL=bounds.Key.ts.map diff --git a/ts/fixtures/lang/expected/bounds.ts b/ts/fixtures/lang/expected/bounds.ts new file mode 100644 index 000000000..039a70a08 --- /dev/null +++ b/ts/fixtures/lang/expected/bounds.ts @@ -0,0 +1,294 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; +import { copy$Bound_u8, copy$Bound_usize } from "./lib.ts"; +import * as Asked from "./bounds.Asked.ts"; +import * as Item from "./bounds.Item.ts"; +import * as Key from "./bounds.Key.ts"; +import { map$Option_Item_u8_bounds_closure4, map$Option_u32_u8_bounds_closure2, map$Option_u8_u8_u8_bounds_closure9 } from "./core/option.ts"; + +export const LOG: rt.LocalKey> = /* @__PURE__ */ new rt.LocalKey(LOG$init, false); + +export function LOG$init(): rt.RefCell { + return new rt.RefCell(new rt.ByteVec()); +} + +export function log(entry: rt.u8): void { + rt.localKeyWithBorrowMut(LOG, (log_: rt.ByteVec): void => { + rt.byteVecPush(log_, entry); + }); +} + +/** Puts what was logged since the last flush. */ +export function flush(out: rt.ByteVec): void { + rt.localKeyWithBorrowMut(LOG, (log_: rt.ByteVec): void => { + rt.byteVecExtendFromSlice(out, rt.byteVecSlice(log_)); + rt.byteVecClear(log_); + }); +} + +export function bound$Key(kind: rt.u8, at: Key.Key): rt.RangeBound { + const _3: rt.u8 = rt.u8Rem(kind, 3); + if (_3 === 0) { + return { tag: "Included", _0: at } as rt.RangeBound; + } + if (_3 === 1) { + return { tag: "Excluded", _0: at } as rt.RangeBound; + } + return { tag: "Unbounded" } as rt.RangeBound; +} + +export function bound$u8(kind: rt.u8, at: rt.u8): rt.RangeBound { + const _3: rt.u8 = rt.u8Rem(kind, 3); + if (_3 === 0) { + return { tag: "Included", _0: at } as rt.RangeBound; + } + if (_3 === 1) { + return { tag: "Excluded", _0: at } as rt.RangeBound; + } + return { tag: "Unbounded" } as rt.RangeBound; +} + +export function bound$u8$2(kind: rt.u8, at: rt.u8): rt.RangeBound { + const _3: rt.u8 = rt.u8Rem(kind, 3); + if (_3 === 0) { + return { tag: "Included", _0: at } as rt.RangeBound; + } + if (_3 === 1) { + return { tag: "Excluded", _0: at } as rt.RangeBound; + } + return { tag: "Unbounded" } as rt.RangeBound; +} + +export function bound$usize(kind: rt.u8, at: rt.usize): rt.RangeBound { + const _3: rt.u8 = rt.u8Rem(kind, 3); + if (_3 === 0) { + return { tag: "Included", _0: at } as rt.RangeBound; + } + if (_3 === 1) { + return { tag: "Excluded", _0: at } as rt.RangeBound; + } + return { tag: "Unbounded" } as rt.RangeBound; +} + +export function bound$usize$2(kind: rt.u8, at: rt.usize): rt.RangeBound { + const _3: rt.u8 = rt.u8Rem(kind, 3); + if (_3 === 0) { + return { tag: "Included", _0: at } as rt.RangeBound; + } + if (_3 === 1) { + return { tag: "Excluded", _0: at } as rt.RangeBound; + } + return { tag: "Unbounded" } as rt.RangeBound; +} + +export function asked$u8(s: rt.u8, e: rt.u8, a: rt.u8, b: rt.u8): Asked.Asked { + return new Asked.Asked(copy$Bound_u8(bound$u8$2(s, a)), copy$Bound_u8(bound$u8$2(e, b))); +} + +export function asked$usize(s: rt.u8, e: rt.u8, a: rt.usize, b: rt.usize): Asked.Asked { + return new Asked.Asked(copy$Bound_usize(bound$usize$2(s, a)), copy$Bound_usize(bound$usize$2(e, b))); +} + +/** The keys of `set` within `range`, through a generic function, as each instance has it. */ +export function within$Asked_u8(set: rt.BTreeSet, range: Asked.Asked): rt.usize { + return rt.iterCount(rt.btreeRangeKeys(set, rt.boundsOf(range, Asked.startBound$RangeBounds_u8_u8, Asked.endBound$RangeBounds_u8_u8))); +} + +/** The keys of `set` within `range`, through a generic function, as each instance has it. */ +export function within$Bound_u8_Bound_u8(set: rt.BTreeSet, range: [rt.RangeBound, rt.RangeBound]): rt.usize { + return rt.iterCount(rt.btreeRangeKeys(set, range)); +} + +/** + * `kind` picks the collection and how its range is given. The first two bytes pick the + * kinds of the range's bounds, the next two their values, and the rest are the items. + */ +export function bounds(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { + rt.localKeyWithBorrowMut(LOG, (a0: rt.ByteVec): void => { + return rt.byteVecClear(a0); + }); + if (data.length >= 4) { + const s: rt.u8 = data.data[data.start]; + const e: rt.u8 = data.data[data.start + 1]; + const a: rt.u8 = data.data[data.start + 2]; + const b: rt.u8 = data.data[data.start + 3]; + const reach: rt.usize = rt.usizeAdd(rt.byteSliceRange(data, 4).length, 2); + const _18: [rt.usize, rt.usize] = [rt.usizeRem(a as rt.usize, reach), rt.usizeRem(b as rt.usize, reach)]; + const i: rt.usize = _18[0]; + const j: rt.usize = _18[1]; + const items: Item.Item[] = rt.iterToArray(rt.iterMap(rt.iterSlice(rt.byteSliceView(rt.byteSliceRange(data, 4))), (_2: rt.u8): Item.Item => { + return new Item.Item(_2); + })); + const _28: rt.u8 = rt.u8Rem(kind, 6); + if (_28 === 0) { + const range: [rt.RangeBound, rt.RangeBound] = [bound$usize$2(s, i), bound$usize$2(e, j)]; + const iter: rt.Iter = rt.vecDrain(items, range, rt.drop); + while (true) { + const _37: Item.Item | rt.Done = iter.next(); + if (_37 === rt.DONE) { + break; + } + const x: Item.Item = _37; + rt.byteVecPush(out, x.tag); + flush(out); + x[Symbol.dispose](); + } + rt.drop(iter); + const bytes: rt.ByteVec = rt.byteSliceToByteVec(rt.byteSliceRange(data, 4)); + const _49: [rt.RangeBound, rt.RangeBound] = [bound$usize(s, i), bound$usize(e, j)]; + rt.iterExtendBytes(out, rt.iterRev(rt.byteVecDrain(bytes, _49)), "fold"); + rt.iterExtendBytes(out, rt.iterSlice(rt.byteVecView(bytes))); + const words: rt.u32[] = rt.iterToArray(rt.iterMap(rt.iterSlice(rt.byteSliceView(rt.byteSliceRange(data, 4))), (_217: rt.u8): rt.u32 => { + return rt.u32Add(_217 as rt.u32, 1000); + })); + const drain: rt.Iter = rt.vecDrain(words, range); + rt.iterExtendBytes(out, rt.iterOption(map$Option_u32_u8_bounds_closure2(rt.iterToOption(drain.nextBack()), (w: rt.u32): rt.u8 => { + return w & 0xff; + }))); + rt.byteVecPush(out, drain.len() & 0xff); + rt.drop(drain); + rt.iterExtendBytes(out, rt.iterMap(rt.iterSlice(words), (_220: rt.u32): rt.u8 => { + return _220 & 0xff; + })); + } else if (_28 === 1) { + const _78: Asked.Asked = asked$usize(s, e, i, j); + const drain2: rt.Iter = rt.vecDrain(items, rt.boundsOf(_78, Asked.startBound$RangeBounds_usize_usize, Asked.endBound$RangeBounds_usize_usize), rt.drop); + flush(out); + rt.iterExtendBytes(out, rt.iterOption(map$Option_Item_u8_bounds_closure4(rt.iterToOption(drain2.next()), (x5: Item.Item): rt.u8 => { + const _010: rt.u8 = x5.tag; + x5[Symbol.dispose](); + return _010; + }))); + flush(out); + rt.drop(drain2); + flush(out); + const bytes2: rt.ByteVec = rt.byteSliceToByteVec(rt.byteSliceRange(data, 4)); + const _94: Asked.Asked = asked$usize(s, e, i, j); + rt.byteVecPush(out, rt.ownedCount(rt.byteVecDrain(bytes2, rt.boundsOf(_94, Asked.startBound$RangeBounds_usize_usize, Asked.endBound$RangeBounds_usize_usize))) & 0xff); + flush(out); + rt.iterExtendBytes(out, rt.iterSlice(rt.byteVecView(bytes2))); + } else if (_28 === 2) { + const queue: rt.VecDeque = rt.dequeNew() as rt.VecDeque; + const iter2: rt.Iter<[rt.usize, rt.u8]> = rt.iterEnumerate(rt.iterSlice(rt.byteSliceView(rt.byteSliceRange(data, 4)))); + while (true) { + const _104: [rt.usize, rt.u8] | rt.Done = iter2.next(); + if (_104 === rt.DONE) { + break; + } + const n: rt.usize = _104[0]; + const tag: rt.u8 = _104[1]; + if (rt.usizeRem(n, 2) === 0) { + rt.dequePushBack(queue, new Item.Item(tag)); + } else { + rt.dequePushFront(queue, new Item.Item(tag)); + } + } + const _117: [rt.RangeBound, rt.RangeBound] = [bound$usize$2(s, i), bound$usize$2(e, j)]; + const drain3: rt.Iter = rt.dequeDrain(queue, _117, rt.drop); + rt.iterExtendBytes(out, rt.iterOption(map$Option_Item_u8_bounds_closure4(rt.iterToOption(drain3.nextBack()), (x2: Item.Item): rt.u8 => { + const _07: rt.u8 = x2.tag; + x2[Symbol.dispose](); + return _07; + }))); + flush(out); + rt.drop(drain3); + flush(out); + rt.iterExtendBytes(out, rt.iterMap(rt.dequeIter(queue), (x3: Item.Item): rt.u8 => { + return x3.tag; + })); + const reach2: rt.usize = rt.usizeAdd(queue.n, 1); + const _138: Asked.Asked = asked$usize(s, e, rt.usizeRem(a as rt.usize, reach2), rt.usizeRem(b as rt.usize, reach2)); + const drain4: rt.Iter = rt.dequeDrain(queue, rt.boundsOf(_138, Asked.startBound$RangeBounds_usize_usize, Asked.endBound$RangeBounds_usize_usize), rt.drop); + flush(out); + rt.byteVecPush(out, rt.ownedCount(drain4, rt.drop) & 0xff); + flush(out); + rt.iterExtendBytes(out, rt.iterMap(rt.dequeIter(queue), (x4: Item.Item): rt.u8 => { + return x4.tag; + })); + rt.dequeDrop(queue, rt.drop); + } else if (_28 === 3) { + const _155: rt.Iter<[rt.u8, rt.u8]> = rt.iterMap(rt.iterEnumerate(rt.iterSlice(rt.byteSliceView(rt.byteSliceRange(data, 4)))), (_214: [rt.usize, rt.u8]): [rt.u8, rt.u8] => { + return [_214[1], _214[0] & 0xff] as [rt.u8, rt.u8]; + }); + const map: rt.BTreeMap = rt.btreeExtend(rt.btreeNew(rt.cmp) as rt.BTreeMap, _155, true); + const range2: [rt.RangeBound, rt.RangeBound] = [bound$u8$2(s, a), bound$u8$2(e, b)]; + const iter3: rt.Iter<[rt.u8, rt.u8]> = rt.btreeRange(map, range2); + while (true) { + const _165: [rt.u8, rt.u8] | rt.Done = iter3.next(); + if (_165 === rt.DONE) { + break; + } + rt.byteVecPush(out, _165[0]); + rt.byteVecPush(out, _165[1]); + } + const _180: [rt.RangeBound, rt.RangeBound] = [bound$u8(s, a), bound$u8(e, b)]; + rt.iterExtendBytes(out, rt.iterOption(map$Option_u8_u8_u8_bounds_closure9(rt.iterToOption(rt.btreeRange(map, _180).nextBack()), (_216: [rt.u8, rt.u8]): rt.u8 => { + return _216[0]; + }))); + const iter4: rt.Iter<[rt.u8, rt.Place]> = rt.btreeRangeMut(map, range2); + while (true) { + const _189: [rt.u8, rt.Place] | rt.Done = iter4.next(); + if (_189 === rt.DONE) { + break; + } + _189[1].v = rt.u8WrappingAdd(_189[1].v, 100); + } + rt.iterExtendBytes(out, rt.btreeValues(map)); + const _199: rt.Iter = rt.iterSlice(rt.byteSliceView(rt.byteSliceRange(data, 4))); + const set: rt.BTreeSet = rt.btreeExtendKeys(rt.btreeNew(rt.cmp) as rt.BTreeSet, _199, true); + rt.iterExtendBytes(out, rt.iterRev(rt.btreeRangeKeys(set, range2))); + rt.byteVecPush(out, within$Bound_u8_Bound_u8(set, range2) & 0xff); + } else if (_28 === 4) { + const empty: rt.BTreeMap = rt.btreeNew(rt.cmp) as rt.BTreeMap; + const _215: Asked.Asked = asked$u8(s, e, a, b); + rt.byteVecPush(out, rt.iterCount(rt.btreeRange(empty, rt.boundsOf(_215, Asked.startBound$RangeBounds_u8_u8, Asked.endBound$RangeBounds_u8_u8))) & 0xff); + flush(out); + const _218: rt.Iter = rt.iterSlice(rt.byteSliceView(rt.byteSliceRange(data, 4))); + const set2: rt.BTreeSet = rt.btreeExtendKeys(rt.btreeNew(rt.cmp) as rt.BTreeSet, _218, true); + const _222: Asked.Asked = asked$u8(s, e, a, b); + const range3: rt.Iter = rt.btreeRangeKeys(set2, rt.boundsOf(_222, Asked.startBound$RangeBounds_u8_u8, Asked.endBound$RangeBounds_u8_u8)); + flush(out); + rt.iterExtendBytes(out, rt.iterOption(rt.iterToOption(range3.next()))); + rt.iterExtendBytes(out, rt.iterOption(rt.iterToOption(range3.nextBack()))); + const _234: Asked.Asked = asked$u8(s, e, a, b); + rt.byteVecPush(out, within$Asked_u8(set2, _234) & 0xff); + flush(out); + rt.btreeDrop(empty, undefined, rt.drop); + } else { + const _238: [Key.Key, Key.Key] = [{ _0: a }, { _0: b }]; + const a2: Key.Key = _238[0]; + const b2: Key.Key = _238[1]; + const _242: rt.Iter = rt.iterMap(rt.iterSlice(rt.byteSliceView(rt.byteSliceRange(data, 4))), (_25: rt.u8): Key.Key => { + return { _0: _25 }; + }); + rt.iterExtendBytes(out, rt.iterMap(rt.btreeRangeKeys(rt.btreeExtendKeys(rt.btreeNew(Key.cmp$Ord) as rt.BTreeSet, _242, true), [bound$Key(s, a2), bound$Key(e, b2)] as [rt.RangeBound, rt.RangeBound]), (k2: Key.Key): rt.u8 => { + return k2._0; + })); + const _254: rt.Iter<[Key.Key, rt.u8]> = rt.iterMap(rt.iterEnumerate(rt.iterSlice(rt.byteSliceView(rt.byteSliceRange(data, 4)))), (_210: [rt.usize, rt.u8]): [Key.Key, rt.u8] => { + const n2: rt.usize = _210[0]; + const _53: Key.Key = { _0: _210[1] }; + return [_53, n2 & 0xff] as [Key.Key, rt.u8]; + }); + const map2: rt.BTreeMap = rt.btreeExtend(rt.btreeNew(Key.cmp$Ord) as rt.BTreeMap, _254, true); + const _260: [rt.RangeBound, rt.RangeBound] = [bound$Key(s, a2), bound$Key(e, b2)]; + const iter5: rt.Iter<[Key.Key, rt.Place]> = rt.btreeRangeMut(map2, _260); + while (true) { + const _266: [Key.Key, rt.Place] | rt.Done = iter5.next(); + if (_266 === rt.DONE) { + break; + } + _266[1].v = _266[1].v ^ 128; + } + rt.iterExtendBytes(out, rt.btreeValues(map2)); + } + flush(out); + rt.iterExtendBytes(out, rt.iterMap(rt.iterSlice(items), (x6: Item.Item): rt.u8 => { + return x6.tag; + })); + rt.dropAll(items); + flush(out); + } +} + +//# sourceMappingURL=bounds.ts.map diff --git a/ts/fixtures/lang/expected/core/option.ts b/ts/fixtures/lang/expected/core/option.ts index 6e1fe3097..144170315 100644 --- a/ts/fixtures/lang/expected/core/option.ts +++ b/ts/fixtures/lang/expected/core/option.ts @@ -3,6 +3,7 @@ import * as rt from "../rt.ts"; import { copy$u8s_u8s } from "../lib.ts"; import type * as Noisy from "../borrows.Noisy.ts"; +import type * as Item from "../bounds.Item.ts"; import type * as Nibble from "../collections.Nibble.ts"; import type * as Rank from "../collections.Rank.ts"; import type * as Tally from "../collections.Tally.ts"; @@ -23,7 +24,7 @@ import type * as Hop from "../keys.Hop.ts"; import type * as Side from "../keys.Side.ts"; import type * as Way from "../keys.Way.ts"; import type * as locals_Noisy from "../locals.Noisy.ts"; -import type * as Item from "../orders.Item.ts"; +import type * as orders_Item from "../orders.Item.ts"; import type * as Pair from "../pointers.Pair.ts"; import { from$From_Dir_u8 } from "../shapes.ts"; import type * as Dir from "../shapes.Dir.ts"; @@ -244,6 +245,13 @@ export function map$Option_Hop_u64_u64_options_closure0(self: Hop.Hop | undefine return f(self); } +export function map$Option_Item_u8_bounds_closure4(self: Item.Item | undefined, f: (a0: Item.Item) => rt.u8): rt.u8 | undefined { + if (self === undefined) { + return undefined; + } + return f(self); +} + export function map$Option_Item_u8_drains_closure10(self: slices_Item.Item | undefined, f: (a0: slices_Item.Item) => rt.u8): rt.u8 | undefined { if (self === undefined) { return undefined; @@ -286,7 +294,7 @@ export function map$Option_u16s_u8_digest_words(self: rt.u16[] | undefined, _: ( return digestWords(self); } -export function map$Option_u32_u8_drains_closure5(self: rt.u32 | undefined, f: (a0: rt.u32) => rt.u8): rt.u8 | undefined { +export function map$Option_u32_u8_bounds_closure2(self: rt.u32 | undefined, f: (a0: rt.u32) => rt.u8): rt.u8 | undefined { if (self === undefined) { return undefined; } @@ -335,6 +343,13 @@ export function map$Option_u8_u8_elements_closure8(self: rt.u8 | undefined, f: ( return f(self); } +export function map$Option_u8_u8_u8_bounds_closure9(self: [rt.u8, rt.u8] | undefined, f: (a0: [rt.u8, rt.u8]) => rt.u8): rt.u8 | undefined { + if (self === undefined) { + return undefined; + } + return f(self); +} + export function map$Option_u8_u8_u8_elements_closure9(self: rt.u8 | undefined, f: (a0: rt.u8) => [rt.u8, rt.u8]): [rt.u8, rt.u8] | undefined { if (self === undefined) { return undefined; @@ -433,7 +448,7 @@ export function mapOr$Option_Hop_u64_options_closure2(self: Hop.Hop | undefined, return f(self); } -export function mapOr$Option_Item_u8_heaps_closure1(self: Item.Item | undefined, default_: rt.u8, f: (a0: Item.Item) => rt.u8): rt.u8 { +export function mapOr$Option_Item_u8_heaps_closure1(self: orders_Item.Item | undefined, default_: rt.u8, f: (a0: orders_Item.Item) => rt.u8): rt.u8 { if (self === undefined) { return default_; } diff --git a/ts/fixtures/lang/expected/harness.ts b/ts/fixtures/lang/expected/harness.ts index 53c3aaa51..d1f9243cf 100644 --- a/ts/fixtures/lang/expected/harness.ts +++ b/ts/fixtures/lang/expected/harness.ts @@ -5,6 +5,7 @@ import { copy$str_forabfnu8au8bmutstdvecVecu8, copy$str_forabfnu8au8bmutstdvecVe import { fromFns } from "./arrays.ts"; import { atomics } from "./atomics.ts"; import { borrows } from "./borrows.ts"; +import { bounds } from "./bounds.ts"; import { captures } from "./captures.ts"; import { cells } from "./cells.ts"; import { chars } from "./chars.ts"; @@ -89,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, _327: rt.ByteSlice, out23: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::rounding", (_2122: rt.u8, _330: 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); @@ -161,7 +162,7 @@ export function cases(): [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) random(data56, out57); }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["floats::formats", formats] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["floats::parses", parses] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["orders::bytes", (_2157: rt.u8, data57: rt.ByteSlice, out58: rt.ByteVec): void => { orders_bytes(data57, out58); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["slices::parts", parts] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["slices::drains", drains] 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], ["slices::parts", parts] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["slices::drains", drains] 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); } /** Run one case. */ diff --git a/ts/fixtures/lang/expected/lib.ts b/ts/fixtures/lang/expected/lib.ts index 35bc2fa9f..5d40e4d79 100644 --- a/ts/fixtures/lang/expected/lib.ts +++ b/ts/fixtures/lang/expected/lib.ts @@ -36,6 +36,14 @@ export function copy$Bound_u64(v: rt.RangeBound): rt.RangeBound return v.tag === "Unbounded" ? v : { tag: v.tag, _0: v._0 } as rt.RangeBound; } +export function copy$Bound_u8(v: rt.RangeBound): rt.RangeBound { + return v.tag === "Unbounded" ? v : { tag: v.tag, _0: v._0 } as rt.RangeBound; +} + +export function copy$Bound_usize(v: rt.RangeBound): rt.RangeBound { + return v.tag === "Unbounded" ? v : { tag: v.tag, _0: v._0 } as rt.RangeBound; +} + export function copy$Bumped(v: Bumped.Bumped): Bumped.Bumped { return { _0: v._0 }; } diff --git a/ts/fixtures/lang/expected/slices.ts b/ts/fixtures/lang/expected/slices.ts index 72c55ff63..8f532f72f 100644 --- a/ts/fixtures/lang/expected/slices.ts +++ b/ts/fixtures/lang/expected/slices.ts @@ -1,7 +1,7 @@ // Generated by rust2-ts. Do not edit. import * as rt from "./rt.ts"; -import { map$Option_Item_u8_drains_closure10, map$Option_u16s_u8_digest_words, map$Option_u32_u8_drains_closure5, map$Option_u8s_u8_digest, map$Option_u8s_u8_parts_closure1 } from "./core/option.ts"; +import { map$Option_Item_u8_drains_closure10, map$Option_u16s_u8_digest_words, map$Option_u32_u8_bounds_closure2, map$Option_u8s_u8_digest, map$Option_u8s_u8_parts_closure1 } from "./core/option.ts"; import * as Item from "./slices.Item.ts"; export const LOG: rt.LocalKey> = /* @__PURE__ */ new rt.LocalKey(LOG$init, false); @@ -177,7 +177,7 @@ export function drains(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { rt.drop(drain2); const _82: rt.RangeInclusive = new rt.RangeInclusive(a, b, rt.usizeStep); const drain3: rt.Iter = rt.vecDrain(words, _82); - rt.iterExtendBytes(out, rt.iterOption(map$Option_u32_u8_drains_closure5(rt.iterToOption(drain3.next()), (w2: rt.u32): rt.u8 => { + rt.iterExtendBytes(out, rt.iterOption(map$Option_u32_u8_bounds_closure2(rt.iterToOption(drain3.next()), (w2: rt.u32): rt.u8 => { return w2 & 0xff; }))); rt.drop(drain3); diff --git a/ts/fixtures/lang/src/bounds.rs b/ts/fixtures/lang/src/bounds.rs new file mode 100644 index 000000000..9e4ed7526 --- /dev/null +++ b/ts/fixtures/lang/src/bounds.rs @@ -0,0 +1,202 @@ +//! Ranges as pairs of `Bound`s and as the crate's own `RangeBounds`: `Vec::drain` and +//! `VecDeque::drain` of values that log as they drop, and a B-tree's `range` and +//! `range_mut`, with each kind of bound at either end, empty, backwards, equal, and out of +//! bounds, which panic where std's do, and when std asks the crate's own range its bounds. + +use std::cell::RefCell; +use std::cmp::Ordering; +use std::collections::{BTreeMap, BTreeSet, VecDeque}; +use std::ops::{Bound, RangeBounds}; + +thread_local! { + static LOG: RefCell> = const { RefCell::new(Vec::new()) }; +} + +fn log(entry: u8) { + LOG.with_borrow_mut(|log| log.push(entry)); +} + +/// Puts what was logged since the last flush. +fn flush(out: &mut Vec) { + LOG.with_borrow_mut(|log| { + out.extend_from_slice(log); + log.clear(); + }); +} + +/// Logs its tag as it drops. +struct Item { + tag: u8, +} + +impl Drop for Item { + fn drop(&mut self) { + log(self.tag); + } +} + +/// The crate's own range, which logs each bound std asks it for: 0xa5 its start, 0xae its end. +struct Asked { + start: Bound, + end: Bound, +} + +impl RangeBounds for Asked { + fn start_bound(&self) -> Bound<&T> { + log(0xa5); + self.start.as_ref() + } + + fn end_bound(&self) -> Bound<&T> { + log(0xae); + self.end.as_ref() + } +} + +/// A key the crate orders by its own `Ord`, high to low, as rangemap's keys are by theirs. +#[derive(PartialEq, Eq)] +struct Key(u8); + +impl Ord for Key { + fn cmp(&self, other: &Self) -> Ordering { + other.0.cmp(&self.0) + } +} + +impl PartialOrd for Key { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +fn bound(kind: u8, at: T) -> Bound { + match kind % 3 { + 0 => Bound::Included(at), + 1 => Bound::Excluded(at), + _ => Bound::Unbounded, + } +} + +fn asked(s: u8, e: u8, a: T, b: T) -> Asked { + Asked { + start: bound(s, a), + end: bound(e, b), + } +} + +/// The keys of `set` within `range`, through a generic function, as each instance has it. +fn within>(set: &BTreeSet, range: R) -> usize { + set.range(range).count() +} + +/// `kind` picks the collection and how its range is given. The first two bytes pick the +/// kinds of the range's bounds, the next two their values, and the rest are the items. +pub fn bounds(kind: u8, data: &[u8], out: &mut Vec) { + // What a case that panicked left, which a translation outlives, as native Rust's trap does not. + LOG.with_borrow_mut(Vec::clear); + let &[s, e, a, b, ref data @ ..] = data else { + return; + }; + let reach = data.len() + 2; + let (i, j) = (a as usize % reach, b as usize % reach); + let mut items: Vec = data.iter().map(|&tag| Item { tag }).collect(); + match kind % 6 { + // A `Vec` drained by a pair of `Bound`s, of indices and of references to them. + 0 => { + let range = (bound(s, i), bound(e, j)); + for x in items.drain(range) { + out.push(x.tag); + flush(out); + } + let mut bytes = data.to_vec(); + out.extend(bytes.drain((bound(s, &i), bound(e, &j))).rev()); + out.extend(&bytes); + let mut words: Vec = data.iter().map(|&b| b as u32 + 1000).collect(); + let mut drain = words.drain(range); + out.extend(drain.next_back().map(|w| w as u8)); + out.push(drain.len() as u8); + drop(drain); + out.extend(words.iter().map(|&w| w as u8)); + } + // Asked its start, then its end, before the drain yields or drops anything. + 1 => { + let mut drain = items.drain(asked(s, e, i, j)); + flush(out); + out.extend(drain.next().map(|x| x.tag)); + flush(out); + drop(drain); + flush(out); + let mut bytes = data.to_vec(); + out.push(bytes.drain(asked(s, e, i, j)).count() as u8); + flush(out); + out.extend(&bytes); + } + // A `VecDeque` whose items wrap around its buffer, drained by each. + 2 => { + let mut queue: VecDeque = VecDeque::new(); + for (n, &tag) in data.iter().enumerate() { + match n % 2 { + 0 => queue.push_back(Item { tag }), + _ => queue.push_front(Item { tag }), + } + } + let mut drain = queue.drain((bound(s, i), bound(e, j))); + out.extend(drain.next_back().map(|x| x.tag)); + flush(out); + drop(drain); + flush(out); + out.extend(queue.iter().map(|x| x.tag)); + let reach = queue.len() + 1; + let drain = queue.drain(asked(s, e, a as usize % reach, b as usize % reach)); + flush(out); + out.push(drain.count() as u8); + flush(out); + out.extend(queue.iter().map(|x| x.tag)); + } + // A B-tree's ranges of keys, through `Bound`s and references to them. + 3 => { + let mut map: BTreeMap = data.iter().enumerate().map(|(n, &k)| (k, n as u8)).collect(); + let range = (bound(s, a), bound(e, b)); + for (k, v) in map.range(range) { + out.push(*k); + out.push(*v); + } + out.extend(map.range((bound(s, &a), bound(e, &b))).next_back().map(|(k, _)| *k)); + for (_, v) in map.range_mut(range) { + *v = v.wrapping_add(100); + } + out.extend(map.values()); + let set: BTreeSet = data.iter().copied().collect(); + out.extend(set.range(range).rev()); + out.push(within(&set, range) as u8); + } + // Asked nothing of an empty map, and its start and then its end of any other. + 4 => { + let empty: BTreeMap = BTreeMap::new(); + out.push(empty.range(asked(s, e, a, b)).count() as u8); + flush(out); + let set: BTreeSet = data.iter().copied().collect(); + let mut range = set.range(asked(s, e, a, b)); + flush(out); + out.extend(range.next()); + out.extend(range.next_back()); + out.push(within(&set, asked(s, e, a, b)) as u8); + flush(out); + } + // Keys the crate orders, ranged by references to them, as rangemap ranges. + _ => { + let (a, b) = (Key(a), Key(b)); + let set: BTreeSet = data.iter().map(|&k| Key(k)).collect(); + out.extend(set.range((bound(s, &a), bound(e, &b))).map(|k| k.0)); + let mut map: BTreeMap = data.iter().enumerate().map(|(n, &k)| (Key(k), n as u8)).collect(); + for (_, v) in map.range_mut((bound(s, &a), bound(e, &b))) { + *v ^= 0x80; + } + out.extend(map.values()); + } + } + flush(out); + out.extend(items.iter().map(|x| x.tag)); + drop(items); + flush(out); +} diff --git a/ts/fixtures/lang/src/harness.rs b/ts/fixtures/lang/src/harness.rs index f722e3c1d..0a27f457f 100644 --- a/ts/fixtures/lang/src/harness.rs +++ b/ts/fixtures/lang/src/harness.rs @@ -2,7 +2,7 @@ //! case and the rest is its input. use crate::{ - arrays, atomics, borrows, captures, cells, chars, chunks, collections, combinators, counts, cows, defaults, + arrays, atomics, borrows, bounds, captures, cells, chars, chunks, collections, combinators, counts, cows, defaults, durations, dyns, edits, floats, fmt, generics, hashes, holds, ints, iters, keys, locals, locks, nested, objects, orders, pointers, refs, shapes, slices, splits, statics, strs, utf8, wrappers, }; @@ -131,6 +131,7 @@ fn cases() -> Vec<(&'static str, Case)> { ("orders::bytes", |_, data, out| orders::bytes(data, out)), ("slices::parts", slices::parts), ("slices::drains", slices::drains), + ("bounds::bounds", bounds::bounds), ] } diff --git a/ts/fixtures/lang/src/lib.rs b/ts/fixtures/lang/src/lib.rs index 2dc6a1240..9c62e3a1f 100644 --- a/ts/fixtures/lang/src/lib.rs +++ b/ts/fixtures/lang/src/lib.rs @@ -9,8 +9,8 @@ //! `Cell`s, `RefCell`s, read-write locks, and thread-locals, the defaults of std types, //! 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`, `Vec::drain`, and -//! the order of bytes. +//! `DefaultHasher` and `RandomState`, slices' `windows` and `chunks`, `Vec::drain`, the +//! order 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. @@ -18,6 +18,7 @@ mod arrays; mod atomics; mod borrows; +mod bounds; mod captures; mod cells; mod chars; diff --git a/ts/fixtures/reject/census.md b/ts/fixtures/reject/census.md index e17c00e14..236da730f 100644 --- a/ts/fixtures/reject/census.md +++ b/ts/fixtures/reject/census.md @@ -1,7 +1,7 @@ # Census of `reject-fixture` -- Refusals: 105 -- Function instances: 117 +- Refusals: 104 +- Function instances: 120 - Generic functions no instance reached, lowered over their type parameters: 7 ## By construct @@ -20,11 +20,11 @@ | 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 | -| 2 | a range of `_`, whose bounds the runtime doesn't read, which is not supported yet | fixtures/reject/src/lib.rs:753:4 | | 2 | an iterator of the crate implementing `_`, which is not supported yet | fixtures/reject/src/lib.rs:169:1 | | 2 | constructing `_` from another crate | fixtures/reject/src/lib.rs:155:23 | | 2 | overriding `_`, which std's adaptors call in place of `next` or beside it | fixtures/reject/src/lib.rs:149:2 | | 2 | passing `_`, a closure that owns a value that drops, to `_`, which is not supported yet | fixtures/reject/src/lib.rs:372:13 | +| 2 | passing `_`, which drops, to `_`, which is not supported yet | fixtures/reject/src/lib.rs:380:4 | | 1 | `Buf::copy_to_bytes` of `_`, a value JS copies, which is not supported | fixtures/reject/src/lib.rs:725:2 | | 1 | `BufMut::put` of `_` into a `BytesMut`, which picks the methods it calls by a capacity that depends on whether the other handles of its storage dropped, which is not supported | fixtures/reject/src/lib.rs:443:2 | | 1 | `Bytes::from_owner` of `_`, which would drop with the last clone of what it makes, which is not supported | fixtures/reject/src/lib.rs:428:2 | @@ -71,7 +71,6 @@ | 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 `_` through `_`'s own `Hash::hash_slice`, which is not supported yet | fixtures/reject/src/lib.rs:363:16 | -| 1 | passing `_`, which drops, to `_`, which is not supported yet | fixtures/reject/src/lib.rs:380:4 | | 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 | @@ -92,7 +91,6 @@ The type or module each refused type or path belongs to. | 3 | `std::sync::Arc` | fixtures/reject/src/lib.rs:321:2 | | 3 | `u8` | fixtures/reject/src/lib.rs:289:6 | | 3 | `{closure}` | fixtures/reject/src/lib.rs:372:13 | -| 2 | `(std::ops::Bound, std::ops::Bound)` | fixtures/reject/src/lib.rs:753:4 | | 2 | `Poll` | fixtures/reject/src/lib.rs:91:20 | | 2 | `Scaled` | fixtures/reject/src/lib.rs:518:23 | | 2 | `dyn Scaled` | fixtures/reject/src/lib.rs:518:23 | @@ -123,8 +121,10 @@ The type or module each refused type or path belongs to. | 1 | `Noisy` | fixtures/reject/src/lib.rs:53:13 | | 1 | `Only` | fixtures/reject/src/lib.rs:701:11 | | 1 | `Span2` | fixtures/reject/src/lib.rs:669:10 | +| 1 | `Spent` | fixtures/reject/src/lib.rs:768:4 | | 1 | `T \| undefined` | fixtures/reject/src/lib.rs:31:2 | | 1 | `Tail` | fixtures/reject/src/lib.rs:545:30 | +| 1 | `Vec` | fixtures/reject/src/lib.rs:768:4 | | 1 | `Waiting` | fixtures/reject/src/lib.rs:556:2 | | 1 | `[u8; 4]` | fixtures/reject/src/lib.rs:535:26 | | 1 | `dyn Keyed` | fixtures/reject/src/lib.rs:593:32 | @@ -158,7 +158,6 @@ The type or module each refused type or path belongs to. | 2 | `_` is not supported: its `None` and `Some` would both be `rt.None` | `Option` | fixtures/reject/src/lib.rs:16:15 | | 2 | `_`, a trait object of another crate's trait, which is not supported yet | `dyn std::iter::Iterator` | fixtures/reject/src/lib.rs:11:16 | | 2 | `_`, a trait object of the generic trait `_`, which is not supported yet | `dyn Scaled`, `Scaled` | fixtures/reject/src/lib.rs:518:23 | -| 2 | a range of `_`, whose bounds the runtime doesn't read, which is not supported yet | `(std::ops::Bound, std::ops::Bound)` | fixtures/reject/src/lib.rs:753:4 | | 2 | an assignment through a reference to `_` | `std::pin::Pin` | fixtures/reject/src/lib.rs:126:2 | | 2 | replacing the value behind a reference to `_` | `std::pin::Pin` | fixtures/reject/src/lib.rs:110:2 | | 2 | unsupported call to `_` | `::sum` | fixtures/reject/src/lib.rs:483:17 | @@ -217,6 +216,7 @@ The type or module each refused type or path belongs to. | 1 | overriding `_`, which std's adaptors call in place of `next` or beside it | `Iterator::size_hint` | fixtures/reject/src/lib.rs:203:2 | | 1 | passing `_`, a closure that owns a value that drops, to `_`, which is not supported yet | `{closure}`, `Entry::or_insert_with` | fixtures/reject/src/lib.rs:372:13 | | 1 | passing `_`, a closure that owns a value that drops, to `_`, which is not supported yet | `{closure}`, `Iterator::for_each` | fixtures/reject/src/lib.rs:373:16 | +| 1 | passing `_`, which drops, to `_`, which is not supported yet | `Spent`, `Vec::drain` | fixtures/reject/src/lib.rs:768:4 | | 1 | passing `_`, which drops, to `_`, which is not supported yet | `{closure}`, `str::contains` | fixtures/reject/src/lib.rs:380:4 | | 1 | replacing the value behind a reference to `_` | `std::sync::MutexGuard` | fixtures/reject/src/lib.rs:131:2 | | 1 | union `_`, whose fields overlap in `_` and `f64`, which JS keeps apart | `Bits`, `u32` | fixtures/reject/src/lib.rs:643:1 | @@ -345,13 +345,6 @@ The type or module each refused type or path belongs to. -
a range of `_`, whose bounds the runtime doesn't read, which is not supported yet (2) - -- fixtures/reject/src/lib.rs:753:4: a range of `(std::ops::Bound, std::ops::Bound)`, whose bounds the runtime doesn't read, which is not supported yet -- fixtures/reject/src/lib.rs:753:52: a range of `(std::ops::Bound, std::ops::Bound)`, whose bounds the runtime doesn't read, which is not supported yet - -
-
an iterator of the crate implementing `_`, which is not supported yet (2) - fixtures/reject/src/lib.rs:169:1: an iterator of the crate implementing `DoubleEndedIterator`, which is not supported yet @@ -380,6 +373,13 @@ The type or module each refused type or path belongs to.
+
passing `_`, which drops, to `_`, which is not supported yet (2) + +- fixtures/reject/src/lib.rs:380:4: passing `{closure@fixtures/reject/src/lib.rs:380:13: 380:27}`, which drops, to `str::contains`, which is not supported yet +- fixtures/reject/src/lib.rs:768:4: passing `Spent`, which drops, to `Vec::drain`, which is not supported yet + +
+
`Buf::copy_to_bytes` of `_`, a value JS copies, which is not supported (1) - fixtures/reject/src/lib.rs:725:2: `Buf::copy_to_bytes` of `Blank`, a value JS copies, which is not supported @@ -656,12 +656,6 @@ The type or module each refused type or path belongs to.
-
passing `_`, which drops, to `_`, which is not supported yet (1) - -- fixtures/reject/src/lib.rs:380:4: passing `{closure@fixtures/reject/src/lib.rs:380:13: 380:27}`, which drops, to `str::contains`, which is not supported yet - -
-
union `_`, whose fields overlap in `_` and `f64`, which JS keeps apart (1) - fixtures/reject/src/lib.rs:643:1: union `Bits`, whose fields overlap in `u32` and `f64`, which JS keeps apart diff --git a/ts/fixtures/reject/expected.txt b/ts/fixtures/reject/expected.txt index 7f18935dd..3f1b867c5 100644 --- a/ts/fixtures/reject/expected.txt +++ b/ts/fixtures/reject/expected.txt @@ -95,8 +95,7 @@ a dangling pointer to `Only`, a value of no bytes made of more than structs and unsupported call to ` as std::iter::Iterator>::is_sorted` at fixtures/reject/src/lib.rs:735:15 comparing pointers to `u32` in an atomic, which JS has no address of at fixtures/reject/src/lib.rs:746:15 a constant of type `&u32`, a pointer into memory translation can't name at fixtures/reject/src/lib.rs:747:3 -a range of `(std::ops::Bound, std::ops::Bound)`, whose bounds the runtime doesn't read, which is not supported yet at fixtures/reject/src/lib.rs:753:4 -a range of `(std::ops::Bound, std::ops::Bound)`, whose bounds the runtime doesn't read, which is not supported yet at fixtures/reject/src/lib.rs:753:52 +passing `Spent`, which drops, to `Vec::drain`, which is not supported yet at fixtures/reject/src/lib.rs:768:4 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 83ca394ed..fbd5e2b47 100644 --- a/ts/fixtures/reject/src/lib.rs +++ b/ts/fixtures/reject/src/lib.rs @@ -747,8 +747,23 @@ pub fn pointer_statics(p: *mut u32) -> u32 { *NAMED } -/// Ranges of `Bound`s, which the runtime reads as unbounded where it reads no `start` or `end`. -pub fn bound_ranges(v: &mut Vec, map: &std::collections::BTreeMap) -> usize { - use std::ops::Bound::{Excluded, Included}; - v.drain((Excluded(0), Included(2))).count() + map.range((Excluded(0), Included(2))).count() +/// A range that drops, which std drops once it has read the range's bounds. +pub struct Spent; + +impl Drop for Spent { + fn drop(&mut self) {} +} + +impl std::ops::RangeBounds for Spent { + fn start_bound(&self) -> std::ops::Bound<&usize> { + std::ops::Bound::Unbounded + } + + fn end_bound(&self) -> std::ops::Bound<&usize> { + std::ops::Bound::Unbounded + } +} + +pub fn spent_range(v: &mut Vec) -> usize { + v.drain(Spent).count() } diff --git a/ts/fixtures/reject/ts.toml b/ts/fixtures/reject/ts.toml index bee7ca06f..63e568078 100644 --- a/ts/fixtures/reject/ts.toml +++ b/ts/fixtures/reject/ts.toml @@ -80,6 +80,6 @@ roots = [ "heap_optional", "places_sorted", "pointer_statics", - "bound_ranges", + "spent_range", ] printable = ["Meters"] diff --git a/ts/runtime/rt.ts b/ts/runtime/rt.ts index c98af748c..aa85113a7 100644 --- a/ts/runtime/rt.ts +++ b/ts/runtime/rt.ts @@ -248,7 +248,7 @@ export function at(slice: ArrayLike, index: number): T { } /** Panic unless `start..end` is in a slice of `length`, as std's range checks do. */ -function rangeCheck(length: number, start: number, end: number): void { +export function rangeCheck(length: number, start: number, end: number): void { if (start > length) panic(`range start index ${start} out of range for slice of length ${length}`); if (end > length) panic(`range end index ${end} out of range for slice of length ${length}`); if (start > end) panic(`slice index starts at ${start} but ends at ${end}`); diff --git a/ts/runtime/rt/collections.ts b/ts/runtime/rt/collections.ts index faf928323..be25f3bd8 100644 --- a/ts/runtime/rt/collections.ts +++ b/ts/runtime/rt/collections.ts @@ -14,10 +14,10 @@ // A function that may drop a key, value, or item std's would, rather than return or keep // it, takes what drops each that does, as `k`, `v`, or `f`, and drops it where std's does. -import { ByteSlice, type ByteVec, type None, type Place, panic } from "../rt.ts"; +import { ByteSlice, type ByteVec, type None, type Place, panic, type RangeBound, rangeCheck } from "../rt.ts"; import type { Hashing } from "./hash.ts"; import { type Limbs, lt } from "./int.ts"; -import { DONE, type Done, Iter, iterMap, iterSlice, type RangeInclusive, type Take } from "./iter.ts"; +import { DONE, type Done, Iter, iterMap, iterSlice, type RangeInclusive, rangeInclusiveEndBound, rangeInclusiveStartBound, type Take } from "./iter.ts"; import { type Arc, drop as dispose, dropAll, dropRange } from "./task.ts"; // Ordering @@ -1339,22 +1339,38 @@ export function btreeValues(m: BTreeMap, k?: (key: K) => void, v?: ( /** * A range of keys or indices, as its value is: `a..b`, `a..=b`, `a..` as `{ start }`, `..b` - * as `{ end }`, or `..` as `undefined`. + * as `{ end }`, `..` as `undefined`, and `(Bound, Bound)` as its pair. Any other + * `RangeBounds` is a function returning its `start_bound` and `end_bound`, called where + * std's calls them. */ export type Bounds = | { readonly start: K; readonly end: K } | RangeInclusive | { readonly start: K } | { readonly end: K; readonly inclusive?: true } + | BoundPair + | (() => BoundPair) | undefined; -/** `RangeBounds`: the start, the end, and whether the end is in the range, as `..=end`'s is. */ -function bounds(r: Bounds): [{ key: K } | undefined, { key: K } | undefined, boolean] { - if (r === undefined) return [undefined, undefined, false]; - // Exhausted by wrapping, an inclusive range excludes its start, as its `end_bound` says. +type BoundPair = readonly [RangeBound, RangeBound]; + +const UNBOUNDED: RangeBound = { tag: "Unbounded" }; + +/** Another `RangeBounds`, `r`, as `Bounds` reads it: by `start` and then `end`, once read. */ +export function boundsOf(r: R, start: (r: R) => RangeBound, end: (r: R) => RangeBound): () => BoundPair { + return () => [start(r), end(r)]; +} + +/** `RangeBounds`: `start_bound`, then `end_bound`. */ +function bounds(r: Bounds): BoundPair { + if (r === undefined) return [UNBOUNDED, UNBOUNDED]; + if (typeof r === "function") return r(); + if (Array.isArray(r)) return r as BoundPair; // A field test, not `instanceof`, so a bundle without `a..=b` leaves the class out. - if ("exhausted" in r) return [{ key: r.lo }, { key: r.exhausted ? r.lo : r.hi }, !r.exhausted]; - return ["start" in r ? { key: r.start } : undefined, "end" in r ? { key: r.end } : undefined, "inclusive" in r]; + if ("exhausted" in r) return [rangeInclusiveStartBound(r), rangeInclusiveEndBound(r)]; + const start: RangeBound = "start" in r ? { tag: "Included", _0: r.start } : UNBOUNDED; + if (!("end" in r)) return [start, UNBOUNDED]; + return [start, { tag: "inclusive" in r ? "Included" : "Excluded", _0: r.end }]; } /** `BTreeMap::range` and `range_mut`: the entries within `r`. */ @@ -1375,13 +1391,17 @@ export function btreeRangeKeys(m: BTreeMap, r: Bounds): Iter { /** The items of `m` within `r`, checked as std checks it. */ function range(m: BTreeMap, r: Bounds, pick: Pick, name: string): Iter { if (!m.r) return new BTreeIter(pick, m.t as Leaf, 0, m.t as Leaf, 0); - const [start, stop, inclusive] = bounds(r); - if (start !== undefined && stop !== undefined && m.c(start.key, stop.key) > 0) { - panic(`range start is greater than range end in ${name}`); + const [start, stop] = bounds(r); + if (start.tag !== "Unbounded" && stop.tag !== "Unbounded") { + const order = m.c(start._0, stop._0); + if (order === 0 && start.tag === "Excluded" && stop.tag === "Excluded") { + panic(`range start and end are equal and excluded in ${name}`); + } + if (order > 0) panic(`range start is greater than range end in ${name}`); } - const [fl, fi] = start === undefined ? [end(m, false), 0] : edge(m, start.key, false); + const [fl, fi] = start.tag === "Unbounded" ? [end(m, false), 0] : edge(m, start._0, start.tag === "Excluded"); const last = end(m, true); - const [bl, bi] = stop === undefined ? [last, last.k.length] : edge(m, stop.key, inclusive); + const [bl, bi] = stop.tag === "Unbounded" ? [last, last.k.length] : edge(m, stop._0, stop.tag === "Included"); return new BTreeIter(pick, fl, fi, bl, bi); } @@ -1916,16 +1936,28 @@ export function dequeDrain(q: VecDeque, r: Bounds, drop?: (item: T /** `slice::range`: the start and end of `r` within `n` elements, panicking as std's does. */ function sliceRange(r: Bounds, n: number): [number, number] { - const [lo, hi, inclusive] = bounds(r); - if (hi !== undefined && (inclusive ? hi.key >= n : hi.key > n)) panic(`range end index ${hi.key} out of range for slice of length ${n}`); - const end = hi === undefined ? n : inclusive ? hi.key + 1 : hi.key; - const start = lo === undefined ? 0 : lo.key; - if (start > end) { - panic(start > n ? `range start index ${start} out of range for slice of length ${n}` : `slice index starts at ${start} but ends at ${end}`); + const [lo, hi] = bounds(r); + let end = n; + if (hi.tag !== "Unbounded") { + end = hi._0; + if (hi.tag === "Included" ? end >= n : end > n) sliceIndexFail(0, end, n); + if (hi.tag === "Included") end++; + } + let start = 0; + if (lo.tag !== "Unbounded") { + start = lo._0; + if (lo.tag === "Excluded" ? start >= end : start > end) sliceIndexFail(start, end, n); + if (lo.tag === "Excluded") start++; } return [start, end]; } +/** std's `slice_index_fail`, which names the end where neither bound is out of order. */ +function sliceIndexFail(start: number, end: number, n: number): never { + rangeCheck(n, start, end); + return panic(`range end index ${end} out of range for slice of length ${n}`); +} + // `Vec::drain`, which leaves the vector only what comes before the range until the drain // drops and moves the tail back, so a drain that never drops, as `mem::forget` leaves // it, leaks the rest, as std's does. diff --git a/ts/src/lower/collections.rs b/ts/src/lower/collections.rs index c6afc7c76..043e3215c 100644 --- a/ts/src/lower/collections.rs +++ b/ts/src/lower/collections.rs @@ -645,6 +645,10 @@ fn btree<'tcx>( let key = arg(coll, 0); let value = if set { None } else { Some(arg(coll, 1)) }; let mut values = args; + if matches!(name, "range" | "range_mut") { + let range = values.pop()?; + values.push(bounds(b, call, range)); + } let last = |mut values: Vec| { values.push(Expr::lit("true")); values @@ -694,7 +698,6 @@ fn btree<'tcx>( }; Expr::rt(f, super::iter::tail(values, droppers)) } - (_, "range" | "range_mut") if let Some(msg) = unread_range(b, call) => b.err(span, msg), (_, "range") if set => Expr::rt("btreeRangeKeys", values), (_, "range_mut") if b.t.needs_place(arg(coll, 1)) => Expr::rt("btreeRangeMut", values), (_, "range" | "range_mut") => Expr::rt("btreeRange", values), @@ -854,8 +857,9 @@ fn deque<'tcx>( } (_, "iter_mut") if b.t.needs_place(item) => Expr::rt("dequeIterMut", values), (_, "iter" | "iter_mut") => Expr::rt("dequeIter", values), - (_, "drain") if let Some(msg) = unread_range(b, call) => b.err(span, msg), (_, "drain") => { + let range = values.pop()?; + values.push(bounds(b, call, range)); let droppers = entry_droppers(b, coll, span); Expr::rt("dequeDrain", super::iter::tail(values, droppers)) } @@ -1253,18 +1257,53 @@ fn bytes<'tcx>(b: &Body<'_, '_, 'tcx>, ty: Ty<'tcx>) -> bool { matches!(elem.kind(), ty::Uint(ty::UintTy::U8)) } -/// Why the runtime can't read the range `call` takes last as an `rt.Bounds`, if it can't: -/// it reads std's range types, not a tuple of `Bound`s or the crate's own `RangeBounds`. -pub fn unread_range<'tcx>(b: &Body<'_, '_, 'tcx>, call: &Call<'tcx>) -> Option { - let range = *b.t.sig(call.def_id, call.args).inputs().last()?; - match b.t.std_of(range) { - Some( - Std::Range | Std::RangeInclusive | Std::RangeFrom | Std::RangeTo | Std::RangeToInclusive | Std::RangeFull, - ) => None, - _ => Some(format!( - "a range of `{range}`, whose bounds the runtime doesn't read, which is not supported yet" - )), +/// Makes `range`, the range `call` takes last, what an `rt.Bounds` reads: std's range types +/// and a pair of `Bound`s are already, and any other `RangeBounds` becomes a function of its +/// `start_bound` and `end_bound`, which the runtime calls where std's does. +pub fn bounds<'tcx>(b: &mut Body<'_, '_, 'tcx>, call: &Call<'tcx>, range: Expr) -> Expr { + let tcx = b.t.tcx; + let span = call.span; + let sig = b.t.sig(call.def_id, call.args); + let ty = *sig.inputs().last().expect("a range"); + let pair = |t: &Ty<'tcx>| b.t.std_of(*t) == Some(Std::Bound); + match (ty.kind(), b.t.std_of(ty)) { + ( + _, + Some( + Std::Range + | Std::RangeInclusive + | Std::RangeFrom + | Std::RangeTo + | Std::RangeToInclusive + | Std::RangeFull, + ), + ) => return range, + (ty::Tuple(tys), _) if tys.len() == 2 && tys.iter().all(|t| pair(&t)) => return range, + // Each instance decides. + _ if b.t.deferred(ty) => return range, + _ => {} } + // `R: RangeBounds` of the function's own `R`, which says what `T` is. + let bounds = tcx.parent(trait_fn(tcx, "RangeBounds", "start_bound")); + let sig = tcx + .fn_sig(call.def_id) + .instantiate_identity() + .skip_norm_wip() + .skip_binder(); + let own = *sig.inputs().last().expect("a range"); + let target = tcx + .param_env(call.def_id) + .caller_bounds() + .find_map(|clause| { + let tr = clause.as_trait_clause()?.skip_binder().trait_ref; + (tr.def_id == bounds && tr.self_ty() == own).then(|| tr.args.type_at(1)) + }) + .expect("a range's `RangeBounds`"); + let target = b.t.subst(call.args, target); + let args = tcx.mk_args(&[ty.into(), target.into()]); + let start = b.fn_value(trait_fn(tcx, "RangeBounds", "start_bound"), args, span); + let end = b.fn_value(trait_fn(tcx, "RangeBounds", "end_bound"), args, span); + Expr::rt("boundsOf", vec![range, start, end]) } /// What `ty` holds if it is an `Arc`. diff --git a/ts/src/lower/library.rs b/ts/src/lower/library.rs index 708c6ef94..0c6559c88 100644 --- a/ts/src/lower/library.rs +++ b/ts/src/lower/library.rs @@ -1547,11 +1547,16 @@ fn mapping<'tcx>( rt2(values, of, &f) } // Of a range, which `Vec::drain::` takes. - ("Vec", "drain") if let Some(msg) = super::collections::unread_range(b, call) => b.err(span, msg), - ("Vec", "drain") if is_u8(targ(gargs, 0)) => Expr::rt("byteVecDrain", args), ("Vec", "drain") => { - let dropper = b.t.dropper(targ(gargs, 0), span); - Expr::rt("vecDrain", super::iter::tail(args, vec![dropper])) + let range = args.pop()?; + args.push(super::collections::bounds(b, call, range)); + match is_u8(targ(gargs, 0)) { + true => Expr::rt("byteVecDrain", args), + false => { + let dropper = b.t.dropper(targ(gargs, 0), span); + Expr::rt("vecDrain", super::iter::tail(args, vec![dropper])) + } + } } ("Vec", "as_slice" | "as_mut_slice") => { let v = args.pop()?; From 5dc4bac26fa08959ffc061ccf314f1a9f636ba20 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 30 Oct 2026 03:06:17 -0700 Subject: [PATCH 4/8] fix(ts): name no type in the range bound's expect, which the census test reads as a message Co-Authored-By: Claude Opus 5.5 --- ts/src/lower/collections.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ts/src/lower/collections.rs b/ts/src/lower/collections.rs index 043e3215c..3e1bad942 100644 --- a/ts/src/lower/collections.rs +++ b/ts/src/lower/collections.rs @@ -1298,7 +1298,7 @@ pub fn bounds<'tcx>(b: &mut Body<'_, '_, 'tcx>, call: &Call<'tcx>, range: Expr) let tr = clause.as_trait_clause()?.skip_binder().trait_ref; (tr.def_id == bounds && tr.self_ty() == own).then(|| tr.args.type_at(1)) }) - .expect("a range's `RangeBounds`"); + .expect("a range's bound"); let target = b.t.subst(call.args, target); let args = tcx.mk_args(&[ty.into(), target.into()]); let start = b.fn_value(trait_fn(tcx, "RangeBounds", "start_bound"), args, span); From 0b5a017a2f923064ff0303640747e3f06950341e Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 30 Oct 2026 03:11:08 -0700 Subject: [PATCH 5/8] fix(ts): refuse a range that drops wherever a drain or B-tree range takes one Co-Authored-By: Claude Opus 5.5 --- ts/fixtures/reject/src/lib.rs | 4 ++-- ts/src/lower/collections.rs | 12 ++++++++++-- ts/src/lower/library.rs | 7 ++++--- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/ts/fixtures/reject/src/lib.rs b/ts/fixtures/reject/src/lib.rs index fbd5e2b47..f98488db2 100644 --- a/ts/fixtures/reject/src/lib.rs +++ b/ts/fixtures/reject/src/lib.rs @@ -764,6 +764,6 @@ impl std::ops::RangeBounds for Spent { } } -pub fn spent_range(v: &mut Vec) -> usize { - v.drain(Spent).count() +pub fn spent_range(v: &mut Vec, q: &mut std::collections::VecDeque, map: &std::collections::BTreeMap) -> usize { + v.drain(Spent).count() + q.drain(Spent).count() + map.range(Spent).count() } diff --git a/ts/src/lower/collections.rs b/ts/src/lower/collections.rs index 3e1bad942..854fb1e5d 100644 --- a/ts/src/lower/collections.rs +++ b/ts/src/lower/collections.rs @@ -1258,13 +1258,21 @@ fn bytes<'tcx>(b: &Body<'_, '_, 'tcx>, ty: Ty<'tcx>) -> bool { } /// Makes `range`, the range `call` takes last, what an `rt.Bounds` reads: std's range types -/// and a pair of `Bound`s are already, and any other `RangeBounds` becomes a function of its -/// `start_bound` and `end_bound`, which the runtime calls where std's does. +/// and a pair of `Bound`s are already, and any other `RangeBounds` becomes `rt.boundsOf` of +/// it and its `start_bound` and `end_bound`, which the runtime calls where std's does. A +/// range that drops is refused, since std drops it once it has read the bounds. pub fn bounds<'tcx>(b: &mut Body<'_, '_, 'tcx>, call: &Call<'tcx>, range: Expr) -> Expr { let tcx = b.t.tcx; let span = call.span; let sig = b.t.sig(call.def_id, call.args); let ty = *sig.inputs().last().expect("a range"); + if b.t.drops(ty) { + let (owner, name) = library::key(tcx, call.def_id).expect("a function of std"); + return b.err( + span, + format!("passing `{ty}`, which drops, to `{owner}::{name}`, which is not supported yet"), + ); + } let pair = |t: &Ty<'tcx>| b.t.std_of(*t) == Some(Std::Bound); match (ty.kind(), b.t.std_of(ty)) { ( diff --git a/ts/src/lower/library.rs b/ts/src/lower/library.rs index 0c6559c88..7c22ff360 100644 --- a/ts/src/lower/library.rs +++ b/ts/src/lower/library.rs @@ -1546,17 +1546,18 @@ fn mapping<'tcx>( }; rt2(values, of, &f) } - // Of a range, which `Vec::drain::` takes. + // Of a range, which `Vec::drain::` takes, and which `bounds` refuses if it + // drops, as the other operand is the vector. ("Vec", "drain") => { let range = args.pop()?; args.push(super::collections::bounds(b, call, range)); - match is_u8(targ(gargs, 0)) { + return Some(match is_u8(targ(gargs, 0)) { true => Expr::rt("byteVecDrain", args), false => { let dropper = b.t.dropper(targ(gargs, 0), span); Expr::rt("vecDrain", super::iter::tail(args, vec![dropper])) } - } + }); } ("Vec", "as_slice" | "as_mut_slice") => { let v = args.pop()?; From 13fdb1358415a0fcc3052e558d3108ad0c0c1201 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 30 Oct 2026 03:16:26 -0700 Subject: [PATCH 6/8] style(ts): format the reject fixture's spent range Co-Authored-By: Claude Opus 5.5 --- ts/fixtures/reject/census.md | 20 ++++++++++---------- ts/fixtures/reject/expected.txt | 6 +++--- ts/fixtures/reject/src/lib.rs | 6 +++++- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/ts/fixtures/reject/census.md b/ts/fixtures/reject/census.md index 42ed29a5f..727d5852b 100644 --- a/ts/fixtures/reject/census.md +++ b/ts/fixtures/reject/census.md @@ -88,7 +88,7 @@ The type or module each refused type or path belongs to. | 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:768:4 | +| 3 | `Spent` | fixtures/reject/src/lib.rs:772:4 | | 3 | `std::sync::Arc` | fixtures/reject/src/lib.rs:321:2 | | 3 | `u8` | fixtures/reject/src/lib.rs:289:6 | | 3 | `{closure}` | fixtures/reject/src/lib.rs:372:13 | @@ -106,7 +106,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 | -| 1 | `BTreeMap` | fixtures/reject/src/lib.rs:768:56 | +| 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 | | 1 | `Bits` | fixtures/reject/src/lib.rs:643:1 | @@ -125,8 +125,8 @@ The type or module each refused type or path belongs to. | 1 | `Span2` | fixtures/reject/src/lib.rs:669:10 | | 1 | `T \| undefined` | fixtures/reject/src/lib.rs:31:2 | | 1 | `Tail` | fixtures/reject/src/lib.rs:545:30 | -| 1 | `VecDeque` | fixtures/reject/src/lib.rs:768:29 | -| 1 | `Vec` | fixtures/reject/src/lib.rs:768:4 | +| 1 | `VecDeque` | fixtures/reject/src/lib.rs:772:29 | +| 1 | `Vec` | fixtures/reject/src/lib.rs:772:4 | | 1 | `Waiting` | fixtures/reject/src/lib.rs:556:2 | | 1 | `[u8; 4]` | fixtures/reject/src/lib.rs:535:26 | | 1 | `dyn Keyed` | fixtures/reject/src/lib.rs:593:32 | @@ -218,9 +218,9 @@ The type or module each refused type or path belongs to. | 1 | overriding `_`, which std's adaptors call in place of `next` or beside it | `Iterator::size_hint` | fixtures/reject/src/lib.rs:203:2 | | 1 | passing `_`, a closure that owns a value that drops, to `_`, which is not supported yet | `{closure}`, `Entry::or_insert_with` | fixtures/reject/src/lib.rs:372:13 | | 1 | passing `_`, a closure that owns a value that drops, to `_`, which is not supported yet | `{closure}`, `Iterator::for_each` | fixtures/reject/src/lib.rs:373:16 | -| 1 | passing `_`, which drops, to `_`, which is not supported yet | `Spent`, `BTreeMap::range` | fixtures/reject/src/lib.rs:768:56 | -| 1 | passing `_`, which drops, to `_`, which is not supported yet | `Spent`, `Vec::drain` | fixtures/reject/src/lib.rs:768:4 | -| 1 | passing `_`, which drops, to `_`, which is not supported yet | `Spent`, `VecDeque::drain` | fixtures/reject/src/lib.rs:768:29 | +| 1 | passing `_`, which drops, to `_`, which is not supported yet | `Spent`, `BTreeMap::range` | fixtures/reject/src/lib.rs:772:56 | +| 1 | passing `_`, which drops, to `_`, which is not supported yet | `Spent`, `Vec::drain` | fixtures/reject/src/lib.rs:772:4 | +| 1 | passing `_`, which drops, to `_`, which is not supported yet | `Spent`, `VecDeque::drain` | fixtures/reject/src/lib.rs:772:29 | | 1 | passing `_`, which drops, to `_`, which is not supported yet | `{closure}`, `str::contains` | fixtures/reject/src/lib.rs:380:4 | | 1 | replacing the value behind a reference to `_` | `std::sync::MutexGuard` | fixtures/reject/src/lib.rs:131:2 | | 1 | union `_`, whose fields overlap in `_` and `f64`, which JS keeps apart | `Bits`, `u32` | fixtures/reject/src/lib.rs:643:1 | @@ -277,9 +277,9 @@ The type or module each refused type or path belongs to.
passing `_`, which drops, to `_`, which is not supported yet (4) - fixtures/reject/src/lib.rs:380:4: passing `{closure@fixtures/reject/src/lib.rs:380:13: 380:27}`, which drops, to `str::contains`, which is not supported yet -- fixtures/reject/src/lib.rs:768:4: passing `Spent`, which drops, to `Vec::drain`, which is not supported yet -- fixtures/reject/src/lib.rs:768:29: passing `Spent`, which drops, to `VecDeque::drain`, which is not supported yet -- fixtures/reject/src/lib.rs:768:56: passing `Spent`, which drops, to `BTreeMap::range`, which is not supported yet +- fixtures/reject/src/lib.rs:772:4: passing `Spent`, which drops, to `Vec::drain`, which is not supported yet +- fixtures/reject/src/lib.rs:772:29: passing `Spent`, which drops, to `VecDeque::drain`, which is not supported yet +- fixtures/reject/src/lib.rs:772:56: passing `Spent`, which drops, to `BTreeMap::range`, which is not supported yet
diff --git a/ts/fixtures/reject/expected.txt b/ts/fixtures/reject/expected.txt index 204cebaa8..b3c5a0317 100644 --- a/ts/fixtures/reject/expected.txt +++ b/ts/fixtures/reject/expected.txt @@ -95,9 +95,9 @@ a dangling pointer to `Only`, a value of no bytes made of more than structs and unsupported call to ` as std::iter::Iterator>::is_sorted` at fixtures/reject/src/lib.rs:735:15 comparing pointers to `u32` in an atomic, which JS has no address of at fixtures/reject/src/lib.rs:746:15 a constant of type `&u32`, a pointer into memory translation can't name at fixtures/reject/src/lib.rs:747:3 -passing `Spent`, which drops, to `Vec::drain`, which is not supported yet at fixtures/reject/src/lib.rs:768:4 -passing `Spent`, which drops, to `VecDeque::drain`, which is not supported yet at fixtures/reject/src/lib.rs:768:29 -passing `Spent`, which drops, to `BTreeMap::range`, which is not supported yet at fixtures/reject/src/lib.rs:768:56 +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 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 7cc7533d0..6fb387a52 100644 --- a/ts/fixtures/reject/src/lib.rs +++ b/ts/fixtures/reject/src/lib.rs @@ -764,7 +764,11 @@ impl std::ops::RangeBounds for Spent { } } -pub fn spent_range(v: &mut Vec, q: &mut std::collections::VecDeque, map: &std::collections::BTreeMap) -> usize { +pub fn spent_range( + v: &mut Vec, + q: &mut std::collections::VecDeque, + map: &std::collections::BTreeMap, +) -> usize { v.drain(Spent).count() + q.drain(Spent).count() + map.range(Spent).count() } From cd413d96653738f5d75c5ab370a6c81ef00e9968 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 30 Oct 2026 03:27:06 -0700 Subject: [PATCH 7/8] fix(ts): check a B-tree range's ends by the crate's own == and > as std does Co-Authored-By: Claude Opus 5.5 --- ts/fixtures/lang/expected/bounds.Key.ts | 16 ++++++- ts/fixtures/lang/expected/bounds.ts | 61 ++++++++++++++----------- ts/fixtures/lang/src/bounds.rs | 26 +++++++++-- ts/runtime/rt/collections.ts | 28 +++++++----- ts/src/lower/collections.rs | 25 +++++++--- 5 files changed, 105 insertions(+), 51 deletions(-) diff --git a/ts/fixtures/lang/expected/bounds.Key.ts b/ts/fixtures/lang/expected/bounds.Key.ts index 74657687f..4f84ac8fd 100644 --- a/ts/fixtures/lang/expected/bounds.Key.ts +++ b/ts/fixtures/lang/expected/bounds.Key.ts @@ -1,14 +1,28 @@ // Generated by rust2-ts. Do not edit. import * as rt from "./rt.ts"; +import { log } from "./bounds.ts"; -/** A key the crate orders by its own `Ord`, high to low, as rangemap's keys are by theirs. */ +/** + * A key the crate orders by its own `Ord`, high to low, as rangemap's keys are by theirs, + * which logs each `==` and `partial_cmp`: 0xe9 and 0xc9. + */ export interface Key { _0: rt.u8; } +export function eq$PartialEq(self: Key, other: Key): boolean { + log(233); + return self._0 === other._0; +} + export function cmp$Ord(self: Key, other: Key): rt.Ordering { return rt.cmp(other._0, self._0); } +export function partialCmp$PartialOrd(self: Key, other: Key): rt.Ordering | undefined { + log(201); + return cmp$Ord(self, other); +} + //# sourceMappingURL=bounds.Key.ts.map diff --git a/ts/fixtures/lang/expected/bounds.ts b/ts/fixtures/lang/expected/bounds.ts index 5d769ab1a..08a73fb7f 100644 --- a/ts/fixtures/lang/expected/bounds.ts +++ b/ts/fixtures/lang/expected/bounds.ts @@ -139,8 +139,8 @@ export function bounds(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { const _49: [rt.RangeBound, rt.RangeBound] = [bound$usize(s, i), bound$usize(e, j)]; rt.iterExtendBytes(out, rt.iterRev(rt.byteVecDrain(bytes, _49)), "fold"); rt.iterExtendBytes(out, rt.iterSlice(rt.byteVecView(bytes))); - const words: rt.u32[] = rt.iterToArray(rt.iterMap(rt.iterSlice(rt.byteSliceView(rt.byteSliceRange(data, 4))), (_217: rt.u8): rt.u32 => { - return rt.u32Add(_217 as rt.u32, 1000); + const words: rt.u32[] = rt.iterToArray(rt.iterMap(rt.iterSlice(rt.byteSliceView(rt.byteSliceRange(data, 4))), (_214: rt.u8): rt.u32 => { + return rt.u32Add(_214 as rt.u32, 1000); })); const drain: rt.Iter = rt.vecDrain(words, range); rt.iterExtendBytes(out, rt.iterOption(map$0a57co2(rt.iterToOption(drain.nextBack()), (w: rt.u32): rt.u8 => { @@ -148,17 +148,17 @@ export function bounds(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { }))); rt.byteVecPush(out, drain.len() & 0xff); rt.drop(drain); - rt.iterExtendBytes(out, rt.iterMap(rt.iterSlice(words), (_220: rt.u32): rt.u8 => { - return _220 & 0xff; + rt.iterExtendBytes(out, rt.iterMap(rt.iterSlice(words), (_216: rt.u32): rt.u8 => { + return _216 & 0xff; })); } else if (_28 === 1) { const _78: Asked.Asked = asked$usize(s, e, i, j); const drain2: rt.Iter = rt.vecDrain(items, rt.boundsOf(_78, Asked.startBound$RangeBounds_usize_usize, Asked.endBound$RangeBounds_usize_usize), rt.drop); flush(out); rt.iterExtendBytes(out, rt.iterOption(map$1iekgsb(rt.iterToOption(drain2.next()), (x5: Item.Item): rt.u8 => { - const _010: rt.u8 = x5.tag; + const _08: rt.u8 = x5.tag; x5[Symbol.dispose](); - return _010; + return _08; }))); flush(out); rt.drop(drain2); @@ -187,9 +187,9 @@ export function bounds(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { const _117: [rt.RangeBound, rt.RangeBound] = [bound$usize$2(s, i), bound$usize$2(e, j)]; const drain3: rt.Iter = rt.dequeDrain(queue, _117, rt.drop); rt.iterExtendBytes(out, rt.iterOption(map$1iekgsb(rt.iterToOption(drain3.nextBack()), (x2: Item.Item): rt.u8 => { - const _07: rt.u8 = x2.tag; + const _05: rt.u8 = x2.tag; x2[Symbol.dispose](); - return _07; + return _05; }))); flush(out); rt.drop(drain3); @@ -208,8 +208,8 @@ export function bounds(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { })); rt.dequeDrop(queue, rt.drop); } else if (_28 === 3) { - const _155: rt.Iter<[rt.u8, rt.u8]> = rt.iterMap(rt.iterEnumerate(rt.iterSlice(rt.byteSliceView(rt.byteSliceRange(data, 4)))), (_214: [rt.usize, rt.u8]): [rt.u8, rt.u8] => { - return [_214[1], _214[0] & 0xff] as [rt.u8, rt.u8]; + const _155: rt.Iter<[rt.u8, rt.u8]> = rt.iterMap(rt.iterEnumerate(rt.iterSlice(rt.byteSliceView(rt.byteSliceRange(data, 4)))), (_25: [rt.usize, rt.u8]): [rt.u8, rt.u8] => { + return [_25[1], _25[0] & 0xff] as [rt.u8, rt.u8]; }); const map: rt.BTreeMap = rt.btreeExtend(rt.btreeNew(rt.cmp) as rt.BTreeMap, _155, true); const range2: [rt.RangeBound, rt.RangeBound] = [bound$u8$2(s, a), bound$u8$2(e, b)]; @@ -223,8 +223,8 @@ export function bounds(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { rt.byteVecPush(out, _165[1]); } const _180: [rt.RangeBound, rt.RangeBound] = [bound$u8(s, a), bound$u8(e, b)]; - rt.iterExtendBytes(out, rt.iterOption(map$1hdkvwd(rt.iterToOption(rt.btreeRange(map, _180).nextBack()), (_216: [rt.u8, rt.u8]): rt.u8 => { - return _216[0]; + rt.iterExtendBytes(out, rt.iterOption(map$1hdkvwd(rt.iterToOption(rt.btreeRange(map, _180).nextBack()), (_210: [rt.u8, rt.u8]): rt.u8 => { + return _210[0]; }))); const iter4: rt.Iter<[rt.u8, rt.Place]> = rt.btreeRangeMut(map, range2); while (true) { @@ -259,27 +259,34 @@ export function bounds(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { const _238: [Key.Key, Key.Key] = [{ _0: a }, { _0: b }]; const a2: Key.Key = _238[0]; const b2: Key.Key = _238[1]; - const _242: rt.Iter = rt.iterMap(rt.iterSlice(rt.byteSliceView(rt.byteSliceRange(data, 4))), (_25: rt.u8): Key.Key => { - return { _0: _25 }; - }); - rt.iterExtendBytes(out, rt.iterMap(rt.btreeRangeKeys(rt.btreeExtendKeys(rt.btreeNew(Key.cmp$Ord) as rt.BTreeSet, _242, true), [bound$Key(s, a2), bound$Key(e, b2)] as [rt.RangeBound, rt.RangeBound]), (k2: Key.Key): rt.u8 => { + const set3: rt.BTreeSet = rt.btreeNew(Key.cmp$Ord) as rt.BTreeSet; + const map2: rt.BTreeMap = rt.btreeNew(Key.cmp$Ord) as rt.BTreeMap; + const iter5: rt.Iter<[rt.usize, rt.u8]> = rt.iterEnumerate(rt.iterSlice(rt.byteSliceView(rt.byteSliceRange(data, 4)))); + while (true) { + const _247: [rt.usize, rt.u8] | rt.Done = iter5.next(); + if (_247 === rt.DONE) { + break; + } + const n2: rt.usize = _247[0]; + const k: rt.u8 = _247[1]; + rt.btreeAdd(set3, { _0: k }); + rt.btreeInsert(map2, { _0: k }, n2 & 0xff); + } + const _263: [rt.RangeBound, rt.RangeBound] = [bound$Key(s, a2), bound$Key(e, b2)]; + rt.iterExtendBytes(out, rt.iterMap(rt.btreeRangeKeys(set3, _263, Key.eq$PartialEq, rt.cmpGt(Key.partialCmp$PartialOrd)), (k2: Key.Key): rt.u8 => { return k2._0; })); - const _254: rt.Iter<[Key.Key, rt.u8]> = rt.iterMap(rt.iterEnumerate(rt.iterSlice(rt.byteSliceView(rt.byteSliceRange(data, 4)))), (_210: [rt.usize, rt.u8]): [Key.Key, rt.u8] => { - const n2: rt.usize = _210[0]; - const _53: Key.Key = { _0: _210[1] }; - return [_53, n2 & 0xff] as [Key.Key, rt.u8]; - }); - const map2: rt.BTreeMap = rt.btreeExtend(rt.btreeNew(Key.cmp$Ord) as rt.BTreeMap, _254, true); - const _260: [rt.RangeBound, rt.RangeBound] = [bound$Key(s, a2), bound$Key(e, b2)]; - const iter5: rt.Iter<[Key.Key, rt.Place]> = rt.btreeRangeMut(map2, _260); + flush(out); + const _272: [rt.RangeBound, rt.RangeBound] = [bound$Key(s, a2), bound$Key(e, b2)]; + const iter6: rt.Iter<[Key.Key, rt.Place]> = rt.btreeRangeMut(map2, _272, Key.eq$PartialEq, rt.cmpGt(Key.partialCmp$PartialOrd)); while (true) { - const _266: [Key.Key, rt.Place] | rt.Done = iter5.next(); - if (_266 === rt.DONE) { + const _278: [Key.Key, rt.Place] | rt.Done = iter6.next(); + if (_278 === rt.DONE) { break; } - _266[1].v = _266[1].v ^ 128; + _278[1].v = _278[1].v ^ 128; } + flush(out); rt.iterExtendBytes(out, rt.btreeValues(map2)); } flush(out); diff --git a/ts/fixtures/lang/src/bounds.rs b/ts/fixtures/lang/src/bounds.rs index 9e4ed7526..a267469ff 100644 --- a/ts/fixtures/lang/src/bounds.rs +++ b/ts/fixtures/lang/src/bounds.rs @@ -53,10 +53,18 @@ impl RangeBounds for Asked { } } -/// A key the crate orders by its own `Ord`, high to low, as rangemap's keys are by theirs. -#[derive(PartialEq, Eq)] +/// A key the crate orders by its own `Ord`, high to low, as rangemap's keys are by theirs, +/// which logs each `==` and `partial_cmp`: 0xe9 and 0xc9. +#[derive(Eq)] struct Key(u8); +impl PartialEq for Key { + fn eq(&self, other: &Self) -> bool { + log(0xe9); + self.0 == other.0 + } +} + impl Ord for Key { fn cmp(&self, other: &Self) -> Ordering { other.0.cmp(&self.0) @@ -65,6 +73,7 @@ impl Ord for Key { impl PartialOrd for Key { fn partial_cmp(&self, other: &Self) -> Option { + log(0xc9); Some(self.cmp(other)) } } @@ -183,15 +192,22 @@ pub fn bounds(kind: u8, data: &[u8], out: &mut Vec) { out.push(within(&set, asked(s, e, a, b)) as u8); flush(out); } - // Keys the crate orders, ranged by references to them, as rangemap ranges. + // Keys the crate orders, ranged by references to them, as rangemap ranges, whose ends + // std checks by `==` and `>`. _ => { let (a, b) = (Key(a), Key(b)); - let set: BTreeSet = data.iter().map(|&k| Key(k)).collect(); + let mut set = BTreeSet::new(); + let mut map = BTreeMap::new(); + for (n, &k) in data.iter().enumerate() { + set.insert(Key(k)); + map.insert(Key(k), n as u8); + } out.extend(set.range((bound(s, &a), bound(e, &b))).map(|k| k.0)); - let mut map: BTreeMap = data.iter().enumerate().map(|(n, &k)| (Key(k), n as u8)).collect(); + flush(out); for (_, v) in map.range_mut((bound(s, &a), bound(e, &b))) { *v ^= 0x80; } + flush(out); out.extend(map.values()); } } diff --git a/ts/runtime/rt/collections.ts b/ts/runtime/rt/collections.ts index be25f3bd8..5bb59c58b 100644 --- a/ts/runtime/rt/collections.ts +++ b/ts/runtime/rt/collections.ts @@ -1373,31 +1373,35 @@ function bounds(r: Bounds): BoundPair { return [start, { tag: "inclusive" in r ? "Included" : "Excluded", _0: r.end }]; } -/** `BTreeMap::range` and `range_mut`: the entries within `r`. */ -export function btreeRange(m: BTreeMap, r: Bounds): Iter<[K, V]> { - return range(m, r, ENTRIES, "BTreeMap"); +/** + * `BTreeMap::range` and `range_mut`: the entries within `r`. A key of the crate's own gives + * its `PartialEq::eq` and `PartialOrd::gt`, which std checks the ends by. + */ +export function btreeRange(m: BTreeMap, r: Bounds, eq?: Test, gt?: Test): Iter<[K, V]> { + return range(m, r, ENTRIES, "BTreeMap", eq, gt); } /** `range_mut` of values JS copies: each key within `r` and the place of its value. */ -export function btreeRangeMut(m: BTreeMap, r: Bounds): Iter<[K, Place]> { - return range(m, r, PLACES, "BTreeMap"); +export function btreeRangeMut(m: BTreeMap, r: Bounds, eq?: Test, gt?: Test): Iter<[K, Place]> { + return range(m, r, PLACES, "BTreeMap", eq, gt); } /** `BTreeSet::range`: the keys within `r`. */ -export function btreeRangeKeys(m: BTreeMap, r: Bounds): Iter { - return range(m, r, KEYS, "BTreeSet"); +export function btreeRangeKeys(m: BTreeMap, r: Bounds, eq?: Test, gt?: Test): Iter { + return range(m, r, KEYS, "BTreeSet", eq, gt); } -/** The items of `m` within `r`, checked as std checks it. */ -function range(m: BTreeMap, r: Bounds, pick: Pick, name: string): Iter { +/** The items of `m` within `r`, checked as std checks it: by `eq` and `gt`, or by `m`'s order. */ +function range(m: BTreeMap, r: Bounds, pick: Pick, name: string, eq?: Test, gt?: Test): Iter { if (!m.r) return new BTreeIter(pick, m.t as Leaf, 0, m.t as Leaf, 0); const [start, stop] = bounds(r); if (start.tag !== "Unbounded" && stop.tag !== "Unbounded") { - const order = m.c(start._0, stop._0); - if (order === 0 && start.tag === "Excluded" && stop.tag === "Excluded") { + const s = start._0; + const e = stop._0; + if (start.tag === "Excluded" && stop.tag === "Excluded" && (eq === undefined ? m.c(s, e) === 0 : eq(s, e))) { panic(`range start and end are equal and excluded in ${name}`); } - if (order > 0) panic(`range start is greater than range end in ${name}`); + if (gt === undefined ? m.c(s, e) > 0 : gt(s, e)) panic(`range start is greater than range end in ${name}`); } const [fl, fi] = start.tag === "Unbounded" ? [end(m, false), 0] : edge(m, start._0, start.tag === "Excluded"); const last = end(m, true); diff --git a/ts/src/lower/collections.rs b/ts/src/lower/collections.rs index 854fb1e5d..98901d569 100644 --- a/ts/src/lower/collections.rs +++ b/ts/src/lower/collections.rs @@ -648,6 +648,13 @@ fn btree<'tcx>( if matches!(name, "range" | "range_mut") { let range = values.pop()?; values.push(bounds(b, call, range)); + // std checks the ends by `==` and `>`, which a std key does as its `Ord` does, and + // the crate's own by its `PartialEq` and `PartialOrd`. + let target = range_target(b, call); + if !std_key(b, target) { + values.push(eq_fn(b, target, span)); + values.push(test_fn(b, target, "gt", span)); + } } let last = |mut values: Vec| { values.push(Expr::lit("true")); @@ -1291,7 +1298,17 @@ pub fn bounds<'tcx>(b: &mut Body<'_, '_, 'tcx>, call: &Call<'tcx>, range: Expr) _ if b.t.deferred(ty) => return range, _ => {} } - // `R: RangeBounds` of the function's own `R`, which says what `T` is. + let target = range_target(b, call); + let args = tcx.mk_args(&[ty.into(), target.into()]); + let start = b.fn_value(trait_fn(tcx, "RangeBounds", "start_bound"), args, span); + let end = b.fn_value(trait_fn(tcx, "RangeBounds", "end_bound"), args, span); + Expr::rt("boundsOf", vec![range, start, end]) +} + +/// The `T` of the `RangeBounds` that the range `call` takes last implements, from the +/// function's own bound on its range's type. +fn range_target<'tcx>(b: &Body<'_, '_, 'tcx>, call: &Call<'tcx>) -> Ty<'tcx> { + let tcx = b.t.tcx; let bounds = tcx.parent(trait_fn(tcx, "RangeBounds", "start_bound")); let sig = tcx .fn_sig(call.def_id) @@ -1307,11 +1324,7 @@ pub fn bounds<'tcx>(b: &mut Body<'_, '_, 'tcx>, call: &Call<'tcx>, range: Expr) (tr.def_id == bounds && tr.self_ty() == own).then(|| tr.args.type_at(1)) }) .expect("a range's bound"); - let target = b.t.subst(call.args, target); - let args = tcx.mk_args(&[ty.into(), target.into()]); - let start = b.fn_value(trait_fn(tcx, "RangeBounds", "start_bound"), args, span); - let end = b.fn_value(trait_fn(tcx, "RangeBounds", "end_bound"), args, span); - Expr::rt("boundsOf", vec![range, start, end]) + b.t.subst(call.args, target) } /// What `ty` holds if it is an `Arc`. From 486f363a2644ed86282bdaa3b5692b57182aa6d7 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 30 Oct 2026 03:34:34 -0700 Subject: [PATCH 8/8] fix(ts): check the ends of a range of std types holding the crate's own by its comparisons Co-Authored-By: Claude Opus 5.5 --- ts/fixtures/lang/expected/bounds.ts | 51 +++++++++++++++++++++-------- ts/fixtures/lang/src/bounds.rs | 8 +++++ ts/src/lower/collections.rs | 8 +++-- 3 files changed, 50 insertions(+), 17 deletions(-) diff --git a/ts/fixtures/lang/expected/bounds.ts b/ts/fixtures/lang/expected/bounds.ts index 08a73fb7f..09894dbd9 100644 --- a/ts/fixtures/lang/expected/bounds.ts +++ b/ts/fixtures/lang/expected/bounds.ts @@ -60,6 +60,17 @@ export function bound$u8$2(kind: rt.u8, at: rt.u8): rt.RangeBound { return { tag: "Unbounded" } as rt.RangeBound; } +export function bound$u8_Key(kind: rt.u8, at: [rt.u8, Key.Key]): rt.RangeBound<[rt.u8, Key.Key]> { + const _3: rt.u8 = rt.u8Rem(kind, 3); + if (_3 === 0) { + return { tag: "Included", _0: at } as rt.RangeBound<[rt.u8, Key.Key]>; + } + if (_3 === 1) { + return { tag: "Excluded", _0: at } as rt.RangeBound<[rt.u8, Key.Key]>; + } + return { tag: "Unbounded" } as rt.RangeBound<[rt.u8, Key.Key]>; +} + export function bound$usize(kind: rt.u8, at: rt.usize): rt.RangeBound { const _3: rt.u8 = rt.u8Rem(kind, 3); if (_3 === 0) { @@ -139,8 +150,8 @@ export function bounds(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { const _49: [rt.RangeBound, rt.RangeBound] = [bound$usize(s, i), bound$usize(e, j)]; rt.iterExtendBytes(out, rt.iterRev(rt.byteVecDrain(bytes, _49)), "fold"); rt.iterExtendBytes(out, rt.iterSlice(rt.byteVecView(bytes))); - const words: rt.u32[] = rt.iterToArray(rt.iterMap(rt.iterSlice(rt.byteSliceView(rt.byteSliceRange(data, 4))), (_214: rt.u8): rt.u32 => { - return rt.u32Add(_214 as rt.u32, 1000); + const words: rt.u32[] = rt.iterToArray(rt.iterMap(rt.iterSlice(rt.byteSliceView(rt.byteSliceRange(data, 4))), (_216: rt.u8): rt.u32 => { + return rt.u32Add(_216 as rt.u32, 1000); })); const drain: rt.Iter = rt.vecDrain(words, range); rt.iterExtendBytes(out, rt.iterOption(map$0a57co2(rt.iterToOption(drain.nextBack()), (w: rt.u32): rt.u8 => { @@ -148,17 +159,17 @@ export function bounds(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { }))); rt.byteVecPush(out, drain.len() & 0xff); rt.drop(drain); - rt.iterExtendBytes(out, rt.iterMap(rt.iterSlice(words), (_216: rt.u32): rt.u8 => { - return _216 & 0xff; + rt.iterExtendBytes(out, rt.iterMap(rt.iterSlice(words), (_217: rt.u32): rt.u8 => { + return _217 & 0xff; })); } else if (_28 === 1) { const _78: Asked.Asked = asked$usize(s, e, i, j); const drain2: rt.Iter = rt.vecDrain(items, rt.boundsOf(_78, Asked.startBound$RangeBounds_usize_usize, Asked.endBound$RangeBounds_usize_usize), rt.drop); flush(out); rt.iterExtendBytes(out, rt.iterOption(map$1iekgsb(rt.iterToOption(drain2.next()), (x5: Item.Item): rt.u8 => { - const _08: rt.u8 = x5.tag; + const _09: rt.u8 = x5.tag; x5[Symbol.dispose](); - return _08; + return _09; }))); flush(out); rt.drop(drain2); @@ -187,9 +198,9 @@ export function bounds(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { const _117: [rt.RangeBound, rt.RangeBound] = [bound$usize$2(s, i), bound$usize$2(e, j)]; const drain3: rt.Iter = rt.dequeDrain(queue, _117, rt.drop); rt.iterExtendBytes(out, rt.iterOption(map$1iekgsb(rt.iterToOption(drain3.nextBack()), (x2: Item.Item): rt.u8 => { - const _05: rt.u8 = x2.tag; + const _06: rt.u8 = x2.tag; x2[Symbol.dispose](); - return _05; + return _06; }))); flush(out); rt.drop(drain3); @@ -208,8 +219,8 @@ export function bounds(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { })); rt.dequeDrop(queue, rt.drop); } else if (_28 === 3) { - const _155: rt.Iter<[rt.u8, rt.u8]> = rt.iterMap(rt.iterEnumerate(rt.iterSlice(rt.byteSliceView(rt.byteSliceRange(data, 4)))), (_25: [rt.usize, rt.u8]): [rt.u8, rt.u8] => { - return [_25[1], _25[0] & 0xff] as [rt.u8, rt.u8]; + const _155: rt.Iter<[rt.u8, rt.u8]> = rt.iterMap(rt.iterEnumerate(rt.iterSlice(rt.byteSliceView(rt.byteSliceRange(data, 4)))), (_210: [rt.usize, rt.u8]): [rt.u8, rt.u8] => { + return [_210[1], _210[0] & 0xff] as [rt.u8, rt.u8]; }); const map: rt.BTreeMap = rt.btreeExtend(rt.btreeNew(rt.cmp) as rt.BTreeMap, _155, true); const range2: [rt.RangeBound, rt.RangeBound] = [bound$u8$2(s, a), bound$u8$2(e, b)]; @@ -223,8 +234,8 @@ export function bounds(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { rt.byteVecPush(out, _165[1]); } const _180: [rt.RangeBound, rt.RangeBound] = [bound$u8(s, a), bound$u8(e, b)]; - rt.iterExtendBytes(out, rt.iterOption(map$1hdkvwd(rt.iterToOption(rt.btreeRange(map, _180).nextBack()), (_210: [rt.u8, rt.u8]): rt.u8 => { - return _210[0]; + rt.iterExtendBytes(out, rt.iterOption(map$1hdkvwd(rt.iterToOption(rt.btreeRange(map, _180).nextBack()), (_214: [rt.u8, rt.u8]): rt.u8 => { + return _214[0]; }))); const iter4: rt.Iter<[rt.u8, rt.Place]> = rt.btreeRangeMut(map, range2); while (true) { @@ -273,8 +284,8 @@ export function bounds(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { rt.btreeInsert(map2, { _0: k }, n2 & 0xff); } const _263: [rt.RangeBound, rt.RangeBound] = [bound$Key(s, a2), bound$Key(e, b2)]; - rt.iterExtendBytes(out, rt.iterMap(rt.btreeRangeKeys(set3, _263, Key.eq$PartialEq, rt.cmpGt(Key.partialCmp$PartialOrd)), (k2: Key.Key): rt.u8 => { - return k2._0; + rt.iterExtendBytes(out, rt.iterMap(rt.btreeRangeKeys(set3, _263, Key.eq$PartialEq, rt.cmpGt(Key.partialCmp$PartialOrd)), (k3: Key.Key): rt.u8 => { + return k3._0; })); flush(out); const _272: [rt.RangeBound, rt.RangeBound] = [bound$Key(s, a2), bound$Key(e, b2)]; @@ -288,6 +299,18 @@ export function bounds(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { } flush(out); rt.iterExtendBytes(out, rt.btreeValues(map2)); + const pairs: rt.BTreeSet<[rt.u8, Key.Key]> = rt.btreeNew(rt.cmpTuple([rt.cmp, Key.cmp$Ord])) as rt.BTreeSet<[rt.u8, Key.Key]>; + for (const k2 of rt.byteSliceView(rt.byteSliceRange(data, 4))) { + rt.btreeAdd(pairs, [k2, { _0: k2 }] as [rt.u8, Key.Key]); + } + const _299: [[rt.u8, Key.Key], [rt.u8, Key.Key]] = [[a2._0, a2] as [rt.u8, Key.Key], [b2._0, b2] as [rt.u8, Key.Key]]; + const a3: [rt.u8, Key.Key] = _299[0]; + const b3: [rt.u8, Key.Key] = _299[1]; + const _308: [rt.RangeBound<[rt.u8, Key.Key]>, rt.RangeBound<[rt.u8, Key.Key]>] = [bound$u8_Key(s, a3), bound$u8_Key(e, b3)]; + rt.iterExtendBytes(out, rt.iterMap(rt.btreeRangeKeys(pairs, _308, rt.eqTuple([rt.same, Key.eq$PartialEq]), rt.cmpGt(rt.cmpTuple([rt.cmp, Key.partialCmp$PartialOrd]))), (_25: [rt.u8, Key.Key]): rt.u8 => { + return _25[0]; + })); + flush(out); } flush(out); rt.iterExtendBytes(out, rt.iterMap(rt.iterSlice(items), (x6: Item.Item): rt.u8 => { diff --git a/ts/fixtures/lang/src/bounds.rs b/ts/fixtures/lang/src/bounds.rs index a267469ff..19fc03aca 100644 --- a/ts/fixtures/lang/src/bounds.rs +++ b/ts/fixtures/lang/src/bounds.rs @@ -209,6 +209,14 @@ pub fn bounds(kind: u8, data: &[u8], out: &mut Vec) { } flush(out); out.extend(map.values()); + // Held in a std type, which compares by it. + let mut pairs = BTreeSet::new(); + for &k in data { + pairs.insert((k, Key(k))); + } + let (a, b) = ((a.0, a), (b.0, b)); + out.extend(pairs.range((bound(s, &a), bound(e, &b))).map(|(k, _)| *k)); + flush(out); } } flush(out); diff --git a/ts/src/lower/collections.rs b/ts/src/lower/collections.rs index 98901d569..75927ee44 100644 --- a/ts/src/lower/collections.rs +++ b/ts/src/lower/collections.rs @@ -648,10 +648,12 @@ fn btree<'tcx>( if matches!(name, "range" | "range_mut") { let range = values.pop()?; values.push(bounds(b, call, range)); - // std checks the ends by `==` and `>`, which a std key does as its `Ord` does, and - // the crate's own by its `PartialEq` and `PartialOrd`. + // std checks the ends by `==` and `>`, which a key of std's alone does as its `Ord` + // does, and one holding the crate's own type by that type's `PartialEq` and + // `PartialOrd`. let target = range_target(b, call); - if !std_key(b, target) { + let own = |t: Ty<'tcx>| matches!(t.kind(), ty::Adt(adt, _) if b.t.own(adt.did())); + if target.walk().filter_map(|arg| arg.as_type()).any(own) { values.push(eq_fn(b, target, span)); values.push(test_fn(b, target, "gt", span)); }