From dc2529be8951f8de4d9c37f65f12cf1efbf56888 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Thu, 29 Oct 2026 21:10:12 -0700 Subject: [PATCH 1/6] chore(quest): claim quest/m3/ts-ordering.md Quest-Claim: ae5726858cb8e8a0 From fc266652d2ef96b4b9aec7175d89d33f8974e237 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Thu, 29 Oct 2026 22:12:06 -0700 Subject: [PATCH 2/6] feat(ts): translate cmp::max and min, is_sorted, and BinaryHeap `cmp::max`, `min`, and their `_by` and `_by_key` forms lower from std's MIR. The `is_sorted` family of slices and iterators maps to runtime ports of std's loops, and `BinaryHeap` with `PeekMut` to a runtime port of std's sifts. Integer comparisons pass `rt.lt` and its siblings instead of a test built from `cmp`, which the bench shows costs in Firefox. Co-Authored-By: Claude Opus 5.5 --- quest/m3/README.md | 2 +- quest/m3/ts-ordering.md | 22 -- quest/m3/ts-slice-gaps.md | 17 + ts/MIGRATE.md | 2 +- ts/README.md | 26 +- ts/bench/moq.ts | 3 + ts/bench/order.ts | 61 +++ ts/bench/workloads.ts | 10 +- ts/fixtures/codec/expected/bench.ts | 56 +++ ts/fixtures/codec/src/bench.rs | 36 +- ts/fixtures/lang/expected/collections.ts | 8 +- ts/fixtures/lang/expected/core/cmp.ts | 142 +++++++ ts/fixtures/lang/expected/core/option.ts | 10 +- ts/fixtures/lang/expected/edits.ts | 8 +- ts/fixtures/lang/expected/harness.ts | 97 ++--- ts/fixtures/lang/expected/iters.ts | 22 +- ts/fixtures/lang/expected/orders.Item.ts | 43 +++ ts/fixtures/lang/expected/orders.ts | 387 ++++++++++++++++++++ ts/fixtures/lang/expected/refs.ts | 2 +- ts/fixtures/lang/expected/wrappers.ts | 2 +- ts/fixtures/lang/src/harness.rs | 5 +- ts/fixtures/lang/src/lib.rs | 1 + ts/fixtures/lang/src/orders.rs | 257 +++++++++++++ ts/fixtures/reject/census.md | 32 +- ts/fixtures/reject/expected.txt | 2 + ts/fixtures/reject/src/lib.rs | 10 + ts/fixtures/reject/ts.toml | 2 + ts/fixtures/task/expected/drops.ts | 2 +- ts/fixtures/task/expected/drops/closures.ts | 2 +- ts/fixtures/task/expected/drops/folds.ts | 6 +- ts/fixtures/task/expected/drops/iters.ts | 2 +- ts/runtime/rt.ts | 20 + ts/runtime/rt/collections.ts | 240 ++++++++++++ ts/runtime/rt/int.ts | 15 + ts/runtime/rt/iter.ts | 25 ++ ts/src/lower/collections.rs | 133 ++++++- ts/src/lower/drop.rs | 10 +- ts/src/lower/fmt.rs | 4 + ts/src/lower/iter.rs | 23 +- ts/src/lower/library.rs | 65 +++- ts/src/lower/mir/mod.rs | 12 +- ts/src/lower/mod.rs | 8 +- 42 files changed, 1696 insertions(+), 136 deletions(-) delete mode 100644 quest/m3/ts-ordering.md create mode 100644 quest/m3/ts-slice-gaps.md create mode 100644 ts/bench/order.ts create mode 100644 ts/fixtures/lang/expected/core/cmp.ts create mode 100644 ts/fixtures/lang/expected/orders.Item.ts create mode 100644 ts/fixtures/lang/expected/orders.ts create mode 100644 ts/fixtures/lang/src/orders.rs diff --git a/quest/m3/README.md b/quest/m3/README.md index 0d20fa142..6ff75d087 100644 --- a/quest/m3/README.md +++ b/quest/m3/README.md @@ -25,10 +25,10 @@ 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 - [Checks on the differential harnesses](/quest/m3/ts-harness.md) - step lists that drift, literal op bytes, and `refs`'s coverage fail a test or get cases -- [Ordering in std](/quest/m3/ts-ordering.md) - `cmp::max`/`min`, `is_sorted`, and `BinaryHeap`, and #173's cost on integers measured - [Arc's unique-access functions](/quest/m3/ts-arc-mut.md) - `Arc::get_mut`, `make_mut`, `pin`, and `as_deref` of an `Option` translate - [Debug and Display of Arc and ranges](/quest/m3/ts-fmt-wrappers.md) - `Arc` formats as `T`, and ranges as std's `Debug` - [Fold a match a bool decided](/quest/m3/ts-fold-bool.md) - `if matches!` and derived `==` drop a later match's dead arms - [Rename one instance](/quest/m3/ffi-instance-rename.md) - `rename` by an instance's path, once a consumer's clash needs it - [Into targets through a chain of From implementations](/quest/m3/ffi-from-chains.md) - `sources` follows more than one level, once a consumer's parameter needs it +- [Slice windows and chunks, Vec drain, and the order of byte arrays](/quest/m3/ts-slice-gaps.md) - `windows`, `chunks`, `Vec::drain`, and byte arrays' order translate - [Port the workflow guard to kixelated/quest](/quest/m3/quest-workflow-guard.md) - quest's environment guard parses YAML as this repository's does diff --git a/quest/m3/ts-ordering.md b/quest/m3/ts-ordering.md deleted file mode 100644 index 52609a6ea..000000000 --- a/quest/m3/ts-ordering.md +++ /dev/null @@ -1,22 +0,0 @@ -# [M] Ordering in std - -## Goal - -rust2-ts translates `cmp::max` and `cmp::min` (the free functions), the `is_sorted` -family, and `BinaryHeap`, proven by differential tests against native Rust, and the cost -of [#173](https://github.com/moq-dev/rust2/pull/173)'s comparison tests on integers is -measured and, if it shows, removed. - -## Plan - -Planned on 2026-10-28; rust2 aims to support all of std, so this has no moq consumer. -#173 found `cmp::max` and `cmp::min` refused. - -- These compare through `PartialOrd::lt` and the rest as #173 builds them, so a crate's own - `lt` is called where std calls it. `BinaryHeap`'s sift order decides which of equal - elements comes out first and which comparisons run, so port std's. -- #173 passes `rt.cmpLt(cmp)` where sorts and `max`/`min` took `cmp`. Measure sorts and - `max`/`min` of integers on the [TS benchmark](/ts/README.md#bench) against the - commit before #173. - -Public API: the translator's output. diff --git a/quest/m3/ts-slice-gaps.md b/quest/m3/ts-slice-gaps.md new file mode 100644 index 000000000..044ada20c --- /dev/null +++ b/quest/m3/ts-slice-gaps.md @@ -0,0 +1,17 @@ +# [S] Slice windows and chunks, Vec drain, and the order of byte arrays + +## Goal + +rust2-ts translates a slice's `windows` and `chunks`, `Vec::drain` of a range, and `<`, +`<=`, `>`, `>=`, and `cmp` of byte arrays such as `[u8; 2]`, matching std's iteration, +drop order of drained values, and comparison results. Proven by differential tests +against native Rust. + +## Plan + +Met by [#252](https://github.com/moq-dev/rust2/pull/252) on 2026-10-29: its ordering +fixtures and bench rewrote around each. rust2 aims to support all of std. Each refuses +with a span today; the byte-array comparison refuses because the mapping of a byte array +it is generic over would take the array, not a slice of it. + +Public API: the translator's output. diff --git a/ts/MIGRATE.md b/ts/MIGRATE.md index f3665ef24..25b99d3cc 100644 --- a/ts/MIGRATE.md +++ b/ts/MIGRATE.md @@ -45,7 +45,7 @@ `true`. - The runtime's `iterMax`, `iterMin`, `iterMaxByKey`, and `iterMinByKey` take a `PartialOrd::lt` after the iterator and any key function, for items or keys of any type: - `rt.iterMax(it, rt.cmpLt(rt.cmp))`. + `rt.iterMax(it, rt.cmpLt(rt.cmpStr))`, or of integers, `rt.iterMax(it, rt.lt)`. - `rt.FromUtf8Error` holds its `rt.Utf8Error` as std's does: `e.validUpTo` and `e.errorLen` are `e.error.validUpTo` and `e.error.errorLen`. - `ts.toml` no longer takes `ignore-macros`: `tracing`'s events are left out without it, diff --git a/ts/README.md b/ts/README.md index 2bd2e4054..480247229 100644 --- a/ts/README.md +++ b/ts/README.md @@ -66,7 +66,7 @@ from. `rt.ts` beside them is the runtime: what std does that JS does not, with iterators in `rt/iter.ts`, `str`'s searches in `rt/str.ts`, collections in `rt/collections.ts`, ownership, atomics, and async in `rt/task.ts`, `f64`'s methods in `rt/float.ts`, and `Duration` in `rt/time.ts`. What translates from std's own MIR, -`Option`'s, `Result`'s, and `Poll`'s methods, goes in a module under its crate's name, as +`Option`'s, `Result`'s, and `Poll`'s methods and `cmp::max` and `min`, goes in a module under its crate's name, as `core/option.ts` holds `map$Option_u64_u64_rescale_closure0`. A dependency's modules go under its crate's name too, `kio.ts` and `kio/lock.ts`, with the instances of its generic functions that the package's types make, so a module at the package's root named for a @@ -93,7 +93,8 @@ 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 | -| `HashMap`, `HashSet`, `BTreeMap`, `BTreeSet`, `VecDeque` | `rt.HashMap` and so on, each a plain object whose functions are the runtime's: `rt.hashGet(map, key)` | +| `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` | `rt.Ordering`, which is `-1`, `0`, or `1`; `rt.Hasher` | | `Duration` | `rt.Duration`: `{ secs, nanos }`, std's whole seconds as an `rt.u64` and the nanoseconds past them, never changed once made | | closure | an arrow function, with `[Symbol.dispose]` if it owns a value that drops | @@ -252,7 +253,8 @@ impl, an `Arc`'s count, or a waker. Bodies lower from MIR, so each drop is where elaboration put it. A panic drops nothing. A value without `[Symbol.dispose]` of its own, such as an enum or a tuple, drops through glue lowered from rustc's `drop_in_place` shim, a function of its type's own (`drop$Held`). `bool`'s, `Option`'s, `Result`'s, and `Poll`'s -methods lower from core's MIR, so they drop what core's do, where core's do. The runtime +methods and `cmp::max`, `min`, and their `_by` and `_by_key` forms lower from core's MIR, +so they drop what core's do, where core's do. The runtime functions for std's collections, `Arc`, and the std functions that may discard an operand (`OnceCell::get_or_init`, a map's `insert`) take what drops each value they may drop, and drop it where std's do. So do iterators: one that owns its items, as a `Vec`'s `into_iter` or a @@ -523,6 +525,14 @@ they need. calling only the comparator, as std permits of an unstable sort: JS's `sort` moves `undefined`, a `None`, to the end without comparing it, and each engine orders its comparisons differently. +- A `BinaryHeap` is std's heap in an array, with std's sifts ported to the runtime: std's + move elements through raw pointers into its `Vec`, which no body of std's lowers + through, and the sifts decide which `PartialOrd` tests run and which of equal elements + leaves first. The sorts, `max` and `min`, the heap, and `is_sorted` take an integer's + tests as the runtime's `rt.lt`, `rt.le`, `rt.gt`, and `rt.ge` rather than through its + `partial_cmp`, `rt.cmpLt(rt.cmp)`: on `just ts bench`'s `order/` workloads that took + Firefox 0.8-0.86x the time on sorts and `max`/`min` and Chrome 0.66x on the heap, and + Chrome the same on the rest. - Iterators are the runtime's lazy classes rather than generators or ES iterator helpers: std's adaptors would lower from std's own bodies, whose slice iterators are raw pointers, ES helpers lack most of them @@ -577,7 +587,8 @@ what it captured, `mem::swap`, `replace`, or `take` of a reference's whole point assigning an `Arc`, a waker, a `MutexGuard`, a pinned reference, or a future through a reference, a switch on or a field of another crate's type without a mapping, and std functions without a mapping in `src/lower/library.rs`, but for `bool`'s, `Option`'s, -`Result`'s, and `Poll`'s methods that take no `&mut`, which lower from core's MIR. +`Result`'s, and `Poll`'s methods that take no `&mut`, and `cmp`'s `max` and `min` forms, +which lower from core's MIR. ## Tests @@ -672,11 +683,12 @@ frame headers decoded as they arrive a few bytes at a time, which prices a decod comes up `Short` and retries, iterator chains over bytes, a `BTreeMap` of 16, 256, and 4096 keys, and timestamps converted between timescales as moq-net's `Timestamp::convert` does, whose products pass 2^53, then added by its `checked_add` and ordered by its `Ord`, -whose cross products do too. Each runs four ways: natively, as wasm32 at moq-wasm's +whose cross products do too, and integers sorted, searched for their greatest and least, +and kept in a `BinaryHeap`. Each runs four ways: natively, as wasm32 at moq-wasm's size-optimized profile, hand-written (moq's `js/net` in `bench/moq.ts` and its `Timestamp` in `bench/timestamp.ts`, ES iterator helpers in `bench/iter.ts`, a sorted array in -`bench/sorted.ts`, and `bigint` in `bench/rescale.ts`, as moq's `js/hang` converts -timescales), and translated; every route's output must match +`bench/sorted.ts`, `bigint` in `bench/rescale.ts`, as moq's `js/hang` converts +timescales, and typed arrays and a heap of its own in `bench/order.ts`), and translated; every route's output must match native Rust's first. The JS routes run minified, the same bundles whose size it reports, in Node, Bun, Chrome, and Firefox, with the WASM route's copies across the boundary timed apart from its compute, and an A/A copy of the diff --git a/ts/bench/moq.ts b/ts/bench/moq.ts index 0e8d3caac..b677dc48b 100644 --- a/ts/bench/moq.ts +++ b/ts/bench/moq.ts @@ -13,6 +13,7 @@ import { Cursor, Reader, type StreamVersion, Writer } from "moq/js/net/src/strea import * as Varint from "moq/js/net/src/varint.ts"; import { iterate } from "./iter.ts"; +import { order } from "./order.ts"; import { rescale } from "./rescale.ts"; import { ordered } from "./sorted.ts"; import { interleave, stamp } from "./timestamp.ts"; @@ -59,6 +60,8 @@ export function run(input: Uint8Array): Uint8Array | Promise { return stamp(data); case 11: return interleave(data); + case 12: + return order(selector, data); default: throw new Error(`unknown workload ${op}`); } diff --git a/ts/bench/order.ts b/ts/bench/order.ts new file mode 100644 index 000000000..e5e5a397c --- /dev/null +++ b/ts/bench/order.ts @@ -0,0 +1,61 @@ +// The `order/` workloads' hand-written route: `bench.rs`'s op 12, as TypeScript orders +// numbers without a library: a typed array's `sort`, a stable `sort` by key, a loop for +// the greatest and least, and a binary heap of its own, as JS has none. + +/** Run op 12, as `bench::run` does, for the selector and the count `data` starts with. */ +export function order(selector: number, data: Uint8Array): Uint8Array { + const padded = new Uint8Array(8); + padded.set(data.subarray(0, 8)); + const count = Math.min(Math.max(Number(new DataView(padded.buffer).getBigUint64(0)), 1), 1 << 16); + let v = Uint32Array.from({ length: count }, (_, i) => (i * 40503) % (1 << 16)); + switch (selector) { + case 0: + v.sort(); + break; + case 1: + v = Uint32Array.from(Array.from(v).sort((a, b) => (a % 1024) - (b % 1024))); + break; + case 2: + case 3: { + let hi = 0; + let lo = 0xffffffff; + for (const x of v) { + if (x > hi) hi = x; + if (x < lo) lo = x; + } + v = Uint32Array.of(hi, lo); + break; + } + default: { + const heap: number[] = []; + for (const x of v) { + let i = heap.push(x) - 1; + while (i > 0 && heap[(i - 1) >> 1] < x) { + heap[i] = heap[(i - 1) >> 1]; + i = (i - 1) >> 1; + } + heap[i] = x; + } + for (let n = 0; n < count; n++) { + v[n] = heap[0]; + const x = heap.pop() as number; + const len = heap.length; + if (len === 0) break; + let i = 0; + for (;;) { + let child = 2 * i + 1; + if (child >= len) break; + if (child + 1 < len && heap[child + 1] > heap[child]) child++; + if (heap[child] <= x) break; + heap[i] = heap[child]; + i = child; + } + heap[i] = x; + } + } + } + const out = new Uint8Array(24); + const view = new DataView(out.buffer); + [v[0], v[v.length >> 1], v[v.length - 1]].forEach((x, i) => view.setBigUint64(8 * i, BigInt(x))); + return out; +} diff --git a/ts/bench/workloads.ts b/ts/bench/workloads.ts index e2e0cf574..267bfc46a 100644 --- a/ts/bench/workloads.ts +++ b/ts/bench/workloads.ts @@ -5,8 +5,8 @@ export interface Workload { /** What is coded, such as `varint/quic/2B` or `frames/video/lite-07`, iterated, or converted. */ name: string; - op: "encode" | "decode" | "iterate" | "map" | "convert" | "add" | "compare" | "handoff"; - /** Varints, frame headers, messages, items, keys, timestamps, or handoffs per call, which each time is divided by. */ + op: "encode" | "decode" | "iterate" | "map" | "convert" | "add" | "compare" | "order" | "handoff"; + /** Varints, frame headers, messages, items, keys, timestamps, integers, or handoffs per call, which each time is divided by. */ items: number; input: Uint8Array; } @@ -93,6 +93,12 @@ export function workloads(encode: (input: Uint8Array) => Uint8Array): Workload[] const time = (op: number, params: bigint[]) => Uint8Array.of(op, 0, ...[BigInt(count), ...params].flatMap(u64)); out.push({ name: "time/checked-add/micros", op: "add", items: count, input: time(10, video) }); out.push({ name: "time/ord/micros-48k", op: "compare", items: count, input: time(11, [...video, ...audio]) }); + + // `bench.rs`'s op 12: integers ordered by std's own `Ord`, which the translation tests. + const orders = ["sort", "sort-by-key", "iter-max-min", "ord-max-min", "binary-heap"]; + orders.forEach((name, selector) => { + out.push({ name: `order/${name}`, op: "order", items: count, input: Uint8Array.of(12, selector, ...u64(BigInt(count))) }); + }); return out; } diff --git a/ts/fixtures/codec/expected/bench.ts b/ts/fixtures/codec/expected/bench.ts index 5fedab81f..1c3a77c7c 100644 --- a/ts/fixtures/codec/expected/bench.ts +++ b/ts/fixtures/codec/expected/bench.ts @@ -43,6 +43,8 @@ export function run(input: rt.ByteSlice): rt.ByteVec { stamp(new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); } else if (op === 11) { interleave(new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); + } else if (op === 12) { + order(selector, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); } else { decoded(decodeGroup(new rt.ByteSlice(input.data, input.start + 2, input.length - 2), liteVersion(selector), out), out); } @@ -302,6 +304,60 @@ export function ordered(data: rt.ByteSlice, out: rt.ByteVec): void { putU64(out, sum); } +/** + * `count` integers sorted, sorted by a key, searched for the greatest and least, or + * pushed onto a `BinaryHeap` and popped off, as `selector` picks, each by `u32`'s own + * `Ord`: the first, middle, and last of the order, or the greatest and least. + */ +export function order(selector: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { + let v: rt.u32[] = rt.iterToArray(rt.iterMap(new rt.Range(0, rt.u64Low(rt.u64Clamp(takeU64(data)[0], 1, 65536))), (i: rt.u32): rt.u32 => { + return rt.u32Rem(rt.u32Mul(i, 40503), 1 << 16); + })); + if (selector === 0) { + rt.sliceSort(v, rt.lt); + } else if (selector === 1) { + rt.sliceSortByKey(v, (_210: rt.u32): rt.u32 => { + return rt.u32Rem(_210, 1024); + }, rt.lt); + } else if (selector === 2) { + v = [rt.optionUnwrap(rt.iterMax(rt.iterSlice(v), rt.lt)), rt.optionUnwrap(rt.iterMin(rt.iterSlice(v), rt.lt))]; + } else if (selector === 3) { + const _35: [rt.u32, rt.u32] = rt.iterFold(rt.iterSlice(v), [0, 4294967295] as [rt.u32, rt.u32], (_2: [rt.u32, rt.u32], _33: rt.u32): [rt.u32, rt.u32] => { + const hi2: rt.u32 = _2[0]; + const lo2: rt.u32 = _2[1]; + const x4: rt.u32 = _33; + return [rt.max(hi2, x4), rt.min(lo2, x4)] as [rt.u32, rt.u32]; + }); + v = [_35[0], _35[1]]; + } else { + const heap: rt.BinaryHeap = rt.heapNew() as rt.BinaryHeap; + for (const _47 of rt.replaceIn(v, [])) { + rt.heapPush(heap, _47, rt.le); + } + while (true) { + const _53: rt.u32 | undefined = rt.heapPop(heap, rt.le); + if (_53 !== undefined) { + v.push(_53); + } else { + break; + } + } + } + const _63: rt.u32[] = v; + const _61: rt.u32 = _63[rt.idx(_63, 0)]; + const _66: rt.u32[] = v; + const _64: rt.u32 = _66[rt.idx(_66, rt.usizeDiv(v.length, 2))]; + const _73: rt.u32[] = v; + const iter2: rt.Iter = rt.iterSlice([_61, _64, _73[rt.idx(_73, rt.usizeSub(v.length, 1))]]); + while (true) { + const _79: rt.u32 | rt.Done = iter2.next(); + if (_79 === rt.DONE) { + break; + } + putU64(out, _79 as rt.u64); + } +} + /** * One iterator chain over every byte, picked by `chain`, with closures that cost little * next to the iterator protocol they run through. diff --git a/ts/fixtures/codec/src/bench.rs b/ts/fixtures/codec/src/bench.rs index bedc2d972..ea6a65ca8 100644 --- a/ts/fixtures/codec/src/bench.rs +++ b/ts/fixtures/codec/src/bench.rs @@ -4,10 +4,11 @@ //! decode's input. Op 6 runs iterator chains over its bytes instead, as moq-net's model //! writes them, op 7 decodes frame headers as they arrive a few bytes at a time, op 8 //! keeps a `BTreeMap`, as moq-net's model keeps its state, op 9 converts timestamps -//! between timescales, and ops 10 and 11 add and order them as moq-net's `Timestamp` does. +//! between timescales, ops 10 and 11 add and order them as moq-net's `Timestamp` does, and +//! op 12 orders integers by std's own `Ord`. use std::cmp::Ordering; -use std::collections::BTreeMap; +use std::collections::{BTreeMap, BinaryHeap}; use crate::coding::{Decode, DecodeError, Decoder, Encode, EncodeError, Encoder, Form, varint}; use crate::harness::{decode_error, encode_error, form, lite_version, put_u64, put_usize, take_u64}; @@ -35,6 +36,7 @@ pub fn run(input: &[u8]) -> Vec { 9 => rescale(selector, data, &mut out), 10 => stamp(data, &mut out), 11 => interleave(data, &mut out), + 12 => order(selector, data, &mut out), _ => decoded(decode_group(data, lite_version(selector), &mut out), &mut out), } out @@ -207,6 +209,36 @@ fn ordered(data: &[u8], out: &mut Vec) { put_u64(out, sum); } +/// `count` integers sorted, sorted by a key, searched for the greatest and least, or +/// pushed onto a `BinaryHeap` and popped off, as `selector` picks, each by `u32`'s own +/// `Ord`: the first, middle, and last of the order, or the greatest and least. +fn order(selector: u8, data: &[u8], out: &mut Vec) { + let (count, _) = take_u64(data); + let count = count.clamp(1, 1 << 16) as u32; + let mut v: Vec = (0..count).map(|i| (i * 40503) % (1 << 16)).collect(); + match selector { + 0 => v.sort(), + 1 => v.sort_by_key(|&x| x % 1024), + 2 => v = vec![*v.iter().max().unwrap(), *v.iter().min().unwrap()], + 3 => { + let (hi, lo) = v.iter().fold((0, u32::MAX), |(hi, lo), &x| (hi.max(x), lo.min(x))); + v = vec![hi, lo]; + } + _ => { + let mut heap = BinaryHeap::new(); + for x in std::mem::take(&mut v) { + heap.push(x); + } + while let Some(x) = heap.pop() { + v.push(x); + } + } + } + for x in [v[0], v[v.len() / 2], v[v.len() - 1]] { + put_u64(out, x as u64); + } +} + /// One iterator chain over every byte, picked by `chain`, with closures that cost little /// next to the iterator protocol they run through. fn iterate(chain: u8, data: &[u8], out: &mut Vec) { diff --git a/ts/fixtures/lang/expected/collections.ts b/ts/fixtures/lang/expected/collections.ts index eeb182fc1..8c53c49ce 100644 --- a/ts/fixtures/lang/expected/collections.ts +++ b/ts/fixtures/lang/expected/collections.ts @@ -653,7 +653,7 @@ export function btreeMaps(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): voi rt.byteVecPush(out, mapOr$Option_u8_u8_u8_btree_maps_closure0(rt.iterToOption(rt.btreeRange(map, { start: 4 }).nextBack()), 255, (_230: [rt.u8, rt.u8]): rt.u8 => { return _230[0] ^ _230[1]; })); - rt.byteVecPush(out, mapOr$Option_u8_u8_btree_maps_closure5(rt.iterMax(rt.btreeValues(map), rt.cmpLt(rt.cmp)), 255, (v6: rt.u8): rt.u8 => { + rt.byteVecPush(out, mapOr$Option_u8_u8_btree_maps_closure5(rt.iterMax(rt.btreeValues(map), rt.lt), 255, (v6: rt.u8): rt.u8 => { return v6; })); rt.btreeRemove(map, 3); @@ -1231,7 +1231,7 @@ export function sorted$OptionMap_Option_String_HashMap_u8_Option_String(self: rt })); rt.sliceSortByKey(all, (_22: [rt.u8, string | undefined]): rt.u8 => { return _22[0]; - }, rt.cmpLt(rt.cmp)); + }, rt.lt); return all; } @@ -1241,7 +1241,7 @@ export function sorted$OptionMap_Option_u8_HashMap_u8_Option_u8(self: rt.HashMap })); rt.sliceSortByKey(all, (_22: [rt.u8, rt.u8 | undefined]): rt.u8 => { return _22[0]; - }, rt.cmpLt(rt.cmp)); + }, rt.lt); return all; } @@ -1253,7 +1253,7 @@ export function sorted$OptionMap_unit_HashMap_u8_unit(self: rt.HashMap { return _22[0]; - }, rt.cmpLt(rt.cmp)); + }, rt.lt); return all; } diff --git a/ts/fixtures/lang/expected/core/cmp.ts b/ts/fixtures/lang/expected/core/cmp.ts new file mode 100644 index 000000000..fdaca3a8e --- /dev/null +++ b/ts/fixtures/lang/expected/core/cmp.ts @@ -0,0 +1,142 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "../rt.ts"; +import * as Item from "../orders.Item.ts"; + +export function min$Item(v1: Item.Item, v2: Item.Item): Item.Item { + return rt.ordMin(v1, v2, Item.lt$PartialOrd); +} + +export function min$Item$2(v1: Item.Item, v2: Item.Item): Item.Item { + return rt.ordMin(v1, v2, Item.lt$PartialOrd, rt.drop); +} + +export function min$String(v1: string, v2: string): string { + return rt.ordMin(v1, v2, rt.cmpLt(rt.cmpStr)); +} + +export function min$u8(v1: rt.u8, v2: rt.u8): rt.u8 { + return rt.min(v1, v2); +} + +export function minBy$Item_cmps_closure2(v1: Item.Item, v2: Item.Item, compare: (a0: Item.Item, a1: Item.Item) => rt.Ordering): Item.Item { + let _0!: Item.Item; + let _10: boolean = false; + let _9: boolean = false; + _10 = true; + _9 = true; + if (!(compare(v1, v2) <= 0)) { + _9 = false; + _0 = v2; + } else { + _10 = false; + _0 = v1; + } + if (_9) { + v2[Symbol.dispose](); + } + if (_10) { + v1[Symbol.dispose](); + return _0; + } + return _0; +} + +export function minByKey$Item_cmps_closure4_Reverse_u8(v1: Item.Item, v2: Item.Item, f: (a0: Item.Item) => rt.u8): Item.Item { + let _0!: Item.Item; + let _16: boolean = false; + let _15: boolean = false; + _16 = true; + _15 = true; + const _6: rt.u8 = f(v2); + const _11: rt.u8 = f(v1); + if (!(rt.cmpReverse(rt.cmp)(_6, _11) === -1)) { + _16 = false; + _0 = v1; + } else { + _15 = false; + _0 = v2; + } + if (_15) { + v2[Symbol.dispose](); + } + if (_16) { + v1[Symbol.dispose](); + return _0; + } + return _0; +} + +export function max$Item(v1: Item.Item, v2: Item.Item): Item.Item { + return rt.ordMax(v1, v2, Item.lt$PartialOrd); +} + +export function max$Item$2(v1: Item.Item, v2: Item.Item): Item.Item { + return rt.ordMax(v1, v2, Item.lt$PartialOrd, rt.drop); +} + +export function max$String(v1: string, v2: string): string { + return rt.ordMax(v1, v2, rt.cmpLt(rt.cmpStr)); +} + +export function max$i8(v1: rt.i8, v2: rt.i8): rt.i8 { + return rt.max(v1, v2); +} + +export function max$u8(v1: rt.u8, v2: rt.u8): rt.u8 { + return rt.max(v1, v2); +} + +export function maxBy$Item_cmps_closure1(v1: Item.Item, v2: Item.Item, compare: (a0: Item.Item, a1: Item.Item) => rt.Ordering): Item.Item { + let _0!: Item.Item; + let _10: boolean = false; + let _9: boolean = false; + _10 = true; + _9 = true; + if (!(compare(v1, v2) > 0)) { + _9 = false; + _0 = v2; + } else { + _10 = false; + _0 = v1; + } + if (_9) { + v2[Symbol.dispose](); + } + if (_10) { + v1[Symbol.dispose](); + return _0; + } + return _0; +} + +export function maxByKey$Item_cmps_closure3_Item(v1: Item.Item, v2: Item.Item, f: (a0: Item.Item) => Item.Item): Item.Item { + let _0!: Item.Item; + let _16: boolean = false; + let _15: boolean = false; + _16 = true; + _15 = true; + const _6: Item.Item = f(v2); + const _11: Item.Item = f(v1); + if (!Item.lt$PartialOrd(_6, _11)) { + _11[Symbol.dispose](); + _6[Symbol.dispose](); + _15 = false; + _0 = v2; + } else { + _11[Symbol.dispose](); + _6[Symbol.dispose](); + _16 = false; + _0 = v1; + } + if (_15) { + v2[Symbol.dispose](); + } + if (_16) { + v1[Symbol.dispose](); + return _0; + } + return _0; +} + +//# sourceMappingURL=cmp.ts.map diff --git a/ts/fixtures/lang/expected/core/option.ts b/ts/fixtures/lang/expected/core/option.ts index 2d4b83127..aa5b51b7e 100644 --- a/ts/fixtures/lang/expected/core/option.ts +++ b/ts/fixtures/lang/expected/core/option.ts @@ -19,6 +19,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 Pair from "../pointers.Pair.ts"; import { from$From_Dir_u8 } from "../shapes.ts"; import type * as Dir from "../shapes.Dir.ts"; @@ -349,6 +350,13 @@ 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 { + if (self === undefined) { + return default_; + } + return f(self); +} + export function mapOr$Option_Meters_u64_copies_closure0(self: Meters.Meters | undefined, default_: rt.u64, f: (a0: Meters.Meters) => rt.u64): rt.u64 { if (self === undefined) { return default_; @@ -622,7 +630,7 @@ export function mapOr$Option_usize_u8_consumers_closure8(self: rt.usize | undefi return f(self); } -export function mapOr$Option_usize_u8_u8_ranges_closure0(self: [rt.usize, rt.u8] | undefined, default_: rt.u8, f: (a0: [rt.usize, rt.u8]) => rt.u8): rt.u8 { +export function mapOr$Option_usize_u8_u8_heaps_closure0(self: [rt.usize, rt.u8] | undefined, default_: rt.u8, f: (a0: [rt.usize, rt.u8]) => rt.u8): rt.u8 { if (self === undefined) { return default_; } diff --git a/ts/fixtures/lang/expected/edits.ts b/ts/fixtures/lang/expected/edits.ts index 22af1c43a..f2dcb0fd9 100644 --- a/ts/fixtures/lang/expected/edits.ts +++ b/ts/fixtures/lang/expected/edits.ts @@ -120,7 +120,7 @@ export function edits(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { rt.byteVecPush(out, names2.length & 0xff); } else if (_8 === 2) { const v3: rt.u16[] = words.slice(); - rt.sliceSort(v3, rt.cmpLt(rt.cmp)); + rt.sliceSort(v3, rt.lt); putU16s(out, v3); const v4: rt.u16[] = words.slice(); rt.sliceSortBy(v4, (a: rt.u16, b6: rt.u16): rt.Ordering => { @@ -132,7 +132,7 @@ export function edits(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { })); rt.sliceSortByKey(pairs2, (p2: [rt.u16, rt.u16]): rt.u16 => { return p2[0]; - }, rt.cmpLt(rt.cmp)); + }, rt.lt); for (const _159 of pairs2) { putU16s(out, [_159[0], _159[1]]); } @@ -493,12 +493,12 @@ export function bytes(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { const v11: rt.ByteVec = rt.byteSliceToByteVec(_101); rt.sliceSortByKey(rt.byteSliceView(rt.byteVecSlice(v11)), (b3: rt.u8): rt.u8 => { return rt.u8Rem(b3, 5); - }, rt.cmpLt(rt.cmp)); + }, rt.lt); rt.byteVecExtendFromSlice(out, rt.byteVecSlice(v11)); const v12: rt.ByteVec = rt.byteSliceToByteVec(_101); rt.sliceSortByKey(rt.byteSliceView(rt.byteVecSlice(v12)), (b4: rt.u8): rt.u8 => { return rt.u8WrappingMul(b4, 13); - }, rt.cmpLt(rt.cmp)); + }, rt.lt); rt.byteVecExtendFromSlice(out, rt.byteVecSlice(v12)); const v13: rt.ByteVec = rt.byteSliceToByteVec(_101); rt.sliceSortBy(rt.byteSliceView(rt.byteVecSlice(v13)), (a2: rt.u8, b5: rt.u8): rt.Ordering => { diff --git a/ts/fixtures/lang/expected/harness.ts b/ts/fixtures/lang/expected/harness.ts index e05388b78..6785822fb 100644 --- a/ts/fixtures/lang/expected/harness.ts +++ b/ts/fixtures/lang/expected/harness.ts @@ -28,6 +28,7 @@ import { locals } from "./locals.ts"; import { locks, relock, rwlocks } from "./locks.ts"; import { nested } from "./nested.ts"; import { objects } from "./objects.ts"; +import { orders } from "./orders.ts"; import { lists, shared as pointers_shared, places, unchecked, nested as pointers_nested, copies, sentinels } from "./pointers.ts"; import { refs } from "./refs.ts"; import { copies as shapes_copies, order, discriminants, loops, patterns, closures, library, flows } from "./shapes.ts"; @@ -42,101 +43,101 @@ import { wrappers } from "./wrappers.ts"; * case added at the end moves no other. */ export function cases(): [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void][] { - return copy$str_forabfnu8au8bmutstdvecVecu8s([["ints::u8_op", u8Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u16_op", u16Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u32_op", u32Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u64_op", u64Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u128_op", u128Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::usize_op", usizeOp] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i8_op", i8Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i16_op", i16Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i32_op", i32Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i64_op", i64Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i128_op", i128Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::isize_op", isizeOp] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::neg", neg] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::cast", cast] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::convert", (_276: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void => { + return copy$str_forabfnu8au8bmutstdvecVecu8s([["ints::u8_op", u8Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u16_op", u16Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u32_op", u32Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u64_op", u64Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u128_op", u128Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::usize_op", usizeOp] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i8_op", i8Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i16_op", i16Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i32_op", i32Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i64_op", i64Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i128_op", i128Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::isize_op", isizeOp] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::neg", neg] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::cast", cast] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::convert", (_279: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void => { convert(data, out); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::copies", (_277: rt.u8, data2: rt.ByteSlice, out2: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::copies", (_280: rt.u8, data2: rt.ByteSlice, out2: rt.ByteVec): void => { shapes_copies(data2, out2); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::loops", (_278: rt.u8, data3: rt.ByteSlice, out3: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::loops", (_281: rt.u8, data3: rt.ByteSlice, out3: rt.ByteVec): void => { loops(data3, out3); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::patterns", (_279: rt.u8, data4: rt.ByteSlice, out4: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::patterns", (_282: rt.u8, data4: rt.ByteSlice, out4: rt.ByteVec): void => { patterns(data4, out4); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::closures", (_280: rt.u8, data5: rt.ByteSlice, out5: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::closures", (_283: rt.u8, data5: rt.ByteSlice, out5: rt.ByteVec): void => { closures(data5, out5); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::discriminants", (_281: rt.u8, data6: rt.ByteSlice, out6: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::discriminants", (_284: rt.u8, data6: rt.ByteSlice, out6: rt.ByteVec): void => { discriminants(data6, out6); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::library", (_282: rt.u8, data7: rt.ByteSlice, out7: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::library", (_285: rt.u8, data7: rt.ByteSlice, out7: rt.ByteVec): void => { library(data7, out7); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::ints", ints] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::strings", (_283: rt.u8, data8: rt.ByteSlice, out8: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::ints", ints] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::strings", (_286: rt.u8, data8: rt.ByteSlice, out8: rt.ByteVec): void => { strings(data8, out8); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::derived", (_284: rt.u8, data9: rt.ByteSlice, out9: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::derived", (_287: rt.u8, data9: rt.ByteSlice, out9: rt.ByteVec): void => { derived(data9, out9); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::written", (_285: rt.u8, data10: rt.ByteSlice, out10: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::written", (_288: rt.u8, data10: rt.ByteSlice, out10: rt.ByteVec): void => { written(data10, out10); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::panics", panics] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["generics::methods", (_286: rt.u8, data11: rt.ByteSlice, out11: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::panics", panics] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["generics::methods", (_289: rt.u8, data11: rt.ByteSlice, out11: rt.ByteVec): void => { methods(data11, out11); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::adaptors", adaptors] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::consumers", consumers] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::ranges", (_287: rt.u8, data12: rt.ByteSlice, out12: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::adaptors", adaptors] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::consumers", consumers] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::ranges", (_290: rt.u8, data12: rt.ByteSlice, out12: rt.ByteVec): void => { ranges(data12, out12); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::options", (_288: rt.u8, data13: rt.ByteSlice, out13: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::options", (_291: rt.u8, data13: rt.ByteSlice, out13: rt.ByteVec): void => { options(data13, out13); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::own", (_289: rt.u8, data14: rt.ByteSlice, out14: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::own", (_292: rt.u8, data14: rt.ByteSlice, out14: rt.ByteVec): void => { own(data14, out14); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::values", (_290: rt.u8, data15: rt.ByteSlice, out15: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::values", (_293: rt.u8, data15: rt.ByteSlice, out15: rt.ByteVec): void => { values(data15, out15); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::order", (_291: rt.u8, data16: rt.ByteSlice, out16: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::order", (_294: rt.u8, data16: rt.ByteSlice, out16: rt.ByteVec): void => { order(data16, out16); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::hash_maps", hashMaps] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::sets", sets] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::btree_maps", btreeMaps] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::deques", (_292: rt.u8, data17: rt.ByteSlice, out17: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::hash_maps", hashMaps] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::sets", sets] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::btree_maps", btreeMaps] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::deques", (_295: rt.u8, data17: rt.ByteSlice, out17: rt.ByteVec): void => { deques(data17, out17); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::flows", (_293: rt.u8, data18: rt.ByteSlice, out18: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::flows", (_296: rt.u8, data18: rt.ByteSlice, out18: rt.ByteVec): void => { flows(data18, out18); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["keys::keys", keys] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["chars::chars", chars] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::strs", strs] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::vecs", (_294: rt.u8, data19: rt.ByteSlice, out19: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["keys::keys", keys] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["chars::chars", chars] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::strs", strs] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::vecs", (_297: rt.u8, data19: rt.ByteSlice, out19: rt.ByteVec): void => { vecs(data19, out19); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::slices", slices] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::stores", (_295: rt.u8, data20: rt.ByteSlice, out20: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::slices", slices] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::stores", (_298: rt.u8, data20: rt.ByteSlice, out20: rt.ByteVec): void => { stores(data20, out20); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["chunks::chunks", chunks] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["cows::cows", cows] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["durations::durations", durations] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["combinators::combinators", combinators] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["splits::splits", splits] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["nested::nested", nested] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["refs::refs", (_296: rt.u8, data21: rt.ByteSlice, out21: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["chunks::chunks", chunks] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["cows::cows", cows] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["durations::durations", durations] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["combinators::combinators", combinators] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["splits::splits", splits] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["nested::nested", nested] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["refs::refs", (_299: rt.u8, data21: rt.ByteSlice, out21: rt.ByteVec): void => { refs(data21, out21); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::counts", (_297: rt.u8, data22: rt.ByteSlice, out22: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::counts", (_2100: 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", (_298: rt.u8, _310: rt.ByteSlice, out23: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::rounding", (_2101: rt.u8, _310: 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", (_299: rt.u8, data23: rt.ByteSlice, out24: rt.ByteVec): void => { + }] 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", (_2102: rt.u8, data23: rt.ByteSlice, out24: rt.ByteVec): void => { dyns(data23, out24); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["cells::cells", cells] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::shared", (_2100: rt.u8, data24: rt.ByteSlice, out25: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["cells::cells", cells] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::shared", (_2103: rt.u8, data24: rt.ByteSlice, out25: rt.ByteVec): void => { shared(data24, out25); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::ranked", ranked] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["captures::captures", captures] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locks::locks", (_2101: rt.u8, data25: rt.ByteSlice, out26: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::ranked", ranked] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["captures::captures", captures] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locks::locks", (_2104: rt.u8, data25: rt.ByteSlice, out26: rt.ByteVec): void => { locks(data25, out26); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locks::relock", (_2102: rt.u8, data26: rt.ByteSlice, out27: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locks::relock", (_2105: rt.u8, data26: rt.ByteSlice, out27: rt.ByteVec): void => { relock(data26, out27); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locals::locals", (_2103: rt.u8, data27: rt.ByteSlice, out28: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locals::locals", (_2106: rt.u8, data27: rt.ByteSlice, out28: rt.ByteVec): void => { locals(data27, out28); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["statics::statics", (_2104: rt.u8, data28: rt.ByteSlice, out29: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["statics::statics", (_2107: rt.u8, data28: rt.ByteSlice, out29: rt.ByteVec): void => { statics(data28, out29); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locks::rwlocks", (_2105: rt.u8, data29: rt.ByteSlice, out30: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locks::rwlocks", (_2108: rt.u8, data29: rt.ByteSlice, out30: rt.ByteVec): void => { rwlocks(data29, out30); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["borrows::borrows", (_2106: rt.u8, data30: rt.ByteSlice, out31: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["borrows::borrows", (_2109: rt.u8, data30: rt.ByteSlice, out31: rt.ByteVec): void => { borrows(data30, out31); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["defaults::defaults", (_2107: rt.u8, data31: rt.ByteSlice, out32: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["defaults::defaults", (_2110: rt.u8, data31: rt.ByteSlice, out32: rt.ByteVec): void => { defaults(data31, out32); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::arcs", (_2108: rt.u8, data32: rt.ByteSlice, out33: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::arcs", (_2111: rt.u8, data32: rt.ByteSlice, out33: rt.ByteVec): void => { arcs(data32, out33); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::rcs", (_2109: rt.u8, data33: rt.ByteSlice, out34: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::rcs", (_2112: rt.u8, data33: rt.ByteSlice, out34: rt.ByteVec): void => { rcs(data33, out34); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::cyclic_arcs", (_2110: rt.u8, data34: rt.ByteSlice, out35: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::cyclic_arcs", (_2113: rt.u8, data34: rt.ByteSlice, out35: rt.ByteVec): void => { cyclicArcs(data34, out35); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::cyclic_rcs", (_2111: rt.u8, data35: rt.ByteSlice, out36: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::cyclic_rcs", (_2114: rt.u8, data35: rt.ByteSlice, out36: rt.ByteVec): void => { cyclicRcs(data35, out36); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::anys", (_2112: rt.u8, data36: rt.ByteSlice, out37: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::anys", (_2115: rt.u8, data36: rt.ByteSlice, out37: rt.ByteVec): void => { anys(data36, out37); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::fns", (_2113: rt.u8, data37: rt.ByteSlice, out38: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::fns", (_2116: rt.u8, data37: rt.ByteSlice, out38: rt.ByteVec): void => { fns(data37, out38); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::copies", (_2114: rt.u8, data38: rt.ByteSlice, out39: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::copies", (_2117: rt.u8, data38: rt.ByteSlice, out39: rt.ByteVec): void => { copies(data38, out39); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::lists", (_2115: rt.u8, data39: rt.ByteSlice, out40: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::lists", (_2118: rt.u8, data39: rt.ByteSlice, out40: rt.ByteVec): void => { lists(data39, out40); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::nested", (_2116: rt.u8, data40: rt.ByteSlice, out41: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::nested", (_2119: rt.u8, data40: rt.ByteSlice, out41: rt.ByteVec): void => { pointers_nested(data40, out41); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::places", (_2117: rt.u8, data41: rt.ByteSlice, out42: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::places", (_2120: rt.u8, data41: rt.ByteSlice, out42: rt.ByteVec): void => { places(data41, out42); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::sentinels", (_2118: rt.u8, data42: rt.ByteSlice, out43: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::sentinels", (_2121: rt.u8, data42: rt.ByteSlice, out43: rt.ByteVec): void => { sentinels(data42, out43); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::shared", (_2119: rt.u8, data43: rt.ByteSlice, out44: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::shared", (_2122: rt.u8, data43: rt.ByteSlice, out44: rt.ByteVec): void => { pointers_shared(data43, out44); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::unchecked", (_2120: rt.u8, data44: rt.ByteSlice, out45: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::unchecked", (_2123: rt.u8, data44: rt.ByteSlice, out45: rt.ByteVec): void => { unchecked(data44, out45); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["edits::bytes", bytes] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["arrays::from_fns", (_2121: rt.u8, data45: rt.ByteSlice, out46: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["edits::bytes", bytes] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["arrays::from_fns", (_2124: rt.u8, data45: rt.ByteSlice, out46: rt.ByteVec): void => { fromFns(data45, out46); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::optionals", optionals] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["objects::objects", (_2122: rt.u8, data46: rt.ByteSlice, out47: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::optionals", optionals] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["objects::objects", (_2125: rt.u8, data46: rt.ByteSlice, out47: rt.ByteVec): void => { objects(data46, out47); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["holds::holds", holds] 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], ["holds::holds", holds] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["orders::orders", orders] 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/iters.ts b/ts/fixtures/lang/expected/iters.ts index 05a9c2c5e..3ee3ec3d6 100644 --- a/ts/fixtures/lang/expected/iters.ts +++ b/ts/fixtures/lang/expected/iters.ts @@ -3,7 +3,7 @@ import * as rt from "./rt.ts"; import { copy$Bumped, copy$Option_u8_Reverse_Option_u8, copy$Option_usize_u8, copy$u8_u8 } from "./lib.ts"; import { thenSome$bool_u16, thenSome$bool_u8 } from "./core/bool.ts"; -import { unwrapOr$Option_NonZero_u64, unwrapOr$Option_i64, unwrapOr$Option_u8, unwrapOr$Option_usize_u8, map$Option_u8_u8_elements_closure8, mapOr$Option_Tilt_u8_ranked_closure11_closure0, mapOr$Option_Vec_u16_u8_consumers_closure23, mapOr$Option_u8_u8_btree_maps_closure5, mapOr$Option_u8_u8_u8_btree_maps_closure0, mapOr$Option_u8_usize_iters_closure1, mapOr$Option_u8s_u8_skips_closure44, mapOr$Option_usize_u8_consumers_closure8, mapOr$Option_usize_u8_u8_ranges_closure0, filter$Option_u8_combinators_closure0 } from "./core/option.ts"; +import { unwrapOr$Option_NonZero_u64, unwrapOr$Option_i64, unwrapOr$Option_u8, unwrapOr$Option_usize_u8, map$Option_u8_u8_elements_closure8, mapOr$Option_Tilt_u8_ranked_closure11_closure0, mapOr$Option_Vec_u16_u8_consumers_closure23, mapOr$Option_u8_u8_btree_maps_closure5, mapOr$Option_u8_u8_u8_btree_maps_closure0, mapOr$Option_u8_usize_iters_closure1, mapOr$Option_u8s_u8_skips_closure44, mapOr$Option_usize_u8_consumers_closure8, mapOr$Option_usize_u8_u8_heaps_closure0, filter$Option_u8_combinators_closure0 } from "./core/option.ts"; import * as Bumped from "./iters.Bumped.ts"; import * as Countdown from "./iters.Countdown.ts"; import * as Doubled from "./iters.Doubled.ts"; @@ -249,14 +249,14 @@ export function consumers(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): voi } rt.byteVecPush(out, back.len() & 0xff); } else if (_5 === 2) { - rt.byteVecPush(out, unwrapOr$Option_u8(rt.iterMax(rt.iterSlice(rt.byteSliceView(data)), rt.cmpLt(rt.cmp)), 0)); - rt.byteVecPush(out, unwrapOr$Option_u8(rt.iterMin(rt.iterSlice(rt.byteSliceView(data)), rt.cmpLt(rt.cmp)), 255)); + rt.byteVecPush(out, unwrapOr$Option_u8(rt.iterMax(rt.iterSlice(rt.byteSliceView(data)), rt.lt), 0)); + rt.byteVecPush(out, unwrapOr$Option_u8(rt.iterMin(rt.iterSlice(rt.byteSliceView(data)), rt.lt), 255)); rt.byteVecPush(out, unwrapOr$Option_u8(rt.iterMaxByKey(rt.iterSlice(rt.byteSliceView(data)), (_218: rt.u8): rt.u8 => { return rt.u8Rem(_218, 16); - }, rt.cmpLt(rt.cmp)), 238)); + }, rt.lt), 238)); rt.byteVecPush(out, unwrapOr$Option_u8(rt.iterMinByKey(rt.iterSlice(rt.byteSliceView(data)), (_219: rt.u8): rt.u8 => { return rt.u8Div(_219, 16); - }, rt.cmpLt(rt.cmp)), 238)); + }, rt.lt), 238)); rt.byteVecPush(out, unwrapOr$Option_u8(rt.iterLast(rt.iterSlice(rt.byteSliceView(data))), 221)); rt.byteVecPush(out, unwrapOr$Option_u8(rt.iterNth(rt.iterSlice(rt.byteSliceView(data)), 3), 204)); const it: rt.Iter = rt.iterSlice(rt.byteSliceView(data)); @@ -623,7 +623,7 @@ export function skips(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { } else { item21 = rt.iterNthBack(it23, n); } - rt.byteVecPush(out, mapOr$Option_usize_u8_u8_ranges_closure0(item21, 238, (_221: [rt.usize, rt.u8]): rt.u8 => { + rt.byteVecPush(out, mapOr$Option_usize_u8_u8_heaps_closure0(item21, 238, (_221: [rt.usize, rt.u8]): rt.u8 => { return _221[1] ^ (_221[0] & 0xff); })); rt.byteVecPush(out, rest11.len() & 0xff); @@ -636,7 +636,7 @@ export function skips(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { } else { item22 = rt.iterNthBack(it24, n); } - rt.byteVecPush(out, mapOr$Option_usize_u8_u8_ranges_closure0(item22, 238, (_223: [rt.usize, rt.u8]): rt.u8 => { + rt.byteVecPush(out, mapOr$Option_usize_u8_u8_heaps_closure0(item22, 238, (_223: [rt.usize, rt.u8]): rt.u8 => { return _223[1] ^ (_223[0] & 0xff); })); rt.byteVecExtendFromSlice(out, rt.byteVecSlice(log11)); @@ -730,7 +730,7 @@ export function skips(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { */ export function ranked(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { const _4: (a0: rt.ByteVec, a1: [rt.usize, rt.u8] | undefined) => void = (out2: rt.ByteVec, found2: [rt.usize, rt.u8] | undefined): void => { - rt.byteVecPush(out2, mapOr$Option_usize_u8_u8_ranges_closure0(found2, 255, (_2: [rt.usize, rt.u8]): rt.u8 => { + rt.byteVecPush(out2, mapOr$Option_usize_u8_u8_heaps_closure0(found2, 255, (_2: [rt.usize, rt.u8]): rt.u8 => { return _2[0] & 0xff; })); }; @@ -910,7 +910,7 @@ export function ranges(data: rt.ByteSlice, out: rt.ByteVec): void { const n: rt.u8 = _149[1]; rt.byteVecPush(out, i3 & 0xff); rt.byteVecPush(out, n); - rt.byteVecPush(out, mapOr$Option_usize_u8_u8_ranges_closure0(rt.iterToOption(counted.next()), 204, (_2: [rt.usize, rt.u8]): rt.u8 => { + rt.byteVecPush(out, mapOr$Option_usize_u8_u8_heaps_closure0(rt.iterToOption(counted.next()), 204, (_2: [rt.usize, rt.u8]): rt.u8 => { return _2[0] & 0xff; })); const stepped: rt.RangeInclusive = new rt.RangeInclusive(a, b, rt.u8Step); @@ -937,8 +937,8 @@ export function ranges(data: rt.ByteSlice, out: rt.ByteVec): void { }), rt.u64Add, 0)))); rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(unwrapOr$Option_NonZero_u64(rt.iterMaxByKey(rt.iterRev(new rt.RangeInclusive(edge, rt.u64Add(edge, 2), rt.u64Step)), (_24: rt.u64): rt.u64 => { return _24; - }, rt.cmpLt(rt.cmp)), 0)))); - rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.i64ToBeBytes(unwrapOr$Option_i64(rt.iterMin(new rt.Range(rt.i64Sub(rt.i64Neg({ hi: 0x200000, lo: 0 } as rt.i64), 1), rt.i64Add(rt.i64Neg({ hi: 0x200000, lo: 0 } as rt.i64), 1), rt.i64Step), rt.cmpLt(rt.cmp)), 0)))); + }, rt.lt), 0)))); + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.i64ToBeBytes(unwrapOr$Option_i64(rt.iterMin(new rt.Range(rt.i64Sub(rt.i64Neg({ hi: 0x200000, lo: 0 } as rt.i64), 1), rt.i64Add(rt.i64Neg({ hi: 0x200000, lo: 0 } as rt.i64), 1), rt.i64Step), rt.lt), 0)))); for (const _295 of rt.iterToArray(rt.iterRev(rt.iterStepBy(new rt.Range(0, 48), 8)))) { rt.byteVecPush(out, _295 & 0xff); } diff --git a/ts/fixtures/lang/expected/orders.Item.ts b/ts/fixtures/lang/expected/orders.Item.ts new file mode 100644 index 000000000..b3a7386e8 --- /dev/null +++ b/ts/fixtures/lang/expected/orders.Item.ts @@ -0,0 +1,43 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; +import { log } from "./orders.ts"; + +/** + * Ordered by `key` alone, so `tag` tells equal ones apart. Each comparison logs which it + * is and the two tags, and a drop logs the tag. + */ +export class Item { + key: rt.u8; + tag: rt.u8; + + constructor(key: rt.u8, tag: rt.u8) { + this.key = key; + this.tag = tag; + } + + [Symbol.dispose](): void { + drop$Drop(this); + } +} + +export function drop$Drop(self: Item): void { + log(Uint8Array.of(100, self.tag, 0)); +} + +export function lt$PartialOrd(self: Item, other: Item): boolean { + log(Uint8Array.of(60, self.tag, other.tag)); + return self.key < other.key; +} + +export function le$PartialOrd(self: Item, other: Item): boolean { + log(Uint8Array.of(108, self.tag, other.tag)); + return self.key <= other.key; +} + +export function ge$PartialOrd(self: Item, other: Item): boolean { + log(Uint8Array.of(103, self.tag, other.tag)); + return self.key >= other.key; +} + +//# sourceMappingURL=orders.Item.ts.map diff --git a/ts/fixtures/lang/expected/orders.ts b/ts/fixtures/lang/expected/orders.ts new file mode 100644 index 000000000..a19908945 --- /dev/null +++ b/ts/fixtures/lang/expected/orders.ts @@ -0,0 +1,387 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; +import { copy$u8_u8 } from "./lib.ts"; +import { min$Item, min$Item$2, min$String, min$u8, minBy$Item_cmps_closure2, minByKey$Item_cmps_closure4_Reverse_u8, max$Item, max$Item$2, max$String, max$i8, max$u8, maxBy$Item_cmps_closure1, maxByKey$Item_cmps_closure3_Item } from "./core/cmp.ts"; +import { mapOr$Option_Item_u8_heaps_closure1, mapOr$Option_usize_u8_u8_heaps_closure0 } from "./core/option.ts"; +import * as Item from "./orders.Item.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: Uint8Array): void { + const _5: rt.ByteSlice = rt.byteSliceFrom(entry); + rt.localKeyWithBorrowMut(LOG, (log_: rt.ByteVec): void => { + rt.iterExtendBytes(log_, rt.iterSlice(rt.byteSliceToArray(_5))); + }); +} + +/** 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 item(tag: rt.usize, key: rt.u8): Item.Item { + return new Item.Item(rt.u8Rem(key, 8), tag & 0xff); +} + +/** Run one case. */ +export function orders(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { + const _5: rt.u8 = rt.u8Rem(kind, 4); + if (_5 === 0) { + cmps(data, out); + } else if (_5 === 1) { + sorted(data, out); + } else if (_5 === 2) { + heaps(data, out); + } else { + values(data, out); + } + flush(out); +} + +/** + * `cmp::max` and `min` and their forms of each byte and the next, as integers, strings, + * and items, which the one not returned drops, and of references to items. + */ +export function cmps(data: rt.ByteSlice, out: rt.ByteVec): void { + let _7!: rt.usize; + let iterStart: rt.usize = 1; + const iterEnd: rt.usize = data.length; + while ((_7 = iterStart++) < iterEnd) { + const i: rt.usize = _7; + const _15: rt.usize = rt.usizeSub(i, 1); + rt.idx(data, _15); + const _14: rt.u8 = data.data[data.start + _15]; + rt.idx(data, i); + const _13: [rt.u8, rt.u8] = [_14, data.data[data.start + i]]; + const a: rt.u8 = _14; + const b: rt.u8 = _13[1]; + rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(max$u8(a, b), min$u8(a, b), max$i8((a << 24) >> 24, (b << 24) >> 24) & 0xff))); + const _37: rt.u8 = rt.u8Div(b, 2); + const _32: [string, string] = [rt.fmtToString((f: rt.Formatter) => rt.u8Display(a, f)), rt.fmtToString((f: rt.Formatter) => rt.u8Display(_37, f))]; + const s: string = _32[0]; + const t: string = _32[1]; + rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(rt.stringLen(max$String(s, t)) & 0xff, rt.stringLen(min$String(s, t)) & 0xff))); + const _55: [Item.Item, Item.Item] = [item(rt.usizeMul(2, i), a), item(rt.usizeAdd(rt.usizeMul(2, i), 1), b)]; + const x: Item.Item = _55[0]; + const y: Item.Item = _55[1]; + rt.byteVecPush(out, max$Item(x, y).tag); + rt.byteVecPush(out, min$Item(x, y).tag); + const _76: Item.Item = max$Item$2(x, y); + rt.byteVecPush(out, _76.tag); + _76[Symbol.dispose](); + const pair: () => [Item.Item, Item.Item] = (): [Item.Item, Item.Item] => { + return [item(rt.usizeMul(2, i), a), item(rt.usizeAdd(rt.usizeMul(2, i), 1), b)] as [Item.Item, Item.Item]; + }; + const _83: [Item.Item, Item.Item] = pair(); + const _87: Item.Item = min$Item$2(_83[0], _83[1]); + rt.byteVecPush(out, _87.tag); + _87[Symbol.dispose](); + const _90: [Item.Item, Item.Item] = pair(); + const _94: Item.Item = maxBy$Item_cmps_closure1(_90[0], _90[1], (l: Item.Item, r: Item.Item): rt.Ordering => { + return rt.orderingReverse(rt.cmp(l.key, r.key)); + }); + rt.byteVecPush(out, _94.tag); + _94[Symbol.dispose](); + const _97: [Item.Item, Item.Item] = pair(); + const _101: Item.Item = minBy$Item_cmps_closure2(_97[0], _97[1], (l2: Item.Item, r2: Item.Item): rt.Ordering => { + return rt.cmp(rt.u8Div(l2.key, 2), rt.u8Div(r2.key, 2)); + }); + rt.byteVecPush(out, _101.tag); + _101[Symbol.dispose](); + const _104: [Item.Item, Item.Item] = pair(); + const _108: Item.Item = maxByKey$Item_cmps_closure3_Item(_104[0], _104[1], (v: Item.Item): Item.Item => { + return item(rt.usizeAdd(v.tag as rt.usize, 100), rt.u8Div(v.key, 2)); + }); + rt.byteVecPush(out, _108.tag); + _108[Symbol.dispose](); + const _111: [Item.Item, Item.Item] = pair(); + const _115: Item.Item = minByKey$Item_cmps_closure4_Reverse_u8(_111[0], _111[1], (v2: Item.Item): rt.u8 => { + return v2.key; + }); + rt.byteVecPush(out, _115.tag); + _115[Symbol.dispose](); + flush(out); + } +} + +/** + * The `is_sorted` family of slices and iterators of items, bytes, and strings, as given + * and sorted: each test std makes, and each item or key it drops. + */ +export function sorted(data: rt.ByteSlice, out: rt.ByteVec): void { + const ascending: rt.ByteVec = rt.byteSliceToByteVec(data); + rt.sliceSortByKey(rt.byteSliceView(rt.byteVecSlice(ascending)), (b: rt.u8): rt.u8 => { + return rt.u8Rem(b, 8); + }, rt.lt); + const iter: rt.Iter = rt.iterSlice([data, rt.byteVecSlice(ascending)]); + while (true) { + const _12: rt.ByteSlice | rt.Done = iter.next(); + if (_12 === rt.DONE) { + break; + } + const items: () => rt.Iter = (): rt.Iter => { + return rt.iterMap(rt.iterEnumerate(rt.iterSlice(rt.byteSliceView(_12))), (_24: [rt.usize, rt.u8]): Item.Item => { + return item(_24[0], _24[1]); + }); + }; + const v: Item.Item[] = rt.iterToArray(items()); + rt.byteVecPush(out, rt.sliceIsSorted(v, Item.le$PartialOrd) ? 1 : 0); + rt.byteVecPush(out, rt.iterIsSortedBy(rt.iterSlice(v), (a: Item.Item, b3: Item.Item): boolean => { + return a.key >= b3.key; + }) ? 1 : 0); + rt.byteVecPush(out, rt.iterIsSortedBy(rt.iterMap(rt.iterSlice(v), (x: Item.Item): Item.Item => { + return item(rt.usizeAdd(x.tag as rt.usize, 100), rt.u8Div(x.key, 2)); + }), Item.le$PartialOrd, undefined, rt.drop) ? 1 : 0); + rt.byteVecPush(out, rt.iterIsSortedBy(rt.iterSlice(v), Item.le$PartialOrd) ? 1 : 0); + rt.byteVecPush(out, rt.iterIsSortedBy(rt.iterMap(rt.iterSlice(rt.sliceRange(v, 0, rt.usizeDiv(v.length, 2))), (x2: Item.Item): rt.u8 => { + return x2.key; + }), rt.le) ? 1 : 0); + rt.dropAll(v); + flush(out); + rt.byteVecPush(out, rt.iterIsSortedBy(items(), Item.le$PartialOrd, undefined, rt.drop) ? 1 : 0); + rt.byteVecPush(out, rt.iterIsSortedBy(items(), (a2: Item.Item, b4: Item.Item): boolean => { + if (!(a2.tag > b4.tag)) { + return a2.key <= b4.key; + } + return true; + }, undefined, rt.drop) ? 1 : 0); + rt.byteVecPush(out, rt.iterIsSortedBy(rt.iterMap(items(), (x3: Item.Item): rt.u8 => { + const _08: rt.u8 = x3.key; + x3[Symbol.dispose](); + return _08; + }), rt.cmpLe(rt.cmpReverse(rt.cmp))) ? 1 : 0); + rt.byteVecPush(out, rt.iterIsSortedBy(rt.iterSlice(rt.iterToArray(items()), rt.drop), Item.le$PartialOrd, true, rt.drop) ? 1 : 0); + flush(out); + const strings: string[] = rt.iterToArray(rt.iterMap(rt.iterSlice(rt.byteSliceView(_12)), (b5: rt.u8): string => { + const _411: rt.u8 = rt.u8Rem(b5, 16); + return rt.fmtToString((f: rt.Formatter) => rt.u8Display(_411, f)); + })); + rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(rt.sliceIsSorted(rt.byteSliceView(_12), rt.le) ? 1 : 0, rt.iterIsSortedBy(rt.iterSlice(rt.byteSliceView(_12)), (a3: rt.u8, b6: rt.u8): boolean => { + return rt.u8Div(a3, 4) <= rt.u8Div(b6, 4); + }) ? 1 : 0, rt.iterIsSortedBy(rt.iterMap(rt.iterSlice(rt.byteSliceView(_12)), (_25: rt.u8): rt.u8 => { + return rt.u8Rem(_25, 8); + }), rt.le) ? 1 : 0, rt.iterIsSortedBy(rt.iterSlice(rt.byteSliceView(_12)), rt.le) ? 1 : 0, rt.sliceIsSorted(strings, rt.cmpLe(rt.cmpStr)) ? 1 : 0, rt.iterIsSortedBy(rt.iterMap(rt.iterSlice(strings), (a06: string): rt.usize => { + return rt.stringLen(a06); + }), rt.le) ? 1 : 0, rt.iterIsSortedBy(rt.iterMap(rt.iterSlice(rt.byteSliceView(_12)), (_29: rt.u8): rt.f64 => { + return (_29 as rt.f64) / 3.0; + }), rt.cmpLe(rt.f64PartialCmp)) ? 1 : 0))); + } +} + +/** + * A heap of items changed by each pair of bytes, an operation and its operand: each + * comparison its sifts make, which of equal items it gives back, and when each drops. + */ +export function heaps(data: rt.ByteSlice, out: rt.ByteVec): void { + let _123: boolean = false; + let heap: rt.BinaryHeap = rt.heapNew() as rt.BinaryHeap; + const other: rt.BinaryHeap = rt.heapNew() as rt.BinaryHeap; + const ops: rt.Iter<[rt.usize, rt.u8]> = rt.iterEnumerate(rt.iterSlice(rt.byteSliceView(data))); + while (true) { + const _8: [rt.usize, rt.u8] | rt.Done = ops.next(); + if (_8 !== rt.DONE) { + const i: rt.usize = _8[0]; + const op: rt.u8 = _8[1]; + const v: rt.u8 = mapOr$Option_usize_u8_u8_heaps_closure0(rt.iterToOption(ops.next()), 0, (_2: [rt.usize, rt.u8]): rt.u8 => { + return _2[1]; + }); + b2: { + const _17: rt.u8 = rt.u8Rem(op, 12); + if (0 <= _17) { + if (_17 <= 2) { + rt.heapPush(heap, item(i, v), Item.le$PartialOrd); + break b2; + } + } + if (_17 === 3) { + rt.byteVecPush(out, mapOr$Option_Item_u8_heaps_closure1(rt.heapPop(heap, Item.le$PartialOrd), 255, (x5: Item.Item): rt.u8 => { + const _06: rt.u8 = x5.tag; + x5[Symbol.dispose](); + return _06; + })); + } else if (_17 === 4) { + const _26: rt.PeekMut | undefined = rt.heapPeekMut(heap, Item.le$PartialOrd, Item.ge$PartialOrd, Item.lt$PartialOrd); + if (_26 !== undefined) { + const top: rt.PeekMut = _26; + rt.peekMutDerefMut(top); + top.v.key = rt.u8Rem(v, 8); + rt.peekMutDrop(top); + } + } else if (_17 === 5) { + const _33: rt.PeekMut | undefined = rt.heapPeekMut(heap, Item.le$PartialOrd, Item.ge$PartialOrd, Item.lt$PartialOrd); + if (_33 !== undefined) { + _123 = true; + const top2: rt.PeekMut = _33; + const _37: rt.u8 = rt.u8Rem(v, 3); + if (_37 === 0) { + _123 = false; + const _40: Item.Item = rt.peekMutPop(top2); + rt.byteVecPush(out, _40.tag); + _40[Symbol.dispose](); + } else if (_37 === 1) { + rt.byteVecPush(out, top2.v.tag); + } else { + _123 = false; + const top3: rt.PeekMut = top2; + rt.peekMutDerefMut(top3); + const _46: Item.Item = top3.v; + _46.tag = _46.tag ^ 64; + const _50: Item.Item = rt.peekMutPop(top3); + rt.byteVecPush(out, _50.tag); + _50[Symbol.dispose](); + } + if (_123) { + rt.peekMutDrop(top2); + } + _123 = false; + } + } else if (_17 === 6) { + const _53: Item.Item = item(i, v); + rt.heapPush(other, _53, Item.le$PartialOrd); + } else if (_17 === 7) { + rt.heapAppend(heap, other, Item.le$PartialOrd, Item.ge$PartialOrd, Item.lt$PartialOrd); + } else if (_17 === 8) { + rt.heapRetain(heap, (x4: Item.Item): boolean => { + return x4.key !== rt.u8Rem(v, 8); + }, Item.le$PartialOrd, Item.ge$PartialOrd, Item.lt$PartialOrd, rt.drop); + } else if (_17 === 9) { + rt.heapExtend(heap, rt.iterMap(rt.iterTake(rt.iterSlice(rt.byteSliceView(data)), rt.usizeRem(v as rt.usize, 6)), (_28: rt.u8): Item.Item => { + return item(rt.usizeAdd(i, 50), _28); + }), Item.le$PartialOrd, Item.ge$PartialOrd, Item.lt$PartialOrd); + } else if (_17 === 10) { + const sorted_: Item.Item[] = rt.heapIntoSortedVec(rt.replaceIn(heap, rt.heapNew() as rt.BinaryHeap), Item.le$PartialOrd, Item.ge$PartialOrd, Item.lt$PartialOrd); + rt.iterExtendBytes(out, rt.iterMap(rt.iterSlice(sorted_), (x3: Item.Item): rt.u8 => { + return x3.tag; + })); + flush(out); + const _77: rt.BinaryHeap = rt.heapFrom(sorted_, Item.le$PartialOrd, Item.ge$PartialOrd, Item.lt$PartialOrd); + rt.heapDrop(heap, rt.drop); + heap = _77; + } else { + rt.iterExtendBytes(out, rt.iterMap(rt.heapIter(heap), (x: Item.Item): rt.u8 => { + return x.tag; + })); + rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(mapOr$Option_Item_u8_heaps_closure1(heap.d[0], 255, (x2: Item.Item): rt.u8 => { + return x2.tag; + }), heap.d.length & 0xff, heap.d.length === 0 ? 1 : 0))); + } + } + flush(out); + } else { + break; + } + } + const drain: rt.Iter = rt.heapDrain(heap, rt.drop); + rt.byteVecPush(out, mapOr$Option_Item_u8_heaps_closure1(rt.iterToOption(drain.next()), 255, (x6: Item.Item): rt.u8 => { + const _07: rt.u8 = x6.tag; + x6[Symbol.dispose](); + return _07; + })); + rt.drop(drain); + flush(out); + const collected: rt.BinaryHeap = rt.heapFrom(rt.iterToArray(rt.iterMap(rt.iterEnumerate(rt.iterSlice(rt.byteSliceView(data))), (_29: [rt.usize, rt.u8]): Item.Item => { + return item(_29[0], _29[1]); + })), Item.le$PartialOrd, Item.ge$PartialOrd, Item.lt$PartialOrd); + rt.byteVecPush(out, mapOr$Option_Item_u8_heaps_closure1(rt.heapPop(collected, Item.le$PartialOrd), 255, (x7: Item.Item): rt.u8 => { + const _09: rt.u8 = x7.tag; + x7[Symbol.dispose](); + return _09; + })); + const rest: rt.Iter = rt.heapIter(collected, rt.drop); + rt.byteVecPush(out, mapOr$Option_Item_u8_heaps_closure1(rt.iterToOption(rest.next()), 255, (x8: Item.Item): rt.u8 => { + const _010: rt.u8 = x8.tag; + x8[Symbol.dispose](); + return _010; + })); + rt.drop(rest); + flush(out); + rt.heapClear(other, rt.drop); + flush(out); + rt.heapDrop(other, rt.drop); + rt.heapDrop(heap, rt.drop); +} + +/** + * Heaps of values with no comparison of their own: bytes, a min-heap of them, strings, + * and tuples, built every way, and their vectors, slices, clones, and debug form. + */ +export function values(data: rt.ByteSlice, out: rt.ByteVec): void { + const bytes: rt.BinaryHeap = rt.heapFrom(Array.from(rt.byteVecView(rt.byteSliceToByteVec(data))), rt.le, rt.ge, rt.lt); + rt.heapPush(bytes, data.length & 0xff, rt.le); + rt.iterExtendBytes(out, rt.iterSlice(rt.byteSliceView(rt.byteSliceFrom(Uint8Array.from(bytes.d))))); + rt.iterExtendBytes(out, rt.iterSlice(rt.byteVecView(rt.byteVecFrom(Uint8Array.from(rt.heapIntoSortedVec(rt.heapClone(bytes), rt.le, rt.ge, rt.lt)))))); + const args2: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtList(bytes.d, f, rt.u8Debug)]; + rt.iterExtendBytes(out, rt.strBytes(rt.fmtFormat([args2[0]]))); + rt.iterExtendBytes(out, rt.iterSlice(rt.byteVecView(rt.byteVecFrom(Uint8Array.from(bytes.d))))); + const least: rt.BinaryHeap = rt.heapFrom(rt.iterToArray(rt.iterMap(rt.iterSlice(rt.byteSliceView(data)), (_2: rt.u8): rt.u8 => { + return _2; + })), rt.cmpLe(rt.cmpReverse(rt.cmp)), rt.cmpGe(rt.cmpReverse(rt.cmp)), rt.cmpLt(rt.cmpReverse(rt.cmp))); + const _37: rt.Iter = rt.iterMap(rt.iterTake(rt.iterSlice(rt.byteSliceView(data)), 3), (_23: rt.u8): rt.u8 => { + return rt.u8Div(_23, 2); + }); + rt.heapExtend(least, _37, rt.cmpLe(rt.cmpReverse(rt.cmp)), rt.cmpGe(rt.cmpReverse(rt.cmp)), rt.cmpLt(rt.cmpReverse(rt.cmp))); + while (true) { + const _40: rt.u8 | undefined = rt.heapPop(least, rt.cmpLe(rt.cmpReverse(rt.cmp))); + if (_40 !== undefined) { + rt.byteVecPush(out, _40); + } else { + break; + } + } + const _51: rt.usize = data.length; + const strings: rt.BinaryHeap = rt.heapFrom(rt.iterToArray(rt.iterSlice(["pear", "apple", rt.fmtToString((f: rt.Formatter) => rt.usizeDisplay(_51, f))])), rt.cmpLe(rt.cmpStr), rt.cmpGe(rt.cmpStr), rt.cmpLt(rt.cmpStr)); + const copy: rt.BinaryHeap = rt.heapClone(strings); + const _56: rt.Iter = rt.iterMap(rt.iterSlice(rt.byteSliceView(data)), (b4: rt.u8): string => { + const _43: rt.u8 = rt.u8Rem(b4, 10); + return rt.fmtToString((f: rt.Formatter) => rt.u8Display(_43, f)); + }); + rt.heapExtend(copy, _56, rt.cmpLe(rt.cmpStr), rt.cmpGe(rt.cmpStr), rt.cmpLt(rt.cmpStr)); + rt.heapRetain(copy, (s: string): boolean => { + if (!(rt.stringLen(s) > 1)) { + return rt.cmpStr(s, "5") === -1; + } + return true; + }, rt.cmpLe(rt.cmpStr), rt.cmpGe(rt.cmpStr), rt.cmpLt(rt.cmpStr)); + rt.iterExtendBytes(out, rt.iterFlatMap(rt.iterSlice(copy.d), (s2: string): rt.Iter => { + return rt.strBytes(s2); + })); + rt.iterExtendBytes(out, rt.iterMap(rt.heapIter(strings), (s3: string): rt.u8 => { + return rt.stringLen(s3) & 0xff; + })); + const pairs: rt.BinaryHeap<[rt.u8, rt.u8]> = rt.heapNew() as rt.BinaryHeap<[rt.u8, rt.u8]>; + const _73: rt.Iter<[rt.u8, rt.u8]> = rt.iterMap(rt.iterSlice(rt.byteSliceView(data)), (_24: rt.u8): [rt.u8, rt.u8] => { + const b5: rt.u8 = _24; + return [rt.u8Rem(b5, 4), rt.u8Div(b5, 64)] as [rt.u8, rt.u8]; + }); + rt.heapExtend(pairs, _73, rt.cmpLe(rt.cmpTuple([rt.cmp, rt.cmp])), rt.cmpGe(rt.cmpTuple([rt.cmp, rt.cmp])), rt.cmpLt(rt.cmpTuple([rt.cmp, rt.cmp]))); + const _78: [rt.u8, rt.u8][] = [[rt.u8Rem(data.length & 0xff, 4), 9] as [rt.u8, rt.u8]]; + rt.heapExtend(pairs, rt.iterMap(rt.iterSlice(_78), copy$u8_u8), rt.cmpLe(rt.cmpTuple([rt.cmp, rt.cmp])), rt.cmpGe(rt.cmpTuple([rt.cmp, rt.cmp])), rt.cmpLt(rt.cmpTuple([rt.cmp, rt.cmp]))); + const _84: rt.PeekMut<[rt.u8, rt.u8]> | undefined = rt.heapPeekMut(pairs, rt.cmpLe(rt.cmpTuple([rt.cmp, rt.cmp])), rt.cmpGe(rt.cmpTuple([rt.cmp, rt.cmp])), rt.cmpLt(rt.cmpTuple([rt.cmp, rt.cmp]))); + if (_84 !== undefined) { + const top: rt.PeekMut<[rt.u8, rt.u8]> = _84; + const _88: rt.u8 = rt.u8WrappingAdd(top.v[1], 1); + rt.peekMutDerefMut(top); + top.v[1] = _88; + rt.peekMutDrop(top); + } + const _94: rt.PeekMut<[rt.u8, rt.u8]> | undefined = rt.heapPeekMut(pairs, rt.cmpLe(rt.cmpTuple([rt.cmp, rt.cmp])), rt.cmpGe(rt.cmpTuple([rt.cmp, rt.cmp])), rt.cmpLt(rt.cmpTuple([rt.cmp, rt.cmp]))); + if (_94 !== undefined) { + const top2: rt.PeekMut<[rt.u8, rt.u8]> = _94; + rt.peekMutDerefMut(top2); + Object.assign(top2.v, [0, 0] as [rt.u8, rt.u8]); + rt.peekMutDrop(top2); + } + rt.iterExtendBytes(out, rt.iterFlatMap(rt.iterSlice(rt.heapIntoSortedVec(pairs, rt.cmpLe(rt.cmpTuple([rt.cmp, rt.cmp])), rt.cmpGe(rt.cmpTuple([rt.cmp, rt.cmp])), rt.cmpLt(rt.cmpTuple([rt.cmp, rt.cmp])))), (_25: [rt.u8, rt.u8]): Uint8Array => { + return Uint8Array.of(_25[0], _25[1]); + }, (value: Uint8Array) => rt.iterSlice(value))); +} + +//# sourceMappingURL=orders.ts.map diff --git a/ts/fixtures/lang/expected/refs.ts b/ts/fixtures/lang/expected/refs.ts index eddf7610b..04c64bb88 100644 --- a/ts/fixtures/lang/expected/refs.ts +++ b/ts/fixtures/lang/expected/refs.ts @@ -694,7 +694,7 @@ export function elements(data: rt.ByteSlice, out: rt.ByteVec): void { } const most: rt.Place | undefined = rt.iterMaxByKey(rt.iterSliceMut(v), (x7: rt.Place): rt.u32 => { return x7.v; - }, rt.cmpLt(rt.cmp)); + }, rt.lt); if (most !== undefined) { most.v = 0; } diff --git a/ts/fixtures/lang/expected/wrappers.ts b/ts/fixtures/lang/expected/wrappers.ts index 764999c84..81db8add3 100644 --- a/ts/fixtures/lang/expected/wrappers.ts +++ b/ts/fixtures/lang/expected/wrappers.ts @@ -324,7 +324,7 @@ export function wrappers(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void putU64(out, unwrapOr$Option_NonZero_u64(rt.iterLast(rt.iterBorrow(last)), 238)); bounds$RangeInclusive_u64(out, last); const most: rt.RangeInclusive = new rt.RangeInclusive(lo, hi, rt.u64Step); - putU64(out, unwrapOr$Option_NonZero_u64(rt.iterMax(rt.iterBorrow(most), rt.cmpLt(rt.cmp)), 238)); + putU64(out, unwrapOr$Option_NonZero_u64(rt.iterMax(rt.iterBorrow(most), rt.lt), 238)); bounds$RangeInclusive_u64(out, most); const collected: rt.RangeInclusive = new rt.RangeInclusive(lo, hi, rt.u64Step); rt.byteVecPush(out, rt.iterToArray(rt.iterBorrow(collected)).length & 0xff); diff --git a/ts/fixtures/lang/src/harness.rs b/ts/fixtures/lang/src/harness.rs index 354738fa1..94e798658 100644 --- a/ts/fixtures/lang/src/harness.rs +++ b/ts/fixtures/lang/src/harness.rs @@ -3,8 +3,8 @@ use crate::{ arrays, atomics, borrows, captures, cells, chars, chunks, collections, combinators, counts, cows, defaults, - durations, dyns, edits, floats, fmt, generics, holds, ints, iters, keys, locals, locks, nested, objects, pointers, - refs, shapes, splits, statics, strs, utf8, wrappers, + durations, dyns, edits, floats, fmt, generics, holds, ints, iters, keys, locals, locks, nested, objects, orders, + pointers, refs, shapes, splits, statics, strs, utf8, wrappers, }; /// The native side's entry: [`run`], except that a case starting with 0xff prints a @@ -114,6 +114,7 @@ fn cases() -> Vec<(&'static str, Case)> { ("collections::optionals", collections::optionals), ("objects::objects", |_, data, out| objects::objects(data, out)), ("holds::holds", holds::holds), + ("orders::orders", orders::orders), ] } diff --git a/ts/fixtures/lang/src/lib.rs b/ts/fixtures/lang/src/lib.rs index dbcb31708..a82b62cf8 100644 --- a/ts/fixtures/lang/src/lib.rs +++ b/ts/fixtures/lang/src/lib.rs @@ -40,6 +40,7 @@ mod locals; mod locks; mod nested; mod objects; +mod orders; mod pointers; mod refs; mod shapes; diff --git a/ts/fixtures/lang/src/orders.rs b/ts/fixtures/lang/src/orders.rs new file mode 100644 index 000000000..6aab8957a --- /dev/null +++ b/ts/fixtures/lang/src/orders.rs @@ -0,0 +1,257 @@ +//! Ordering in std: `cmp`'s `max` and `min` and their `_by` and `_by_key` forms, the +//! `is_sorted` family, and `BinaryHeap`, of a type that logs each comparison std makes of +//! it and each drop, so which tests run, in what order, which of equal values comes out, +//! and when each value drops all show. + +use std::cell::RefCell; +use std::cmp::{self, Ordering, Reverse}; +use std::collections::BinaryHeap; +use std::collections::binary_heap::PeekMut; + +thread_local! { + static LOG: RefCell> = const { RefCell::new(Vec::new()) }; +} + +fn log(entry: [u8; 3]) { + LOG.with_borrow_mut(|log| log.extend(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(); + }); +} + +/// Ordered by `key` alone, so `tag` tells equal ones apart. Each comparison logs which it +/// is and the two tags, and a drop logs the tag. +struct Item { + key: u8, + tag: u8, +} + +fn item(tag: usize, key: u8) -> Item { + Item { + key: key % 8, + tag: tag as u8, + } +} + +impl Drop for Item { + fn drop(&mut self) { + log([b'd', self.tag, 0]); + } +} + +impl PartialEq for Item { + fn eq(&self, other: &Self) -> bool { + self.key == other.key + } +} + +impl Eq for Item {} + +impl Ord for Item { + fn cmp(&self, other: &Self) -> Ordering { + log([b'c', self.tag, other.tag]); + self.key.cmp(&other.key) + } +} + +impl PartialOrd for Item { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } + + fn lt(&self, other: &Self) -> bool { + log([b'<', self.tag, other.tag]); + self.key < other.key + } + + fn le(&self, other: &Self) -> bool { + log([b'l', self.tag, other.tag]); + self.key <= other.key + } + + fn gt(&self, other: &Self) -> bool { + log([b'>', self.tag, other.tag]); + self.key > other.key + } + + fn ge(&self, other: &Self) -> bool { + log([b'g', self.tag, other.tag]); + self.key >= other.key + } +} + +/// Run one case. +pub fn orders(kind: u8, data: &[u8], out: &mut Vec) { + match kind % 4 { + 0 => cmps(data, out), + 1 => sorted(data, out), + 2 => heaps(data, out), + _ => values(data, out), + } + flush(out); +} + +/// `cmp::max` and `min` and their forms of each byte and the next, as integers, strings, +/// and items, which the one not returned drops, and of references to items. +fn cmps(data: &[u8], out: &mut Vec) { + for i in 1..data.len() { + let (a, b) = (data[i - 1], data[i]); + out.extend([cmp::max(a, b), cmp::min(a, b), cmp::max(a as i8, b as i8) as u8]); + let (s, t) = (a.to_string(), (b / 2).to_string()); + out.extend([cmp::max(s.clone(), t.clone()).len() as u8, cmp::min(s, t).len() as u8]); + let (x, y) = (item(2 * i, a), item(2 * i + 1, b)); + out.push(cmp::max(&x, &y).tag); + out.push(cmp::min(&x, &y).tag); + out.push(cmp::max(x, y).tag); + let pair = || (item(2 * i, a), item(2 * i + 1, b)); + let (x, y) = pair(); + out.push(cmp::min(x, y).tag); + let (x, y) = pair(); + out.push(cmp::max_by(x, y, |l, r| l.key.cmp(&r.key).reverse()).tag); + let (x, y) = pair(); + out.push(cmp::min_by(x, y, |l, r| (l.key / 2).cmp(&(r.key / 2))).tag); + let (x, y) = pair(); + out.push(cmp::max_by_key(x, y, |v| item(v.tag as usize + 100, v.key / 2)).tag); + let (x, y) = pair(); + out.push(cmp::min_by_key(x, y, |v| Reverse(v.key)).tag); + flush(out); + } +} + +/// The `is_sorted` family of slices and iterators of items, bytes, and strings, as given +/// and sorted: each test std makes, and each item or key it drops. +fn sorted(data: &[u8], out: &mut Vec) { + let mut ascending = data.to_vec(); + ascending.sort_by_key(|b| b % 8); + for bytes in [data, &ascending] { + let items = || bytes.iter().enumerate().map(|(i, &b)| item(i, b)); + let v: Vec = items().collect(); + out.push(v.is_sorted() as u8); + out.push(v.is_sorted_by(|a, b| a.key >= b.key) as u8); + out.push(v.is_sorted_by_key(|x| item(x.tag as usize + 100, x.key / 2)) as u8); + out.push(v.iter().is_sorted() as u8); + out.push(v[..v.len() / 2].iter().is_sorted_by_key(|x| x.key) as u8); + drop(v); + flush(out); + out.push(items().is_sorted() as u8); + out.push(items().is_sorted_by(|a, b| a.tag > b.tag || a.key <= b.key) as u8); + out.push(items().is_sorted_by_key(|x| Reverse(x.key)) as u8); + out.push(items().collect::>().into_iter().is_sorted() as u8); + flush(out); + let strings: Vec = bytes.iter().map(|b| (b % 16).to_string()).collect(); + out.extend([ + bytes.is_sorted() as u8, + bytes.is_sorted_by(|a, b| a / 4 <= b / 4) as u8, + bytes.is_sorted_by_key(|&b| b % 8) as u8, + bytes.iter().is_sorted() as u8, + strings.is_sorted() as u8, + strings.iter().map(String::len).is_sorted() as u8, + bytes.iter().map(|&b| b as f64 / 3.0).is_sorted() as u8, + ]); + } +} + +/// A heap of items changed by each pair of bytes, an operation and its operand: each +/// comparison its sifts make, which of equal items it gives back, and when each drops. +fn heaps(data: &[u8], out: &mut Vec) { + let mut heap = BinaryHeap::new(); + let mut other = BinaryHeap::with_capacity(4); + let mut ops = data.iter().copied().enumerate(); + while let Some((i, op)) = ops.next() { + let v = ops.next().map_or(0, |(_, v)| v); + match op % 12 { + 0..=2 => heap.push(item(i, v)), + 3 => out.push(heap.pop().map_or(0xff, |x| x.tag)), + // `DerefMut` sifts the top once it drops, but of a heap of one. + 4 => { + if let Some(mut top) = heap.peek_mut() { + top.key = v % 8; + } + } + // `Deref` alone sifts nothing, and `PeekMut::pop` pops. + 5 => { + if let Some(top) = heap.peek_mut() { + match v % 3 { + 0 => out.push(PeekMut::pop(top).tag), + 1 => out.push(top.tag), + _ => { + let mut top = top; + top.tag ^= 0x40; + out.push(PeekMut::pop(top).tag); + } + } + } + } + 6 => other.push(item(i, v)), + 7 => heap.append(&mut other), + 8 => heap.retain(|x| x.key != v % 8), + 9 => heap.extend(data.iter().take(v as usize % 6).map(|&b| item(i + 50, b))), + 10 => { + let sorted = std::mem::take(&mut heap).into_sorted_vec(); + out.extend(sorted.iter().map(|x| x.tag)); + flush(out); + heap = BinaryHeap::from(sorted); + } + _ => { + out.extend(heap.iter().map(|x| x.tag)); + out.extend([ + heap.peek().map_or(0xff, |x| x.tag), + heap.len() as u8, + heap.is_empty() as u8, + ]); + } + } + flush(out); + } + let mut drain = heap.drain(); + out.push(drain.next().map_or(0xff, |x| x.tag)); + drop(drain); + flush(out); + let mut collected: BinaryHeap = data.iter().enumerate().map(|(i, &b)| item(i, b)).collect(); + out.push(collected.pop().map_or(0xff, |x| x.tag)); + let mut rest = collected.into_iter(); + out.push(rest.next().map_or(0xff, |x| x.tag)); + drop(rest); + flush(out); + other.clear(); + flush(out); +} + +/// Heaps of values with no comparison of their own: bytes, a min-heap of them, strings, +/// and tuples, built every way, and their vectors, slices, clones, and debug form. +fn values(data: &[u8], out: &mut Vec) { + let mut bytes = BinaryHeap::from(data.to_vec()); + bytes.push(data.len() as u8); + out.extend(bytes.as_slice()); + out.extend(bytes.clone().into_sorted_vec()); + out.extend(format!("{bytes:?}").bytes()); + let v: Vec = bytes.into(); + out.extend(v); + let mut least: BinaryHeap> = data.iter().map(|&b| Reverse(b)).collect(); + least.extend(data.iter().take(3).map(|&b| Reverse(b / 2))); + while let Some(Reverse(b)) = least.pop() { + out.push(b); + } + let strings: BinaryHeap = + BinaryHeap::from([String::from("pear"), String::from("apple"), data.len().to_string()]); + let mut copy = strings.clone(); + copy.extend(data.iter().map(|b| (b % 10).to_string())); + copy.retain(|s| s.len() > 1 || s.as_str() < "5"); + out.extend(copy.into_vec().iter().flat_map(|s| s.bytes())); + out.extend(strings.into_iter().map(|s| s.len() as u8)); + let mut pairs: BinaryHeap<(u8, u8)> = BinaryHeap::new(); + pairs.extend(data.iter().map(|&b| (b % 4, b / 64))); + pairs.extend(&[(data.len() as u8 % 4, 9)]); + if let Some(mut top) = pairs.peek_mut() { + top.1 = top.1.wrapping_add(1); + } + if let Some(mut top) = pairs.peek_mut() { + *top = (0, 0); + } + out.extend(pairs.into_sorted_vec().into_iter().flat_map(|(a, b)| [a, b])); +} diff --git a/ts/fixtures/reject/census.md b/ts/fixtures/reject/census.md index fc1213895..212eae86c 100644 --- a/ts/fixtures/reject/census.md +++ b/ts/fixtures/reject/census.md @@ -1,21 +1,21 @@ # Census of `reject-fixture` -- Refusals: 96 -- Function instances: 109 +- Refusals: 98 +- Function instances: 111 - Generic functions no instance reached, lowered over their type parameters: 7 ## By construct | Count | Construct | First | | ---: | --- | --- | -| 16 | unsupported call to `_` | fixtures/reject/src/lib.rs:21:4 | +| 17 | unsupported call to `_` | fixtures/reject/src/lib.rs:21:4 | | 4 | a variant of `_`, which is not supported: either is the same JS string | fixtures/reject/src/lib.rs:385:2 | +| 3 | `_` of `_`, whose `None` is `rt.None`, which is not supported yet | fixtures/reject/src/lib.rs:41:42 | | 3 | a copy of a closure that changes what it captured | fixtures/reject/src/lib.rs:260:14 | | 3 | an assignment through a reference to `_` | fixtures/reject/src/lib.rs:102:2 | | 3 | replacing the value behind a reference to `_` | fixtures/reject/src/lib.rs:110:2 | | 3 | unsupported type `_` | fixtures/reject/src/lib.rs:7:14 | | 2 | `_` is not supported: its `None` and `Some` would both be `rt.None` | fixtures/reject/src/lib.rs:16:15 | -| 2 | `_` of `_`, whose `None` is `rt.None`, which is not supported yet | fixtures/reject/src/lib.rs:41:42 | | 2 | `_` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it | fixtures/reject/src/lib.rs:250:16 | | 2 | `_`, a trait object of 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 | @@ -76,8 +76,8 @@ The type or module each refused type or path belongs to. | Count | API | First | | ---: | --- | --- | +| 7 | `Option` | fixtures/reject/src/lib.rs:16:15 | | 7 | `std::borrow::Cow` | fixtures/reject/src/lib.rs:385:2 | -| 6 | `Option` | fixtures/reject/src/lib.rs:16:15 | | 6 | `f32` | fixtures/reject/src/lib.rs:7:14 | | 4 | `Iterator` | fixtures/reject/src/lib.rs:41:42 | | 4 | `std::pin::Pin` | fixtures/reject/src/lib.rs:110:2 | @@ -101,6 +101,7 @@ The type or module each refused type or path belongs to. | 2 | `std::sync::MutexGuard` | fixtures/reject/src/lib.rs:102:2 | | 2 | `std::vec::IntoIter` | fixtures/reject/src/lib.rs:483:17 | | 1 | `Batched` | fixtures/reject/src/lib.rs:363:16 | +| 1 | `BinaryHeap` | fixtures/reject/src/lib.rs:706:7 | | 1 | `Bits` | fixtures/reject/src/lib.rs:643:1 | | 1 | `BytesMut` | fixtures/reject/src/lib.rs:437:6 | | 1 | `Counted` | fixtures/reject/src/lib.rs:443:2 | @@ -128,6 +129,7 @@ The type or module each refused type or path belongs to. | 1 | `std::iter::Chain` | fixtures/reject/src/lib.rs:182:51 | | 1 | `std::iter::Rev` | fixtures/reject/src/lib.rs:176:14 | | 1 | `std::option::Option` | fixtures/reject/src/lib.rs:423:2 | +| 1 | `std::slice::IterMut` | fixtures/reject/src/lib.rs:711:15 | | 1 | `std::slice::Iter` | fixtures/reject/src/lib.rs:136:11 | | 1 | `std::std::Layout` | fixtures/reject/src/lib.rs:626:15 | | 1 | `std::string::String` | fixtures/reject/src/lib.rs:74:10 | @@ -159,6 +161,7 @@ The type or module each refused type or path belongs to. | 1 | `_` of `_` by `_`, which is not supported yet | `HashMap::contains_key`, `std::collections::HashMap`, `u8` | fixtures/reject/src/lib.rs:289:6 | | 1 | `_` of `_`, which drops `_`, which is not supported yet | `into_keys`, `std::collections::HashMap`, `Noisy` | fixtures/reject/src/lib.rs:53:13 | | 1 | `_` of `_`, which is not supported yet | `std::any::Any::type_id`, `dyn std::any::Any` | fixtures/reject/src/lib.rs:599:4 | +| 1 | `_` of `_`, whose `None` is `rt.None`, which is not supported yet | `BinaryHeap::pop`, `Option` | fixtures/reject/src/lib.rs:706:7 | | 1 | `_` of `_`, whose `None` is `rt.None`, which is not supported yet | `HashSet::get`, `Option` | fixtures/reject/src/lib.rs:266:6 | | 1 | `_` of `_`, whose `None` is `rt.None`, which is not supported yet | `Iterator::find`, `Option` | fixtures/reject/src/lib.rs:41:42 | | 1 | `_` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it | `std::iter::Iterator::count` | fixtures/reject/src/lib.rs:250:38 | @@ -211,6 +214,7 @@ The type or module each refused type or path belongs to. | 1 | unsupported call to `_` | `::deref` | fixtures/reject/src/lib.rs:397:2 | | 1 | unsupported call to `_` | `::eq` | fixtures/reject/src/lib.rs:423:2 | | 1 | unsupported call to `_` | `::cycle` | fixtures/reject/src/lib.rs:136:11 | +| 1 | unsupported call to `_` | `::is_sorted` | fixtures/reject/src/lib.rs:711:15 | | 1 | unsupported call to `_` | `::write_fmt` | fixtures/reject/src/lib.rs:74:10 | | 1 | unsupported call to `_` | `::hash` | fixtures/reject/src/lib.rs:354:10 | | 1 | unsupported call to `_` | `std::any::TypeId::of` | fixtures/reject/src/lib.rs:599:17 | @@ -223,7 +227,7 @@ The type or module each refused type or path belongs to. ## Spans -
unsupported call to `_` (16) +
unsupported call to `_` (17) - fixtures/reject/src/lib.rs:21:4: unsupported call to `core::slice::::rotate_left` - fixtures/reject/src/lib.rs:74:10: unsupported call to `::write_fmt` @@ -241,6 +245,7 @@ The type or module each refused type or path belongs to. - fixtures/reject/src/lib.rs:626:15: unsupported call to `std::alloc::Layout::new::` - fixtures/reject/src/lib.rs:628:11: unsupported call to `std::alloc::alloc` - fixtures/reject/src/lib.rs:631:3: unsupported call to `std::alloc::dealloc` +- fixtures/reject/src/lib.rs:711:15: unsupported call to ` as std::iter::Iterator>::is_sorted`
@@ -253,6 +258,14 @@ The type or module each refused type or path belongs to.
+
`_` of `_`, whose `None` is `rt.None`, which is not supported yet (3) + +- fixtures/reject/src/lib.rs:41:42: `Iterator::find` of `Option>`, whose `None` is `rt.None`, which is not supported yet +- fixtures/reject/src/lib.rs:266:6: `HashSet::get` of `Option<&std::option::Option>`, whose `None` is `rt.None`, which is not supported yet +- fixtures/reject/src/lib.rs:706:7: `BinaryHeap::pop` of `Option>`, whose `None` is `rt.None`, which is not supported yet + +
+
a copy of a closure that changes what it captured (3) - fixtures/reject/src/lib.rs:260:14: a copy of a closure that changes what it captured @@ -292,13 +305,6 @@ The type or module each refused type or path belongs to.
-
`_` of `_`, whose `None` is `rt.None`, which is not supported yet (2) - -- fixtures/reject/src/lib.rs:41:42: `Iterator::find` of `Option>`, whose `None` is `rt.None`, which is not supported yet -- fixtures/reject/src/lib.rs:266:6: `HashSet::get` of `Option<&std::option::Option>`, whose `None` is `rt.None`, which is not supported yet - -
-
`_` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it (2) - fixtures/reject/src/lib.rs:250:16: `std::iter::Iterator::filter` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it diff --git a/ts/fixtures/reject/expected.txt b/ts/fixtures/reject/expected.txt index e4c73c741..e6a92c6ed 100644 --- a/ts/fixtures/reject/expected.txt +++ b/ts/fixtures/reject/expected.txt @@ -88,6 +88,8 @@ a raw pointer to `(u32, u32)` in a variable assigned again while it may point th a raw pointer to `(u32, u32)` in an element, a payload, or a union field, which is not supported yet at fixtures/reject/src/lib.rs:685:10 comparing pointers to `()`, which has no bytes and so no address of its own at fixtures/reject/src/lib.rs:692:2 a dangling pointer to `Only`, a value of no bytes made of more than structs and tuples at fixtures/reject/src/lib.rs:701:11 +`BinaryHeap::pop` of `Option>`, whose `None` is `rt.None`, which is not supported yet at fixtures/reject/src/lib.rs:706:7 +unsupported call to ` as std::iter::Iterator>::is_sorted` at fixtures/reject/src/lib.rs:711:15 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 290eac556..f17505f88 100644 --- a/ts/fixtures/reject/src/lib.rs +++ b/ts/fixtures/reject/src/lib.rs @@ -700,3 +700,13 @@ pub enum Only { pub fn dangling_enum() -> bool { unsafe { std::ptr::NonNull::::dangling().as_ptr().as_ref().is_some() } } + +/// A heap's `pop` of an item that can be `undefined`: its `None` and `Some(None)` would both be. +pub fn heap_optional(heap: &mut std::collections::BinaryHeap>) -> bool { + heap.pop().is_some() +} + +/// The places `iter_mut` lends, which the runtime would compare as the objects they are. +pub fn places_sorted(v: &mut [u8]) -> bool { + v.iter_mut().is_sorted() +} diff --git a/ts/fixtures/reject/ts.toml b/ts/fixtures/reject/ts.toml index c53534ecf..347199fa6 100644 --- a/ts/fixtures/reject/ts.toml +++ b/ts/fixtures/reject/ts.toml @@ -76,5 +76,7 @@ roots = [ "element_pointer", "units_compared", "dangling_enum", + "heap_optional", + "places_sorted", ] printable = ["Meters"] diff --git a/ts/fixtures/task/expected/drops.ts b/ts/fixtures/task/expected/drops.ts index bfd232f25..4f7884423 100644 --- a/ts/fixtures/task/expected/drops.ts +++ b/ts/fixtures/task/expected/drops.ts @@ -944,7 +944,7 @@ export function edited(log: Log.Log, id: rt.u32, arg: rt.u8): void { v11.push(_315); const part6 = mapOr$Option_Key_u32_drained_closure4(rt.iterMaxByKey(rt.iterSlice(v11, rt.drop), (k2: Key.Key): rt.u32 => { return rt.u32Rem(rt.u32Sub(k2.id, id), 3); - }, rt.cmpLt(rt.cmp), true, rt.drop), 0, (k3: Key.Key): rt.u32 => { + }, rt.lt, true, rt.drop), 0, (k3: Key.Key): rt.u32 => { const _05: rt.u32 = rt.u32Sub(k3.id, id); k3[Symbol.dispose](); return _05; diff --git a/ts/fixtures/task/expected/drops/closures.ts b/ts/fixtures/task/expected/drops/closures.ts index 647220553..b73494425 100644 --- a/ts/fixtures/task/expected/drops/closures.ts +++ b/ts/fixtures/task/expected/drops/closures.ts @@ -653,7 +653,7 @@ export function iterated(log: Log.Log, id: rt.u32): void { }, (): void => { captured21[Symbol.dispose](); }); - const part9 = map$Option_Guard_u32_folded_closure5_closure0(rt.dropAfter(_441, () => rt.iterMaxByKey(_435, _441, rt.cmpLt(rt.cmp), undefined, rt.drop)), (g18: Guard.Guard): rt.u32 => { + const part9 = map$Option_Guard_u32_folded_closure5_closure0(rt.dropAfter(_441, () => rt.iterMaxByKey(_435, _441, rt.lt, undefined, rt.drop)), (g18: Guard.Guard): rt.u32 => { const _028: rt.u32 = Guard.id(g18); g18[Symbol.dispose](); return _028; diff --git a/ts/fixtures/task/expected/drops/folds.ts b/ts/fixtures/task/expected/drops/folds.ts index 0b871c547..411d507ff 100644 --- a/ts/fixtures/task/expected/drops/folds.ts +++ b/ts/fixtures/task/expected/drops/folds.ts @@ -141,10 +141,10 @@ export function folded(log: Log.Log, id: rt.u32, arg: rt.u8): void { captured61[Symbol.dispose](); })), (g73: Guard.Guard): rt.u32 => { return rt.u32Rem(Guard.id(g73), 3); - }, rt.cmpLt(rt.cmp), true, rt.drop); + }, rt.lt, true, rt.drop); const least: Guard.Guard | undefined = rt.iterMinByKey(rt.iterSlice(guards(6, 8), rt.drop), (g74: Guard.Guard): rt.u32 => { return rt.u32Add(Guard.id(g74), Guard.id(d)); - }, rt.cmpLt(rt.cmp), true, rt.drop); + }, rt.lt, true, rt.drop); const _131: rt.u32 | undefined = map$Option_Guard_u32_folded_closure5_closure0(best, (g75: Guard.Guard): rt.u32 => { const _082: rt.u32 = Guard.id(g75); g75[Symbol.dispose](); @@ -167,7 +167,7 @@ export function folded(log: Log.Log, id: rt.u32, arg: rt.u8): void { return _077; }, (): void => { captured58[Symbol.dispose](); - })), rt.cmpLt(rt.cmp), true); + })), rt.lt, true); const _148: rt.Iter = rt.iterSlice(guards(4, 7), rt.drop); _1085 = false; const captured59 = id; diff --git a/ts/fixtures/task/expected/drops/iters.ts b/ts/fixtures/task/expected/drops/iters.ts index 4b8c6fa5c..79b23577d 100644 --- a/ts/fixtures/task/expected/drops/iters.ts +++ b/ts/fixtures/task/expected/drops/iters.ts @@ -157,7 +157,7 @@ export function looped(log: Log.Log, id: rt.u32): void { v9.push(_303); const best: Guard.Guard | undefined = rt.iterMaxByKey(rt.iterSlice(v9, rt.drop), (g9: Guard.Guard): rt.u32 => { return rt.u32Rem(Guard.id(g9), 2); - }, rt.cmpLt(rt.cmp), true, rt.drop); + }, rt.lt, true, rt.drop); const v10: Array = []; const _314: Guard.Guard | undefined = guard(30); v10.push(_314); diff --git a/ts/runtime/rt.ts b/ts/runtime/rt.ts index 6f4c2b9b2..b6574d82d 100644 --- a/ts/runtime/rt.ts +++ b/ts/runtime/rt.ts @@ -390,6 +390,26 @@ export function sliceSort(s: { [i: number]: T; length: number }, less: (a: T, } } +/** + * `is_sorted`, by `le`, the elements' `PartialOrd::le`: as std's, which below 33 elements + * stops at the first pair out of order, and past that tests every pair of each chunk of 33 + * before it stops, the last element of one the first of the next. + */ +export function sliceIsSorted(s: { [i: number]: T; length: number }, le: (a: T, b: T) => boolean): boolean { + const CHUNK = 33; + const n = s.length; + let i = 0; + if (n >= CHUNK) { + for (; i < n - CHUNK; i += CHUNK - 1) { + let sorted = true; + for (let j = i + 1; j < i + CHUNK; j++) sorted = le(s[j - 1], s[j]) && sorted; + if (!sorted) return false; + } + } + for (i++; i < n; i++) if (!le(s[i - 1], s[i])) return false; + return true; +} + /** * Keeps each of the first `n` elements of `a` that `keep` of its index returns true for, * in order, and drops each other one once `keep` rejects it: how many it kept. diff --git a/ts/runtime/rt/collections.ts b/ts/runtime/rt/collections.ts index cc6295370..fe9d55ed3 100644 --- a/ts/runtime/rt/collections.ts +++ b/ts/runtime/rt/collections.ts @@ -2015,3 +2015,243 @@ export function dequeClone(q: VecDeque, item: (value: T) => T = itself): V for (let i = 0; i < q.n; i++) dequePushBack(out, item(q.b[at(q, i)] as T)); return out; } + +// `BinaryHeap`: std's max-heap in an array, sifted as std's sifts it, so equal elements +// leave in std's order and each comparison std makes runs. Every function that sifts is +// given the elements' `PartialOrd` tests std's sifts make: `le` up, and `le`, `ge`, and +// `lt` down. + +/** A `PartialOrd` test. */ +type Test = (a: T, b: T) => boolean; + +/** `BinaryHeap`: its elements in `d`, in std's order. */ +export interface BinaryHeap { + d: T[]; +} + +/** `BinaryHeap::new`, `with_capacity`, and `default`. */ +export function heapNew(): BinaryHeap { + return { d: [] }; +} + +/** `From>`: the vector's elements, rebuilt into a heap in place, as std's are. */ +export function heapFrom(d: T[], le: Test, ge: Test, lt: Test): BinaryHeap { + rebuild(d, le, ge, lt); + return { d }; +} + +/** `push`: `item` sifted up from the end. */ +export function heapPush(h: BinaryHeap, item: T, le: Test): void { + h.d.push(item); + siftUp(h.d, 0, h.d.length - 1, le); +} + +/** `pop`: the greatest element, or `undefined` if empty, the last element sifted into its place to the bottom and back up. */ +export function heapPop(h: BinaryHeap, le: Test): T | undefined { + const d = h.d; + if (d.length === 0) return undefined; + let item = d.pop() as T; + if (d.length > 0) { + [item, d[0]] = [d[0], item]; + siftDownToBottom(d, 0, le); + } + return item; +} + +/** `into_sorted_vec`: the elements in ascending order, each greatest swapped to the end and the rest sifted. */ +export function heapIntoSortedVec(h: BinaryHeap, le: Test, ge: Test, lt: Test): T[] { + const d = h.d; + for (let end = d.length - 1; end > 0; end--) { + [d[0], d[end]] = [d[end], d[0]]; + siftDownRange(d, 0, end, le, ge, lt); + } + return d; +} + +/** `append`: `other`'s elements added to the larger of the two, which `h` then holds, and `other` left empty. */ +export function heapAppend(h: BinaryHeap, other: BinaryHeap, le: Test, ge: Test, lt: Test): void { + if (h.d.length < other.d.length) [h.d, other.d] = [other.d, h.d]; + const start = h.d.length; + for (const item of other.d) h.d.push(item); + other.d = []; + rebuildTail(h.d, start, le, ge, lt); +} + +/** `extend`, taking `items` as `take` says, and then the heap rebuilt past what it held. */ +export function heapExtend(h: BinaryHeap, items: Iter, le: Test, ge: Test, lt: Test, take?: Take): BinaryHeap { + const start = h.d.length; + extending(h, items, (item) => h.d.push(item), take); + rebuildTail(h.d, start, le, ge, lt); + return h; +} + +/** + * `retain`: the elements `f` keeps, in order, each other dropped by `drop` as `f` rejects + * it, and then the heap rebuilt from the first it rejected. + */ +export function heapRetain(h: BinaryHeap, f: (item: T) => boolean, le: Test, ge: Test, lt: Test, drop?: (item: T) => void): void { + const d = h.d; + const n = d.length; + let from = n; + let kept = 0; + for (let i = 0; i < n; i++) { + const item = d[i]; + if (f(item)) d[kept++] = item; + else { + if (from === n) from = i; + drop?.(item); + } + } + d.length = kept; + rebuildTail(d, from, le, ge, lt); +} + +/** `clear`, which leaves the heap empty and then drops each element in order. */ +export function heapClear(h: BinaryHeap, drop?: (item: T) => void): void { + const d = h.d; + h.d = []; + dropAll(d, drop); +} + +/** `drain`: an iterator of the elements in order, which leaves the heap empty and drops those it still holds by `drop`. */ +export function heapDrain(h: BinaryHeap, drop?: (item: T) => void): Iter { + const d = h.d; + h.d = []; + return iterSlice(d, drop); +} + +/** `iter` and `into_iter`: the elements in order, any not read dropped by `drop` as the iterator drops. */ +export function heapIter(h: BinaryHeap, drop?: (item: T) => void): Iter { + return iterSlice(h.d, drop); +} + +/** The heap's drop: each element in order. */ +export function heapDrop(h: BinaryHeap, drop: (item: T) => void): void { + dropAll(h.d, drop); +} + +/** `clone`, with each element through its `Clone`, or as itself without one. */ +export function heapClone(h: BinaryHeap, item: (value: T) => T = itself): BinaryHeap { + return { d: h.d.map((value) => item(value)) }; +} + +/** + * `PeekMut`, the greatest element lent: a place, which writes it, of a heap with at least + * one. Once `DerefMut` lends it of a heap of more, its drop sifts it down. + */ +export class PeekMut implements Place { + readonly h: BinaryHeap; + readonly le: Test; + readonly ge: Test; + readonly lt: Test; + /** Whether its drop sifts, as std's sets `original_len`. */ + s = false; + + constructor(h: BinaryHeap, le: Test, ge: Test, lt: Test) { + this.h = h; + this.le = le; + this.ge = ge; + this.lt = lt; + } + + get v(): T { + return this.h.d[0]; + } + + set v(value: T) { + this.h.d[0] = value; + } +} + +/** `peek_mut`: the greatest element lent, or `undefined` if empty. */ +export function heapPeekMut(h: BinaryHeap, le: Test, ge: Test, lt: Test): PeekMut | undefined { + return h.d.length === 0 ? undefined : new PeekMut(h, le, ge, lt); +} + +/** `PeekMut`'s `DerefMut`, which marks it to sift as it drops if the heap holds more than the one element. */ +export function peekMutDerefMut(p: PeekMut): void { + if (p.h.d.length > 1) p.s = true; +} + +/** `PeekMut`'s drop: the element sifted down from the top, if `DerefMut` lent it. */ +export function peekMutDrop(p: PeekMut): void { + if (p.s) siftDownRange(p.h.d, 0, p.h.d.length, p.le, p.ge, p.lt); +} + +/** `PeekMut::pop`: the heap's `pop`, which leaves the drop nothing to sift. */ +export function peekMutPop(p: PeekMut): T { + p.s = false; + return heapPop(p.h, p.le) as T; +} + +/** std's `sift_up`: the element at `pos` moved up past each parent it is not `le`, but not above `start`. */ +function siftUp(d: T[], start: number, pos: number, le: Test): number { + const item = d[pos]; + while (pos > start) { + const parent = (pos - 1) >>> 1; + if (le(item, d[parent])) break; + d[pos] = d[parent]; + pos = parent; + } + d[pos] = item; + return pos; +} + +/** std's `sift_down_range`: the element at `pos` moved down past each greater child, within the first `end`. */ +function siftDownRange(d: T[], pos: number, end: number, le: Test, ge: Test, lt: Test): number { + const item = d[pos]; + let child = 2 * pos + 1; + while (child <= Math.max(end - 2, 0)) { + if (le(d[child], d[child + 1])) child++; + if (ge(item, d[child])) { + d[pos] = item; + return pos; + } + d[pos] = d[child]; + pos = child; + child = 2 * pos + 1; + } + if (child === end - 1 && lt(item, d[child])) { + d[pos] = d[child]; + pos = child; + } + d[pos] = item; + return pos; +} + +/** std's `sift_down_to_bottom`: the element at `pos` moved down to a leaf past the greater child at each level, then sifted up. */ +function siftDownToBottom(d: T[], pos: number, le: Test): void { + const end = d.length; + const start = pos; + const item = d[pos]; + let child = 2 * pos + 1; + while (child <= Math.max(end - 2, 0)) { + if (le(d[child], d[child + 1])) child++; + d[pos] = d[child]; + pos = child; + child = 2 * pos + 1; + } + if (child === end - 1) { + d[pos] = d[child]; + pos = child; + } + d[pos] = item; + siftUp(d, start, pos, le); +} + +/** std's `rebuild_tail`: the elements from `start` on added to the heap before them, by a rebuild or by sifting each up, as std's estimate picks. */ +function rebuildTail(d: T[], start: number, le: Test, ge: Test, lt: Test): void { + if (start === d.length) return; + const tail = d.length - start; + const better = start < tail || (d.length <= 2048 ? 2 * d.length < tail * (31 - Math.clz32(start)) : 2 * d.length < tail * 11); + if (better) rebuild(d, le, ge, lt); + else for (let i = start; i < d.length; i++) siftUp(d, 0, i, le); +} + +/** std's `rebuild`: each parent sifted down, the last first. */ +function rebuild(d: T[], le: Test, ge: Test, lt: Test): void { + for (let n = d.length >>> 1; n > 0; ) { + n--; + siftDownRange(d, n, d.length, le, ge, lt); + } +} diff --git a/ts/runtime/rt/int.ts b/ts/runtime/rt/int.ts index 825b17d2f..78df491fa 100644 --- a/ts/runtime/rt/int.ts +++ b/ts/runtime/rt/int.ts @@ -103,6 +103,21 @@ export function lt(a: number | Limbs, b: number | Limbs): boolean { return typeof b === "number" ? a.hi < 0 : compare(a, b) < 0; } +/** `PartialOrd::le` of integers, by `lt`. */ +export function le(a: number | Limbs, b: number | Limbs): boolean { + return !lt(b, a); +} + +/** `PartialOrd::gt` of integers, by `lt`. */ +export function gt(a: number | Limbs, b: number | Limbs): boolean { + return lt(b, a); +} + +/** `PartialOrd::ge` of integers, by `lt`. */ +export function ge(a: number | Limbs, b: number | Limbs): boolean { + return !lt(a, b); +} + /** The larger of two integers. */ export function max(a: number, b: number): number { return a >= b ? a : b; diff --git a/ts/runtime/rt/iter.ts b/ts/runtime/rt/iter.ts index 097adba58..8deccff9d 100644 --- a/ts/runtime/rt/iter.ts +++ b/ts/runtime/rt/iter.ts @@ -606,6 +606,31 @@ function best(it: Iter, f: (item: T) => K, lt: Lt, least: boolean, d return item; } +/** + * `is_sorted_by`: whether `compare` holds of each item and the next, read by `next` and + * then as `all` folds, stopping at the first it fails. Each item drops by `drop` as the + * next replaces it, or after the one it failed against; then `it`, if `drops`. + */ +export function iterIsSortedBy(it: Iter, compare: (a: T, b: T) => boolean, drops = false, drop?: Drop): boolean { + let last = it.next(); + let sorted = true; + if (last !== DONE) { + const folded = it.folded(); + for (let item = folded.next(); item !== DONE; item = folded.next()) { + if (!compare(last, item)) { + drop?.(item); + sorted = false; + break; + } + drop?.(last); + last = item; + } + drop?.(last); + } + if (drops) dispose(it); + return sorted; +} + /** `collect` into a `Vec`, taking `it` as `take` says. */ export function iterToArray(it: Iter, take?: Take): T[] { const out: T[] = []; diff --git a/ts/src/lower/collections.rs b/ts/src/lower/collections.rs index d1ac6f868..96d9aec1c 100644 --- a/ts/src/lower/collections.rs +++ b/ts/src/lower/collections.rs @@ -167,6 +167,8 @@ pub fn call<'tcx>( Std::HashMap | Std::HashSet => hash(b, coll, call, owner, name, args, out), Std::BTreeMap | Std::BTreeSet => btree(b, coll, call, owner, name, args), Std::VecDeque => deque(b, coll, call, owner, name, args, out), + Std::BinaryHeap => heap(b, coll, call, owner, name, args, out), + Std::PeekMut if name == "pop" => Some(Expr::rt("peekMutPop", args)), Std::Entry => entry(b, coll, name, args, span), Std::Ordering => ordering(name, args), Std::Hasher if matches!(name, "new" | "default") => Some(Expr::rt("hasher", vec![])), @@ -216,6 +218,7 @@ fn discarded(std: Std, owner: &str, name: &str) -> Option<&'static [usize]> { (_, "clear" | "retain" | "retain_mut") if map => &[0, 1], (_, "clear" | "retain" | "retain_mut") if set => &[0], (_, "clear" | "retain" | "retain_mut" | "truncate") if std == Std::VecDeque => &[0], + (_, "clear" | "retain") if std == Std::BinaryHeap => &[0], _ => return None, }) } @@ -276,7 +279,7 @@ fn same_key<'tcx>(b: &Body<'_, '_, 'tcx>, key: Ty<'tcx>, by: Ty<'tcx>) -> bool { fn undefined_arg<'tcx>(b: &Body<'_, '_, 'tcx>, std: Std, coll: Ty<'tcx>) -> Option> { if !matches!( std, - Std::HashMap | Std::BTreeMap | Std::Entry | Std::HashSet | Std::BTreeSet | Std::VecDeque + Std::HashMap | Std::BTreeMap | Std::Entry | Std::HashSet | Std::BTreeSet | Std::VecDeque | Std::BinaryHeap ) { return None; } @@ -560,6 +563,10 @@ fn own_test<'tcx>(b: &Body<'_, '_, 'tcx>, ty: Ty<'tcx>, test: &str) -> bool { pub fn test_fn<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, test: &str, span: Span) -> Expr { let tcx = b.t.tcx; let ty = peel(b, ty); + // An integer's, which the runtime tests directly rather than through `partial_cmp`. + if matches!(ty.kind(), ty::Char | ty::Int(_) | ty::Uint(_)) { + return Expr::Rt(test.into()); + } if !own_test(b, ty, test) { let by = match test { "lt" => "cmpLt", @@ -868,6 +875,119 @@ fn deque<'tcx>( Some(e) } +/// `PartialOrd`'s tests of `item` that std's heap sifts make: `le` up, and `le`, `ge`, and +/// `lt` down. +fn heap_tests<'tcx>(b: &mut Body<'_, '_, 'tcx>, item: Ty<'tcx>, down: bool, span: Span) -> Vec { + let tests: &[&str] = if down { &["le", "ge", "lt"] } else { &["le"] }; + tests.iter().map(|test| test_fn(b, item, test, span)).collect() +} + +/// A method of `BinaryHeap`, or of a trait it implements, each that sifts given the tests +/// its sifts make. +fn heap<'tcx>( + b: &mut Body<'_, '_, 'tcx>, + coll: Ty<'tcx>, + call: &Call<'tcx>, + owner: &str, + name: &str, + args: Vec, + out: &mut Vec, +) -> Option { + let span = call.span; + let item = arg(coll, 0); + let bytes = matches!(item.kind(), ty::Uint(ty::UintTy::U8)); + let mut values = args; + let e = match (owner, name) { + (_, "new" | "with_capacity") | ("Default", "default") => { + discard(values, out); + empty(b, Expr::rt("heapNew", vec![]), coll, span) + } + (_, "len") => values.pop()?.member("d").member("length"), + (_, "is_empty") => values.pop()?.member("d").member("length").binary("===", Expr::lit("0")), + (_, "peek") => values.pop()?.member("d").index(Expr::lit("0")), + (_, "push" | "pop" | "peek_mut" | "append" | "retain" | "into_sorted_vec") => { + values.extend(heap_tests(b, item, !matches!(name, "push" | "pop"), span)); + let f = match name { + "push" => "heapPush", + "pop" => "heapPop", + "peek_mut" => "heapPeekMut", + "append" => "heapAppend", + "retain" => "heapRetain", + _ => return Some(heap_vec(Expr::rt("heapIntoSortedVec", values), bytes)), + }; + Expr::rt(f, values) + } + (_, "into_vec") => heap_vec(values.pop()?.member("d"), bytes), + (_, "as_slice") if bytes => Expr::rt( + "byteSliceFrom", + vec![ + Expr::ident("Uint8Array") + .member("from") + .call(vec![values.pop()?.member("d")]), + ], + ), + (_, "as_slice") => values.pop()?.member("d"), + (_, "clear") => Expr::rt("heapClear", values), + (_, "drain") => { + let droppers = entry_droppers(b, coll, span); + Expr::rt("heapDrain", super::iter::tail(values, droppers)) + } + (_, "iter") => { + let by_ref = Ty::new_imm_ref(b.t.tcx, b.t.tcx.lifetimes.re_erased, coll); + into_iter(b, by_ref, values.pop()?, span)? + } + (_, "reserve" | "reserve_exact" | "shrink_to_fit" | "shrink_to") => { + discard(values, out); + Expr::undefined() + } + ("Clone", "clone") => { + let clone = clone_fn(b, item, span); + Expr::rt("heapClone", super::iter::tail(values, vec![clone])) + } + // `From>` rebuilds the vector in place, and `From<[T; N]>` collects the array + // into one first. + ("From", "from") if b.t.std_of(library::targ(call.args, 1)) == Some(Std::Vec) => { + let v = values.pop()?; + let d = match bytes { + true => Expr::ident("Array") + .member("from") + .call(vec![Expr::rt("byteVecView", vec![v])]), + false => v, + }; + Expr::rt("heapFrom", [vec![d], heap_tests(b, item, true, span)].concat()) + } + ("Extend", "extend") | ("From", "from") => { + let (source, iter) = extend_source(b, coll, call, owner, values.pop()?)?; + if owner == "From" { + let d = super::iter::to_array(b, source, iter, item, span); + return Some(Expr::rt( + "heapFrom", + [vec![d], heap_tests(b, item, true, span)].concat(), + )); + } + // Into its vector, of a `TrustedLen` iterator by `for_each`. + let folds = super::iter::implements(b.t, iter, "TrustedLen"); + let take = super::iter::iter_take(b, iter, folds, span); + let mut args = vec![values.pop()?, source]; + args.extend(heap_tests(b, item, true, span)); + Expr::rt("heapExtend", super::iter::tail(args, vec![take])) + } + _ => return None, + }; + Some(e) +} + +/// A `Vec` of a heap's elements `d`, an `rt.ByteVec` of bytes. +pub fn heap_vec(d: Expr, bytes: bool) -> Expr { + match bytes { + true => Expr::rt( + "byteVecFrom", + vec![Expr::ident("Uint8Array").member("from").call(vec![d])], + ), + false => d, + } +} + /// What `extend` or `from` adds to a collection: an `rt.Iter` of what `source` holds, and /// the type of the iterator std's makes of it. fn extend_source<'tcx>( @@ -1264,6 +1384,11 @@ pub fn into_iter<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, value: Expr, sp Std::HashMap if places(1) => "hashIterMut", Std::BTreeMap if places(1) => "btreeIterMut", Std::VecDeque if places(0) => "dequeIterMut", + // A heap's elements in order, as its `Vec`'s, which by reference are borrowed. + Std::BinaryHeap if ty.is_ref() => { + return Some(super::iter::by_ref(Expr::rt("heapIter", vec![value]), arg(coll, 0))); + } + Std::BinaryHeap => "heapIter", Std::HashMap => "hashIter", Std::HashSet => "hashKeys", Std::BTreeMap => "btreeIter", @@ -1293,6 +1418,12 @@ pub fn collect<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, iter: Expr, from: hash_extend(b, ty, target, iter, from, span) } Std::BTreeMap | Std::BTreeSet => btree_collect(b, ty, iter, from, span), + // std's collects into a `Vec`, and rebuilds it in place. + Std::BinaryHeap => { + let item = arg(ty, 0); + let d = super::iter::to_array(b, iter, from, item, span); + Expr::rt("heapFrom", [vec![d], heap_tests(b, item, true, span)].concat()) + } // std's collects into a `Vec` first. Std::VecDeque => { let target = deque_new(b, ty, span); diff --git a/ts/src/lower/drop.rs b/ts/src/lower/drop.rs index a97b10ae1..511e77889 100644 --- a/ts/src/lower/drop.rs +++ b/ts/src/lower/drop.rs @@ -31,8 +31,11 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { let drops = match ty.kind() { ty::Adt(adt, _) if let Some(drops) = self.bound_drops(adt.did()) => drops, ty::Adt(adt, args) => match Std::of(tcx, adt.did()) { - // A guard unlocks its mutex or ends its borrow, and a `Weak` uncounts itself. - Some(Std::Arc | Std::Weak | Std::Waker | Std::MutexGuard | Std::Ref | Std::RefMut) => true, + // A guard unlocks its mutex or ends its borrow, a `Weak` uncounts itself, and a + // `PeekMut` sifts what it lent. + Some(Std::Arc | Std::Weak | Std::Waker | Std::MutexGuard | Std::Ref | Std::RefMut | Std::PeekMut) => { + true + } Some( Std::Option | Std::Result @@ -53,6 +56,7 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { | Std::BTreeMap | Std::BTreeSet | Std::VecDeque + | Std::BinaryHeap | Std::Bound | Std::TryLockError | Std::Reverse, @@ -298,6 +302,8 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { Std::HashSet => ("hashDrop", vec![value, each(self, 0)?]), Std::BTreeSet => ("btreeDrop", vec![value, each(self, 0)?]), Std::VecDeque => ("dequeDrop", vec![value, each(self, 0)?]), + Std::BinaryHeap => ("heapDrop", vec![value, each(self, 0)?]), + Std::PeekMut => ("peekMutDrop", vec![value]), // A vacant entry holds the key it was given; an occupied one dropped it as // `entry` found the map's. Std::Entry => match self.tcx.item_name(adt.did()).as_str() { diff --git a/ts/src/lower/fmt.rs b/ts/src/lower/fmt.rs index fe50951fa..148669ed4 100644 --- a/ts/src/lower/fmt.rs +++ b/ts/src/lower/fmt.rs @@ -274,6 +274,10 @@ pub fn std_impl<'tcx>( let item = b.fmt_fn(tr, args.type_at(0), span); rt("list", vec![library::elements(value, ty), f, item]) } + (Std::BinaryHeap, Trait::Debug) => { + let item = b.fmt_fn(tr, args.type_at(0), span); + rt("list", vec![value.member("d"), f, item]) + } (Std::Option, Trait::Debug) => { let some = b.fmt_fn(tr, args.type_at(0), span); let none = b.t.none_arg(args.type_at(0)); diff --git a/ts/src/lower/iter.rs b/ts/src/lower/iter.rs index fb266568c..0eda48198 100644 --- a/ts/src/lower/iter.rs +++ b/ts/src/lower/iter.rs @@ -196,7 +196,7 @@ pub fn into_iter<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, value: Expr, sp /// `iter`, over the elements of a slice or `Vec` of `elem`s, yielding references to them: /// a slice of each array of bytes. -fn by_ref(iter: Expr, elem: Ty<'_>) -> Expr { +pub(super) fn by_ref(iter: Expr, elem: Ty<'_>) -> Expr { match super::byte_array(elem) { true => Expr::rt("iterMap", vec![iter, Expr::Rt("byteSliceFrom".into())]), false => iter, @@ -482,6 +482,27 @@ pub fn call<'tcx>( let droppers = vec![iter_drops(b, self_ty, span), b.t.dropper(item, span)]; method(&camel, tail([rest, vec![lt]].concat(), droppers)) } + // Whether each item and the next are in order: by `PartialOrd::le`, by a comparator, + // or by `le` of the keys a function makes, as std's maps to them. Not of places, as + // `max` is not. + "is_sorted" | "is_sorted_by" | "is_sorted_by_key" => { + let (items, compare, each) = match name { + "is_sorted_by" => (recv.clone(), rest.into_iter().next()?, item), + _ if b.t.has_place(item) => return None, + "is_sorted" => (recv.clone(), super::collections::test_fn(b, item, "le", span), item), + _ => { + let key = targ(2)?; + let le = super::collections::test_fn(b, key, "le", span); + ( + Expr::rt("iterMap", vec![recv.clone(), rest.into_iter().next()?]), + le, + key, + ) + } + }; + let droppers = vec![iter_drops(b, self_ty, span), b.t.dropper(each, span)]; + Some(Expr::rt("iterIsSortedBy", tail(vec![items, compare], droppers))) + } "rposition" => method(&camel, rest), "rfind" => { let dropper = b.t.dropper(item, span); diff --git a/ts/src/lower/library.rs b/ts/src/lower/library.rs index 9fd70328c..074f1ff0b 100644 --- a/ts/src/lower/library.rs +++ b/ts/src/lower/library.rs @@ -124,6 +124,9 @@ pub enum Std { BTreeSet, /// `rt.VecDeque`, a ring buffer. VecDeque, + /// `rt.BinaryHeap`, std's max-heap in an array, and `rt.PeekMut`, its greatest element lent. + BinaryHeap, + PeekMut, /// `rt.Entry`: a map's `Entry`, `OccupiedEntry`, or `VacantEntry`. Entry, /// `rt.Ordering`: its discriminant, -1, 0, or 1. @@ -187,6 +190,7 @@ impl Std { (rustc_span::Symbol::intern("BTreeMap"), Self::BTreeMap), (rustc_span::Symbol::intern("BTreeSet"), Self::BTreeSet), (rustc_span::Symbol::intern("VecDeque"), Self::VecDeque), + (rustc_span::Symbol::intern("BinaryHeap"), Self::BinaryHeap), (rustc_span::Symbol::intern("Duration"), Self::Duration), ] { if tcx.is_diagnostic_item(item, did) { @@ -246,6 +250,7 @@ impl Std { ["cell", "once", "OnceCell"] => Some(Self::OnceCell), ["sync", "once_lock", "OnceLock"] => Some(Self::OnceLock), ["cmp", "Reverse"] => Some(Self::Reverse), + ["collections", "binary_heap", "PeekMut"] => Some(Self::PeekMut), ["ops", "range", "Bound"] => Some(Self::Bound), ["num", "nonzero", "NonZero"] => Some(Self::NonZero), ["hash", "random", "DefaultHasher"] => Some(Self::Hasher), @@ -504,7 +509,7 @@ pub fn inherent_std(tcx: TyCtxt<'_>, def_id: DefId) -> Option { /// Whether std's `instance` lowers from its own MIR, as the crate's functions do, rather /// than mapping here: a method of `bool`, `Option`, `Result`, `Poll`, or `LocalKey`, or -/// their `Clone`. +/// their `Clone`, or `cmp`'s `max` and `min` and their `_by` and `_by_key` forms. /// Not one taking a `&mut` or a raw pointer, whose writes the crate's analysis of what it /// writes into would miss, nor an `unsafe` one, nor one mapped below as one expression or /// as the runtime's panic. @@ -513,7 +518,17 @@ pub fn from_mir<'tcx>(t: &Translator<'_, 'tcx>, instance: Instance<'tcx>) -> boo let ty::InstanceKind::Item(did) = instance.def else { return false; }; - if !is_std(tcx, did) || tcx.def_kind(did) != DefKind::AssocFn { + if !is_std(tcx, did) { + return false; + } + if tcx.def_kind(did) == DefKind::Fn { + let path = std_path(tcx, did); + return matches!( + path.iter().map(String::as_str).collect::>().as_slice(), + ["cmp", "max" | "min" | "max_by" | "min_by" | "max_by_key" | "min_by_key"] + ); + } + if tcx.def_kind(did) != DefKind::AssocFn { return false; } let Some(imp) = tcx.impl_of_assoc(did) else { @@ -1003,6 +1018,12 @@ fn mapping<'tcx>( if to == from || some || (string(tcx, to) && string(tcx, from.peel_refs())) { return Some(value); } + // A heap's `Vec` is its elements. + if let ty::Adt(adt, args) = from.kind() + && Std::of(tcx, adt.did()) == Some(Std::BinaryHeap) + { + return Some(super::collections::heap_vec(value.member("d"), is_u8(args.type_at(0)))); + } // Each `From` between integers, `char`, and `f64`, or from `bool`, keeps the // value, as `as` then does. let scalar = |t: Ty<'tcx>| Int::of(t).is_some() || t.is_char() || super::float::is(t); @@ -1328,6 +1349,35 @@ fn mapping<'tcx>( } } } + // Whether each element and the next are in order, as `sort` would leave them: by + // `PartialOrd::le` in std's chunks, or as the slice's iterator's `is_sorted_by` and + // `is_sorted_by_key` test them, which std's are. + ("slice", "is_sorted") => { + let elem = targ(gargs, 0); + let mut values = args; + values[0] = elements(values[0].clone(), Ty::new_slice(tcx, elem)); + values.push(super::collections::test_fn(b, elem, "le", span)); + Expr::rt("sliceIsSorted", values) + } + ("slice", "is_sorted_by" | "is_sorted_by_key") => { + let elem = targ(gargs, 0); + let mut values = args.into_iter(); + let slice = elements(values.next()?, Ty::new_slice(tcx, elem)); + let items = super::iter::by_ref(Expr::rt("iterSlice", vec![slice]), elem); + let f = values.next()?; + let (items, compare, drop) = match name.as_str() { + "is_sorted_by" => (items, f, Expr::undefined()), + _ => { + let key = targ(gargs, 2); + let le = super::collections::test_fn(b, key, "le", span); + (Expr::rt("iterMap", vec![items, f]), le, b.t.dropper(key, span)) + } + }; + Expr::rt( + "iterIsSortedBy", + super::iter::tail(vec![items, compare], vec![Expr::undefined(), drop]), + ) + } // A boxed slice's elements, and `vec![a, b]`'s array once `mir::vec` took the box // away. A `Copy` aggregate's operand may be shared with where it came from, which the // vector must not change. @@ -1466,6 +1516,17 @@ fn mapping<'tcx>( Some(Std::Vec) => vec_view(v, vargs.type_at(0)), Some(Std::String | Std::Box) => v, Some(Std::Cow) if string(tcx, ty) => v, + // The greatest element of a heap, which `DerefMut` marks to sift once lent. + Some(Std::PeekMut) => { + let p = match v.is_path() { + true => v, + false => b.spill(v, "peek", out), + }; + if name == "deref_mut" { + out.push(StmtKind::Expr(Expr::rt("peekMutDerefMut", vec![p.clone()])).at(None)); + } + p.member("v") + } _ => return None, }, _ => return None, diff --git a/ts/src/lower/mir/mod.rs b/ts/src/lower/mir/mod.rs index 6db80de7a..f0e751e00 100644 --- a/ts/src/lower/mir/mod.rs +++ b/ts/src/lower/mir/mod.rs @@ -111,9 +111,9 @@ fn instance_mir<'tcx>(t: &mut Translator<'_, 'tcx>, instance: Instance<'tcx>, sp /// Why a body of std's can't lower as the crate's do, if it can't. The crate's analysis of /// what it writes into reads only its own bodies, so one must change nothing in place: no -/// assignment through a projection or a reference, and no mutable borrow or pointer. And -/// core's plain integer arithmetic wraps, as `wrapping_add` lowers, where the crate's is -/// checked. +/// assignment through a projection or a reference, and no mutable borrow or pointer but of +/// a function to call it, as `FnMut` is, whose writes are its own body's. And core's plain +/// integer arithmetic wraps, as `wrapping_add` lowers, where the crate's is checked. fn std_refused<'tcx>(tcx: TyCtxt<'tcx>, body: &mir::Body<'tcx>) -> Option<&'static str> { const WRITE: &str = "a change in place, which a body of std's is not supported to make"; const WRAP: &str = "arithmetic that wraps, which a body of std's is not supported to do"; @@ -122,6 +122,12 @@ fn std_refused<'tcx>(tcx: TyCtxt<'tcx>, body: &mir::Body<'tcx>) -> Option<&'stat match &stmt.kind { StatementKind::Assign(assign) if !assign.0.projection.is_empty() => return Some(WRITE), StatementKind::Assign(assign) => match &assign.1 { + Rvalue::Ref(_, mir::BorrowKind::Mut { .. }, place) + if place.projection.is_empty() + && matches!( + body.local_decls[place.local].ty.kind(), + ty::Closure(..) | ty::FnDef(..) | ty::FnPtr(..) + ) => {} Rvalue::Ref(_, mir::BorrowKind::Mut { .. }, _) | Rvalue::RawPtr(mir::RawPtrKind::Mut, _) => { return Some(WRITE); } diff --git a/ts/src/lower/mod.rs b/ts/src/lower/mod.rs index 8c1227cf0..c1264264a 100644 --- a/ts/src/lower/mod.rs +++ b/ts/src/lower/mod.rs @@ -1148,6 +1148,8 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { | library::Std::BTreeMap | library::Std::BTreeSet | library::Std::VecDeque + | library::Std::BinaryHeap + | library::Std::PeekMut | library::Std::Entry | library::Std::Hasher | library::Std::BytesMut @@ -1485,13 +1487,17 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { | library::Std::Entry | library::Std::HashSet | library::Std::BTreeSet - | library::Std::VecDeque => { + | library::Std::VecDeque + | library::Std::BinaryHeap + | library::Std::PeekMut => { let (name, n) = match std { library::Std::HashMap => ("HashMap", 2), library::Std::BTreeMap => ("BTreeMap", 2), library::Std::Entry => ("Entry", 2), library::Std::HashSet => ("HashSet", 1), library::Std::BTreeSet => ("BTreeSet", 1), + library::Std::BinaryHeap => ("BinaryHeap", 1), + library::Std::PeekMut => ("PeekMut", 1), _ => ("VecDeque", 1), }; let tys: Vec> = args.types().take(n).collect(); From efd5461b71d7d821ed3acb4f2685e9502a7aba14 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Thu, 29 Oct 2026 22:30:27 -0700 Subject: [PATCH 3/6] fix(ts): leak a forgotten PeekMut's tail, and drop is_sorted_by's comparator first `PeekMut`'s `DerefMut` hides the heap's tail behind a heap of the top alone, as std's `set_len(1)` does, so a forgotten guard leaves the heap as std's. `is_sorted_by` drops its comparator before the last item and the iterator, as std's `all` does, which also lets an iterator that drops take one. Co-Authored-By: Claude Opus 5.5 --- ts/fixtures/lang/expected/orders.ts | 107 +++++++++++++++++++--------- ts/fixtures/lang/src/orders.rs | 15 +++- ts/runtime/rt/collections.ts | 26 +++++-- ts/runtime/rt/iter.ts | 14 +++- ts/src/lower/iter.rs | 5 +- ts/src/lower/library.rs | 15 ++-- 6 files changed, 133 insertions(+), 49 deletions(-) diff --git a/ts/fixtures/lang/expected/orders.ts b/ts/fixtures/lang/expected/orders.ts index a19908945..3f671034c 100644 --- a/ts/fixtures/lang/expected/orders.ts +++ b/ts/fixtures/lang/expected/orders.ts @@ -117,6 +117,9 @@ export function cmps(data: rt.ByteSlice, out: rt.ByteVec): void { * and sorted: each test std makes, and each item or key it drops. */ export function sorted(data: rt.ByteSlice, out: rt.ByteVec): void { + const _77: () => Item.Item = (): Item.Item => { + return item(200, 0); + }; const ascending: rt.ByteVec = rt.byteSliceToByteVec(data); rt.sliceSortByKey(rt.byteSliceView(rt.byteVecSlice(ascending)), (b: rt.u8): rt.u8 => { return rt.u8Rem(b, 8); @@ -154,18 +157,50 @@ export function sorted(data: rt.ByteSlice, out: rt.ByteVec): void { return true; }, undefined, rt.drop) ? 1 : 0); rt.byteVecPush(out, rt.iterIsSortedBy(rt.iterMap(items(), (x3: Item.Item): rt.u8 => { - const _08: rt.u8 = x3.key; + const _09: rt.u8 = x3.key; x3[Symbol.dispose](); - return _08; + return _09; }), rt.cmpLe(rt.cmpReverse(rt.cmp))) ? 1 : 0); rt.byteVecPush(out, rt.iterIsSortedBy(rt.iterSlice(rt.iterToArray(items()), rt.drop), Item.le$PartialOrd, true, rt.drop) ? 1 : 0); flush(out); - const strings: string[] = rt.iterToArray(rt.iterMap(rt.iterSlice(rt.byteSliceView(_12)), (b5: rt.u8): string => { - const _411: rt.u8 = rt.u8Rem(b5, 16); - return rt.fmtToString((f: rt.Formatter) => rt.u8Display(_411, f)); + const _81: [Item.Item, Item.Item, Item.Item] = [_77(), _77(), _77()]; + const g: Item.Item = _81[0]; + const h: Item.Item = _81[1]; + const k: Item.Item = _81[2]; + const _91: rt.Iter = items(); + const captured = g; + rt.byteVecPush(out, rt.iterIsSortedBy(_91, rt.closure((a3: Item.Item, b5: Item.Item): boolean => { + if (!(captured.key <= a3.key)) { + return false; + } + return a3.key <= b5.key; + }, (): void => { + captured[Symbol.dispose](); + }), undefined, rt.drop, rt.drop) ? 1 : 0); + const _100: rt.Iter = rt.iterSlice(rt.iterToArray(items()), rt.drop); + const captured2 = h; + rt.byteVecPush(out, rt.iterIsSortedBy(_100, rt.closure((a4: Item.Item, b6: Item.Item): boolean => { + return a4.key <= rt.u8Add(b6.key, captured2.key); + }, (): void => { + captured2[Symbol.dispose](); + }), true, rt.drop, rt.drop) ? 1 : 0); + const _105: rt.Iter = items(); + const captured3 = k; + const _107: (a0: Item.Item) => rt.u8 = rt.closure((x4: Item.Item): rt.u8 => { + const _012: rt.u8 = rt.u8Add(x4.key, captured3.key); + x4[Symbol.dispose](); + return _012; + }, (): void => { + captured3[Symbol.dispose](); + }); + rt.byteVecPush(out, rt.dropAfter(_107, () => rt.iterIsSortedBy(rt.iterMap(_105, _107), rt.le)) ? 1 : 0); + flush(out); + const strings: string[] = rt.iterToArray(rt.iterMap(rt.iterSlice(rt.byteSliceView(_12)), (b7: rt.u8): string => { + const _414: rt.u8 = rt.u8Rem(b7, 16); + return rt.fmtToString((f: rt.Formatter) => rt.u8Display(_414, f)); })); - rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(rt.sliceIsSorted(rt.byteSliceView(_12), rt.le) ? 1 : 0, rt.iterIsSortedBy(rt.iterSlice(rt.byteSliceView(_12)), (a3: rt.u8, b6: rt.u8): boolean => { - return rt.u8Div(a3, 4) <= rt.u8Div(b6, 4); + rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(rt.sliceIsSorted(rt.byteSliceView(_12), rt.le) ? 1 : 0, rt.iterIsSortedBy(rt.iterSlice(rt.byteSliceView(_12)), (a5: rt.u8, b8: rt.u8): boolean => { + return rt.u8Div(a5, 4) <= rt.u8Div(b8, 4); }) ? 1 : 0, rt.iterIsSortedBy(rt.iterMap(rt.iterSlice(rt.byteSliceView(_12)), (_25: rt.u8): rt.u8 => { return rt.u8Rem(_25, 8); }), rt.le) ? 1 : 0, rt.iterIsSortedBy(rt.iterSlice(rt.byteSliceView(_12)), rt.le) ? 1 : 0, rt.sliceIsSorted(strings, rt.cmpLe(rt.cmpStr)) ? 1 : 0, rt.iterIsSortedBy(rt.iterMap(rt.iterSlice(strings), (a06: string): rt.usize => { @@ -181,7 +216,8 @@ export function sorted(data: rt.ByteSlice, out: rt.ByteVec): void { * comparison its sifts make, which of equal items it gives back, and when each drops. */ export function heaps(data: rt.ByteSlice, out: rt.ByteVec): void { - let _123: boolean = false; + let _127: boolean = false; + let _128: boolean = false; let heap: rt.BinaryHeap = rt.heapNew() as rt.BinaryHeap; const other: rt.BinaryHeap = rt.heapNew() as rt.BinaryHeap; const ops: rt.Iter<[rt.usize, rt.u8]> = rt.iterEnumerate(rt.iterSlice(rt.byteSliceView(data))); @@ -210,42 +246,49 @@ export function heaps(data: rt.ByteSlice, out: rt.ByteVec): void { } else if (_17 === 4) { const _26: rt.PeekMut | undefined = rt.heapPeekMut(heap, Item.le$PartialOrd, Item.ge$PartialOrd, Item.lt$PartialOrd); if (_26 !== undefined) { + _127 = true; const top: rt.PeekMut = _26; rt.peekMutDerefMut(top); top.v.key = rt.u8Rem(v, 8); - rt.peekMutDrop(top); + if ((v & 16) !== 0) { + _127 = false; + } + if (_127) { + rt.peekMutDrop(top); + } + _127 = false; } } else if (_17 === 5) { - const _33: rt.PeekMut | undefined = rt.heapPeekMut(heap, Item.le$PartialOrd, Item.ge$PartialOrd, Item.lt$PartialOrd); - if (_33 !== undefined) { - _123 = true; - const top2: rt.PeekMut = _33; - const _37: rt.u8 = rt.u8Rem(v, 3); - if (_37 === 0) { - _123 = false; - const _40: Item.Item = rt.peekMutPop(top2); - rt.byteVecPush(out, _40.tag); - _40[Symbol.dispose](); - } else if (_37 === 1) { + const _37: rt.PeekMut | undefined = rt.heapPeekMut(heap, Item.le$PartialOrd, Item.ge$PartialOrd, Item.lt$PartialOrd); + if (_37 !== undefined) { + _128 = true; + const top2: rt.PeekMut = _37; + const _41: rt.u8 = rt.u8Rem(v, 3); + if (_41 === 0) { + _128 = false; + const _44: Item.Item = rt.peekMutPop(top2); + rt.byteVecPush(out, _44.tag); + _44[Symbol.dispose](); + } else if (_41 === 1) { rt.byteVecPush(out, top2.v.tag); } else { - _123 = false; + _128 = false; const top3: rt.PeekMut = top2; rt.peekMutDerefMut(top3); - const _46: Item.Item = top3.v; - _46.tag = _46.tag ^ 64; - const _50: Item.Item = rt.peekMutPop(top3); - rt.byteVecPush(out, _50.tag); - _50[Symbol.dispose](); + const _50: Item.Item = top3.v; + _50.tag = _50.tag ^ 64; + const _54: Item.Item = rt.peekMutPop(top3); + rt.byteVecPush(out, _54.tag); + _54[Symbol.dispose](); } - if (_123) { + if (_128) { rt.peekMutDrop(top2); } - _123 = false; + _128 = false; } } else if (_17 === 6) { - const _53: Item.Item = item(i, v); - rt.heapPush(other, _53, Item.le$PartialOrd); + const _57: Item.Item = item(i, v); + rt.heapPush(other, _57, Item.le$PartialOrd); } else if (_17 === 7) { rt.heapAppend(heap, other, Item.le$PartialOrd, Item.ge$PartialOrd, Item.lt$PartialOrd); } else if (_17 === 8) { @@ -262,9 +305,9 @@ export function heaps(data: rt.ByteSlice, out: rt.ByteVec): void { return x3.tag; })); flush(out); - const _77: rt.BinaryHeap = rt.heapFrom(sorted_, Item.le$PartialOrd, Item.ge$PartialOrd, Item.lt$PartialOrd); + const _81: rt.BinaryHeap = rt.heapFrom(sorted_, Item.le$PartialOrd, Item.ge$PartialOrd, Item.lt$PartialOrd); rt.heapDrop(heap, rt.drop); - heap = _77; + heap = _81; } else { rt.iterExtendBytes(out, rt.iterMap(rt.heapIter(heap), (x: Item.Item): rt.u8 => { return x.tag; diff --git a/ts/fixtures/lang/src/orders.rs b/ts/fixtures/lang/src/orders.rs index 6aab8957a..550816677 100644 --- a/ts/fixtures/lang/src/orders.rs +++ b/ts/fixtures/lang/src/orders.rs @@ -143,6 +143,15 @@ fn sorted(data: &[u8], out: &mut Vec) { out.push(items().is_sorted_by_key(|x| Reverse(x.key)) as u8); out.push(items().collect::>().into_iter().is_sorted() as u8); flush(out); + // A function that owns an item drops as std's `all` drops it: before the last item + // and the iterator. + let owned = || item(200, 0); + let (g, h, k) = (owned(), owned(), owned()); + out.push(items().is_sorted_by(move |a, b| (&g).key <= a.key && a.key <= b.key) as u8); + let v = items().collect::>(); + out.push(v.into_iter().is_sorted_by(move |a, b| a.key <= b.key + (&h).key) as u8); + out.push(items().is_sorted_by_key(move |x| x.key + (&k).key) as u8); + flush(out); let strings: Vec = bytes.iter().map(|b| (b % 16).to_string()).collect(); out.extend([ bytes.is_sorted() as u8, @@ -167,10 +176,14 @@ fn heaps(data: &[u8], out: &mut Vec) { match op % 12 { 0..=2 => heap.push(item(i, v)), 3 => out.push(heap.pop().map_or(0xff, |x| x.tag)), - // `DerefMut` sifts the top once it drops, but of a heap of one. + // `DerefMut` sifts the top once it drops, but of a heap of one, and forgotten, + // leaves the top alone in the heap. 4 => { if let Some(mut top) = heap.peek_mut() { top.key = v % 8; + if v & 0x10 != 0 { + std::mem::forget(top); + } } } // `Deref` alone sifts nothing, and `PeekMut::pop` pops. diff --git a/ts/runtime/rt/collections.ts b/ts/runtime/rt/collections.ts index fe9d55ed3..2ad6fc356 100644 --- a/ts/runtime/rt/collections.ts +++ b/ts/runtime/rt/collections.ts @@ -2144,8 +2144,8 @@ export class PeekMut implements Place { readonly le: Test; readonly ge: Test; readonly lt: Test; - /** Whether its drop sifts, as std's sets `original_len`. */ - s = false; + /** The heap's elements `DerefMut` hid behind a heap of the top alone, as std's `set_len(1)` does, so a forgotten guard leaks them as std's does. */ + t: T[] | undefined; constructor(h: BinaryHeap, le: Test, ge: Test, lt: Test) { this.h = h; @@ -2168,19 +2168,33 @@ export function heapPeekMut(h: BinaryHeap, le: Test, ge: Test, lt: T return h.d.length === 0 ? undefined : new PeekMut(h, le, ge, lt); } -/** `PeekMut`'s `DerefMut`, which marks it to sift as it drops if the heap holds more than the one element. */ +/** `PeekMut`'s `DerefMut`, which hides the rest of a heap of more than the one element until the guard drops. */ export function peekMutDerefMut(p: PeekMut): void { - if (p.h.d.length > 1) p.s = true; + const d = p.h.d; + if (d.length > 1) { + p.t = d; + p.h.d = [d[0]]; + } +} + +/** The elements `DerefMut` hid back in the heap, with the top as written; whether there were any. */ +function peekMutRestore(p: PeekMut): boolean { + const t = p.t; + if (t === undefined) return false; + t[0] = p.h.d[0]; + p.h.d = t; + p.t = undefined; + return true; } /** `PeekMut`'s drop: the element sifted down from the top, if `DerefMut` lent it. */ export function peekMutDrop(p: PeekMut): void { - if (p.s) siftDownRange(p.h.d, 0, p.h.d.length, p.le, p.ge, p.lt); + if (peekMutRestore(p)) siftDownRange(p.h.d, 0, p.h.d.length, p.le, p.ge, p.lt); } /** `PeekMut::pop`: the heap's `pop`, which leaves the drop nothing to sift. */ export function peekMutPop(p: PeekMut): T { - p.s = false; + peekMutRestore(p); return heapPop(p.h, p.le) as T; } diff --git a/ts/runtime/rt/iter.ts b/ts/runtime/rt/iter.ts index 8deccff9d..5301b1f13 100644 --- a/ts/runtime/rt/iter.ts +++ b/ts/runtime/rt/iter.ts @@ -609,9 +609,16 @@ function best(it: Iter, f: (item: T) => K, lt: Lt, least: boolean, d /** * `is_sorted_by`: whether `compare` holds of each item and the next, read by `next` and * then as `all` folds, stopping at the first it fails. Each item drops by `drop` as the - * next replaces it, or after the one it failed against; then `it`, if `drops`. + * next replaces it, or after the one it failed against; then `compare` by `dropCompare`, + * as `all` drops it, the last item, and `it`, if `drops`. */ -export function iterIsSortedBy(it: Iter, compare: (a: T, b: T) => boolean, drops = false, drop?: Drop): boolean { +export function iterIsSortedBy( + it: Iter, + compare: (a: T, b: T) => boolean, + drops = false, + drop?: Drop, + dropCompare?: Drop<(a: T, b: T) => boolean>, +): boolean { let last = it.next(); let sorted = true; if (last !== DONE) { @@ -625,8 +632,9 @@ export function iterIsSortedBy(it: Iter, compare: (a: T, b: T) => boolean, drop?.(last); last = item; } - drop?.(last); } + dropCompare?.(compare); + if (last !== DONE) drop?.(last); if (drops) dispose(it); return sorted; } diff --git a/ts/src/lower/iter.rs b/ts/src/lower/iter.rs index 0eda48198..b23d44e71 100644 --- a/ts/src/lower/iter.rs +++ b/ts/src/lower/iter.rs @@ -500,7 +500,10 @@ pub fn call<'tcx>( ) } }; - let droppers = vec![iter_drops(b, self_ty, span), b.t.dropper(each, span)]; + let mut droppers = vec![iter_drops(b, self_ty, span), b.t.dropper(each, span)]; + if name == "is_sorted_by" { + droppers.push(b.t.dropper(targ(1)?, span)); + } Some(Expr::rt("iterIsSortedBy", tail(vec![items, compare], droppers))) } "rposition" => method(&camel, rest), diff --git a/ts/src/lower/library.rs b/ts/src/lower/library.rs index 074f1ff0b..6e538bed0 100644 --- a/ts/src/lower/library.rs +++ b/ts/src/lower/library.rs @@ -632,6 +632,8 @@ fn discards(owner: &str, name: &str) -> Option<&'static [usize]> { // `get_or_init::` drops its function uncalled if the cell is set. ("OnceCell" | "OnceLock", "get_or_init") => &[1], ("Arc" | "Rc", "new_cyclic") => &[1], + // `is_sorted_by::` drops its comparator before the last item, as `all` takes it. + ("Iterator", "is_sorted_by") => &[1], ("LocalKey", "with" | "try_with" | "initialize_with" | "with_borrow_mut") => &[1], _ => return None, }) @@ -787,19 +789,20 @@ fn called_closures<'tcx>( if lent { continue; } + let index = call.args[..param.index as usize] + .iter() + .filter(|a| a.as_type().is_some()) + .count(); + let discards = discarded.is_some_and(|d| d.contains(&index)); let kept = inputs.iter().enumerate().any(|(j, &t)| j != i && mentions(t)); - if bound != ty::ClosureKind::FnOnce && !kept && i < args.len() { + if bound != ty::ClosureKind::FnOnce && !kept && !discards && i < args.len() { match dropping_operand(b, call) { None => closures.spent.push(args[i].clone()), Some(_) => closures.leaked = closures.leaked.or(Some(ty)), } continue; } - let index = call.args[..param.index as usize] - .iter() - .filter(|a| a.as_type().is_some()) - .count(); - if !discarded.is_some_and(|d| d.contains(&index)) { + if !discards { closures.leaked = closures.leaked.or(Some(ty)); continue; } From a02c1895321617fde4882d3c123cafe66c323192 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Thu, 29 Oct 2026 22:39:06 -0700 Subject: [PATCH 4/6] fix(ts): lend is_sorted_by_key a borrowed key function `is_sorted_by_key(&f)` over an iterator that drops disposed the map holding `f`, which dropped what `f` owns before its owner did. Co-Authored-By: Claude Opus 5.5 --- ts/fixtures/lang/expected/orders.ts | 37 +++++++++++++++++++---------- ts/fixtures/lang/src/orders.rs | 4 +++- ts/src/lower/iter.rs | 11 +++++---- 3 files changed, 33 insertions(+), 19 deletions(-) diff --git a/ts/fixtures/lang/expected/orders.ts b/ts/fixtures/lang/expected/orders.ts index 3f671034c..33fed8fc1 100644 --- a/ts/fixtures/lang/expected/orders.ts +++ b/ts/fixtures/lang/expected/orders.ts @@ -163,13 +163,14 @@ export function sorted(data: rt.ByteSlice, out: rt.ByteVec): void { }), rt.cmpLe(rt.cmpReverse(rt.cmp))) ? 1 : 0); rt.byteVecPush(out, rt.iterIsSortedBy(rt.iterSlice(rt.iterToArray(items()), rt.drop), Item.le$PartialOrd, true, rt.drop) ? 1 : 0); flush(out); - const _81: [Item.Item, Item.Item, Item.Item] = [_77(), _77(), _77()]; - const g: Item.Item = _81[0]; - const h: Item.Item = _81[1]; - const k: Item.Item = _81[2]; - const _91: rt.Iter = items(); + const _82: [Item.Item, Item.Item, Item.Item, Item.Item] = [_77(), _77(), _77(), _77()]; + const g: Item.Item = _82[0]; + const h: Item.Item = _82[1]; + const k: Item.Item = _82[2]; + const l: Item.Item = _82[3]; + const _94: rt.Iter = items(); const captured = g; - rt.byteVecPush(out, rt.iterIsSortedBy(_91, rt.closure((a3: Item.Item, b5: Item.Item): boolean => { + rt.byteVecPush(out, rt.iterIsSortedBy(_94, rt.closure((a3: Item.Item, b5: Item.Item): boolean => { if (!(captured.key <= a3.key)) { return false; } @@ -177,27 +178,36 @@ export function sorted(data: rt.ByteSlice, out: rt.ByteVec): void { }, (): void => { captured[Symbol.dispose](); }), undefined, rt.drop, rt.drop) ? 1 : 0); - const _100: rt.Iter = rt.iterSlice(rt.iterToArray(items()), rt.drop); + const _103: rt.Iter = rt.iterSlice(rt.iterToArray(items()), rt.drop); const captured2 = h; - rt.byteVecPush(out, rt.iterIsSortedBy(_100, rt.closure((a4: Item.Item, b6: Item.Item): boolean => { + rt.byteVecPush(out, rt.iterIsSortedBy(_103, rt.closure((a4: Item.Item, b6: Item.Item): boolean => { return a4.key <= rt.u8Add(b6.key, captured2.key); }, (): void => { captured2[Symbol.dispose](); }), true, rt.drop, rt.drop) ? 1 : 0); - const _105: rt.Iter = items(); + const _108: rt.Iter = items(); const captured3 = k; - const _107: (a0: Item.Item) => rt.u8 = rt.closure((x4: Item.Item): rt.u8 => { + const _110: (a0: Item.Item) => rt.u8 = rt.closure((x4: Item.Item): rt.u8 => { const _012: rt.u8 = rt.u8Add(x4.key, captured3.key); x4[Symbol.dispose](); return _012; }, (): void => { captured3[Symbol.dispose](); }); - rt.byteVecPush(out, rt.dropAfter(_107, () => rt.iterIsSortedBy(rt.iterMap(_105, _107), rt.le)) ? 1 : 0); + rt.byteVecPush(out, rt.dropAfter(_110, () => rt.iterIsSortedBy(rt.iterMap(_108, _110), rt.le)) ? 1 : 0); + const captured4 = l; + const key: (a0: Item.Item) => rt.u8 = rt.closure((x5: Item.Item): rt.u8 => { + const _013: rt.u8 = rt.u8Add(x5.key, captured4.key); + x5[Symbol.dispose](); + return _013; + }, (): void => { + captured4[Symbol.dispose](); + }); + rt.byteVecPush(out, rt.iterIsSortedBy(rt.iterMap(rt.iterSlice(rt.iterToArray(items()), rt.drop), rt.lent(key)), rt.le, true) ? 1 : 0); flush(out); const strings: string[] = rt.iterToArray(rt.iterMap(rt.iterSlice(rt.byteSliceView(_12)), (b7: rt.u8): string => { - const _414: rt.u8 = rt.u8Rem(b7, 16); - return rt.fmtToString((f: rt.Formatter) => rt.u8Display(_414, f)); + const _415: rt.u8 = rt.u8Rem(b7, 16); + return rt.fmtToString((f: rt.Formatter) => rt.u8Display(_415, f)); })); rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(rt.sliceIsSorted(rt.byteSliceView(_12), rt.le) ? 1 : 0, rt.iterIsSortedBy(rt.iterSlice(rt.byteSliceView(_12)), (a5: rt.u8, b8: rt.u8): boolean => { return rt.u8Div(a5, 4) <= rt.u8Div(b8, 4); @@ -208,6 +218,7 @@ export function sorted(data: rt.ByteSlice, out: rt.ByteVec): void { }), rt.le) ? 1 : 0, rt.iterIsSortedBy(rt.iterMap(rt.iterSlice(rt.byteSliceView(_12)), (_29: rt.u8): rt.f64 => { return (_29 as rt.f64) / 3.0; }), rt.cmpLe(rt.f64PartialCmp)) ? 1 : 0))); + rt.drop(key); } } diff --git a/ts/fixtures/lang/src/orders.rs b/ts/fixtures/lang/src/orders.rs index 550816677..0fb0fea0e 100644 --- a/ts/fixtures/lang/src/orders.rs +++ b/ts/fixtures/lang/src/orders.rs @@ -146,11 +146,13 @@ fn sorted(data: &[u8], out: &mut Vec) { // A function that owns an item drops as std's `all` drops it: before the last item // and the iterator. let owned = || item(200, 0); - let (g, h, k) = (owned(), owned(), owned()); + let (g, h, k, l) = (owned(), owned(), owned(), owned()); out.push(items().is_sorted_by(move |a, b| (&g).key <= a.key && a.key <= b.key) as u8); let v = items().collect::>(); out.push(v.into_iter().is_sorted_by(move |a, b| a.key <= b.key + (&h).key) as u8); out.push(items().is_sorted_by_key(move |x| x.key + (&k).key) as u8); + let key = move |x: Item| x.key + (&l).key; + out.push(items().collect::>().into_iter().is_sorted_by_key(&key) as u8); flush(out); let strings: Vec = bytes.iter().map(|b| (b % 16).to_string()).collect(); out.extend([ diff --git a/ts/src/lower/iter.rs b/ts/src/lower/iter.rs index b23d44e71..79c08f47e 100644 --- a/ts/src/lower/iter.rs +++ b/ts/src/lower/iter.rs @@ -493,11 +493,12 @@ pub fn call<'tcx>( _ => { let key = targ(2)?; let le = super::collections::test_fn(b, key, "le", span); - ( - Expr::rt("iterMap", vec![recv.clone(), rest.into_iter().next()?]), - le, - key, - ) + // A reference to a function is the function, which the map would drop. + let (mut f, f_ty) = (rest.into_iter().next()?, targ(1)?); + if f_ty.is_ref() && b.t.drops(f_ty.peel_refs()) { + f = Expr::rt("lent", vec![f]); + } + (Expr::rt("iterMap", vec![recv.clone(), f]), le, key) } }; let mut droppers = vec![iter_drops(b, self_ty, span), b.t.dropper(each, span)]; From e10ad42da3d1e00ef7d58bca91f2c1f73f6756c3 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Thu, 29 Oct 2026 22:51:43 -0700 Subject: [PATCH 5/6] fix(ts): lend is_sorted_by_key a key function through a box too Co-Authored-By: Claude Opus 5.5 --- ts/fixtures/lang/expected/orders.ts | 1 + ts/fixtures/lang/src/orders.rs | 1 + ts/src/lower/iter.rs | 6 ++++-- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/ts/fixtures/lang/expected/orders.ts b/ts/fixtures/lang/expected/orders.ts index 33fed8fc1..6ce39a64c 100644 --- a/ts/fixtures/lang/expected/orders.ts +++ b/ts/fixtures/lang/expected/orders.ts @@ -204,6 +204,7 @@ export function sorted(data: rt.ByteSlice, out: rt.ByteVec): void { captured4[Symbol.dispose](); }); rt.byteVecPush(out, rt.iterIsSortedBy(rt.iterMap(rt.iterSlice(rt.iterToArray(items()), rt.drop), rt.lent(key)), rt.le, true) ? 1 : 0); + rt.byteVecPush(out, rt.iterIsSortedBy(rt.iterMap(rt.iterSlice(rt.iterToArray(items()), rt.drop), rt.lent(key)), rt.le, true) ? 1 : 0); flush(out); const strings: string[] = rt.iterToArray(rt.iterMap(rt.iterSlice(rt.byteSliceView(_12)), (b7: rt.u8): string => { const _415: rt.u8 = rt.u8Rem(b7, 16); diff --git a/ts/fixtures/lang/src/orders.rs b/ts/fixtures/lang/src/orders.rs index 0fb0fea0e..250b5e90f 100644 --- a/ts/fixtures/lang/src/orders.rs +++ b/ts/fixtures/lang/src/orders.rs @@ -153,6 +153,7 @@ fn sorted(data: &[u8], out: &mut Vec) { out.push(items().is_sorted_by_key(move |x| x.key + (&k).key) as u8); let key = move |x: Item| x.key + (&l).key; out.push(items().collect::>().into_iter().is_sorted_by_key(&key) as u8); + out.push(items().collect::>().into_iter().is_sorted_by_key(Box::new(&key)) as u8); flush(out); let strings: Vec = bytes.iter().map(|b| (b % 16).to_string()).collect(); out.extend([ diff --git a/ts/src/lower/iter.rs b/ts/src/lower/iter.rs index 8522ac4c2..7ade1d9b3 100644 --- a/ts/src/lower/iter.rs +++ b/ts/src/lower/iter.rs @@ -493,9 +493,11 @@ pub fn call<'tcx>( _ => { let key = targ(2)?; let le = super::collections::test_fn(b, key, "le", span); - // A reference to a function is the function, which the map would drop. + // The map drops its function as the iterator drops, so one through a reference + // or box is lent, as one owned that drops is refused with such an iterator. let (mut f, f_ty) = (rest.into_iter().next()?, targ(1)?); - if f_ty.is_ref() && b.t.drops(f_ty.peel_refs()) { + let owned = matches!(f_ty.kind(), ty::Closure(..) | ty::FnDef(..) | ty::FnPtr(..)); + if !owned && drops_iter(b, self_ty, span) { f = Expr::rt("lent", vec![f]); } (Expr::rt("iterMap", vec![recv.clone(), f]), le, key) From c5bf6fc676ce5f5b748311709d70883e4fce6a77 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Thu, 29 Oct 2026 23:00:23 -0700 Subject: [PATCH 6/6] quest: replace rt.BinaryHeap with std's MIR once raw pointers translate Co-Authored-By: Claude Opus 5.5 --- quest/m3/ts-unsafe.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/quest/m3/ts-unsafe.md b/quest/m3/ts-unsafe.md index 43fe628af..8c66a5fee 100644 --- a/quest/m3/ts-unsafe.md +++ b/quest/m3/ts-unsafe.md @@ -45,5 +45,11 @@ none of its own `unsafe`. range, and each integer's `from_le_bytes` and `from_ne_bytes`, and `chunks_vectored` needs `std::io::IoSlice`: 184 of moq-net's 197 refusals in `bytes`, the rest being 12 of [`f32`](/quest/m3/ts-f32.md) and one of [`&mut &[u8]`](/quest/m3/ts-mut-ref.md). +- `BinaryHeap` and `PeekMut` translate from std's MIR, whose `Hole` moves elements + through raw pointers into the `Vec`'s storage, replacing `rt.BinaryHeap` and + `rt.PeekMut`, the hand-written port of std's sifts that + [#252](https://github.com/moq-dev/rust2/pull/252) mapped them to and that must track + std's by hand. The lang fixture's `orders` differential case proves the translation + still matches std's comparisons, pop order, and drops. Public API: the translator's output.