From 6540b208a69a382cd2018179663631f37688aac4 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 24 Oct 2026 00:45:25 -0700 Subject: [PATCH 01/13] chore(quest): claim quest/m2/ts-iter.md Quest-Claim: f96b1d626afed789 From f76a4c14efd652b98e9f10b5d2a4cbffd7db7239 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 24 Oct 2026 01:12:36 -0700 Subject: [PATCH 02/13] feat(ts): translate iterators through a DONE protocol (checkpoint) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- ts/fixtures/lang/expected/harness.ts | 17 +- ts/fixtures/lang/expected/iters.ts | 568 ++++++++++++++ ts/fixtures/lang/src/harness.rs | 12 +- ts/fixtures/lang/src/iters.rs | 387 ++++++++++ ts/fixtures/lang/src/lib.rs | 1 + ts/fixtures/reject/census.md | 72 +- ts/fixtures/reject/expected.txt | 7 +- ts/fixtures/reject/src/lib.rs | 49 +- ts/fixtures/reject/ts.toml | 3 + ts/runtime/rt.ts | 2 + ts/runtime/rt/iter.ts | 1040 ++++++++++++++++++++++++++ ts/runtime/tsconfig.json | 2 +- ts/src/census.rs | 1 + ts/src/lower/body.rs | 171 ++++- ts/src/lower/iter.rs | 360 +++++++++ ts/src/lower/library.rs | 78 +- ts/src/lower/mod.rs | 29 +- ts/src/program.rs | 19 +- ts/tests/fixtures.rs | 3 +- 19 files changed, 2728 insertions(+), 93 deletions(-) create mode 100644 ts/fixtures/lang/expected/iters.ts create mode 100644 ts/fixtures/lang/src/iters.rs create mode 100644 ts/runtime/rt/iter.ts create mode 100644 ts/src/lower/iter.rs diff --git a/ts/fixtures/lang/expected/harness.ts b/ts/fixtures/lang/expected/harness.ts index 601416bc9..d9da631ac 100644 --- a/ts/fixtures/lang/expected/harness.ts +++ b/ts/fixtures/lang/expected/harness.ts @@ -3,6 +3,7 @@ import * as rt from "./rt.ts"; import { ints, strings, derived, written, panics } from "./fmt.ts"; import { u8Op, u16Op, u32Op, u64Op, u128Op, usizeOp, i8Op, i16Op, i32Op, i64Op, i128Op, isizeOp, i8Neg, i16Neg, i32Neg, i64Neg, i128Neg, isizeNeg, cast, convert } from "./ints.ts"; +import { adaptors, consumers, ranges, options, own, values } from "./iters.ts"; import { copies, loops, patterns, closures, discriminants, library } from "./shapes.ts"; /** Run one case. */ @@ -15,7 +16,7 @@ export function run(input: Uint8Array): rt.ByteVec { const kind = input[1]; const data = input.subarray(2, input.length - 0); const [op2, kind2] = [op, kind] as [number, number]; - const value = rt.u8.rem(op2, 26); + const value = rt.u8.rem(op2, 32); if (value === 0) { u8Op(kind2, data, out); } else if (value === 1) { @@ -86,8 +87,20 @@ export function run(input: Uint8Array): rt.ByteVec { derived(data, out); } else if (value === 24) { written(data, out); - } else { + } else if (value === 25) { panics(kind2, data, out); + } else if (value === 26) { + adaptors(kind2, data, out); + } else if (value === 27) { + consumers(kind2, data, out); + } else if (value === 28) { + ranges(data, out); + } else if (value === 29) { + options(data, out); + } else if (value === 30) { + own(data, out); + } else { + values(data, out); } return out; } diff --git a/ts/fixtures/lang/expected/iters.ts b/ts/fixtures/lang/expected/iters.ts new file mode 100644 index 000000000..345286f3c --- /dev/null +++ b/ts/fixtures/lang/expected/iters.ts @@ -0,0 +1,568 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; + +/** Adaptor chains over the input bytes, each consumed one way. */ +export function adaptors(kind: number, data: Uint8Array, out: rt.ByteVec): void { + const value = rt.u8.rem(kind, 8); + if (value === 0) { + const doubled = rt.iter.slice(data).map((arg: number): number => { + const b = arg; + return rt.u8.div(b, 2); + }).filter((b2: number): boolean => { + return rt.u8.rem(b2, 3) !== 0; + }).toByteVec(); + out.extendFromSlice(doubled.view()); + const small = rt.iter.slice(data).filterMap((arg2: number): number | undefined => { + const b3 = arg2; + return rt.thenSome(b3 < 100, rt.u16.mul(b3, 200)); + }).toArray(); + for (const v of small) { + out.extendFromSlice(rt.u16.toBeBytes(v)); + } + } else if (value === 1) { + const iter = rt.iter.slice(data).enumerate().skip(1).stepBy(2); + while (true) { + const item = iter.next(); + if (item === rt.DONE) { + break; + } + const i = item[0]; + const b4 = item[1]; + out.push(i & 0xff); + out.push(b4); + } + const iter2 = rt.iter.slice(data).rev().enumerate().take(3); + while (true) { + const item2 = iter2.next(); + if (item2 === rt.DONE) { + break; + } + const i2 = item2[0]; + const b5 = item2[1]; + out.push((i2 & 0xff) ^ b5); + } + } else if (value === 2) { + const [front, back] = rt.slice.splitAt(data, rt.usize.div(data.length, 2)); + const zipped = rt.iter.slice(front).zip(rt.iter.slice(back).rev()).map((arg3: [number, number]): number => { + const a = arg3[0]; + const b6 = arg3[1]; + return a ^ b6; + }).toByteVec(); + out.extendFromSlice(zipped.view()); + const chained = rt.iter.slice(front).chain(rt.iter.slice(back)).rev().toByteVec(); + out.extendFromSlice(chained.view()); + } else if (value === 3) { + const taken = rt.iter.slice(data).takeWhile((arg4: number): boolean => { + const b7 = arg4; + return b7 !== 0; + }).toByteVec(); + const skipped = rt.iter.slice(data).skipWhile((arg5: number): boolean => { + const b8 = arg5; + return b8 < 0x80; + }).toByteVec(); + const mapped = rt.iter.slice(data).mapWhile((arg6: number): number | undefined => { + const b9 = arg6; + return rt.u8.checkedAdd(b9, 0x40); + }).toByteVec(); + out.push(taken.length & 0xff); + out.push(skipped.length & 0xff); + out.extendFromSlice(mapped.view()); + } else if (value === 4) { + const words = chunksByZero(data); + const flat = rt.iter.slice(words).flatMap((w: rt.ByteVec): rt.Iter => { + return rt.iter.slice(w.view()).rev(); + }).toByteVec(); + out.extendFromSlice(flat.view()); + const nested = rt.iter.slice(words).map((w2: rt.ByteVec): rt.Iter => { + return rt.iter.slice(w2.view()).map((arg7: number): number => { + const b10 = arg7; + return rt.u8.wrappingAdd(b10, 3); + }); + }).flatten(); + rt.iter.extend(out, nested.rev()); + } else if (value === 5) { + let it: rt.Peekable = rt.iter.slice(data).peekable(); + while (true) { + const next = rt.iter.toOption(it.next()); + if (next !== undefined) { + const b11 = next; + let run: number = 1; + while (it.nextIf((arg8: number): boolean => { + const next2 = arg8; + return next2 === b11; + }) !== undefined) { + run = rt.u8.add(run, 1); + } + out.push(b11); + out.push(run); + const peek = it.peek(); + if (peek !== undefined) { + const next3 = peek; + out.push(next3); + } + } else { + break; + } + } + } else if (value === 6) { + let it2: rt.Iter = rt.iter.slice(data); + const firsts = it2.take(2).toByteVec(); + out.extendFromSlice(firsts.view()); + out.push(it2.len() & 0xff); + const nextBack = rt.iter.toOption(it2.nextBack()); + if (nextBack !== undefined) { + const last = nextBack; + out.push(last); + } else { + out.push(0xee); + } + const rest = it2.rev().stepBy(3).toByteVec(); + out.extendFromSlice(rest.view()); + } else { + const pairs = rt.iter.slice(data).map((arg9: number): Uint8Array => { + const b12 = arg9; + return Uint8Array.of(b12, ~b12 & 0xff); + }).toArray(); + let copies: Uint8Array[] = rt.iter.slice(pairs).map(rt.copy).toArray(); + if (!((copies.length === 0))) { + const value2 = copies[rt.idx(copies, 0)]; + value2[0] = 0x55; + } + const iter3 = rt.iter.slice(pairs).chain(rt.iter.slice(copies)); + while (true) { + const pair = iter3.next(); + if (pair === rt.DONE) { + break; + } + out.extendFromSlice(pair); + } + let v2: number[] = []; + rt.iter.extend(v2, rt.iter.slice(data).map((arg10: number): number => { + const b13 = arg10; + return rt.u16.add(b13, 1); + })); + out.push(v2.length & 0xff); + rt.iter.extend(out, rt.iter.slice(data).filter((arg11: number): boolean => { + const b14 = arg11; + return b14 > 0x80; + })); + } +} + +/** The runs between zero bytes. */ +export function chunksByZero(self: Uint8Array): rt.ByteVec[] { + let words: rt.ByteVec[] = []; + let word: rt.ByteVec = new rt.ByteVec(); + for (const item of self) { + const b = item; + if (b === 0) { + const arg = words; + const taken = word; + word = new rt.ByteVec(); + arg.push(taken); + } else { + word.push(b); + } + } + words.push(word); + return words; +} + +/** Consumers, and how far each reads: `log` records every item a closure sees. */ +export function consumers(kind: number, data: Uint8Array, out: rt.ByteVec): void { + let log: rt.ByteVec = new rt.ByteVec(); + const value = rt.u8.rem(kind, 6); + if (value === 0) { + const any = rt.iter.slice(data).inspect((arg: number): void => { + const b = arg; + log.push(b); + }).any((arg2: number): boolean => { + const b2 = arg2; + return b2 > 0xf0; + }); + out.push(any ? 1 : 0); + const all = rt.iter.slice(data).all((arg3: number): boolean => { + const b3 = arg3; + return b3 !== 0; + }); + out.push(all ? 1 : 0); + out.push(rt.iter.slice(data).count() & 0xff); + out.push(rt.iter.slice(data).filter((arg4: number): boolean => { + const b4 = arg4; + return rt.u8.rem(b4, 2) === 0; + }).count() & 0xff); + } else if (value === 1) { + const found = rt.iter.slice(data).inspect((arg5: number): void => { + const b5 = arg5; + log.push(b5); + }).find((arg6: number): boolean => { + const b6 = arg6; + return rt.u8.rem(b6, 7) === 3; + }); + out.push(rt.option.unwrapOr(found, 0xff)); + const mapped = rt.iter.slice(data).findMap((arg7: number): number | undefined => { + const b7 = arg7; + if (b7 > 0x50) { + return rt.u8.checkedMul(b7, 3); + } else { + return undefined; + } + }); + out.push(rt.option.unwrapOr(mapped, 0)); + const position = rt.iter.slice(data).position((arg8: number): boolean => { + const b8 = arg8; + return b8 === 0x40; + }); + out.push(rt.option.mapOr(position, 0xff, (p: number): number => { + return p & 0xff; + })); + } else if (value === 2) { + out.push(rt.option.unwrapOr(rt.iter.slice(data).max(), 0)); + out.push(rt.option.unwrapOr(rt.iter.slice(data).min(), 0xff)); + out.push(rt.option.unwrapOr(rt.iter.slice(data).maxByKey((arg9: number): number => { + const b9 = arg9; + return rt.u8.rem(b9, 16); + }), 0xee)); + out.push(rt.option.unwrapOr(rt.iter.slice(data).minByKey((arg10: number): number => { + const b10 = arg10; + return rt.u8.div(b10, 16); + }), 0xee)); + out.push(rt.option.unwrapOr(rt.iter.slice(data).last(), 0xdd)); + out.push(rt.option.unwrapOr(rt.iter.slice(data).nth(3), 0xcc)); + } else if (value === 3) { + const wide = rt.iter.slice(data).map((arg11: number): bigint => { + const b11 = arg11; + return rt.u64.mul(BigInt(b11), 0x0101_0101_0101n); + }).sum(rt.u64.add, 0n); + out.extendFromSlice(rt.u64.toBeBytes(wide)); + const folded = rt.iter.slice(data).fold(0, (acc: number, arg12: number): number => { + const b12 = arg12; + return rt.u32.wrappingAdd((acc << 3) >>> 0, (b12 ^ acc) >>> 0); + }); + out.extendFromSlice(rt.u32.toBeBytes(folded)); + rt.iter.slice(data).forEach((arg13: number): void => { + const b13 = arg13; + log.push(rotateFree(b13)); + }); + } else if (value === 4) { + const total = rt.iter.slice(data).sum(rt.u8.add, 0); + out.push(total); + const product = rt.iter.slice(data).map((arg14: number): number => { + const b14 = arg14; + return rt.u16.add(rt.u16.rem(b14, 4), 1); + }).product(rt.u16.mul, 1); + out.extendFromSlice(rt.u16.toBeBytes(product)); + } else { + const parsed = rt.iter.slice(data).map((arg15: number): rt.Result => { + const b15 = arg15; + if (b15 < 0xf0) { + return rt.Ok(b15); + } else { + return rt.Err(b15); + } + }).collectResult((values__: rt.Iter) => values__.toByteVec()); + if (parsed.ok) { + const v = parsed.value; + out.extendFromSlice(v.view()); + } else { + const e = parsed.error; + out.push(e); + } + const some = rt.iter.slice(data).inspect((arg16: number): void => { + const b16 = arg16; + log.push(b16); + }).map((arg17: number): number | undefined => { + const b17 = arg17; + return rt.thenSome(b17 !== 0x20, b17); + }).collectOption((values__: rt.Iter) => values__.toArray()); + out.push(rt.option.mapOr(some, 0xff, (v2: number[]): number => { + return v2.length & 0xff; + })); + } + out.extendFromSlice(log.view()); +} + +export function rotateFree(self: number): number { + return ((self << 1) & 0xff) | (self >> 7); +} + +/** Ranges as values and as iterators. */ +export function ranges(data: Uint8Array, out: rt.ByteVec): void { + const a = rt.option.unwrapOr(rt.slice.first(data), 0); + const b = rt.option.unwrapOr(rt.slice.get(data, 1), 0); + const range = new rt.Range(a, b); + out.push(range.start); + out.push(range.end); + out.push(range.isEmpty() ? 1 : 0); + out.push(range.contains(0x40) ? 1 : 0); + let copy: rt.Range = range.clone(); + out.push(rt.option.unwrapOr(rt.iter.toOption(copy.next()), 0xff)); + out.push(range.len() & 0xff); + const iter = range.rev().stepBy(3); + while (true) { + const i = iter.next(); + if (i === rt.DONE) { + break; + } + out.push(i); + } + const top = new rt.RangeInclusive(rt.max(a, 250), 255).toByteVec(); + out.extendFromSlice(top.view()); + const inclusive = new rt.RangeInclusive(a, b); + out.push(inclusive.start()); + out.push(inclusive.end()); + out.push(inclusive.contains(b) ? 1 : 0); + let wide: rt.Range = new rt.Range(BigInt(a), rt.u64.mul(BigInt(b), 3n)); + out.push(wide.map((n: bigint): bigint => { + return rt.u64.rem(n, 5n); + }).filter((arg: bigint): boolean => { + const n2 = arg; + return n2 === 0n; + }).count() & 0xff); + out.push(wide.isEmpty() ? 1 : 0); + const total = new rt.Range(0n, BigInt(a)).sum(rt.u64.add, 0n); + out.extendFromSlice(rt.u64.toBeBytes(total)); + const steps = new rt.Range(0, 48).stepBy(8).rev().toArray(); + for (const s of steps) { + out.push(s & 0xff); + } +} + +/** Iterators of `Option`, which keep each `None` apart from their end. */ +export function options(data: Uint8Array, out: rt.ByteVec): void { + const maybe = (b: number): number | undefined => { + if (rt.u8.rem(b, 3) === 0) { + return undefined; + } else { + return b; + } + }; + const all = rt.iter.slice(data).map(maybe).toArray(); + out.push(all.length & 0xff); + const iter = rt.iter.slice(data).map(maybe); + while (true) { + const item = iter.next(); + if (item === rt.DONE) { + break; + } + if (item !== undefined) { + const b2 = item; + out.push(b2); + } else { + out.push(0); + } + } + const flat = rt.iter.slice(data).map(maybe).flatten((value: number | undefined) => rt.iter.option(value)).toByteVec(); + out.extendFromSlice(flat.view()); + const somes = rt.iter.slice(all).filter((o: number | undefined): boolean => { + return o !== undefined; + }).count(); + out.push(somes & 0xff); + const first = rt.slice.first(data); + const iter2 = rt.iter.option(first).chain(rt.iter.once(0x99)); + while (true) { + const b3 = iter2.next(); + if (b3 === rt.DONE) { + break; + } + out.push(b3); + } + rt.iter.extend(out, rt.iter.option(first).chain(rt.iter.empty())); + let n: number = 0; + const counted = rt.iter.fromFn((): number | undefined => { + n = rt.u8.add(n, 1); + return rt.thenSome(n < 4, n); + }).toByteVec(); + out.extendFromSlice(counted.view()); + const gaps = new Gaps({ data, at: 0 }); + while (true) { + const gap = gaps.next(); + if (gap === rt.DONE) { + break; + } + out.push(rt.option.mapOr(gap, 0xaa, (b4: number): number => { + return b4; + })); + } +} + +/** Every byte, or `None` where it is zero. */ +export class Gaps { + data: Uint8Array; + at: number; + + constructor(fields: { data: Uint8Array; at: number }) { + this.data = fields.data; + this.at = fields.at; + } + + next(): number | undefined | rt.Done { + const get = rt.slice.get(this.data, this.at); + if (get === undefined) { + return rt.DONE; + } + const b = get; + this.at = rt.usize.add(this.at, 1); + if (b === 0) { + return undefined; + } + return b; + } +} + +/** A countdown: the crate's own iterator. */ +export class Countdown { + left: number; + step: number; + + constructor(fields: { left: number; step: number }) { + this.left = fields.left; + this.step = fields.step; + } + + next(): number | rt.Done { + if (this.left === 0) { + return rt.DONE; + } else { + const left = this.left; + this.left = rt.u8.saturatingSub(left, this.step); + return left; + } + } +} + +/** The even bytes, read through another iterator, whose `None` ends this one. */ +export class Evens { + inner: rt.Iter; + + constructor(fields: { inner: rt.Iter }) { + this.inner = fields.inner; + } + + next(): number | rt.Done { + while (true) { + const next = rt.iter.toOption(this.inner.next()); + if (next !== undefined) { + const b = next; + if (rt.u8.rem(b, 2) === 0) { + return b; + } + } else { + break; + } + } + return rt.DONE; + } +} + +/** The crate's own iterators, through `for`, adaptors, and `next`. */ +export function own(data: Uint8Array, out: rt.ByteVec): void { + const left = rt.option.unwrapOr(rt.slice.first(data), 0); + const step = rt.max(rt.option.unwrapOr(rt.slice.get(data, 1), 1), 1); + const iter = new Countdown({ left, step }); + while (true) { + const n = iter.next(); + if (n === rt.DONE) { + break; + } + out.push(n); + } + const total = rt.iter.from(new Countdown({ left, step }), (it: Countdown) => it.next()).map((n2: number): bigint => { + return rt.u64.div(rt.u64.mul(BigInt(n2), 1000n), 7n); + }).sum(rt.u64.add, 0n); + out.extendFromSlice(rt.u64.toBeBytes(total)); + let down: Countdown = new Countdown({ left, step }); + while (true) { + const next = rt.iter.toOption(down.next()); + if (next !== undefined) { + const n3 = next; + if (rt.u8.rem(n3, 5) === 0) { + break; + } + } else { + break; + } + } + out.push(down.left); + const evens = rt.iter.from(new Evens({ inner: rt.iter.slice(data) }), (it: Evens) => it.next()).toByteVec(); + out.extendFromSlice(evens.view()); + let byRef: Evens = new Evens({ inner: rt.iter.slice(rt.slice.range(data, rt.min(data.length, 1))) }); + const firsts = rt.iter.from(byRef, (it: Evens) => it.next()).take(2).toByteVec(); + out.extendFromSlice(firsts.view()); + out.push(rt.iter.from(byRef, (it: Evens) => it.next()).count() & 0xff); +} + +/** + * The last two bytes of `data`, as a `slice::Iter` made from the second argument: a + * `for` must iterate it, not the first. + */ +export function tail(_head: Uint8Array, data: Uint8Array): rt.Iter { + return rt.iter.slice(rt.slice.range(data, rt.usize.saturatingSub(data.length, 2))); +} + +/** The odd bytes, from a function returning `impl Iterator`. */ +export function odds(data: Uint8Array): rt.Iter { + return rt.iter.slice(data).filter((b: number): boolean => { + return rt.u8.rem(b, 2) === 1; + }); +} + +/** Iterators as values: returned, stored, and printed. */ +export function values(data: Uint8Array, out: rt.ByteVec): void { + const iter = tail(Uint8Array.of(1, 2, 3), data); + while (true) { + const item = iter.next(); + if (item === rt.DONE) { + break; + } + const b = item; + out.push(b); + } + let held: Held = new Held({ it: rt.iter.slice(data) }); + out.push(held.skipOne()); + const iter2 = odds(data); + while (true) { + const b2 = iter2.next(); + if (b2 === rt.DONE) { + break; + } + out.push(b2); + } + const arg = new Doubled(data); + const arg2 = new Doubled(rt.slice.range(data, 0, rt.min(data.length, 2))); + const printed = rt.fmt.format([(f: rt.Formatter) => arg.fmt(f), "|", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => arg2.fmt(f))]); + out.extendFromSlice(rt.string.asBytes(printed)); +} + +/** An iterator in a field. */ +export class Held { + it: rt.Iter; + + constructor(fields: { it: rt.Iter }) { + this.it = fields.it; + } + + skipOne(): number { + rt.iter.toOption(this.it.next()); + return rt.option.unwrapOr(rt.iter.toOption(this.it.next()), 0xbb); + } +} + +/** Each byte doubled, as a `Debug` list built from an iterator. */ +export class Doubled { + _0: Uint8Array; + + constructor(_0: Uint8Array) { + this._0 = _0; + } + + fmt(f: rt.Formatter): rt.Result { + return f.debugList().entries(rt.iter.slice(this._0).map((arg: number): number => { + const b = arg; + return rt.u16.mul(b, 2); + }), rt.u16.display).finish(); + } +} + +//# sourceMappingURL=iters.ts.map diff --git a/ts/fixtures/lang/src/harness.rs b/ts/fixtures/lang/src/harness.rs index b702b1df3..a219d45b0 100644 --- a/ts/fixtures/lang/src/harness.rs +++ b/ts/fixtures/lang/src/harness.rs @@ -1,7 +1,7 @@ //! The entry point both sides of the differential test run. The first byte picks a //! case and the rest is its input. -use crate::{fmt, ints, shapes}; +use crate::{fmt, ints, iters, shapes}; /// The native side's entry: [`run`], except that a case starting with 0xff prints a /// [`fmt::Fault`], which the translated side prints from TypeScript in `caller.ts`. @@ -19,7 +19,7 @@ pub fn run(input: &[u8]) -> Vec { return out; }; let (op, kind) = (*op, *kind); - match op % 26 { + match op % 32 { 0 => ints::u8_op(kind, data, &mut out), 1 => ints::u16_op(kind, data, &mut out), 2 => ints::u32_op(kind, data, &mut out), @@ -55,7 +55,13 @@ pub fn run(input: &[u8]) -> Vec { 22 => fmt::strings(data, &mut out), 23 => fmt::derived(data, &mut out), 24 => fmt::written(data, &mut out), - _ => fmt::panics(kind, data, &mut out), + 25 => fmt::panics(kind, data, &mut out), + 26 => iters::adaptors(kind, data, &mut out), + 27 => iters::consumers(kind, data, &mut out), + 28 => iters::ranges(data, &mut out), + 29 => iters::options(data, &mut out), + 30 => iters::own(data, &mut out), + _ => iters::values(data, &mut out), } out } diff --git a/ts/fixtures/lang/src/iters.rs b/ts/fixtures/lang/src/iters.rs new file mode 100644 index 000000000..be9c83d1a --- /dev/null +++ b/ts/fixtures/lang/src/iters.rs @@ -0,0 +1,387 @@ +//! Iterators: std's adaptors and consumers, ranges, iterators of `Option`, and the +//! crate's own `impl Iterator`. Closures with effects log each call, so laziness and +//! where a consumer stops are part of the output. + +use std::fmt; + +/// Adaptor chains over the input bytes, each consumed one way. +pub fn adaptors(kind: u8, data: &[u8], out: &mut Vec) { + match kind % 8 { + 0 => { + let doubled: Vec = data.iter().map(|&b| b / 2).filter(|b| b % 3 != 0).collect(); + out.extend_from_slice(&doubled); + let small: Vec = data + .iter() + .filter_map(|&b| (b < 100).then_some(b as u16 * 200)) + .collect(); + for v in small { + out.extend_from_slice(&v.to_be_bytes()); + } + } + 1 => { + for (i, &b) in data.iter().enumerate().skip(1).step_by(2) { + out.push(i as u8); + out.push(b); + } + for (i, b) in data.iter().rev().enumerate().take(3) { + out.push(i as u8 ^ b); + } + } + 2 => { + let (front, back) = data.split_at(data.len() / 2); + let zipped: Vec = front.iter().zip(back.iter().rev()).map(|(a, b)| a ^ b).collect(); + out.extend_from_slice(&zipped); + let chained: Vec = front.iter().chain(back).copied().rev().collect(); + out.extend_from_slice(&chained); + } + 3 => { + let taken: Vec = data.iter().copied().take_while(|&b| b != 0).collect(); + let skipped: Vec = data.iter().copied().skip_while(|&b| b < 0x80).collect(); + let mapped: Vec = data.iter().map_while(|&b| b.checked_add(0x40)).collect(); + out.push(taken.len() as u8); + out.push(skipped.len() as u8); + out.extend_from_slice(&mapped); + } + 4 => { + let words: Vec> = data.chunks_by_zero(); + let flat: Vec = words.iter().flat_map(|w| w.iter().rev()).copied().collect(); + out.extend_from_slice(&flat); + let nested = words.iter().map(|w| w.iter().map(|&b| b.wrapping_add(3))).flatten(); + out.extend(nested.rev()); + } + 5 => { + let mut it = data.iter().peekable(); + while let Some(&b) = it.next() { + let mut run = 1u8; + while it.next_if(|&&next| next == b).is_some() { + run += 1; + } + out.push(b); + out.push(run); + if let Some(&&next) = it.peek() { + out.push(next); + } + } + } + 6 => { + let mut it = data.iter(); + let firsts: Vec = it.by_ref().take(2).copied().collect(); + out.extend_from_slice(&firsts); + out.push(it.len() as u8); + match it.next_back() { + Some(&last) => out.push(last), + None => out.push(0xee), + } + let rest: Vec = it.rev().step_by(3).copied().collect(); + out.extend_from_slice(&rest); + } + _ => { + // Each element of `pairs` is a copy: changing one changes nothing else. + let pairs: Vec<[u8; 2]> = data.iter().map(|&b| [b, !b]).collect(); + let mut copies: Vec<[u8; 2]> = pairs.iter().copied().collect(); + if !copies.is_empty() { + copies[0][0] = 0x55; + } + for pair in pairs.iter().chain(copies.iter()) { + out.extend_from_slice(pair); + } + let mut v: Vec = Vec::new(); + v.extend(data.iter().map(|&b| b as u16 + 1)); + out.push(v.len() as u8); + out.extend(data.iter().filter(|&&b| b > 0x80)); + } + } +} + +trait ChunksByZero { + fn chunks_by_zero(&self) -> Vec>; +} + +impl ChunksByZero for [u8] { + /// The runs between zero bytes. + fn chunks_by_zero(&self) -> Vec> { + let mut words = Vec::new(); + let mut word = Vec::new(); + for &b in self { + if b == 0 { + words.push(std::mem::take(&mut word)); + } else { + word.push(b); + } + } + words.push(word); + words + } +} + +/// Consumers, and how far each reads: `log` records every item a closure sees. +pub fn consumers(kind: u8, data: &[u8], out: &mut Vec) { + let mut log = Vec::new(); + match kind % 6 { + 0 => { + let any = data.iter().inspect(|&&b| log.push(b)).any(|&b| b > 0xf0); + out.push(any as u8); + let all = data.iter().all(|&b| b != 0); + out.push(all as u8); + out.push(data.iter().count() as u8); + out.push(data.iter().filter(|&&b| b % 2 == 0).count() as u8); + } + 1 => { + let found = data.iter().inspect(|&&b| log.push(b)).find(|&&b| b % 7 == 3); + out.push(found.copied().unwrap_or(0xff)); + let mapped = data + .iter() + .find_map(|&b| if b > 0x50 { b.checked_mul(3) } else { None }); + out.push(mapped.unwrap_or(0)); + let position = data.iter().position(|&b| b == 0x40); + out.push(position.map_or(0xff, |p| p as u8)); + } + 2 => { + out.push(data.iter().copied().max().unwrap_or(0)); + out.push(data.iter().min().copied().unwrap_or(0xff)); + out.push(*data.iter().max_by_key(|&&b| b % 16).unwrap_or(&0xee)); + out.push(*data.iter().min_by_key(|&&b| b / 16).unwrap_or(&0xee)); + out.push(data.iter().last().copied().unwrap_or(0xdd)); + out.push(data.iter().nth(3).copied().unwrap_or(0xcc)); + } + 3 => { + let wide: u64 = data.iter().map(|&b| b as u64 * 0x0101_0101_0101).sum(); + out.extend_from_slice(&wide.to_be_bytes()); + let folded = data + .iter() + .fold(0u32, |acc, &b| (acc << 3).wrapping_add(b as u32 ^ acc)); + out.extend_from_slice(&folded.to_be_bytes()); + data.iter().for_each(|&b| log.push(b.rotate_free())); + } + 4 => { + // Panics on overflow, as a debug build's `sum` does. + let total: u8 = data.iter().sum(); + out.push(total); + let product: u16 = data.iter().map(|&b| b as u16 % 4 + 1).product(); + out.extend_from_slice(&product.to_be_bytes()); + } + _ => { + let parsed: Result, u8> = data.iter().map(|&b| if b < 0xf0 { Ok(b) } else { Err(b) }).collect(); + match parsed { + Ok(v) => out.extend_from_slice(&v), + Err(e) => out.push(e), + } + let some: Option> = data + .iter() + .inspect(|&&b| log.push(b)) + .map(|&b| (b != 0x20).then_some(b as u16)) + .collect(); + out.push(some.map_or(0xff, |v| v.len() as u8)); + } + } + out.extend_from_slice(&log); +} + +trait RotateFree { + fn rotate_free(self) -> Self; +} + +impl RotateFree for u8 { + fn rotate_free(self) -> u8 { + (self << 1) | (self >> 7) + } +} + +/// Ranges as values and as iterators. +pub fn ranges(data: &[u8], out: &mut Vec) { + let a = data.first().copied().unwrap_or(0); + let b = data.get(1).copied().unwrap_or(0); + let range = a..b; + out.push(range.start); + out.push(range.end); + out.push(range.is_empty() as u8); + out.push(range.contains(&0x40) as u8); + let mut copy = range.clone(); + out.push(copy.next().unwrap_or(0xff)); + out.push(range.len() as u8); + for i in range.rev().step_by(3) { + out.push(i); + } + // `..=` at the top of `u8`, which ends without overflowing. + let top: Vec = (a.max(250)..=255).collect(); + out.extend_from_slice(&top); + let inclusive = a..=b; + out.push(*inclusive.start()); + out.push(*inclusive.end()); + out.push(inclusive.contains(&b) as u8); + let mut wide = (a as u64)..(b as u64 * 3); + out.push(wide.by_ref().map(|n| n % 5).filter(|&n| n == 0).count() as u8); + out.push(wide.is_empty() as u8); + let total: u64 = (0..a as u64).sum(); + out.extend_from_slice(&total.to_be_bytes()); + let steps: Vec = (0..48).step_by(8).rev().collect(); + for s in steps { + out.push(s as u8); + } +} + +/// Iterators of `Option`, which keep each `None` apart from their end. +pub fn options(data: &[u8], out: &mut Vec) { + let maybe = |b: &u8| if b % 3 == 0 { None } else { Some(*b) }; + let all: Vec> = data.iter().map(maybe).collect(); + out.push(all.len() as u8); + for item in data.iter().map(maybe) { + match item { + Some(b) => out.push(b), + None => out.push(0), + } + } + let flat: Vec = data.iter().map(maybe).flatten().collect(); + out.extend_from_slice(&flat); + let somes = all.iter().filter(|o| o.is_some()).count(); + out.push(somes as u8); + let first = data.first().copied(); + for b in first.iter().chain(std::iter::once(&0x99)) { + out.push(*b); + } + out.extend(first.into_iter().chain(std::iter::empty())); + let mut n = 0u8; + let counted: Vec = std::iter::from_fn(|| { + n += 1; + (n < 4).then_some(n) + }) + .collect(); + out.extend_from_slice(&counted); + // The crate's own iterator, whose items are options. + let gaps = Gaps { data, at: 0 }; + for gap in gaps { + out.push(gap.map_or(0xaa, |b| b)); + } +} + +/// Every byte, or `None` where it is zero. +struct Gaps<'a> { + data: &'a [u8], + at: usize, +} + +impl Iterator for Gaps<'_> { + type Item = Option; + + fn next(&mut self) -> Option> { + let &b = self.data.get(self.at)?; + self.at += 1; + if b == 0 { + return Some(None); + } + Some(Some(b)) + } +} + +/// A countdown: the crate's own iterator. +struct Countdown { + left: u8, + step: u8, +} + +impl Iterator for Countdown { + type Item = u8; + + fn next(&mut self) -> Option { + match self.left { + 0 => None, + left => { + self.left = left.saturating_sub(self.step); + Some(left) + } + } + } +} + +/// The even bytes, read through another iterator, whose `None` ends this one. +struct Evens<'a> { + inner: std::iter::Copied>, +} + +impl Iterator for Evens<'_> { + type Item = u8; + + fn next(&mut self) -> Option { + while let Some(b) = self.inner.next() { + if b % 2 == 0 { + return Some(b); + } + } + None + } +} + +/// The crate's own iterators, through `for`, adaptors, and `next`. +pub fn own(data: &[u8], out: &mut Vec) { + let left = data.first().copied().unwrap_or(0); + let step = data.get(1).copied().unwrap_or(1).max(1); + for n in (Countdown { left, step }) { + out.push(n); + } + let total: u64 = Countdown { left, step }.map(|n| n as u64 * 1000 / 7).sum(); + out.extend_from_slice(&total.to_be_bytes()); + let mut down = Countdown { left, step }; + while let Some(n) = down.next() { + if n % 5 == 0 { + break; + } + } + out.push(down.left); + let evens: Vec = Evens { + inner: data.iter().copied(), + } + .collect(); + out.extend_from_slice(&evens); + let mut by_ref = Evens { + inner: data[data.len().min(1)..].iter().copied(), + }; + let firsts: Vec = by_ref.by_ref().take(2).collect(); + out.extend_from_slice(&firsts); + out.push(by_ref.count() as u8); +} + +/// The last two bytes of `data`, as a `slice::Iter` made from the second argument: a +/// `for` must iterate it, not the first. +fn tail<'a>(_head: &'a [u8], data: &'a [u8]) -> std::slice::Iter<'a, u8> { + data[data.len().saturating_sub(2)..].iter() +} + +/// The odd bytes, from a function returning `impl Iterator`. +fn odds(data: &[u8]) -> impl Iterator + '_ { + data.iter().copied().filter(|b| b % 2 == 1) +} + +/// Iterators as values: returned, stored, and printed. +pub fn values(data: &[u8], out: &mut Vec) { + for &b in tail(&[1, 2, 3], data) { + out.push(b); + } + let mut held = Held { it: data.iter() }; + out.push(held.skip_one()); + for b in odds(data) { + out.push(b); + } + let printed = format!("{:?}|{:#?}", Doubled(data), Doubled(&data[..data.len().min(2)])); + out.extend_from_slice(printed.as_bytes()); +} + +/// An iterator in a field. +struct Held<'a> { + it: std::slice::Iter<'a, u8>, +} + +impl Held<'_> { + fn skip_one(&mut self) -> u8 { + self.it.next(); + self.it.next().copied().unwrap_or(0xbb) + } +} + +/// Each byte doubled, as a `Debug` list built from an iterator. +struct Doubled<'a>(&'a [u8]); + +impl fmt::Debug for Doubled<'_> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_list().entries(self.0.iter().map(|&b| b as u16 * 2)).finish() + } +} diff --git a/ts/fixtures/lang/src/lib.rs b/ts/fixtures/lang/src/lib.rs index 934c4b0e0..07428083c 100644 --- a/ts/fixtures/lang/src/lib.rs +++ b/ts/fixtures/lang/src/lib.rs @@ -8,6 +8,7 @@ mod fmt; pub mod harness; mod ints; +mod iters; mod shapes; #[cfg(target_arch = "wasm32")] #[path = "../../wasm.rs"] diff --git a/ts/fixtures/reject/census.md b/ts/fixtures/reject/census.md index 9d45a70bc..beca8f559 100644 --- a/ts/fixtures/reject/census.md +++ b/ts/fixtures/reject/census.md @@ -1,17 +1,18 @@ # Census of `reject-fixture` -- Refusals: 39 -- Function instances: 19 -- Generic functions no instance reached, lowered over their type parameters: 1 +- Refusals: 40 +- Function instances: 26 +- Generic functions no instance reached, lowered over their type parameters: 2 ## By construct | Count | Construct | First | | ---: | --- | --- | -| 5 | unsupported call to `_` | fixtures/reject/src/lib.rs:36:4 | | 5 | unsupported type `_` | fixtures/reject/src/lib.rs:6:1 | | 3 | a `_` binding: a JS value of `_` is a copy, so it can't change | fixtures/reject/src/lib.rs:19:13 | | 3 | replacing the value behind a reference to `_` | fixtures/reject/src/lib.rs:99:17 | +| 3 | unsupported call to `_` | fixtures/reject/src/lib.rs:36:4 | +| 2 | `_` is not supported: its `None` and `Some` would both be `undefined` | fixtures/reject/src/lib.rs:31:15 | | 2 | `_`: a JS value of `_` is a copy, so a reference can't change it | fixtures/reject/src/lib.rs:19:13 | | 1 | `Binary` formatting, as in `{:b}`, is not supported | fixtures/reject/src/lib.rs:63:3 | | 1 | `LowerExp` formatting, as in `{:e}`, is not supported | fixtures/reject/src/lib.rs:65:3 | @@ -19,7 +20,6 @@ | 1 | `Pointer` formatting, as in `{:p}`, is not supported | fixtures/reject/src/lib.rs:68:3 | | 1 | `_` implements `Drop`, which is not supported yet | fixtures/reject/src/lib.rs:43:1 | | 1 | `_` is async, which is not supported yet | fixtures/reject/src/lib.rs:53:1 | -| 1 | `_` is not supported: its `None` and `Some` would both be `undefined` | fixtures/reject/src/lib.rs:31:15 | | 1 | a cast from `_` | fixtures/reject/src/lib.rs:11:2 | | 1 | a cast to `_` | fixtures/reject/src/lib.rs:110:2 | | 1 | a closure that copies a mutable variable | fixtures/reject/src/lib.rs:80:18 | @@ -27,9 +27,11 @@ | 1 | a precision, as in `{:.2}`, is not supported | fixtures/reject/src/lib.rs:60:3 | | 1 | a printable type must implement `Display` or `Debug` | fixtures/reject/src/lib.rs:106:1 | | 1 | a width or precision from an argument, as in `{:1$}`, is not supported | fixtures/reject/src/lib.rs:67:3 | +| 1 | an iterator of the crate implementing `_`, which is not supported yet | fixtures/reject/src/lib.rs:148:1 | | 1 | an or-pattern with bindings | fixtures/reject/src/lib.rs:87:3 | | 1 | arithmetic on `_` | fixtures/reject/src/lib.rs:7:2 | | 1 | hex `Debug`, as in `{:x?}`, is not supported | fixtures/reject/src/lib.rs:66:3 | +| 1 | overriding `_`, which std's adaptors call in place of `next` | fixtures/reject/src/lib.rs:128:2 | | 1 | the `+` flag of a format spec is not supported | fixtures/reject/src/lib.rs:61:3 | | 1 | the `-` flag of a format spec is not supported | fixtures/reject/src/lib.rs:62:3 | | 1 | unsupported expression: StaticRef | fixtures/reject/src/lib.rs:94:2 | @@ -45,14 +47,15 @@ The type or module each refused type or path belongs to. | 4 | `std::string::String` | fixtures/reject/src/lib.rs:73:16 | | 3 | `Meters` | fixtures/reject/src/lib.rs:99:17 | | 3 | `u32` | fixtures/reject/src/lib.rs:19:13 | +| 2 | `Option` | fixtures/reject/src/lib.rs:31:15 | | 2 | `char` | fixtures/reject/src/lib.rs:10:18 | -| 2 | `std::slice` | fixtures/reject/src/lib.rs:36:4 | +| 1 | `DoubleEndedIterator` | fixtures/reject/src/lib.rs:148:1 | | 1 | `Guard` | fixtures/reject/src/lib.rs:43:1 | -| 1 | `Option` | fixtures/reject/src/lib.rs:31:15 | +| 1 | `Iterator` | fixtures/reject/src/lib.rs:128:2 | | 1 | `dyn std::ops::Fn(u8) -> u8` | fixtures/reject/src/lib.rs:14:16 | | 1 | `later` | fixtures/reject/src/lib.rs:53:1 | -| 1 | `std::iter::Filter` | fixtures/reject/src/lib.rs:40:31 | -| 1 | `std::slice::Iter` | fixtures/reject/src/lib.rs:40:11 | +| 1 | `std::slice::Iter` | fixtures/reject/src/lib.rs:115:11 | +| 1 | `std::slice` | fixtures/reject/src/lib.rs:36:4 | ## By type or path @@ -60,21 +63,21 @@ The type or module each refused type or path belongs to. | ---: | --- | --- | --- | | 3 | replacing the value behind a reference to `_` | `Meters` | fixtures/reject/src/lib.rs:99:17 | | 3 | unsupported type `_` | `f64` | fixtures/reject/src/lib.rs:6:1 | +| 2 | `_` is not supported: its `None` and `Some` would both be `undefined` | `Option` | fixtures/reject/src/lib.rs:31:15 | | 2 | a `_` binding: a JS value of `_` is a copy, so it can't change | `&mut u32`, `u32` | fixtures/reject/src/lib.rs:19:13 | | 1 | `_` implements `Drop`, which is not supported yet | `Guard` | fixtures/reject/src/lib.rs:43:1 | | 1 | `_` is async, which is not supported yet | `later` | fixtures/reject/src/lib.rs:53:1 | -| 1 | `_` is not supported: its `None` and `Some` would both be `undefined` | `Option` | fixtures/reject/src/lib.rs:31:15 | | 1 | `_`: a JS value of `_` is a copy, so a reference can't change it | `&mut std::string::String`, `std::string::String` | fixtures/reject/src/lib.rs:73:16 | | 1 | `_`: a JS value of `_` is a copy, so a reference can't change it | `&mut u32`, `u32` | fixtures/reject/src/lib.rs:19:13 | | 1 | a `_` binding: a JS value of `_` is a copy, so it can't change | `&mut std::string::String`, `std::string::String` | fixtures/reject/src/lib.rs:73:16 | | 1 | a cast from `_` | `char` | fixtures/reject/src/lib.rs:11:2 | | 1 | a cast to `_` | `f64` | fixtures/reject/src/lib.rs:110:2 | | 1 | a mutable reference to `_`, which is no object it can change in place | `std::string::String` | fixtures/reject/src/lib.rs:75:17 | +| 1 | an iterator of the crate implementing `_`, which is not supported yet | `DoubleEndedIterator` | fixtures/reject/src/lib.rs:148:1 | | 1 | arithmetic on `_` | `f64` | fixtures/reject/src/lib.rs:7:2 | -| 1 | unsupported call to `_` | `::count` | fixtures/reject/src/lib.rs:40:31 | -| 1 | unsupported call to `_` | `::filter` | fixtures/reject/src/lib.rs:40:11 | +| 1 | overriding `_`, which std's adaptors call in place of `next` | `Iterator::fold` | fixtures/reject/src/lib.rs:128:2 | +| 1 | unsupported call to `_` | `::cycle` | fixtures/reject/src/lib.rs:115:11 | | 1 | unsupported call to `_` | `::write_fmt` | fixtures/reject/src/lib.rs:75:10 | -| 1 | unsupported call to `_` | `std::slice::iter` | fixtures/reject/src/lib.rs:40:4 | | 1 | unsupported call to `_` | `std::slice::sort` | fixtures/reject/src/lib.rs:36:4 | | 1 | unsupported literal of type `_` | `f64` | fixtures/reject/src/lib.rs:7:6 | | 1 | unsupported type `_` | `char` | fixtures/reject/src/lib.rs:10:18 | @@ -82,16 +85,6 @@ The type or module each refused type or path belongs to. ## Spans -
unsupported call to `_` (5) - -- fixtures/reject/src/lib.rs:36:4: unsupported call to `std::slice::::sort` -- fixtures/reject/src/lib.rs:40:4: unsupported call to `core::slice::::iter` -- fixtures/reject/src/lib.rs:40:11: unsupported call to ` as std::iter::Iterator>::filter::<{closure@fixtures/reject/src/lib.rs:40:18: 40:21}>` -- fixtures/reject/src/lib.rs:40:31: unsupported call to `, {closure@fixtures/reject/src/lib.rs:40:18: 40:21}> as std::iter::Iterator>::count` -- fixtures/reject/src/lib.rs:75:10: unsupported call to `::write_fmt` - -
-
unsupported type `_` (5) - fixtures/reject/src/lib.rs:6:1: unsupported type `f64` @@ -118,6 +111,21 @@ The type or module each refused type or path belongs to.
+
unsupported call to `_` (3) + +- fixtures/reject/src/lib.rs:36:4: unsupported call to `std::slice::::sort` +- fixtures/reject/src/lib.rs:75:10: unsupported call to `::write_fmt` +- fixtures/reject/src/lib.rs:115:11: unsupported call to ` as std::iter::Iterator>::cycle` + +
+ +
`_` is not supported: its `None` and `Some` would both be `undefined` (2) + +- fixtures/reject/src/lib.rs:31:15: `Option>` is not supported: its `None` and `Some` would both be `undefined` +- fixtures/reject/src/lib.rs:40:42: `Option>` is not supported: its `None` and `Some` would both be `undefined` + +
+
`_`: a JS value of `_` is a copy, so a reference can't change it (2) - fixtures/reject/src/lib.rs:19:13: `&mut u32`: a JS value of `u32` is a copy, so a reference can't change it @@ -161,12 +169,6 @@ The type or module each refused type or path belongs to.
-
`_` is not supported: its `None` and `Some` would both be `undefined` (1) - -- fixtures/reject/src/lib.rs:31:15: `Option>` is not supported: its `None` and `Some` would both be `undefined` - -
-
a cast from `_` (1) - fixtures/reject/src/lib.rs:11:2: a cast from `char` @@ -209,6 +211,12 @@ The type or module each refused type or path belongs to.
+
an iterator of the crate implementing `_`, which is not supported yet (1) + +- fixtures/reject/src/lib.rs:148:1: an iterator of the crate implementing `DoubleEndedIterator`, which is not supported yet + +
+
an or-pattern with bindings (1) - fixtures/reject/src/lib.rs:87:3: an or-pattern with bindings @@ -227,6 +235,12 @@ The type or module each refused type or path belongs to.
+
overriding `_`, which std's adaptors call in place of `next` (1) + +- fixtures/reject/src/lib.rs:128:2: overriding `Iterator::fold`, which std's adaptors call in place of `next` + +
+
the `+` flag of a format spec is not supported (1) - fixtures/reject/src/lib.rs:61:3: the `+` flag of a format spec is not supported diff --git a/ts/fixtures/reject/expected.txt b/ts/fixtures/reject/expected.txt index d9aae12b3..3951554e2 100644 --- a/ts/fixtures/reject/expected.txt +++ b/ts/fixtures/reject/expected.txt @@ -13,9 +13,7 @@ a `&mut u32` binding: a JS value of `u32` is a copy, so it can't change at fixtu a `&mut u32` binding: a JS value of `u32` is a copy, so it can't change at fixtures/reject/src/lib.rs:25:6 `Option>` is not supported: its `None` and `Some` would both be `undefined` at fixtures/reject/src/lib.rs:31:15 unsupported call to `std::slice::::sort` at fixtures/reject/src/lib.rs:36:4 -unsupported call to `core::slice::::iter` at fixtures/reject/src/lib.rs:40:4 -unsupported call to ` as std::iter::Iterator>::filter::<{closure@fixtures/reject/src/lib.rs:40:18: 40:21}>` at fixtures/reject/src/lib.rs:40:11 -unsupported call to `, {closure@fixtures/reject/src/lib.rs:40:18: 40:21}> as std::iter::Iterator>::count` at fixtures/reject/src/lib.rs:40:31 +`Option>` is not supported: its `None` and `Some` would both be `undefined` at fixtures/reject/src/lib.rs:40:42 `Guard` implements `Drop`, which is not supported yet at fixtures/reject/src/lib.rs:50:2 `later` is async, which is not supported yet at fixtures/reject/src/lib.rs:53:1 a precision, as in `{:.2}`, is not supported at fixtures/reject/src/lib.rs:60:3 @@ -37,3 +35,6 @@ unsupported expression: StaticRef at fixtures/reject/src/lib.rs:94:2 replacing the value behind a reference to `Meters` at fixtures/reject/src/lib.rs:99:17 replacing the value behind a reference to `Meters` at fixtures/reject/src/lib.rs:99:20 replacing the value behind a reference to `Meters` at fixtures/reject/src/lib.rs:103:25 +unsupported call to ` as std::iter::Iterator>::cycle` at fixtures/reject/src/lib.rs:115:11 +overriding `Iterator::fold`, which std's adaptors call in place of `next` at fixtures/reject/src/lib.rs:128:2 +an iterator of the crate implementing `DoubleEndedIterator`, which is not supported yet at fixtures/reject/src/lib.rs:148:1 diff --git a/ts/fixtures/reject/src/lib.rs b/ts/fixtures/reject/src/lib.rs index ea964364b..329858bac 100644 --- a/ts/fixtures/reject/src/lib.rs +++ b/ts/fixtures/reject/src/lib.rs @@ -36,8 +36,8 @@ pub fn sorted(v: &mut [u8]) { v.sort(); } -pub fn iterators(v: &[u8]) -> usize { - v.iter().filter(|b| **b > 1).count() +pub fn iterators(v: &[u8]) -> bool { + v.iter().map(|&b| (b > 1).then_some(b)).next().is_some() } pub struct Guard; @@ -109,3 +109,48 @@ pub fn generic>(t: &mut T) -> f64 { // A census lowers this over `T`: what hinges on `T` waits for an instance, the float does not. (*t).into() as f64 } + +/// An adaptor without a mapping. +pub fn cycled(v: &[u8]) -> usize { + v.iter().cycle().take(3).count() +} + +/// An iterator whose `fold` std's adaptors would call in place of its `next`. +pub struct Folds; + +impl Iterator for Folds { + type Item = u8; + + fn next(&mut self) -> Option { + None + } + + fn fold B>(self, init: B, _f: F) -> B { + init + } +} + +pub fn folds() -> usize { + Folds.map(|b| b + 1).count() +} + +/// An iterator with a `next_back`, which an adaptor over it would need. +pub struct Backs; + +impl Iterator for Backs { + type Item = u8; + + fn next(&mut self) -> Option { + None + } +} + +impl DoubleEndedIterator for Backs { + fn next_back(&mut self) -> Option { + None + } +} + +pub fn backs() -> usize { + Backs.rev().count() +} diff --git a/ts/fixtures/reject/ts.toml b/ts/fixtures/reject/ts.toml index 23b0b5341..0d2733a58 100644 --- a/ts/fixtures/reject/ts.toml +++ b/ts/fixtures/reject/ts.toml @@ -18,6 +18,9 @@ roots = [ "swapped", "replaced", "generic", + "cycled", + "folds", + "backs", "missing", ] printable = ["Meters"] diff --git a/ts/runtime/rt.ts b/ts/runtime/rt.ts index f65a8ab68..9715c38d7 100644 --- a/ts/runtime/rt.ts +++ b/ts/runtime/rt.ts @@ -4,6 +4,8 @@ // a debug build checks it: overflow panics, it never wraps silently. Every panic's // message is the one native Rust panics with. +export * from "./rt/iter.ts"; + /** A Rust panic. */ export class Panic extends Error { override name = "Panic"; diff --git a/ts/runtime/rt/iter.ts b/ts/runtime/rt/iter.ts new file mode 100644 index 000000000..62d6f52d4 --- /dev/null +++ b/ts/runtime/rt/iter.ts @@ -0,0 +1,1040 @@ +// Iterators, for rust2-ts output: std's `Iterator`, its adaptors, and `Range`, lazy as +// Rust's are. `rt.ts` re-exports all of it. +// +// `next` returns an item or `DONE`, never `undefined`, so an iterator of `Option` keeps +// its `None` items apart from its end. Each adaptor calls what it wraps, and the closures +// it was given, in the order and as many times as std's does, and stops where std's +// stops: a `find` or `any` reads no further than its match. + +import { ByteVec, Err, Ok, panic, type Result } from "../rt.ts"; + +/** What `next` returns once an iterator is exhausted, which no item is. */ +export const DONE: unique symbol = Symbol("DONE"); + +/** The type of `DONE`. */ +export type Done = typeof DONE; + +/** `Iterator`, with the provided methods std's iterators share. */ +export abstract class Iter implements Iterable { + /** The next item, or `DONE`. */ + abstract next(): T | Done; + + /** `DoubleEndedIterator::next_back`, which Rust calls only where std implements it. */ + nextBack(): T | Done { + throw new Error("rust2-ts: an iterator without `next_back`"); + } + + /** `ExactSizeIterator::len`, which Rust calls only where std implements it. */ + len(): number { + throw new Error("rust2-ts: an iterator without `len`"); + } + + /** Its items, for a JS `for ... of`. */ + *[Symbol.iterator](): Iterator { + for (let item = this.next(); item !== DONE; item = this.next()) yield item; + } + + // Adaptors + + map(f: (item: T) => U): Iter { + return new MapIter(this, f); + } + + filter(f: (item: T) => boolean): Iter { + return new FilterIter(this, f); + } + + filterMap(f: (item: T) => U | undefined): Iter { + return new FilterMapIter(this, f); + } + + enumerate(): Iter<[number, T]> { + return new EnumerateIter(this); + } + + /** `chain`, with `other` already made an iterator, as std does when it is called. */ + chain(other: Iter): Iter { + return new ChainIter(this, other); + } + + /** `zip`, with `other` already made an iterator, as std does when it is called. */ + zip(other: Iter): Iter<[T, U]> { + return new ZipIter(this, other); + } + + rev(): Iter { + return new RevIter(this); + } + + skip(n: number): Iter { + return new SkipIter(this, n); + } + + take(n: number): Iter { + return new TakeIter(this, n); + } + + skipWhile(f: (item: T) => boolean): Iter { + return new SkipWhileIter(this, f); + } + + takeWhile(f: (item: T) => boolean): Iter { + return new TakeWhileIter(this, f); + } + + mapWhile(f: (item: T) => U | undefined): Iter { + return new MapWhileIter(this, f); + } + + stepBy(step: number): Iter { + return new StepByIter(this, step); + } + + inspect(f: (item: T) => void): Iter { + return new InspectIter(this, f); + } + + fuse(): Iter { + return new FuseIter(this); + } + + peekable(): Peekable { + return new Peekable(this); + } + + /** `flat_map`: `into` makes each of `f`'s results an iterator, as `IntoIterator` does. */ + flatMap(f: (item: T) => Iter): Iter; + flatMap(f: (item: T) => U, into: (value: U) => Iter): Iter; + flatMap(f: (item: T) => U, into?: (value: U) => Iter): Iter { + return new FlattenIter(new MapIter(this, f), into ?? (itself as (value: U) => Iter)); + } + + /** `flatten`: `into` makes each item an iterator, as `IntoIterator` does. */ + flatten(this: Iter>): Iter; + flatten(into: (value: T) => Iter): Iter; + flatten(into?: (value: T) => Iter): Iter { + return new FlattenIter(this, into ?? (itself as (value: T) => Iter)); + } + + // Consumers + + count(): number { + let n = 0; + while (this.next() !== DONE) n++; + return n; + } + + last(): T | undefined { + let last: T | undefined; + for (let item = this.next(); item !== DONE; item = this.next()) last = item; + return last; + } + + /** `advance_by`: whether `n` items were there to skip. */ + advanceBy(n: number): boolean { + for (let i = 0; i < n; i++) if (this.next() === DONE) return false; + return true; + } + + /** `advance_back_by`. */ + advanceBackBy(n: number): boolean { + for (let i = 0; i < n; i++) if (this.nextBack() === DONE) return false; + return true; + } + + /** `nth`, as `next` returns it. */ + nthItem(n: number): T | Done { + return this.advanceBy(n) ? this.next() : DONE; + } + + /** `nth_back`, as `next_back` returns it. */ + nthBackItem(n: number): T | Done { + return this.advanceBackBy(n) ? this.nextBack() : DONE; + } + + nth(n: number): T | undefined { + return toOption(this.nthItem(n)); + } + + any(f: (item: T) => boolean): boolean { + for (let item = this.next(); item !== DONE; item = this.next()) if (f(item)) return true; + return false; + } + + all(f: (item: T) => boolean): boolean { + for (let item = this.next(); item !== DONE; item = this.next()) if (!f(item)) return false; + return true; + } + + /** `find`, as `next` returns it. */ + findItem(f: (item: T) => boolean): T | Done { + for (let item = this.next(); item !== DONE; item = this.next()) if (f(item)) return item; + return DONE; + } + + /** `rfind`, as `next_back` returns it. */ + rfindItem(f: (item: T) => boolean): T | Done { + for (let item = this.nextBack(); item !== DONE; item = this.nextBack()) if (f(item)) return item; + return DONE; + } + + find(f: (item: T) => boolean): T | undefined { + return toOption(this.findItem(f)); + } + + findMap(f: (item: T) => U | undefined): U | undefined { + for (let item = this.next(); item !== DONE; item = this.next()) { + const found = f(item); + if (found !== undefined) return found; + } + return undefined; + } + + position(f: (item: T) => boolean): number | undefined { + let i = 0; + for (let item = this.next(); item !== DONE; item = this.next()) { + if (f(item)) return i; + i++; + } + return undefined; + } + + fold(init: A, f: (acc: A, item: T) => A): A { + let acc = init; + for (let item = this.next(); item !== DONE; item = this.next()) acc = f(acc, item); + return acc; + } + + forEach(f: (item: T) => void): void { + for (let item = this.next(); item !== DONE; item = this.next()) f(item); + } + + /** `sum`, adding with an integer type's checked `add`, so overflow panics as Rust's does. */ + sum(add: (a: T, b: T) => T, zero: T): T { + return this.fold(zero, add); + } + + /** `product`, multiplying with an integer type's checked `mul`. */ + product(mul: (a: T, b: T) => T, one: T): T { + return this.fold(one, mul); + } + + /** `max` of integers or booleans: the last of the greatest, as `Ord::max` keeps it. */ + max(this: Iter): K | undefined { + return this.maxByKey((item) => item); + } + + /** `min` of integers or booleans: the first of the least. */ + min(this: Iter): K | undefined { + return this.minByKey((item) => item); + } + + /** `max_by_key` with an integer or boolean key, each item's taken once. */ + maxByKey(f: (item: T) => K): T | undefined { + let best = this.next(); + if (best === DONE) return undefined; + let key = f(best); + for (let item = this.next(); item !== DONE; item = this.next()) { + const k = f(item); + if (k >= key) [best, key] = [item, k]; + } + return best; + } + + /** `min_by_key` with an integer or boolean key, each item's taken once. */ + minByKey(f: (item: T) => K): T | undefined { + let best = this.next(); + if (best === DONE) return undefined; + let key = f(best); + for (let item = this.next(); item !== DONE; item = this.next()) { + const k = f(item); + if (k < key) [best, key] = [item, k]; + } + return best; + } + + /** `collect` into a `Vec`. */ + toArray(): T[] { + const out: T[] = []; + for (let item = this.next(); item !== DONE; item = this.next()) out.push(item); + return out; + } + + /** `collect` into a `Vec`. */ + toByteVec(this: Iter): ByteVec { + const out = new ByteVec(); + for (let item = this.next(); item !== DONE; item = this.next()) out.push(item); + return out; + } + + /** `collect` of `Result`s: the first error, or every value collected by `into`. */ + collectResult(this: Iter>, into: (values: Iter) => C): Result { + const shunt = new ResultShunt(this); + const values = into(shunt); + return shunt.error === undefined ? Ok(values) : Err(shunt.error.value); + } + + /** `collect` of `Option`s: `None` at the first `None`, or every value collected by `into`. */ + collectOption(this: Iter, into: (values: Iter) => C): C | undefined { + const shunt = new OptionShunt(this); + const values = into(shunt); + return shunt.none ? undefined : values; + } +} + +/** The item `next` returned, as an `Option`. */ +export function toOption(item: T | Done): T | undefined { + return item === DONE ? undefined : item; +} + +/** An `Option`, as `next` returns it. */ +export function fromOption(option: T | undefined): T | Done { + return option === undefined ? DONE : option; +} + +/** Where std's iterators come from. */ +export const iter = { + toOption, + fromOption, + /** `slice::Iter`, and what iterating a `Vec` or array by value makes. */ + slice(s: ArrayLike): Iter { + return new SliceIter(s); + }, + /** `Option::iter`, and `option::IntoIter`. */ + option(o: T | undefined): Iter { + return new OptionIter(o); + }, + once(value: T): Iter { + return new OptionIter(value, true); + }, + empty(): Iter { + return new OptionIter(undefined, false); + }, + /** `iter::from_fn`, whose closure returns an `Option`. */ + fromFn(f: () => T | undefined): Iter { + return new FromNext(f, (f) => fromOption(f())); + }, + /** An `impl Iterator` of the translated crate, which `next` calls the `next` of. */ + from(source: S, next: (source: S) => T | Done): Iter { + return new FromNext(source, next); + }, + /** `Extend::extend` of a `Vec`. */ + extend(v: { push(item: T): void }, items: Iter): void { + for (let item = items.next(); item !== DONE; item = items.next()) v.push(item); + }, +}; + +class SliceIter extends Iter { + private readonly s: ArrayLike; + private front = 0; + private back: number; + + constructor(s: ArrayLike) { + super(); + this.s = s; + this.back = s.length; + } + + next(): T | Done { + return this.front < this.back ? this.s[this.front++] : DONE; + } + + override nextBack(): T | Done { + return this.front < this.back ? this.s[--this.back] : DONE; + } + + override len(): number { + return this.back - this.front; + } +} + +/** `option::IntoIter`, `Once`, and `Empty`: at most one item. */ +class OptionIter extends Iter { + private item: T | undefined; + private some: boolean; + + constructor(item: T | undefined, some = item !== undefined) { + super(); + this.item = item; + this.some = some; + } + + next(): T | Done { + if (!this.some) return DONE; + this.some = false; + const item = this.item as T; + this.item = undefined; + return item; + } + + override nextBack(): T | Done { + return this.next(); + } + + override len(): number { + return this.some ? 1 : 0; + } +} + +/** An iterator whose `next` is a function of its source. */ +class FromNext extends Iter { + private readonly source: S; + private readonly step: (source: S) => T | Done; + + constructor(source: S, step: (source: S) => T | Done) { + super(); + this.source = source; + this.step = step; + } + + next(): T | Done { + return this.step(this.source); + } +} + +class MapIter extends Iter { + private readonly inner: Iter; + private readonly f: (item: T) => U; + + constructor(inner: Iter, f: (item: T) => U) { + super(); + this.inner = inner; + this.f = f; + } + + next(): U | Done { + const item = this.inner.next(); + return item === DONE ? DONE : this.f(item); + } + + override nextBack(): U | Done { + const item = this.inner.nextBack(); + return item === DONE ? DONE : this.f(item); + } + + override len(): number { + return this.inner.len(); + } +} + +class FilterIter extends Iter { + private readonly inner: Iter; + private readonly f: (item: T) => boolean; + + constructor(inner: Iter, f: (item: T) => boolean) { + super(); + this.inner = inner; + this.f = f; + } + + next(): T | Done { + return this.inner.findItem(this.f); + } + + override nextBack(): T | Done { + return this.inner.rfindItem(this.f); + } +} + +class FilterMapIter extends Iter { + private readonly inner: Iter; + private readonly f: (item: T) => U | undefined; + + constructor(inner: Iter, f: (item: T) => U | undefined) { + super(); + this.inner = inner; + this.f = f; + } + + next(): U | Done { + for (let item = this.inner.next(); item !== DONE; item = this.inner.next()) { + const mapped = this.f(item); + if (mapped !== undefined) return mapped; + } + return DONE; + } + + override nextBack(): U | Done { + for (let item = this.inner.nextBack(); item !== DONE; item = this.inner.nextBack()) { + const mapped = this.f(item); + if (mapped !== undefined) return mapped; + } + return DONE; + } +} + +class EnumerateIter extends Iter<[number, T]> { + private readonly inner: Iter; + private index = 0; + + constructor(inner: Iter) { + super(); + this.inner = inner; + } + + next(): [number, T] | Done { + const item = this.inner.next(); + return item === DONE ? DONE : [this.index++, item]; + } + + override nextBack(): [number, T] | Done { + const item = this.inner.nextBack(); + return item === DONE ? DONE : [this.index + this.inner.len(), item]; + } + + override len(): number { + return this.inner.len(); + } +} + +/** `Chain`, which stops reading its first iterator once it ends, but not its second. */ +class ChainIter extends Iter { + private a: Iter | undefined; + private b: Iter | undefined; + + constructor(a: Iter, b: Iter) { + super(); + this.a = a; + this.b = b; + } + + next(): T | Done { + if (this.a !== undefined) { + const item = this.a.next(); + if (item !== DONE) return item; + this.a = undefined; + } + return this.b === undefined ? DONE : this.b.next(); + } + + override nextBack(): T | Done { + if (this.b !== undefined) { + const item = this.b.nextBack(); + if (item !== DONE) return item; + this.b = undefined; + } + return this.a === undefined ? DONE : this.a.nextBack(); + } +} + +class ZipIter extends Iter<[A, B]> { + private readonly a: Iter; + private readonly b: Iter; + + constructor(a: Iter, b: Iter) { + super(); + this.a = a; + this.b = b; + } + + next(): [A, B] | Done { + const a = this.a.next(); + if (a === DONE) return DONE; + const b = this.b.next(); + return b === DONE ? DONE : [a, b]; + } + + /** std's: the longer side drops its extra items from the back first. */ + override nextBack(): [A, B] | Done { + const aLen = this.a.len(); + const bLen = this.b.len(); + for (let i = bLen; i < aLen; i++) this.a.nextBack(); + for (let i = aLen; i < bLen; i++) this.b.nextBack(); + const a = this.a.nextBack(); + const b = this.b.nextBack(); + if (a === DONE && b === DONE) return DONE; + if (a === DONE || b === DONE) panic("internal error: entered unreachable code"); + return [a, b]; + } + + override len(): number { + return Math.min(this.a.len(), this.b.len()); + } +} + +class RevIter extends Iter { + private readonly inner: Iter; + + constructor(inner: Iter) { + super(); + this.inner = inner; + } + + next(): T | Done { + return this.inner.nextBack(); + } + + override nextBack(): T | Done { + return this.inner.next(); + } + + override len(): number { + return this.inner.len(); + } +} + +class SkipIter extends Iter { + private readonly inner: Iter; + private n: number; + + constructor(inner: Iter, n: number) { + super(); + this.inner = inner; + this.n = n; + } + + next(): T | Done { + if (this.n === 0) return this.inner.next(); + const n = this.n; + this.n = 0; + return this.inner.nthItem(n); + } + + override nextBack(): T | Done { + return this.len() > 0 ? this.inner.nextBack() : DONE; + } + + override len(): number { + return Math.max(this.inner.len() - this.n, 0); + } +} + +class TakeIter extends Iter { + private readonly inner: Iter; + private n: number; + + constructor(inner: Iter, n: number) { + super(); + this.inner = inner; + this.n = n; + } + + next(): T | Done { + if (this.n === 0) return DONE; + this.n--; + return this.inner.next(); + } + + override nextBack(): T | Done { + if (this.n === 0) return DONE; + const n = this.n--; + return this.inner.nthBackItem(Math.max(this.inner.len() - n, 0)); + } + + override len(): number { + return Math.min(this.inner.len(), this.n); + } +} + +class SkipWhileIter extends Iter { + private readonly inner: Iter; + private readonly f: (item: T) => boolean; + private flag = false; + + constructor(inner: Iter, f: (item: T) => boolean) { + super(); + this.inner = inner; + this.f = f; + } + + next(): T | Done { + return this.inner.findItem((item) => { + if (this.flag || !this.f(item)) { + this.flag = true; + return true; + } + return false; + }); + } +} + +class TakeWhileIter extends Iter { + private readonly inner: Iter; + private readonly f: (item: T) => boolean; + private flag = false; + + constructor(inner: Iter, f: (item: T) => boolean) { + super(); + this.inner = inner; + this.f = f; + } + + next(): T | Done { + if (this.flag) return DONE; + const item = this.inner.next(); + if (item === DONE) return DONE; + if (this.f(item)) return item; + this.flag = true; + return DONE; + } +} + +/** `MapWhile`, which reads on after `f` returns `None` if called again, as std's does. */ +class MapWhileIter extends Iter { + private readonly inner: Iter; + private readonly f: (item: T) => U | undefined; + + constructor(inner: Iter, f: (item: T) => U | undefined) { + super(); + this.inner = inner; + this.f = f; + } + + next(): U | Done { + const item = this.inner.next(); + return item === DONE ? DONE : fromOption(this.f(item)); + } +} + +class StepByIter extends Iter { + private readonly inner: Iter; + private readonly step: number; + private firstTake = true; + + constructor(inner: Iter, step: number) { + super(); + if (step === 0) panic("assertion failed: step != 0"); + this.inner = inner; + this.step = step - 1; + } + + next(): T | Done { + const n = this.firstTake ? 0 : this.step; + this.firstTake = false; + return this.inner.nthItem(n); + } + + override nextBack(): T | Done { + const rem = this.inner.len() % (this.step + 1); + const n = this.firstTake ? (rem === 0 ? this.step : rem - 1) : rem; + return this.inner.nthBackItem(n); + } + + override len(): number { + const n = this.inner.len(); + if (this.firstTake) return n === 0 ? 0 : 1 + Math.floor((n - 1) / (this.step + 1)); + return Math.floor(n / (this.step + 1)); + } +} + +class InspectIter extends Iter { + private readonly inner: Iter; + private readonly f: (item: T) => void; + + constructor(inner: Iter, f: (item: T) => void) { + super(); + this.inner = inner; + this.f = f; + } + + next(): T | Done { + const item = this.inner.next(); + if (item !== DONE) this.f(item); + return item; + } + + override nextBack(): T | Done { + const item = this.inner.nextBack(); + if (item !== DONE) this.f(item); + return item; + } + + override len(): number { + return this.inner.len(); + } +} + +class FuseIter extends Iter { + private inner: Iter | undefined; + + constructor(inner: Iter) { + super(); + this.inner = inner; + } + + next(): T | Done { + if (this.inner === undefined) return DONE; + const item = this.inner.next(); + if (item === DONE) this.inner = undefined; + return item; + } + + override nextBack(): T | Done { + if (this.inner === undefined) return DONE; + const item = this.inner.nextBack(); + if (item === DONE) this.inner = undefined; + return item; + } + + override len(): number { + return this.inner === undefined ? 0 : this.inner.len(); + } +} + +/** `Flatten` and `FlatMap`, which stop reading their outer iterator once it ends. */ +class FlattenIter extends Iter { + private outer: Iter | undefined; + private readonly into: (value: T) => Iter; + private front: Iter | undefined; + private back: Iter | undefined; + + constructor(outer: Iter, into: (value: T) => Iter) { + super(); + this.outer = outer; + this.into = into; + } + + next(): V | Done { + for (;;) { + if (this.front !== undefined) { + const item = this.front.next(); + if (item !== DONE) return item; + this.front = undefined; + } + const inner = this.outer === undefined ? DONE : this.outer.next(); + if (inner === DONE) { + this.outer = undefined; + if (this.back === undefined) return DONE; + const item = this.back.next(); + if (item === DONE) this.back = undefined; + return item; + } + this.front = this.into(inner); + } + } + + override nextBack(): V | Done { + for (;;) { + if (this.back !== undefined) { + const item = this.back.nextBack(); + if (item !== DONE) return item; + this.back = undefined; + } + const inner = this.outer === undefined ? DONE : this.outer.nextBack(); + if (inner === DONE) { + this.outer = undefined; + if (this.front === undefined) return DONE; + const item = this.front.nextBack(); + if (item === DONE) this.front = undefined; + return item; + } + this.back = this.into(inner); + } + } +} + +/** `Peekable`. */ +export class Peekable extends Iter { + private readonly inner: Iter; + private peeked = false; + private item: T | Done = DONE; + + constructor(inner: Iter) { + super(); + this.inner = inner; + } + + next(): T | Done { + if (!this.peeked) return this.inner.next(); + this.peeked = false; + return this.item; + } + + override nextBack(): T | Done { + if (!this.peeked) return this.inner.nextBack(); + if (this.item === DONE) return DONE; + const item = this.inner.nextBack(); + if (item !== DONE) return item; + // The peeked item is the last: taking it leaves the end peeked. + const peeked = this.item; + this.item = DONE; + return peeked; + } + + override len(): number { + if (!this.peeked) return this.inner.len(); + return this.item === DONE ? 0 : this.inner.len() + 1; + } + + /** `peek`, as `next` returns it. */ + peekItem(): T | Done { + if (!this.peeked) { + this.item = this.inner.next(); + this.peeked = true; + } + return this.item; + } + + peek(): T | undefined { + return toOption(this.peekItem()); + } + + /** `next_if`, as `next` returns it. */ + nextIfItem(f: (item: T) => boolean): T | Done { + const item = this.next(); + if (item !== DONE && f(item)) return item; + this.peeked = true; + this.item = item; + return DONE; + } + + nextIf(f: (item: T) => boolean): T | undefined { + return toOption(this.nextIfItem(f)); + } +} + +/** What `collectResult` collects from: values until the first error, which it keeps. */ +class ResultShunt extends Iter { + private readonly inner: Iter>; + error: { value: E } | undefined; + + constructor(inner: Iter>) { + super(); + this.inner = inner; + } + + next(): U | Done { + if (this.error !== undefined) return DONE; + const item = this.inner.next(); + if (item === DONE) return DONE; + if (item.ok) return item.value; + this.error = { value: item.error }; + return DONE; + } +} + +/** What `collectOption` collects from: values until the first `None`. */ +class OptionShunt extends Iter { + private readonly inner: Iter; + none = false; + + constructor(inner: Iter) { + super(); + this.inner = inner; + } + + next(): U | Done { + if (this.none) return DONE; + const item = this.inner.next(); + if (item === DONE) return DONE; + if (item !== undefined) return item; + this.none = true; + return DONE; + } +} + +/** `a < b` of integers, numbers or bigints alike. */ +function lt(a: T, b: T): boolean { + return (a as number) < (b as number); +} + +/** One past `n`, in its own representation. */ +function succ(n: T): T { + return (typeof n === "bigint" ? n + 1n : (n as number) + 1) as T; +} + +/** One before `n`, in its own representation. */ +function pred(n: T): T { + return (typeof n === "bigint" ? n - 1n : (n as number) - 1) as T; +} + +/** `ops::Range`: `start..end`, which iterates its integers by moving `start`. */ +export class Range extends Iter { + start: T; + end: T; + + constructor(start: T, end: T) { + super(); + this.start = start; + this.end = end; + } + + next(): T | Done { + const start = this.start; + if (!lt(start, this.end)) return DONE; + this.start = succ(start); + return start; + } + + override nextBack(): T | Done { + if (!lt(this.start, this.end)) return DONE; + this.end = pred(this.end); + return this.end; + } + + override len(): number { + return lt(this.start, this.end) ? Number(this.end) - Number(this.start) : 0; + } + + isEmpty(): boolean { + return !lt(this.start, this.end); + } + + contains(item: T): boolean { + return !lt(item, this.start) && lt(item, this.end); + } + + clone(): Range { + return new Range(this.start, this.end); + } +} + +/** `ops::RangeInclusive`: `start..=end`, which remembers once it has yielded `end`. */ +export class RangeInclusive extends Iter { + private lo: T; + private hi: T; + private exhausted = false; + + constructor(start: T, end: T) { + super(); + this.lo = start; + this.hi = end; + } + + start(): T { + return this.lo; + } + + end(): T { + return this.hi; + } + + isEmpty(): boolean { + return this.exhausted || lt(this.hi, this.lo); + } + + contains(item: T): boolean { + return !lt(item, this.lo) && (this.exhausted ? lt(item, this.hi) : !lt(this.hi, item)); + } + + clone(): RangeInclusive { + const range = new RangeInclusive(this.lo, this.hi); + range.exhausted = this.exhausted; + return range; + } + + next(): T | Done { + if (this.isEmpty()) return DONE; + const lo = this.lo; + if (lt(lo, this.hi)) this.lo = succ(lo); + else this.exhausted = true; + return lo; + } + + override nextBack(): T | Done { + if (this.isEmpty()) return DONE; + const hi = this.hi; + if (lt(this.lo, hi)) this.hi = pred(hi); + else this.exhausted = true; + return hi; + } + + override len(): number { + return this.isEmpty() ? 0 : Number(this.hi) - Number(this.lo) + 1; + } +} + +/** An iterator as itself: what `into_iter` makes of one. */ +function itself(iter: Iter): Iter { + return iter; +} diff --git a/ts/runtime/tsconfig.json b/ts/runtime/tsconfig.json index 781df47f4..b45ac6972 100644 --- a/ts/runtime/tsconfig.json +++ b/ts/runtime/tsconfig.json @@ -11,5 +11,5 @@ "verbatimModuleSyntax": true, "erasableSyntaxOnly": true }, - "files": ["rt.ts"] + "files": ["rt.ts", "rt/iter.ts"] } diff --git a/ts/src/census.rs b/ts/src/census.rs index 090b1b11e..e17b92378 100644 --- a/ts/src/census.rs +++ b/ts/src/census.rs @@ -157,6 +157,7 @@ const VERBATIM: &[&str] = &[ "if", "let", "let ... else", + "next", "ref mut", "self", "undefined", diff --git a/ts/src/lower/body.rs b/ts/src/lower/body.rs index 8524219a0..8d216c194 100644 --- a/ts/src/lower/body.rs +++ b/ts/src/lower/body.rs @@ -42,8 +42,12 @@ pub fn function<'a, 'tcx>( breakables: Vec::new(), ret: sig.output(), visited: FxHashSet::default(), + done: super::iter::is_next(tcx, def_id), }; let span = tcx.def_span(def_id); + if b.done { + super::iter::check_impl(b.t, def_id); + } let mut body = Vec::new(); let mut params = Vec::new(); for (i, param) in thir.params.iter().enumerate() { @@ -90,7 +94,10 @@ pub fn function<'a, 'tcx>( false => Dest::Return, }; b.stmt(expr, dest, &mut body); - let ret = b.t.ret_ty(ret, span); + let ret = match (b.done, ret.kind()) { + (true, ty::Adt(_, args)) => Type::Union(vec![b.t.ty(args.type_at(0), span), Type::Rt("Done", Vec::new())]), + _ => b.t.ret_ty(ret, span), + }; Some(ts::Function { sym: 0, method, @@ -147,6 +154,9 @@ pub struct Body<'b, 'a, 'tcx> { /// Expressions lowered so far, by THIR and id, so a refused call lowers only the /// arguments its mapping did not. visited: FxHashSet<(*const Thir<'tcx>, ExprId)>, + /// Whether this is an `Iterator::next`, which returns an item or `rt.DONE` rather + /// than an `Option`: its closures are not. + done: bool, } /// One `&&`-joined part of an `if` condition. @@ -311,6 +321,20 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { self.push(out, StmtKind::Expr(value), span); } } + Dest::Return if self.done => { + let ty::Adt(_, args) = self.ret.kind() else { + unreachable!("Iterator::next returns an Option") + }; + if !self.t.option_payload_ok(args.type_at(0)) { + let ret = self.ret; + self.t.err( + span, + format!("`{ret}` is not supported: its `None` and `Some` would both be `undefined`"), + ); + } + let value = Expr::Rt("iter").member("fromOption").call(vec![value]); + self.push(out, StmtKind::Return(Some(value)), span); + } Dest::Return => self.push(out, StmtKind::Return(Some(value.typed())), span), Dest::Assign(name) => self.push(out, StmtKind::Expr(Expr::ident(name).assign(value.typed())), span), } @@ -375,12 +399,43 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { ExprKind::Assign { lhs, rhs } => self.assign(*lhs, *rhs, span, out), ExprKind::AssignOp { op, lhs, rhs } => self.assign_op(*op, *lhs, *rhs, span, out), _ => { + if self.done + && let Dest::Return = dest + && let Some(value) = self.done_value(id, out) + { + self.push(out, StmtKind::Return(Some(value)), span); + return; + } let value = self.expr(id, out); self.deliver(value, &dest, span, out); } } } + /// What an `Iterator::next` returns for `id` without an `Option` between: `rt.DONE` + /// for `None`, the item for `Some(item)`, and another iterator's `next` as is. + fn done_value(&mut self, id: ExprId, out: &mut Vec) -> Option { + match &self.thir[id].kind { + ExprKind::Adt(adt) if library::Std::of(self.t.tcx, adt.adt_def.did()) == Some(library::Std::Option) => { + self.visit(id); + Some(match adt.fields.first() { + None => Expr::Rt("DONE"), + Some(field) => self.operand(field.expr, out).typed(), + }) + } + ExprKind::Call { ty, args, .. } + if let ty::FnDef(def_id, _) = *self.subst(*ty).kind() + && super::iter::is_next(self.t.tcx, def_id) => + { + self.visit(id); + let recv_ty = self.ty_of(args[0]); + let recv = self.operand(args[0], out); + Some(super::iter::next(self, recv_ty, recv)) + } + _ => None, + } + } + fn block(&mut self, id: BlockId, dest: Dest, out: &mut Vec) { let block = &self.thir[id]; if block.targeted_by_break { @@ -1151,7 +1206,13 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { out, StmtKind::If { cond: t.clone().binary("===", Expr::undefined()), - then: vec![StmtKind::Return(Some(Expr::undefined())).at(None)], + then: vec![ + StmtKind::Return(Some(match self.done { + true => Expr::Rt("DONE"), + false => Expr::undefined(), + })) + .at(None), + ], els: Vec::new(), }, span, @@ -1316,6 +1377,24 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { iter, body: stmts, }, + // `while (true) { const x = iter.next(); if (x === rt.DONE) break; ... }` + Iterable::Next(next) => { + let mut body = vec![ + StmtKind::decl(false, name.clone(), None, Some(next)).at(None), + StmtKind::If { + cond: Expr::ident(&name).binary("===", Expr::Rt("DONE")), + then: vec![StmtKind::Break(None).at(None)], + els: Vec::new(), + } + .at(None), + ]; + body.extend(stmts); + StmtKind::While { + label, + cond: Expr::lit("true"), + body, + } + } }; self.push(out, kind, span); } @@ -1380,36 +1459,52 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { } // An array by value iterates its elements, already copied. ty::Array(..) => Some(Iterable::Of(self.operand(id, out))), - ty::Ref(_, inner, _) => match inner.kind() { - ty::Slice(_) | ty::Array(..) => Some(Iterable::Of(self.operand(id, out))), - ty::Adt(adt, _) if library::Std::of(tcx, adt.did()) == Some(library::Std::Vec) => { - let v = self.operand(id, out); - Some(Iterable::Of(library::vec_view(v, *inner))) - } - _ => { - self.t.err(span, format!("a `for` loop over `{ty}`")); - None - } - }, - ty::Adt(adt, _) if library::Std::of(tcx, adt.did()) == Some(library::Std::Vec) => { + ty::Ref(_, inner, _) if matches!(inner.kind(), ty::Slice(_) | ty::Array(..)) => { + Some(Iterable::Of(self.operand(id, out))) + } + ty::Ref(_, inner, _) if self.is_vec(*inner) => { + let v = self.operand(id, out); + Some(Iterable::Of(library::vec_view(v, *inner))) + } + ty::Adt(..) if self.is_vec(ty) => { let v = self.operand(id, out); Some(Iterable::Of(library::vec_view(v, ty))) } - ty::Adt(adt, _) if library::is_slice_iter(tcx, adt.did()) => { - // `slice.iter()`: iterate the slice itself. - let ExprKind::Call { args: call_args, .. } = &self.thir[self.strip(id)].kind else { - self.t.err(span, format!("a `for` loop over `{ty}`")); - return None; - }; - Some(Iterable::Of(self.operand(call_args[0], out))) + // `slice.iter()` iterates as the slice itself does. + _ if let Some(slice) = library::iterated_slice(self, id) => { + self.visit(id); + Some(Iterable::Of(self.operand(slice, out))) } + // Any other iterator, by the protocol. _ => { - self.t.err(span, format!("a `for` loop over `{ty}`")); - None + let value = self.operand(id, out); + let iterator = self.t.iter_item(ty).is_some(); + let value = match iterator { + true => value, + false => match super::iter::into_iter(self, ty, value, span) { + Some(value) => value, + None => { + self.t.err(span, format!("a `for` loop over `{ty}`")); + return None; + } + }, + }; + let iter = match value.is_path() { + true => value, + false => self.spill(value, "iter", out), + }; + Some(Iterable::Next(match iterator { + true => super::iter::next(self, ty, iter), + false => iter.member("next").call(Vec::new()), + })) } } } + fn is_vec(&self, ty: Ty<'tcx>) -> bool { + matches!(ty.kind(), ty::Adt(adt, _) if library::Std::of(self.t.tcx, adt.did()) == Some(library::Std::Vec)) + } + pub fn spill(&mut self, value: Expr, base: &str, out: &mut Vec) -> Expr { let name = self.fresh(base); out.push(StmtKind::decl(false, name.clone(), None, Some(value)).at(None)); @@ -1969,6 +2064,7 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { _ => base.member("error"), }, Some(library::Std::Box) => base, + Some(library::Std::Range) => base.member(adt.non_enum_variant().fields[field].name.as_str()), None if adt.did().is_local() => { let info = self.t.adt(*adt, span); match info.repr { @@ -1998,6 +2094,15 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { (library::Std::Result, 0) => Expr::rt("Ok", values), (library::Std::Result, _) => Expr::rt("Err", values), (library::Std::FmtError, _) => Expr::New(Box::new(Expr::Rt("FmtError")), Vec::new()), + (library::Std::Range, _) => { + let mut ordered: Vec<(usize, Expr)> = + adt.fields.iter().map(|f| f.name.index()).zip(values).collect(); + ordered.sort_by_key(|(i, _)| *i); + Expr::New( + Box::new(Expr::Rt("Range")), + ordered.into_iter().map(|(_, v)| v).collect(), + ) + } (library::Std::AssertKind, _) => match variant.name.as_str() { "Eq" => Expr::Str("==".into()), "Ne" => Expr::Str("!=".into()), @@ -2131,6 +2236,7 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { } }; let saved_ret = std::mem::replace(&mut self.ret, sig.output()); + let saved_done = std::mem::replace(&mut self.done, false); let mut body_stmts = Vec::new(); let mut params = Vec::new(); // The first parameter is the closure's environment. @@ -2153,6 +2259,7 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { self.thir = saved_thir; self.breakables = saved_breakables; self.ret = saved_ret; + self.done = saved_done; Expr::Arrow(params, ret_ty, body_stmts) } @@ -2231,7 +2338,7 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { { let sym = self.t.instance(instance); let values: Vec = self.args(args, out).into_iter().map(Expr::typed).collect(); - return match self.t.program.syms[sym].kind { + let call = match self.t.program.syms[sym].kind { SymKind::Method { instance: true } => { let mut values = values; let recv = values.remove(0); @@ -2239,6 +2346,13 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { } _ => Expr::Sym(sym).call(values), }; + // The crate's own `next` returns an item or `rt.DONE`, and its caller an `Option`. + if super::iter::is_next(tcx, did) { + let sig = tcx.fn_sig(did).instantiate(tcx, instance.args).skip_norm_wip(); + self.t.ty(tcx.instantiate_bound_regions_with_erased(sig).output(), span); + return Expr::Rt("iter").member("toOption").call(vec![call]); + } + return call; } let call = library::Call { def_id, @@ -2395,8 +2509,15 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { } enum Iterable { - Range { start: Expr, end: Expr, inclusive: bool }, + Range { + start: Expr, + end: Expr, + inclusive: bool, + }, + /// A JS array or `Uint8Array`, which `for ... of` iterates. Of(Expr), + /// An iterator, by its `next`: an item or `rt.DONE`. + Next(Expr), } /// The variables `thir` reads, those its closures capture included. diff --git a/ts/src/lower/iter.rs b/ts/src/lower/iter.rs new file mode 100644 index 000000000..705568b2d --- /dev/null +++ b/ts/src/lower/iter.rs @@ -0,0 +1,360 @@ +//! Iterators, as `rt/iter.ts` gives them: std's iterators are `rt.Iter` objects whose +//! adaptors are its methods, and whose `next` returns an item or `rt.DONE`. The crate's +//! own `impl Iterator` keeps that protocol in its `next`, and joins std's adaptors through +//! `rt.iter.from`. + +use rustc_hir::def_id::DefId; +use rustc_middle::ty::{self, Instance, Ty}; +use rustc_span::Span; + +use super::Translator; +use super::body::Body; +use super::library::{self, Args, Call, Std}; +use super::num::Int; +use crate::program::SymKind; +use crate::ts::{Expr, Param, Stmt, Type}; + +/// `Iterator::next`, and the trait's `Item`. +fn trait_items(tcx: ty::TyCtxt<'_>) -> Option<(DefId, DefId)> { + let iterator = tcx.get_diagnostic_item(rustc_span::sym::Iterator)?; + let items = tcx.associated_items(iterator); + let next = items.filter_by_name_unhygienic(rustc_span::sym::next).next()?.def_id; + let item = items.filter_by_name_unhygienic(rustc_span::sym::Item).next()?.def_id; + Some((next, item)) +} + +/// Whether `def_id` is `Iterator::next`, or an impl's. +pub fn is_next(tcx: ty::TyCtxt<'_>, def_id: DefId) -> bool { + let Some((next, _)) = trait_items(tcx) else { + return false; + }; + def_id == next || tcx.trait_item_of(def_id) == Some(next) +} + +impl<'tcx> Translator<'_, 'tcx> { + /// `Iterator::Item` of `ty`, if `ty` is an iterator. + pub fn iter_item(&self, ty: Ty<'tcx>) -> Option> { + let tcx = self.tcx; + let (next, item) = trait_items(tcx)?; + let env = self.typing_env_pub(); + Instance::try_resolve(tcx, env, next, tcx.mk_args(&[ty.into()])).ok()??; + let projection = Ty::new_projection(tcx, ty::IsRigid::No, item, [ty]); + tcx.try_normalize_erasing_regions(env, ty::Unnormalized::new_wip(projection)) + .ok() + } +} + +/// The crate's own iterator a `ty` is, through any references: its `next`, whose +/// instance the translation names. +fn own_next<'tcx>(b: &Body<'_, '_, 'tcx>, ty: Ty<'tcx>) -> Option> { + let tcx = b.t.tcx; + let ty = ty.peel_refs(); + if !matches!(ty.kind(), ty::Adt(adt, _) if adt.did().is_local()) { + return None; + } + let (next, _) = trait_items(tcx)?; + Instance::try_resolve(tcx, b.t.typing_env_pub(), next, tcx.mk_args(&[ty.into()])).ok()? +} + +/// Refuse an `impl Iterator` of the crate, whose `next` is `next_fn`, that std's adaptors +/// would read other than through `next`: one that overrides `fold` or `nth`, which they +/// call in its place, or that has a `next_back` or `len`, which this protocol lacks. +pub fn check_impl(t: &mut Translator<'_, '_>, next_fn: DefId) { + let tcx = t.tcx; + let Some(imp) = tcx.impl_of_assoc(next_fn) else { + return; + }; + for item in tcx.associated_items(imp).in_definition_order() { + let name = item.name(); + if item.is_fn() && name != rustc_span::sym::next && name.as_str() != "size_hint" { + t.err( + tcx.def_span(item.def_id), + format!("overriding `Iterator::{name}`, which std's adaptors call in place of `next`"), + ); + } + } + let ty::Adt(adt, _) = tcx.type_of(imp).instantiate_identity().skip_norm_wip().kind() else { + return; + }; + for (&tr, impls) in tcx.all_local_trait_impls(()) { + let path = library::std_path(tcx, tr); + let Some(name) = path + .last() + .filter(|n| matches!(n.as_str(), "DoubleEndedIterator" | "ExactSizeIterator")) + else { + continue; + }; + for other in impls { + let self_ty = tcx.type_of(other.to_def_id()).instantiate_identity().skip_norm_wip(); + if matches!(self_ty.kind(), ty::Adt(other, _) if other.did() == adt.did()) { + t.err( + tcx.def_span(other.to_def_id()), + format!("an iterator of the crate implementing `{name}`, which is not supported yet"), + ); + } + } + } +} + +/// `iter.next()` by the protocol, for an iterator of type `ty`: an item or `rt.DONE`. +pub fn next<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, iter: Expr) -> Expr { + match own_next(b, ty) { + Some(instance) => { + let sym = b.t.instance(instance); + Expr::Method(Box::new(iter), sym, Vec::new()) + } + None => iter.member("next").call(Vec::new()), + } +} + +/// What `IntoIterator::into_iter` makes of `value`, a `ty`: an `rt.Iter`, or `None` for a +/// type without a mapping. +pub fn into_iter<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, value: Expr, span: Span) -> Option { + let tcx = b.t.tcx; + if b.t.iter_item(ty).is_some() { + return Some(match own_next(b, ty) { + Some(instance) => { + let sym = b.t.instance(instance); + let param_ty = b.t.ty(ty.peel_refs(), span); + let it = "it".to_string(); + let next = match b.t.program.syms[sym].kind { + SymKind::Method { instance: true } => Expr::Method(Box::new(Expr::ident(&it)), sym, Vec::new()), + _ => Expr::Sym(sym).call(vec![Expr::ident(&it)]), + }; + let param = Param { + name: it, + ty: Some(param_ty), + }; + rt_iter("from").call(vec![value, Expr::Lambda(vec![param], Box::new(next))]) + } + None => value, + }); + } + match ty.peel_refs().kind() { + ty::Slice(_) | ty::Array(..) => Some(rt_iter("slice").call(vec![value])), + ty::Adt(adt, args) => match Std::of(tcx, adt.did())? { + Std::Vec => Some(rt_iter("slice").call(vec![library::vec_view(value, ty)])), + Std::Option => { + option_ok(b, args.type_at(0), span); + Some(rt_iter("option").call(vec![value])) + } + _ => None, + }, + _ => None, + } +} + +/// A function doing `into_iter` on each `ty`, or `None` where it is the value itself. +fn into_fn<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, span: Span) -> Option> { + if b.t.iter_item(ty).is_some() && own_next(b, ty).is_none() { + return Some(None); + } + let param = Param { + name: "value".into(), + ty: Some(b.t.ty(ty, span)), + }; + let body = into_iter(b, ty, Expr::ident("value"), span)?; + Some(Some(Expr::Lambda(vec![param], Box::new(body)))) +} + +/// Refuse an `Option` of `ty` that `T | undefined` can't hold, as the type would be. +fn option_ok<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, span: Span) { + if !b.t.option_payload_ok(ty) { + b.t.err( + span, + format!("`Option<{ty}>` is not supported: its `None` and `Some` would both be `undefined`"), + ); + } +} + +/// `rt.iter.`. +fn rt_iter(name: &str) -> Expr { + Expr::Rt("iter").member(name) +} + +/// The mapping of a call on or to an iterator, if it is one. +pub fn call<'tcx>( + b: &mut Body<'_, '_, 'tcx>, + call: &Call<'tcx>, + owner: &str, + name: &str, + args: Args<'_>, + out: &mut Vec, +) -> Option { + let tcx = b.t.tcx; + let span = call.span; + let gargs = call.args; + let targ = |n: usize| gargs.types().nth(n); + let path = library::std_path(tcx, call.def_id); + let path: Vec<&str> = path.iter().map(String::as_str).collect(); + + // Where iterators come from. + match (owner, name) { + ("slice", "iter" | "iter_mut") => { + let values = b.args(args, out); + return Some(rt_iter("slice").call(values)); + } + ("Option", "iter" | "iter_mut") => { + option_ok(b, targ(0)?, span); + let values = b.args(args, out); + return Some(rt_iter("option").call(values)); + } + ("IntoIterator", "into_iter") => { + let value = b.args(args, out).pop()?; + return into_iter(b, targ(0)?, value, span); + } + _ => {} + } + match path.as_slice() { + ["iter", "sources", "once", "once"] => return Some(rt_iter("once").call(b.args(args, out))), + ["iter", "sources", "empty", "empty"] => return Some(rt_iter("empty").call(Vec::new())), + ["iter", "sources", "from_fn", "from_fn"] => return Some(rt_iter("fromFn").call(b.args(args, out))), + _ => {} + } + + // `Vec::extend` with an iterator. + if (owner, name) == ("Extend", "extend") { + let (vec, item, source) = (targ(0)?, targ(1)?, targ(2)?); + let ty::Adt(adt, vec_args) = vec.kind() else { + return None; + }; + if Std::of(tcx, adt.did()) != Some(Std::Vec) { + return None; + } + let [v, items] = <[Expr; 2]>::try_from(b.args(args, out)).ok()?; + let mut items = into_iter(b, source, items, span)?; + // `Extend<&T>` copies each element. + if item != vec_args.type_at(0) && b.needs_copy(vec_args.type_at(0)) { + items = items.member("map").call(vec![Expr::Rt("copy")]); + } + return Some(rt_iter("extend").call(vec![v, items])); + } + + let self_ty = match (owner, name) { + ("Iterator" | "DoubleEndedIterator" | "ExactSizeIterator", _) => targ(0)?, + ("Peekable", "peek" | "next_if") if library::inherent_std(tcx, call.def_id) == Some(Std::Peekable) => { + let imp = tcx.inherent_impl_of_assoc(call.def_id)?; + let generics = tcx.generics_of(imp).count(); + tcx.type_of(imp).instantiate(tcx, &gargs[..generics]).skip_norm_wip() + } + _ => return None, + }; + let item = b.t.iter_item(self_ty.peel_refs())?; + let mut values = b.args(args, out).into_iter(); + let recv = values.next()?; + let rest: Vec = values.collect(); + + // `next` by the protocol, as an `Option`. + if matches!(name, "next" | "next_back") { + option_ok(b, item, span); + let item = match name { + "next" => next(b, self_ty, recv), + _ => recv.member("nextBack").call(Vec::new()), + }; + return Some(rt_iter("toOption").call(vec![item])); + } + // The same object, borrowed. + if name == "by_ref" { + return Some(recv); + } + + let recv = into_iter(b, self_ty, recv, span)?; + let method = |name: &str, args: Vec| Some(recv.clone().member(name).call(args)); + match name { + "map" | "filter" | "filter_map" | "enumerate" | "rev" | "skip" | "take" | "skip_while" | "take_while" + | "map_while" | "step_by" | "inspect" | "fuse" | "peekable" | "count" | "any" | "all" | "find_map" + | "position" | "fold" | "for_each" | "len" => method(&super::camel(name), rest), + "last" | "nth" | "find" | "peek" | "next_if" => { + option_ok(b, item, span); + method(&super::camel(name), rest) + } + "max" | "min" if primitive(item.peel_refs()) => { + option_ok(b, item, span); + method(name, rest) + } + "max_by_key" | "min_by_key" if primitive(targ(1)?) => { + option_ok(b, item, span); + method(&super::camel(name), rest) + } + "chain" | "zip" => { + let other = into_iter(b, targ(1)?, rest.into_iter().next()?, span)?; + method(name, vec![other]) + } + "flat_map" => { + let mut rest = rest; + rest.extend(into_fn(b, targ(1)?, span)?); + method("flatMap", rest) + } + "flatten" => { + let into = into_fn(b, item, span)?; + method("flatten", into.into_iter().collect()) + } + "copied" | "cloned" => { + let ty::Ref(_, inner, _) = item.kind() else { + return None; + }; + match () { + _ if b.needs_copy(*inner) => method("map", vec![Expr::Rt("copy")]), + _ if b.t.is_copy(*inner) => Some(recv), + _ if matches!(inner.kind(), ty::Adt(adt, _) if Std::of(tcx, adt.did()) == Some(Std::String)) => { + Some(recv) + } + _ => None, + } + } + "sum" | "product" => { + let int = Int::of(targ(1)?)?; + let (op, unit) = match name { + "sum" => ("add", 0), + _ => ("mul", 1), + }; + method(name, vec![Expr::Rt(int.rt()).member(op), int.lit(unit)]) + } + "collect" => collect(b, targ(1)?, item, recv, span), + _ => None, + } +} + +/// Whether `ty` compares in JS as Rust's `Ord` does: an integer or a `bool`. +fn primitive(ty: Ty<'_>) -> bool { + matches!(ty.kind(), ty::Bool | ty::Int(_) | ty::Uint(_)) +} + +/// `iter.collect::()` of `item`s. +fn collect<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, item: Ty<'tcx>, iter: Expr, span: Span) -> Option { + let tcx = b.t.tcx; + let ty::Adt(adt, args) = ty.kind() else { + return None; + }; + match Std::of(tcx, adt.did())? { + Std::Vec => Some(match args.type_at(0).kind() { + ty::Uint(ty::UintTy::U8) => iter.member("toByteVec").call(Vec::new()), + _ => iter.member("toArray").call(Vec::new()), + }), + // The values, collected as the `Ok` or `Some` type is, until the first `Err` or `None`. + std @ (Std::Result | Std::Option) => { + let ty::Adt(_, item_args) = item.kind() else { + return None; + }; + let value = item_args.type_at(0); + if std == Std::Option { + option_ok(b, args.type_at(0), span); + } + let value_ty = b.t.ty(value, span); + let values = collect(b, args.type_at(0), value, Expr::ident("values"), span)?; + let param = Param { + name: "values".into(), + ty: Some(Type::Rt("Iter", vec![value_ty])), + }; + let method = match std { + Std::Result => "collectResult", + _ => "collectOption", + }; + Some( + iter.member(method) + .call(vec![Expr::Lambda(vec![param], Box::new(values))]), + ) + } + _ => None, + } +} diff --git a/ts/src/lower/library.rs b/ts/src/lower/library.rs index 3ba20bbec..4bc5fea8d 100644 --- a/ts/src/lower/library.rs +++ b/ts/src/lower/library.rs @@ -42,6 +42,12 @@ pub enum Std { DebugList, /// What `assert_eq!` and `assert_ne!` compare by: its operator, as a string. AssertKind, + /// `rt.Range`, an iterator with `start` and `end` fields. + Range, + /// `rt.RangeInclusive`. + RangeInclusive, + /// `rt.Peekable`, an iterator with `peek`. + Peekable, } impl Std { @@ -65,6 +71,12 @@ impl Std { if tcx.is_lang_item(did, rustc_attr_ir::LangItem::OwnedBox) { return Some(Self::Box); } + if tcx.is_lang_item(did, rustc_attr_ir::LangItem::Range) { + return Some(Self::Range); + } + if tcx.is_lang_item(did, rustc_attr_ir::LangItem::RangeInclusiveStruct) { + return Some(Self::RangeInclusive); + } match tcx.item_name(did).as_str() { "FromUtf8Error" => return Some(Self::FromUtf8Error), "TryFromIntError" => return Some(Self::TryFromIntError), @@ -83,6 +95,7 @@ impl Std { ["fmt", "builders", "DebugTuple"] => Some(Self::DebugTuple), ["fmt", "builders", "DebugList"] => Some(Self::DebugList), ["panicking", "AssertKind"] => Some(Self::AssertKind), + ["iter", "adapters", "peekable", "Peekable"] => Some(Self::Peekable), _ => None, } } @@ -102,15 +115,19 @@ pub fn std_path(tcx: TyCtxt<'_>, did: DefId) -> Vec { .collect() } -/// Whether `did` is `core::slice::Iter`, which `slice.iter()` makes. -pub fn is_slice_iter(tcx: TyCtxt<'_>, did: DefId) -> bool { - std_path(tcx, did) == ["slice", "iter", "Iter"] -} - fn is_std(tcx: TyCtxt<'_>, did: DefId) -> bool { matches!(tcx.crate_name(did.krate).as_str(), "core" | "alloc" | "std") } +/// The standard type an inherent method is of, such as `Range` for `Range::contains`. +pub fn inherent_std(tcx: TyCtxt<'_>, def_id: DefId) -> Option { + let imp = tcx.inherent_impl_of_assoc(def_id)?; + match tcx.type_of(imp).instantiate_identity().skip_norm_wip().kind() { + ty::Adt(adt, _) => Std::of(tcx, adt.did()), + _ => None, + } +} + /// A call's arguments: still THIR, or already lowered. pub enum Args<'x> { Thir(&'x [ExprId]), @@ -197,6 +214,7 @@ pub fn call<'tcx>(b: &mut Body<'_, '_, 'tcx>, call: &Call<'tcx>, args: Args<'_>, } }; let is_u8 = |t: Ty<'tcx>| matches!(t.kind(), ty::Uint(ty::UintTy::U8)); + let range = matches!(inherent_std(tcx, call.def_id), Some(Std::Range | Std::RangeInclusive)); let e = match (owner.as_str(), name.as_str()) { // Conversions. @@ -302,6 +320,9 @@ pub fn call<'tcx>(b: &mut Body<'_, '_, 'tcx>, call: &Call<'tcx>, args: Args<'_>, .call(vec![value.method("view", vec![])]) } ty::Adt(adt, _) if Std::of(tcx, adt.did()) == Some(Std::String) => value, + ty::Adt(adt, _) if matches!(Std::of(tcx, adt.did()), Some(Std::Range | Std::RangeInclusive)) => { + value.method("clone", vec![]) + } _ if b.t.is_copy(ty) => b.copied(value, ty), _ => return None, } @@ -353,6 +374,14 @@ pub fn call<'tcx>(b: &mut Body<'_, '_, 'tcx>, call: &Call<'tcx>, args: Args<'_>, } ("Result", "unwrap_or") => rt2(b, args, out, "result", "unwrapOr"), + // Ranges, which are their own iterators. + ("RangeInclusive", "new") if range => Expr::New(Box::new(Expr::Rt("RangeInclusive")), b.args(args, out)), + ("Range" | "RangeInclusive", "is_empty" | "contains" | "start" | "end") if range => { + let mut values = b.args(args, out); + let recv = values.remove(0); + recv.method(&super::camel(&name), values) + } + ("bool", "then_some") => { let values = b.args(args, out); Expr::rt("thenSome", values) @@ -654,7 +683,7 @@ pub fn call<'tcx>(b: &mut Body<'_, '_, 'tcx>, call: &Call<'tcx>, args: Args<'_>, let debug = b.fmt_fn(fmt::Trait::Debug, targ(gargs, 0), span); list.method("entries", vec![items, debug]) } - _ => return None, + _ => return super::iter::call(b, call, &owner, &name, args, out), }; Some(e) } @@ -840,30 +869,37 @@ fn literal(parts: &Expr) -> Option { .collect() } -/// What an `IntoIterator` of a slice's items iterates, as an iterable JS value: -/// `slice.iter()`, or a slice, array, or `Vec`, by value or by reference. +/// The slice `id` iterates if it is `<[T]>::iter(slice)`, which iterates as the JS array +/// or `Uint8Array` itself does. +pub fn iterated_slice(b: &Body<'_, '_, '_>, id: ExprId) -> Option { + let ExprKind::Call { ty: fn_ty, args, .. } = &b.thir[b.strip(id)].kind else { + return None; + }; + let ty::FnDef(iter, _) = *b.subst(*fn_ty).kind() else { + return None; + }; + (key(b.t.tcx, iter) == Some(("slice".into(), "iter".into()))).then(|| args[0]) +} + +/// What an `IntoIterator` iterates, as an iterable JS value: a slice, array, or `Vec` +/// itself, by value or by reference, or else an `rt.Iter`. fn items<'tcx>(b: &mut Body<'_, '_, 'tcx>, id: ExprId, out: &mut Vec) -> Option { let tcx = b.t.tcx; + if let Some(slice) = iterated_slice(b, id) { + b.visit(id); + return Some(b.operand(slice, out)); + } let id = b.strip(id); let ty = b.ty_of(id); match ty.peel_refs().kind() { - ty::Adt(adt, _) if is_slice_iter(tcx, adt.did()) && !matches!(ty.kind(), ty::Ref(..)) => { - let ExprKind::Call { ty: fn_ty, args, .. } = &b.thir[id].kind else { - return None; - }; - let ty::FnDef(iter, _) = *b.subst(*fn_ty).kind() else { - return None; - }; - if key(tcx, iter) != Some(("slice".into(), "iter".into())) { - return None; - } - Some(b.operand(args[0], out)) - } ty::Slice(_) | ty::Array(..) => Some(b.operand(id, out)), ty::Adt(adt, _) if Std::of(tcx, adt.did()) == Some(Std::Vec) => { let v = b.operand(id, out); Some(vec_view(v, ty)) } - _ => None, + _ => { + let value = b.operand(id, out); + super::iter::into_iter(b, ty, value, b.span(id)) + } } } diff --git a/ts/src/lower/mod.rs b/ts/src/lower/mod.rs index b6dc553d6..9582d6d45 100644 --- a/ts/src/lower/mod.rs +++ b/ts/src/lower/mod.rs @@ -7,6 +7,7 @@ mod body; mod consts; mod fmt; +mod iter; mod library; mod num; mod pat; @@ -786,7 +787,10 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { library::Std::Formatter | library::Std::DebugStruct | library::Std::DebugTuple - | library::Std::DebugList, + | library::Std::DebugList + | library::Std::Range + | library::Std::RangeInclusive + | library::Std::Peekable, ) => Some(ObjectKind::Class), Some(_) => None, None if adt.is_struct() => Some(ObjectKind::Class), @@ -907,8 +911,20 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { library::Std::DebugTuple => Type::Rt("DebugTuple", vec![]), library::Std::DebugList => Type::Rt("DebugList", vec![]), library::Std::AssertKind => Type::Builtin("string"), + library::Std::Range => Type::Rt("Range", vec![self.ty(args.type_at(0), span)]), + library::Std::RangeInclusive => Type::Rt("RangeInclusive", vec![self.ty(args.type_at(0), span)]), + library::Std::Peekable => { + let item = self.iter_item(Ty::new_adt(tcx, adt, args)); + Type::Rt("Peekable", vec![self.item_ty(item, span)]) + } }; } + // Every other iterator std has is an `rt.Iter` of its items. + if !adt.did().is_local() + && let Some(item) = self.iter_item(Ty::new_adt(tcx, adt, args)) + { + return Type::Rt("Iter", vec![self.ty(item, span)]); + } if !adt.did().is_local() { self.err( span, @@ -921,6 +937,17 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { Type::Sym(info.sym, args) } + /// The type of an iterator's items, which every iterator has. + fn item_ty(&mut self, item: Option>, span: Span) -> Type { + match item { + Some(item) => self.ty(item, span), + None => { + self.err(span, "an iterator without an item type"); + Type::Builtin("unknown") + } + } + } + /// Whether `undefined` is free to mean `None` around a `ty`. pub fn option_payload_ok(&self, ty: Ty<'tcx>) -> bool { match ty.kind() { diff --git a/ts/src/program.rs b/ts/src/program.rs index dd3799583..e461cdf45 100644 --- a/ts/src/program.rs +++ b/ts/src/program.rs @@ -6,8 +6,12 @@ use std::path::{Path, PathBuf}; use crate::print; use crate::ts::{FieldId, Function, Item, SymId}; -/// The runtime every module imports helpers from, written beside the crate root. -pub const RUNTIME: &str = include_str!("../runtime/rt.ts"); +/// The runtime every module imports helpers from, written beside the crate root: each +/// file's path from there, and its text. +pub const RUNTIME: &[(&str, &str)] = &[ + ("rt.ts", include_str!("../runtime/rt.ts")), + ("rt/iter.ts", include_str!("../runtime/rt/iter.ts")), +]; /// The first line of every generated file, which also marks it as safe to replace. pub const HEADER: &str = "// Generated by rust2-ts. Do not edit."; @@ -159,14 +163,19 @@ impl Program { for item in &self.items { modules.entry(&self.syms[item_sym(item)].module).or_default().push(item); } - if modules.keys().any(|module| module.as_slice() == ["rt"]) { - return Err("a top-level module named `rt` would replace the runtime, rt.ts".into()); + if modules + .keys() + .any(|module| module.first().is_some_and(|name| name == "rt")) + { + return Err("a top-level module named `rt` would replace the runtime, rt.ts and rt/".into()); } for items in modules.values_mut() { items.sort_by_key(|item| self.syms[item_sym(item)].order); } - write_file(&out.join("rt.ts"), &format!("{HEADER}\n{RUNTIME}"))?; + for (path, text) in RUNTIME { + write_file(&out.join(path), &format!("{HEADER}\n{text}"))?; + } for (module, items) in modules { let path = out.join(module_file(module)); let printed = print::module(self, module, &items, &path)?; diff --git a/ts/tests/fixtures.rs b/ts/tests/fixtures.rs index 357c0e142..2db133554 100644 --- a/ts/tests/fixtures.rs +++ b/ts/tests/fixtures.rs @@ -64,7 +64,8 @@ fn sources(dir: &Path) -> Vec<(PathBuf, String)> { let rel = path.strip_prefix(dir).unwrap().to_path_buf(); let text = std::fs::read_to_string(&path).unwrap(); // A hand-written caller copied in beside the output is no part of it. - if rel != Path::new("rt.ts") && text.starts_with("// Generated by rust2-ts.") { + if rel != Path::new("rt.ts") && !rel.starts_with("rt") && text.starts_with("// Generated by rust2-ts.") + { files.push((rel, text)); } } From 4214fa05c986559bdddb8946d6d2afc58a3ac6ba Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 24 Oct 2026 01:17:16 -0700 Subject: [PATCH 03/13] feat(ts): cloned through Clone, O(1) Range::nth, census without resolving over parameters Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- ts/fixtures/lang/expected/iters.ts | 45 ++++++++++++++++++++++++++++++ ts/fixtures/lang/src/iters.rs | 19 +++++++++++++ ts/runtime/rt/iter.ts | 35 ++++++++++++++++++++--- ts/src/lower/iter.rs | 25 ++++++++++------- 4 files changed, 110 insertions(+), 14 deletions(-) diff --git a/ts/fixtures/lang/expected/iters.ts b/ts/fixtures/lang/expected/iters.ts index 345286f3c..b557683e0 100644 --- a/ts/fixtures/lang/expected/iters.ts +++ b/ts/fixtures/lang/expected/iters.ts @@ -307,6 +307,15 @@ export function ranges(data: Uint8Array, out: rt.ByteVec): void { } out.push(i); } + const iter2 = new rt.Range(a, b).skip(1).stepBy(4); + while (true) { + const i2 = iter2.next(); + if (i2 === rt.DONE) { + break; + } + out.push(i2); + } + out.push(rt.option.unwrapOr(new rt.Range(a, b).nth(5), 0xee)); const top = new rt.RangeInclusive(rt.max(a, 250), 255).toByteVec(); out.extendFromSlice(top.view()); const inclusive = new rt.RangeInclusive(a, b); @@ -533,6 +542,42 @@ export function values(data: Uint8Array, out: rt.ByteVec): void { const arg2 = new Doubled(rt.slice.range(data, 0, rt.min(data.length, 2))); const printed = rt.fmt.format([(f: rt.Formatter) => arg.fmt(f), "|", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => arg2.fmt(f))]); out.extendFromSlice(rt.string.asBytes(printed)); + const tags = rt.iter.slice(data).map((arg3: number): Tag => { + const b3 = arg3; + return new Tag({ bytes: rt.slice.toByteVec(Uint8Array.of(b3, ~b3 & 0xff)) }); + }).toArray(); + let cloned: Tag[] = rt.iter.slice(tags).map((a0: Tag): Tag => { + return a0.clone(); + }).toArray(); + const iter3 = rt.iter.slice(cloned); + while (true) { + const tag = iter3.next(); + if (tag === rt.DONE) { + break; + } + tag.bytes.push(0x77); + } + const iter4 = rt.iter.slice(tags).chain(rt.iter.slice(cloned)); + while (true) { + const tag2 = iter4.next(); + if (tag2 === rt.DONE) { + break; + } + out.extendFromSlice(tag2.bytes.view()); + } +} + +/** A `Clone` that is not `Copy`, which `cloned` calls for each item. */ +export class Tag { + bytes: rt.ByteVec; + + constructor(fields: { bytes: rt.ByteVec }) { + this.bytes = fields.bytes; + } + + clone(): Tag { + return new Tag({ bytes: rt.ByteVec.from(this.bytes.view()) }); + } } /** An iterator in a field. */ diff --git a/ts/fixtures/lang/src/iters.rs b/ts/fixtures/lang/src/iters.rs index be9c83d1a..b48f42364 100644 --- a/ts/fixtures/lang/src/iters.rs +++ b/ts/fixtures/lang/src/iters.rs @@ -202,6 +202,10 @@ pub fn ranges(data: &[u8], out: &mut Vec) { for i in range.rev().step_by(3) { out.push(i); } + for i in (a..b).skip(1).step_by(4) { + out.push(i); + } + out.push((a..b).nth(5).unwrap_or(0xee)); // `..=` at the top of `u8`, which ends without overflowing. let top: Vec = (a.max(250)..=255).collect(); out.extend_from_slice(&top); @@ -363,6 +367,21 @@ pub fn values(data: &[u8], out: &mut Vec) { } let printed = format!("{:?}|{:#?}", Doubled(data), Doubled(&data[..data.len().min(2)])); out.extend_from_slice(printed.as_bytes()); + // Clones that change leave what they cloned alone. + let tags: Vec = data.iter().map(|&b| Tag { bytes: [b, !b].to_vec() }).collect(); + let mut cloned: Vec = tags.iter().cloned().collect(); + for tag in cloned.iter_mut() { + tag.bytes.push(0x77); + } + for tag in tags.iter().chain(cloned.iter()) { + out.extend_from_slice(&tag.bytes); + } +} + +/// A `Clone` that is not `Copy`, which `cloned` calls for each item. +#[derive(Clone)] +struct Tag { + bytes: Vec, } /// An iterator in a field. diff --git a/ts/runtime/rt/iter.ts b/ts/runtime/rt/iter.ts index 62d6f52d4..5adda4dff 100644 --- a/ts/runtime/rt/iter.ts +++ b/ts/runtime/rt/iter.ts @@ -928,14 +928,19 @@ function lt(a: T, b: T): boolean { return (a as number) < (b as number); } -/** One past `n`, in its own representation. */ +/** `n + by`, in `n`'s representation. */ +function step(n: T, by: number): T { + return (typeof n === "bigint" ? n + BigInt(by) : (n as number) + by) as T; +} + +/** One past `n`. */ function succ(n: T): T { - return (typeof n === "bigint" ? n + 1n : (n as number) + 1) as T; + return step(n, 1); } -/** One before `n`, in its own representation. */ +/** One before `n`. */ function pred(n: T): T { - return (typeof n === "bigint" ? n - 1n : (n as number) - 1) as T; + return step(n, -1); } /** `ops::Range`: `start..end`, which iterates its integers by moving `start`. */ @@ -962,6 +967,28 @@ export class Range extends Iter { return this.end; } + /** std's `nth`, in one step: past the end, the range ends. */ + override nthItem(n: number): T | Done { + const item = step(this.start, n); + if (lt(item, this.end)) { + this.start = succ(item); + return item; + } + this.start = this.end; + return DONE; + } + + /** std's `nth_back`, in one step. */ + override nthBackItem(n: number): T | Done { + const item = step(this.end, -n); + if (lt(this.start, item)) { + this.end = pred(item); + return this.end; + } + this.end = this.start; + return DONE; + } + override len(): number { return lt(this.start, this.end) ? Number(this.end) - Number(this.start) : 0; } diff --git a/ts/src/lower/iter.rs b/ts/src/lower/iter.rs index 705568b2d..56a542d1b 100644 --- a/ts/src/lower/iter.rs +++ b/ts/src/lower/iter.rs @@ -4,7 +4,7 @@ //! `rt.iter.from`. use rustc_hir::def_id::DefId; -use rustc_middle::ty::{self, Instance, Ty}; +use rustc_middle::ty::{self, Instance, Ty, TypeVisitableExt}; use rustc_span::Span; use super::Translator; @@ -32,14 +32,13 @@ pub fn is_next(tcx: ty::TyCtxt<'_>, def_id: DefId) -> bool { } impl<'tcx> Translator<'_, 'tcx> { - /// `Iterator::Item` of `ty`, if `ty` is an iterator. + /// `Iterator::Item` of `ty`, if `ty` is an iterator. A type over parameters, which a + /// generic type's fields and a census have, may leave it a projection. pub fn iter_item(&self, ty: Ty<'tcx>) -> Option> { let tcx = self.tcx; - let (next, item) = trait_items(tcx)?; - let env = self.typing_env_pub(); - Instance::try_resolve(tcx, env, next, tcx.mk_args(&[ty.into()])).ok()??; + let (_, item) = trait_items(tcx)?; let projection = Ty::new_projection(tcx, ty::IsRigid::No, item, [ty]); - tcx.try_normalize_erasing_regions(env, ty::Unnormalized::new_wip(projection)) + tcx.try_normalize_erasing_regions(self.typing_env_pub(), ty::Unnormalized::new_wip(projection)) .ok() } } @@ -49,7 +48,8 @@ impl<'tcx> Translator<'_, 'tcx> { fn own_next<'tcx>(b: &Body<'_, '_, 'tcx>, ty: Ty<'tcx>) -> Option> { let tcx = b.t.tcx; let ty = ty.peel_refs(); - if !matches!(ty.kind(), ty::Adt(adt, _) if adt.did().is_local()) { + // Over parameters, only a census's instance, which never runs, would call it. + if !matches!(ty.kind(), ty::Adt(adt, _) if adt.did().is_local()) || ty.has_param() || ty.has_aliases() { return None; } let (next, _) = trait_items(tcx)?; @@ -295,11 +295,16 @@ pub fn call<'tcx>( }; match () { _ if b.needs_copy(*inner) => method("map", vec![Expr::Rt("copy")]), - _ if b.t.is_copy(*inner) => Some(recv), - _ if matches!(inner.kind(), ty::Adt(adt, _) if Std::of(tcx, adt.did()) == Some(Std::String)) => { + // A JS string is a value already. + _ if b.t.is_copy(*inner) || matches!(inner.kind(), ty::Adt(adt, _) if Std::of(tcx, adt.did()) == Some(Std::String)) => { Some(recv) } - _ => None, + // Each item through its `Clone`. + _ => { + let clone = tcx.lang_items().clone_fn()?; + let clone = b.fn_value(clone, tcx.mk_args(&[(*inner).into()]), span); + method("map", vec![clone]) + } } } "sum" | "product" => { From 8e131e95b7824d46185da05e0bea3860a69e75e7 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 24 Oct 2026 01:25:55 -0700 Subject: [PATCH 04/13] feat(ts): exact RangeInclusive::nth, ? on next, iterator bench workloads, README Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- ts/README.md | 26 +++++++++++--- ts/bench/iter.ts | 52 +++++++++++++++++++++++++++ ts/bench/main.ts | 7 ++-- ts/bench/moq.ts | 4 +++ ts/bench/workloads.ts | 14 ++++++-- ts/fixtures/codec/expected/bench.ts | 54 +++++++++++++++++++++++++++++ ts/fixtures/codec/src/bench.rs | 31 ++++++++++++++++- ts/fixtures/lang/expected/iters.ts | 50 ++++++++++++++++++++------ ts/fixtures/lang/src/iters.rs | 33 ++++++++++++++++-- ts/runtime/rt/iter.ts | 44 +++++++++++++++++++++-- ts/src/lower/body.rs | 42 +++++++++++++++------- ts/src/lower/iter.rs | 9 +++-- ts/src/lower/library.rs | 7 +++- 13 files changed, 330 insertions(+), 43 deletions(-) create mode 100644 ts/bench/iter.ts diff --git a/ts/README.md b/ts/README.md index 4b4fb73c4..b2d4dacaf 100644 --- a/ts/README.md +++ b/ts/README.md @@ -46,7 +46,7 @@ it finds; translation still fails on any refusal. Each Rust module with translated items becomes a file (`lib.ts`, `coding/varint.ts`), with a `.ts.map` source map into the Rust it came from. `rt.ts` beside them is the -runtime: what std does that JS does not. +runtime: what std does that JS does not, with iterators in `rt/iter.ts`. | Rust | TypeScript | | --- | --- | @@ -60,6 +60,8 @@ runtime: what std does that JS does not. | `Result` | `rt.Result`: `{ ok: true, value }` or `{ ok: false, error }` | | tuple, `[T; N]`, `&[T]`, `Vec` | an array, `Uint8Array` for bytes, `rt.ByteVec` for `Vec` | | `&T`, `&mut T`, `Box` | the value: a `&mut` must point at an object JS can change in place | +| an iterator | `rt.Iter`, whose adaptors are its methods: `v.iter().map(f)` | +| `a..b`, `a..=b` | `rt.Range`, `rt.RangeInclusive`, which are iterators too | | closure | an arrow function | | constant | its value, inlined as Rust inlines it | @@ -79,6 +81,15 @@ prints it. A caller prints a `printable` type as `to_string()` and `format!("{er would: `rt.fmt.toString((f) => DecodeError.fmt$Display(err, f))` and `rt.fmt.format([(f) => DecodeError.fmt$Debug(err, f)])`. +An iterator's `next()` returns its next item or `rt.DONE`, never `undefined`, so an +iterator of `Option` keeps each `None` apart from its end. std's adaptors and consumers +are `rt.Iter` methods, lazy and short-circuiting as std's are, and call their closures in +the same order and as often. A `for` loop reads an iterator by that protocol, a slice, +array, `Vec`, or `slice.iter()` with `for ... of`, and a range written in place with a +counter. The crate's own `impl Iterator` returns `rt.DONE` from its `next` too, and joins +std's adaptors through `rt.iter.from`. A caller calls `next()` for one item, or iterates +an `rt.Iter` with `for ... of`. + Functions and members are camelCase. Functions are monomorphized: each instance is its own function, and instances that share a name are suffixed by type, as in `roundtripLite$Group`. Types are translated once, generically. A `Copy` struct, tuple, or @@ -94,6 +105,10 @@ Imports name the `.ts` file, which Node, Bun, and bundlers run as is. A `tsc` bu faster but waits until `just ts bench` measures `bigint` as the cost. - `Result` is a plain `{ ok, value } | { ok, error }` object rather than an exception, so `?` and `match` keep Rust's control flow exactly. +- Iterators are the runtime's lazy classes rather than generators or ES iterator + helpers: std's adaptors have no THIR to lower, ES helpers lack most of them + (`rev`, `zip`, `peekable`), and a `next` returning an item or `rt.DONE` allocates + nothing per item. - Bodies lower from THIR, which keeps loops, `match`, and `?` shaped as written. It does not say where values drop; [Drop](/quest/m2/ts-drop.md) decides how to get that from MIR. @@ -107,8 +122,10 @@ bind to hand-written TS through config ([model](/quest/m2/ts-model.md)). ## Not yet These fail translation: floats, `char`, statics, `async`, -types that implement `Drop`, iterator adaptors (`for` over ranges, slices, arrays, and -`Vec` works), or-patterns that bind, other format specs (a precision, `+`, `{:b}`, `{:x?}`, +types that implement `Drop`, iterators of std's collections and the adaptors without a +mapping in `src/lower/iter.rs` (such as `cycle`, `unzip`, and `partition`), an own +`impl Iterator` with `next_back`, `len`, or another provided method of its own, +`while let` or `match` on the `next` of an iterator of `Option`, or-patterns that bind, other format specs (a precision, `+`, `{:b}`, `{:x?}`, and widths from arguments), writing to a `String` with `write!`, a `move` closure copying a mutable variable, `mem::swap`, `replace`, or `take` of a reference's whole pointee, and std functions without a mapping in `src/lower/library.rs`. @@ -130,7 +147,8 @@ built natively for wasm32 and run beside its translation on the same inputs by - `codec` vendors moq-net's sync codec, which a harness outside the crate could not reach, and writes its errors' `Display` and `Debug`. - `lang` covers every integer width's arithmetic and casts, control flow, patterns, - `Copy` aggregates, and formatting. Its differential test runs through `caller.ts`, a + `Copy` aggregates, formatting, and iterators, with closures that log what each + adaptor reads. Its differential test runs through `caller.ts`, a hand-written caller that also prints a `printable` error nothing translated prints. - `reject` holds constructs outside the subset; its expected errors are `expected.txt`, and its expected census is `census.md`. diff --git a/ts/bench/iter.ts b/ts/bench/iter.ts new file mode 100644 index 000000000..f6d85e8e6 --- /dev/null +++ b/ts/bench/iter.ts @@ -0,0 +1,52 @@ +// The iterator workloads' hand-written route: each chain in the codec fixture's +// `bench.rs` op 6, written with ES iterator helpers (`Iterator.prototype.map` and the +// rest), as TypeScript would write it without the translation's `rt.Iter`. Helpers +// without an ES counterpart, `enumerate` here, are a `for ... of` with a counter. + +/** Run one chain, as `bench::run`'s op 6 does, over `data`. */ +export function iterate(chain: number, data: Uint8Array): Uint8Array { + switch (chain) { + case 0: + return u64( + data + .values() + .map((b) => b >> 1) + .filter((b) => (b & 1) === 0) + .reduce((n) => n + 1, 0), + ); + case 1: + return Uint8Array.of(data.values().some((b) => b === 0xff) ? 1 : 0); + case 2: + return u64( + data + .values() + .filter((b) => (b & 1) === 1) + .toArray().length, + ); + case 3: + return u64(data.values().reduce((sum, b) => sum + b, 0)); + case 4: + return u64( + data + .values() + .drop(16) + .take(512) + .reduce((acc, b) => acc ^ b, 0), + ); + default: { + let last = 0; + let i = 0; + for (const b of data.values()) { + if (b === 0x80) last = i; + i++; + } + return u64(last); + } + } +} + +function u64(value: number): Uint8Array { + const out = new Uint8Array(8); + new DataView(out.buffer).setBigUint64(0, BigInt(value)); + return out; +} diff --git a/ts/bench/main.ts b/ts/bench/main.ts index 3d3d7ac00..3c83244bc 100644 --- a/ts/bench/main.ts +++ b/ts/bench/main.ts @@ -193,13 +193,14 @@ function render(): string { `- moq: ${text(["git", "-C", checkout, "rev-parse", "HEAD"])}`, ...engines.map((e) => `- ${e.engine}`), "", - "Each cell is nanoseconds per item (a varint, a frame header, or a message): the median", + "Each cell is nanoseconds per item (a varint, a frame header, a message, or a byte iterated): the median", `of ${rounds} samples of at least ${sampleMs} ms, ± half their interquartile range. Each round interleaves`, "every route and workload. `native` is a release build on the host. `wasm` is the same", "Rust at moq-wasm's size-optimized profile, run with its input already in memory;", "copying the input in and the output out are timed apart. `hand-written` is moq's", - "`js/net`, called as its own benchmarks call it. `A/A` is the translation loaded a", - "second time: its gap from `translated` is the noise.", + "`js/net`, called as its own benchmarks call it, and for `iter/` the same chain with ES", + "iterator helpers (`bench/iter.ts`). `A/A` is the translation loaded a second time: its", + "gap from `translated` is the noise.", "", "## Bundle size", "", diff --git a/ts/bench/moq.ts b/ts/bench/moq.ts index 8b84179be..5aa5d8318 100644 --- a/ts/bench/moq.ts +++ b/ts/bench/moq.ts @@ -12,6 +12,8 @@ import { Version as Lite } from "moq/js/net/src/lite/version.ts"; import { Cursor, Reader, Writer } from "moq/js/net/src/stream.ts"; import * as Varint from "moq/js/net/src/varint.ts"; +import { iterate } from "./iter.ts"; + type Version = (typeof Lite)[keyof typeof Lite]; // bench.rs reads a selector through the harness's `form` and `lite_version`; these are the @@ -41,6 +43,8 @@ export function run(input: Uint8Array): Uint8Array | Promise { return encodeGroup(data, lite(selector)); case 5: return decodeGroup(data, lite(selector)); + case 6: + return iterate(selector, data); default: throw new Error(`unknown workload ${op}`); } diff --git a/ts/bench/workloads.ts b/ts/bench/workloads.ts index ac434e1c3..9baec325a 100644 --- a/ts/bench/workloads.ts +++ b/ts/bench/workloads.ts @@ -3,10 +3,10 @@ // reads what its encode wrote. export interface Workload { - /** What is coded, such as `varint/quic/2B` or `frames/video/lite-07`. */ + /** What is coded, such as `varint/quic/2B` or `frames/video/lite-07`, or iterated. */ name: string; - op: "encode" | "decode"; - /** Varints, frame headers, or messages per call, which each time is divided by. */ + op: "encode" | "decode" | "iterate"; + /** Varints, frame headers, messages, or items per call, which each time is divided by. */ items: number; input: Uint8Array; } @@ -44,6 +44,14 @@ export function workloads(encode: (input: Uint8Array) => Uint8Array): Workload[] pair(`frames/audio/${version}`, 50, 2, selector, [50n, 20_000n, 160n, 160n, 0n, 0n]); pair(`group/${version}`, 1, 4, selector, [3n, 1_234n]); } + + // Iterator chains over bytes, as `bench.rs`'s op 6 picks them by selector; no byte is + // 0xff, so `any` reads them all. + const bytes = Uint8Array.from({ length: 1024 }, (_, i) => (i * 167 + 13) % 255); + const chains = ["map-filter-count", "any", "filter-collect", "sum", "skip-take-fold", "enumerate-for"]; + chains.forEach((chain, selector) => { + out.push({ name: `iter/${chain}`, op: "iterate", items: bytes.length, input: Uint8Array.of(6, selector, ...bytes) }); + }); return out; } diff --git a/ts/fixtures/codec/expected/bench.ts b/ts/fixtures/codec/expected/bench.ts index 38e047b2f..812a4fdfa 100644 --- a/ts/fixtures/codec/expected/bench.ts +++ b/ts/fixtures/codec/expected/bench.ts @@ -33,6 +33,8 @@ export function run(input: Uint8Array): rt.ByteVec { decoded(decodeFrames(data2, liteVersion(selector), out), out); } else if (op === 4) { encoded(encodeGroup(data2, liteVersion(selector), out), out); + } else if (op === 6) { + iterate(selector, data2, out); } else { decoded(decodeGroup(data2, liteVersion(selector), out), out); } @@ -161,6 +163,58 @@ export function encodeGroup(data: Uint8Array, version: Version, out: rt.ByteVec) return group.encode(Encoder.new(out, Form.from(version)), version); } +/** + * One iterator chain over every byte, picked by `chain`, with closures that cost little + * next to the iterator protocol they run through. + */ +export function iterate(chain: number, data: Uint8Array, out: rt.ByteVec): void { + if (chain === 0) { + putUsize(out, rt.iter.slice(data).map((arg: number): number => { + const b = arg; + return b >> 1; + }).filter((arg2: number): boolean => { + const b2 = arg2; + return (b2 & 1) === 0; + }).count()); + } else if (chain === 1) { + out.push(rt.iter.slice(data).any((arg3: number): boolean => { + const b3 = arg3; + return b3 === 0xff; + }) ? 1 : 0); + } else if (chain === 2) { + const odd = rt.iter.slice(data).filter((arg4: number): boolean => { + const b4 = arg4; + return (b4 & 1) === 1; + }).toByteVec(); + putUsize(out, odd.length); + } else if (chain === 3) { + putU64(out, BigInt(rt.iter.slice(data).map((arg5: number): number => { + const b5 = arg5; + return b5; + }).sum(rt.u32.add, 0))); + } else if (chain === 4) { + putU64(out, BigInt(rt.iter.slice(data).skip(16).take(512).fold(0, (acc: number, arg6: number): number => { + const b6 = arg6; + return (acc ^ b6) >>> 0; + }))); + } else { + let last: number = 0; + const iter = rt.iter.slice(data).enumerate(); + while (true) { + const item = iter.next(); + if (item === rt.DONE) { + break; + } + const i = item[0]; + const b7 = item[1]; + if (b7 === 0x80) { + last = i; + } + } + putUsize(out, last); + } +} + export function decodeGroup(wire: Uint8Array, version: Version, out: rt.ByteVec): rt.Result { const decodeSlice = Group.decodeSlice(wire, version); if (!decodeSlice.ok) { diff --git a/ts/fixtures/codec/src/bench.rs b/ts/fixtures/codec/src/bench.rs index a2f88f292..3ba3f600b 100644 --- a/ts/fixtures/codec/src/bench.rs +++ b/ts/fixtures/codec/src/bench.rs @@ -1,7 +1,8 @@ //! The workloads `just ts bench` times on every route, shaped like moq's own TypeScript //! benchmarks: `js/net/bench/varint.ts` and `lite-varint.ts`. The first input byte picks //! the workload and the second its wire format; an encode's output is the matching -//! decode's input. +//! decode's input. Op 6 runs iterator chains over its bytes instead, as moq-net's model +//! writes them. 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}; @@ -23,6 +24,7 @@ pub fn run(input: &[u8]) -> Vec { 2 => encoded(encode_frames(data, lite_version(selector), &mut out), &mut out), 3 => decoded(decode_frames(data, lite_version(selector), &mut out), &mut out), 4 => encoded(encode_group(data, lite_version(selector), &mut out), &mut out), + 6 => iterate(selector, data, &mut out), _ => decoded(decode_group(data, lite_version(selector), &mut out), &mut out), } out @@ -120,6 +122,33 @@ fn encode_group(data: &[u8], version: lite::Version, out: &mut Vec) -> Resul group.encode(&mut Encoder::new(out, version.into()), version) } +/// 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) { + match chain { + 0 => put_usize(out, data.iter().map(|&b| b >> 1).filter(|&b| b & 1 == 0).count()), + 1 => out.push(data.iter().any(|&b| b == 0xff) as u8), + 2 => { + let odd: Vec = data.iter().copied().filter(|&b| b & 1 == 1).collect(); + put_usize(out, odd.len()); + } + 3 => put_u64(out, data.iter().map(|&b| b as u32).sum::() as u64), + 4 => put_u64( + out, + data.iter().skip(16).take(512).fold(0u32, |acc, &b| acc ^ b as u32) as u64, + ), + _ => { + let mut last = 0; + for (i, &b) in data.iter().enumerate() { + if b == 0x80 { + last = i; + } + } + put_usize(out, last); + } + } +} + fn decode_group(wire: &[u8], version: lite::Version, out: &mut Vec) -> Result<(), DecodeError> { let (group, used) = lite::Group::decode_slice(wire, version)?; put_u64(out, group.subscribe); diff --git a/ts/fixtures/lang/expected/iters.ts b/ts/fixtures/lang/expected/iters.ts index b557683e0..4ae8bc7d9 100644 --- a/ts/fixtures/lang/expected/iters.ts +++ b/ts/fixtures/lang/expected/iters.ts @@ -316,12 +316,27 @@ export function ranges(data: Uint8Array, out: rt.ByteVec): void { out.push(i2); } out.push(rt.option.unwrapOr(new rt.Range(a, b).nth(5), 0xee)); - const top = new rt.RangeInclusive(rt.max(a, 250), 255).toByteVec(); + const top = new rt.RangeInclusive(rt.max(a, 250), 255, rt.u8).toByteVec(); out.extendFromSlice(top.view()); - const inclusive = new rt.RangeInclusive(a, b); + const inclusive = new rt.RangeInclusive(a, b, rt.u8); out.push(inclusive.start()); out.push(inclusive.end()); out.push(inclusive.contains(b) ? 1 : 0); + let near: rt.RangeInclusive = new rt.RangeInclusive(rt.max(a, 200), 255, rt.u8); + out.push(rt.option.unwrapOr(near.nth(b), 0xee)); + out.push(near.start()); + out.push(near.isEmpty() ? 1 : 0); + out.push(near.contains(255) ? 1 : 0); + let low: rt.RangeInclusive = new rt.RangeInclusive(0, b, rt.u8); + out.push(rt.option.unwrapOr(rt.iter.toOption(low.nthBackItem(rt.usize.rem(a, 16))), 0xee)); + out.push(low.end()); + let stepped: rt.RangeInclusive = new rt.RangeInclusive(a, b, rt.u8); + out.push(stepped.stepBy(3).count() & 0xff); + out.push(stepped.start()); + const signed = new rt.RangeInclusive((a << 24) >> 24, (b << 24) >> 24, rt.i8); + rt.iter.extend(out, signed.rev().map((i3: number): number => { + return i3 & 0xff; + })); let wide: rt.Range = new rt.Range(BigInt(a), rt.u64.mul(BigInt(b), 3n)); out.push(wide.map((n: bigint): bigint => { return rt.u64.rem(n, 5n); @@ -368,6 +383,7 @@ export function options(data: Uint8Array, out: rt.ByteVec): void { return o !== undefined; }).count(); out.push(somes & 0xff); + out.push(rt.option.unwrapOr(second(rt.iter.slice(data).map(maybe)), 0xcd)); const first = rt.slice.first(data); const iter2 = rt.iter.option(first).chain(rt.iter.once(0x99)); while (true) { @@ -396,6 +412,20 @@ export function options(data: Uint8Array, out: rt.ByteVec): void { } } +/** The second item of an iterator of options, through `?`, which tests for its end. */ +export function second(items: rt.Iter): number | undefined { + const next = items.next(); + if (next === rt.DONE) { + return undefined; + } + const next2 = items.next(); + if (next2 === rt.DONE) { + return undefined; + } + const item = next2; + return rt.option.unwrapOr(item, 0xab); +} + /** Every byte, or `None` where it is zero. */ export class Gaps { data: Uint8Array; @@ -451,17 +481,15 @@ export class Evens { next(): number | rt.Done { while (true) { - const next = rt.iter.toOption(this.inner.next()); - if (next !== undefined) { - const b = next; - if (rt.u8.rem(b, 2) === 0) { - return b; - } - } else { - break; + const next = this.inner.next(); + if (next === rt.DONE) { + return rt.DONE; + } + const b = next; + if (rt.u8.rem(b, 2) === 0) { + return b; } } - return rt.DONE; } } diff --git a/ts/fixtures/lang/src/iters.rs b/ts/fixtures/lang/src/iters.rs index b48f42364..cf5b190b1 100644 --- a/ts/fixtures/lang/src/iters.rs +++ b/ts/fixtures/lang/src/iters.rs @@ -213,6 +213,20 @@ pub fn ranges(data: &[u8], out: &mut Vec) { out.push(*inclusive.start()); out.push(*inclusive.end()); out.push(inclusive.contains(&b) as u8); + // `nth` past the end, or past the type's last value, leaves the range as std's does. + let mut near = a.max(200)..=255; + out.push(near.nth(b as usize).unwrap_or(0xee)); + out.push(*near.start()); + out.push(near.is_empty() as u8); + out.push(near.contains(&255) as u8); + let mut low = 0..=b; + out.push(low.nth_back(a as usize % 16).unwrap_or(0xee)); + out.push(*low.end()); + let mut stepped = a..=b; + out.push(stepped.by_ref().step_by(3).count() as u8); + out.push(*stepped.start()); + let signed = (a as i8)..=(b as i8); + out.extend(signed.rev().map(|i| i as u8)); let mut wide = (a as u64)..(b as u64 * 3); out.push(wide.by_ref().map(|n| n % 5).filter(|&n| n == 0).count() as u8); out.push(wide.is_empty() as u8); @@ -239,6 +253,7 @@ pub fn options(data: &[u8], out: &mut Vec) { out.extend_from_slice(&flat); let somes = all.iter().filter(|o| o.is_some()).count(); out.push(somes as u8); + out.push(second(data.iter().map(maybe)).unwrap_or(0xcd)); let first = data.first().copied(); for b in first.iter().chain(std::iter::once(&0x99)) { out.push(*b); @@ -258,6 +273,13 @@ pub fn options(data: &[u8], out: &mut Vec) { } } +/// The second item of an iterator of options, through `?`, which tests for its end. +fn second(mut items: impl Iterator>) -> Option { + items.next()?; + let item = items.next()?; + Some(item.unwrap_or(0xab)) +} + /// Every byte, or `None` where it is zero. struct Gaps<'a> { data: &'a [u8], @@ -306,12 +328,12 @@ impl Iterator for Evens<'_> { type Item = u8; fn next(&mut self) -> Option { - while let Some(b) = self.inner.next() { + loop { + let b = self.inner.next()?; if b % 2 == 0 { return Some(b); } } - None } } @@ -368,7 +390,12 @@ pub fn values(data: &[u8], out: &mut Vec) { let printed = format!("{:?}|{:#?}", Doubled(data), Doubled(&data[..data.len().min(2)])); out.extend_from_slice(printed.as_bytes()); // Clones that change leave what they cloned alone. - let tags: Vec = data.iter().map(|&b| Tag { bytes: [b, !b].to_vec() }).collect(); + let tags: Vec = data + .iter() + .map(|&b| Tag { + bytes: [b, !b].to_vec(), + }) + .collect(); let mut cloned: Vec = tags.iter().cloned().collect(); for tag in cloned.iter_mut() { tag.bytes.push(0x77); diff --git a/ts/runtime/rt/iter.ts b/ts/runtime/rt/iter.ts index 5adda4dff..48fdc5e14 100644 --- a/ts/runtime/rt/iter.ts +++ b/ts/runtime/rt/iter.ts @@ -1011,11 +1011,14 @@ export class RangeInclusive extends Iter { private lo: T; private hi: T; private exhausted = false; + /** The integer type's bounds, which `nth` wraps within as std's does. */ + private readonly bounds: Bounds | undefined; - constructor(start: T, end: T) { + constructor(start: T, end: T, bounds?: Bounds) { super(); this.lo = start; this.hi = end; + this.bounds = bounds; } start(): T { @@ -1035,7 +1038,7 @@ export class RangeInclusive extends Iter { } clone(): RangeInclusive { - const range = new RangeInclusive(this.lo, this.hi); + const range = new RangeInclusive(this.lo, this.hi, this.bounds); range.exhausted = this.exhausted; return range; } @@ -1059,6 +1062,43 @@ export class RangeInclusive extends Iter { override len(): number { return this.isEmpty() ? 0 : Number(this.hi) - Number(this.lo) + 1; } + + /** std's `nth`: `start` moves past the item, wrapping if it overflows, which ends the range. */ + override nthItem(n: number): T | Done { + if (this.isEmpty()) return DONE; + const [item, over] = overflowing(this.lo, n, this.bounds); + const [after, overAfter] = overflowing(item, 1, this.bounds); + this.lo = after; + this.exhausted = over || overAfter; + return !over && !lt(this.hi, item) ? item : DONE; + } + + /** std's `nth_back`, from the end. */ + override nthBackItem(n: number): T | Done { + if (this.isEmpty()) return DONE; + const [item, over] = overflowing(this.hi, -n, this.bounds); + const [before, overBefore] = overflowing(item, -1, this.bounds); + this.hi = before; + this.exhausted = over || overBefore; + return !over && !lt(item, this.lo) ? item : DONE; + } +} + +/** An integer type's least and greatest values. */ +export interface Bounds { + readonly MIN: T; + readonly MAX: T; +} + +/** `Step::forward_overflowing` and `backward_overflowing`: `n + by`, wrapped within `bounds`, and whether it wrapped. */ +function overflowing(n: T, by: number, bounds: Bounds | undefined): [T, boolean] { + if (bounds === undefined) throw new Error("rust2-ts: stepping a range of no integer type"); + const exact = BigInt(n as number | bigint) + BigInt(by); + const [min, max] = [BigInt(bounds.MIN as number | bigint), BigInt(bounds.MAX as number | bigint)]; + if (exact >= min && exact <= max) return [step(n, by), false]; + const span = max - min + 1n; + const wrapped = ((((exact - min) % span) + span) % span) + min; + return [(typeof n === "bigint" ? wrapped : Number(wrapped)) as T, true]; } /** An iterator as itself: what `into_iter` makes of one. */ diff --git a/ts/src/lower/body.rs b/ts/src/lower/body.rs index 3f625cbf3..22480e889 100644 --- a/ts/src/lower/body.rs +++ b/ts/src/lower/body.rs @@ -423,19 +423,28 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { Some(field) => self.operand(field.expr, out).typed(), }) } - ExprKind::Call { ty, args, .. } - if let ty::FnDef(def_id, _) = *self.subst(*ty).kind() - && super::iter::is_next(self.t.tcx, def_id) => - { - self.visit(id); - let recv_ty = self.ty_of(args[0]); - let recv = self.operand(args[0], out); - Some(super::iter::next(self, recv_ty, recv)) - } - _ => None, + _ => self.raw_next(id, out), } } + /// `id` lowered by the iterator protocol if it is a call to `Iterator::next`: an item + /// or `rt.DONE`, rather than an `Option`. + fn raw_next(&mut self, id: ExprId, out: &mut Vec) -> Option { + let ExprKind::Call { ty, args, .. } = &self.thir[self.strip(id)].kind else { + return None; + }; + let ty::FnDef(def_id, _) = *self.subst(*ty).kind() else { + return None; + }; + if !super::iter::is_next(self.t.tcx, def_id) { + return None; + } + self.visit(id); + let recv_ty = self.ty_of(args[0]); + let recv = self.operand(args[0], out); + Some(super::iter::next(self, recv_ty, recv)) + } + fn block(&mut self, id: BlockId, dest: Dest, out: &mut Vec) { let block = &self.thir[id]; if block.targeted_by_break { @@ -1193,7 +1202,16 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { }; let inner = args[0]; let inner_ty = self.ty_of(inner); - let value = self.operand(inner, out); + // `it.next()?` tests for the end itself, so its item may be an `Option`. + let raw = self.raw_next(inner, out); + let none = match raw { + Some(_) => Expr::Rt("DONE"), + None => Expr::undefined(), + }; + let value = match raw { + Some(value) => value, + None => self.operand(inner, out), + }; let name = match value { Expr::Ident(ref name) => name.clone(), value => { @@ -1218,7 +1236,7 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { self.push( out, StmtKind::If { - cond: t.clone().binary("===", Expr::undefined()), + cond: t.clone().binary("===", none), then: vec![ StmtKind::Return(Some(match self.done { true => Expr::Rt("DONE"), diff --git a/ts/src/lower/iter.rs b/ts/src/lower/iter.rs index 56a542d1b..6d901db82 100644 --- a/ts/src/lower/iter.rs +++ b/ts/src/lower/iter.rs @@ -245,11 +245,12 @@ pub fn call<'tcx>( let rest: Vec = values.collect(); // `next` by the protocol, as an `Option`. - if matches!(name, "next" | "next_back") { + if matches!(name, "next" | "next_back" | "nth_back") { option_ok(b, item, span); let item = match name { "next" => next(b, self_ty, recv), - _ => recv.member("nextBack").call(Vec::new()), + "next_back" => recv.member("nextBack").call(Vec::new()), + _ => recv.member("nthBackItem").call(rest), }; return Some(rt_iter("toOption").call(vec![item])); } @@ -296,7 +297,9 @@ pub fn call<'tcx>( match () { _ if b.needs_copy(*inner) => method("map", vec![Expr::Rt("copy")]), // A JS string is a value already. - _ if b.t.is_copy(*inner) || matches!(inner.kind(), ty::Adt(adt, _) if Std::of(tcx, adt.did()) == Some(Std::String)) => { + _ if b.t.is_copy(*inner) + || matches!(inner.kind(), ty::Adt(adt, _) if Std::of(tcx, adt.did()) == Some(Std::String)) => + { Some(recv) } // Each item through its `Clone`. diff --git a/ts/src/lower/library.rs b/ts/src/lower/library.rs index 4bc5fea8d..14b989c35 100644 --- a/ts/src/lower/library.rs +++ b/ts/src/lower/library.rs @@ -375,7 +375,12 @@ pub fn call<'tcx>(b: &mut Body<'_, '_, 'tcx>, call: &Call<'tcx>, args: Args<'_>, ("Result", "unwrap_or") => rt2(b, args, out, "result", "unwrapOr"), // Ranges, which are their own iterators. - ("RangeInclusive", "new") if range => Expr::New(Box::new(Expr::Rt("RangeInclusive")), b.args(args, out)), + // An integer range wraps within its type's bounds where std's `nth` does. + ("RangeInclusive", "new") if range => { + let mut values = b.args(args, out); + values.extend(self_ty().and_then(Int::of).map(|int| Expr::Rt(int.rt()))); + Expr::New(Box::new(Expr::Rt("RangeInclusive")), values) + } ("Range" | "RangeInclusive", "is_empty" | "contains" | "start" | "end") if range => { let mut values = b.args(args, out); let recv = values.remove(0); From 5a651c052af28fc6408a791f5f4e5046494cc2a3 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 24 Oct 2026 01:29:02 -0700 Subject: [PATCH 05/13] chore: finish the iterator quest; share the Option check Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- quest/m2/README.md | 1 - quest/m2/ts-collections.md | 4 +++- quest/m2/ts-iter.md | 28 ---------------------------- quest/m2/ts-model.md | 1 - ts/runtime/rt/iter.ts | 6 +++--- ts/src/lower/body.rs | 11 ++--------- ts/src/lower/iter.rs | 2 +- 7 files changed, 9 insertions(+), 44 deletions(-) delete mode 100644 quest/m2/ts-iter.md diff --git a/quest/m2/README.md b/quest/m2/README.md index d70161207..74fb444ab 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -20,7 +20,6 @@ deletes the quests that follow it. - [Drop in the TypeScript translator](/quest/m2/ts-drop.md) - a `Drop` type's `drop()` runs where Rust drops it - [Async in the TypeScript translator](/quest/m2/ts-async.md) - async code translates with Rust's lazy futures - [Shared ownership](/quest/m2/ts-shared.md) - `Arc`/`Rc`/`Weak` keep exact counts, and locks and atomics become plain values -- [Iterators](/quest/m2/ts-iter.md) - lazy `rt.ts` adaptors with a `DONE` protocol - [Collections](/quest/m2/ts-collections.md) - maps, sets, and queues keep Rust's key semantics - [Methods on generic types](/quest/m2/ts-generic-methods.md) - generic classes' methods type-check, derived `Debug` included - [Source order](/quest/m2/ts-source-order.md) - each module's items come out in Rust source order diff --git a/quest/m2/ts-collections.md b/quest/m2/ts-collections.md index 490d021c3..fb4d42be8 100644 --- a/quest/m2/ts-collections.md +++ b/quest/m2/ts-collections.md @@ -19,10 +19,12 @@ by the translated `Ord`, and `VecDeque` a ring buffer. - Which keys are primitive depends on the [wide-integer representation](/quest/m2/ts-wide-ints.md): a hi/lo `u64` is not one. +- Each collection's iterators (`iter`, `values`, `range`, `drain`) are `rt.Iter`s, + as `rt/iter.ts` makes std's, so the adaptors over them already translate. moq-net's + `BTreeMap::range` takes `RangeFrom` and `RangeTo`, which are not values yet. Public API: the translator's output for collections. ## Required - [Wide integer spike](/quest/m2/ts-wide-ints.md) - which keys lower to primitives -- [Iterators in the TypeScript translator](/quest/m2/ts-iter.md) - collections iterate through the same protocol diff --git a/quest/m2/ts-iter.md b/quest/m2/ts-iter.md deleted file mode 100644 index 2121dc2e3..000000000 --- a/quest/m2/ts-iter.md +++ /dev/null @@ -1,28 +0,0 @@ -# [M] Iterators in the TypeScript translator - -## Goal - -[rust2-ts](/ts/README.md) translates iterator adaptor chains and user -`impl Iterator`, with Rust's laziness and short-circuiting, proven by -differential tests, including iterators whose items are `Option`. - -## Plan - -Decided with the maintainer on 2026-10-23: std's adaptors live in `rt.ts` as -lazy classes, since the driver only has THIR for the local crate. Their -`next()` returns the item or a unique `DONE` symbol, so an iterator of -`Option` never collapses into the refused nested `Option`. A user -`impl Iterator` translates to the same protocol, and `for` loops drive any -iterator through it. - -- `for` accepts only a real iterator. Today it treats the first argument of - whatever call built the iterator as a slice when the type is `slice::Iter`; - the formatting PR (#8) fixed the same bug in `DebugList::entries`. Cover it - with a differential case. - -- Measure against ES iterator helpers on the [TS benchmark](/ts/README.md#bench) - and record the numbers, but keep the `DONE` protocol unless they show it loses. -- Cover what moq-net uses (about 1,100 adaptor sites); the - census (`just ts census`) names them. - -Public API: the translator's output for iterators. diff --git a/quest/m2/ts-model.md b/quest/m2/ts-model.md index 23ff5d609..120746c10 100644 --- a/quest/m2/ts-model.md +++ b/quest/m2/ts-model.md @@ -35,6 +35,5 @@ Public API: the translator's output for moq-net's model. - [Drop in the TypeScript translator](/quest/m2/ts-drop.md) - kio closes a channel when its last producer drops - [Async in the TypeScript translator](/quest/m2/ts-async.md) - kio's producers and consumers are futures - [Shared ownership](/quest/m2/ts-shared.md) - kio's waiters are `Arc`/`Weak` with observable counts -- [Iterators](/quest/m2/ts-iter.md) - the model's adaptor chains - [Collections](/quest/m2/ts-collections.md) - the model's maps and queues - [Methods on generic types](/quest/m2/ts-generic-methods.md) - the model's generic types diff --git a/ts/runtime/rt/iter.ts b/ts/runtime/rt/iter.ts index 48fdc5e14..1f3e01946 100644 --- a/ts/runtime/rt/iter.ts +++ b/ts/runtime/rt/iter.ts @@ -283,12 +283,12 @@ export abstract class Iter implements Iterable { } /** The item `next` returned, as an `Option`. */ -export function toOption(item: T | Done): T | undefined { +function toOption(item: T | Done): T | undefined { return item === DONE ? undefined : item; } /** An `Option`, as `next` returns it. */ -export function fromOption(option: T | undefined): T | Done { +function fromOption(option: T | undefined): T | Done { return option === undefined ? DONE : option; } @@ -1085,7 +1085,7 @@ export class RangeInclusive extends Iter { } /** An integer type's least and greatest values. */ -export interface Bounds { +interface Bounds { readonly MIN: T; readonly MAX: T; } diff --git a/ts/src/lower/body.rs b/ts/src/lower/body.rs index 22480e889..1dd9ddf09 100644 --- a/ts/src/lower/body.rs +++ b/ts/src/lower/body.rs @@ -322,15 +322,8 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { } } Dest::Return if self.done => { - let ty::Adt(_, args) = self.ret.kind() else { - unreachable!("Iterator::next returns an Option") - }; - if !self.t.option_payload_ok(args.type_at(0)) { - let ret = self.ret; - self.t.err( - span, - format!("`{ret}` is not supported: its `None` and `Some` would both be `undefined`"), - ); + if let ty::Adt(_, args) = self.ret.kind() { + super::iter::option_ok(self, args.type_at(0), span); } let value = Expr::Rt("iter").member("fromOption").call(vec![value]); self.push(out, StmtKind::Return(Some(value)), span); diff --git a/ts/src/lower/iter.rs b/ts/src/lower/iter.rs index 6d901db82..5290acf54 100644 --- a/ts/src/lower/iter.rs +++ b/ts/src/lower/iter.rs @@ -158,7 +158,7 @@ fn into_fn<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, span: Span) -> Option } /// Refuse an `Option` of `ty` that `T | undefined` can't hold, as the type would be. -fn option_ok<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, span: Span) { +pub fn option_ok<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, span: Span) { if !b.t.option_payload_ok(ty) { b.t.err( span, From 65f1bd917f19df2a3028229adff5fdfcedcd8c9b Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 24 Oct 2026 05:02:34 -0700 Subject: [PATCH 06/13] docs(ts): iterators in the README and bench, with measured numbers Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- ts/README.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/ts/README.md b/ts/README.md index b2d4dacaf..9c28aa933 100644 --- a/ts/README.md +++ b/ts/README.md @@ -108,7 +108,8 @@ Imports name the `.ts` file, which Node, Bun, and bundlers run as is. A `tsc` bu - Iterators are the runtime's lazy classes rather than generators or ES iterator helpers: std's adaptors have no THIR to lower, ES helpers lack most of them (`rev`, `zip`, `peekable`), and a `next` returning an item or `rt.DONE` allocates - nothing per item. + nothing per item. `just ts bench` measures chains of `map`, `filter`, `any`, `skip`, + and `take` 1.4-8x faster than the same ES helpers in Node, Bun, and Chrome. - Bodies lower from THIR, which keeps loops, `match`, and `?` shaped as written. It does not say where values drop; [Drop](/quest/m2/ts-drop.md) decides how to get that from MIR. @@ -160,9 +161,10 @@ nix develop .#bench --command just ts bench # report to target/bench/report.md ``` The codec fixture's `bench::run` holds the cases moq's own TypeScript benchmarks time -(`js/net/bench/varint.ts` and `lite-varint.ts`), at the pinned moq revision. Each runs -four ways: natively, as wasm32 at moq-wasm's size-optimized profile, as moq's -hand-written `js/net` (`bench/moq.ts`), and translated; every route's output must match +(`js/net/bench/varint.ts` and `lite-varint.ts`), at the pinned moq revision, and +iterator chains over bytes. 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 ES iterator +helpers in `bench/iter.ts`), and translated; every route's output must match native Rust's first. The JS routes run in Node, Bun, and Chrome, with the WASM route's copies across the boundary timed apart from its compute, and an A/A copy of the translation to show the noise. The report records the host and engine versions, and From 6b4fc70b62ffb095ee7a3c7af422a8451594885c Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 24 Oct 2026 05:15:25 -0700 Subject: [PATCH 07/13] fix(ts): Take's nth and nth_back, and an exhausted RangeInclusive contains nothing, as std's Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- ts/fixtures/lang/expected/iters.ts | 7 +++++++ ts/fixtures/lang/src/iters.rs | 8 ++++++++ ts/runtime/rt/iter.ts | 28 +++++++++++++++++++++++++++- 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/ts/fixtures/lang/expected/iters.ts b/ts/fixtures/lang/expected/iters.ts index 4ae8bc7d9..b8192797c 100644 --- a/ts/fixtures/lang/expected/iters.ts +++ b/ts/fixtures/lang/expected/iters.ts @@ -230,6 +230,11 @@ export function consumers(kind: number, data: Uint8Array, out: rt.ByteVec): void }), 0xee)); out.push(rt.option.unwrapOr(rt.iter.slice(data).last(), 0xdd)); out.push(rt.option.unwrapOr(rt.iter.slice(data).nth(3), 0xcc)); + let it: rt.Iter = rt.iter.slice(data); + out.push(rt.option.unwrapOr(rt.iter.toOption(it.take(rt.usize.rem(data.length, 3)).nthBackItem(1)), 0xbb)); + out.push(it.len() & 0xff); + out.push(rt.option.unwrapOr(it.take(rt.usize.rem(data.length, 5)).nth(2), 0xbb)); + out.push(it.len() & 0xff); } else if (value === 3) { const wide = rt.iter.slice(data).map((arg11: number): bigint => { const b11 = arg11; @@ -327,9 +332,11 @@ export function ranges(data: Uint8Array, out: rt.ByteVec): void { out.push(near.start()); out.push(near.isEmpty() ? 1 : 0); out.push(near.contains(255) ? 1 : 0); + out.push(near.contains(100) ? 1 : 0); let low: rt.RangeInclusive = new rt.RangeInclusive(0, b, rt.u8); out.push(rt.option.unwrapOr(rt.iter.toOption(low.nthBackItem(rt.usize.rem(a, 16))), 0xee)); out.push(low.end()); + out.push(low.contains(rt.u8.div(b, 2)) ? 1 : 0); let stepped: rt.RangeInclusive = new rt.RangeInclusive(a, b, rt.u8); out.push(stepped.stepBy(3).count() & 0xff); out.push(stepped.start()); diff --git a/ts/fixtures/lang/src/iters.rs b/ts/fixtures/lang/src/iters.rs index cf5b190b1..76db0de68 100644 --- a/ts/fixtures/lang/src/iters.rs +++ b/ts/fixtures/lang/src/iters.rs @@ -143,6 +143,12 @@ pub fn consumers(kind: u8, data: &[u8], out: &mut Vec) { out.push(*data.iter().min_by_key(|&&b| b / 16).unwrap_or(&0xee)); out.push(data.iter().last().copied().unwrap_or(0xdd)); out.push(data.iter().nth(3).copied().unwrap_or(0xcc)); + // `take`'s `nth` and `nth_back` read on past its limit as std's do. + let mut it = data.iter(); + out.push(it.by_ref().take(data.len() % 3).nth_back(1).copied().unwrap_or(0xbb)); + out.push(it.len() as u8); + out.push(it.by_ref().take(data.len() % 5).nth(2).copied().unwrap_or(0xbb)); + out.push(it.len() as u8); } 3 => { let wide: u64 = data.iter().map(|&b| b as u64 * 0x0101_0101_0101).sum(); @@ -219,9 +225,11 @@ pub fn ranges(data: &[u8], out: &mut Vec) { out.push(*near.start()); out.push(near.is_empty() as u8); out.push(near.contains(&255) as u8); + out.push(near.contains(&100) as u8); let mut low = 0..=b; out.push(low.nth_back(a as usize % 16).unwrap_or(0xee)); out.push(*low.end()); + out.push(low.contains(&(b / 2)) as u8); let mut stepped = a..=b; out.push(stepped.by_ref().step_by(3).count() as u8); out.push(*stepped.start()); diff --git a/ts/runtime/rt/iter.ts b/ts/runtime/rt/iter.ts index 1f3e01946..e09db2d2f 100644 --- a/ts/runtime/rt/iter.ts +++ b/ts/runtime/rt/iter.ts @@ -621,6 +621,31 @@ class TakeIter extends Iter { return this.inner.nthBackItem(Math.max(this.inner.len() - n, 0)); } + /** std's `nth`: past the limit, it reads the rest of the limit through and ends. */ + override nthItem(n: number): T | Done { + if (this.n > n) { + this.n -= n + 1; + return this.inner.nthItem(n); + } + if (this.n > 0) { + this.inner.nthItem(this.n - 1); + this.n = 0; + } + return DONE; + } + + /** std's `nth_back`: past the limit, it reads the whole rest from the back, even at a limit of 0. */ + override nthBackItem(n: number): T | Done { + const len = this.inner.len(); + if (this.n > n) { + const m = Math.max(len - this.n, 0) + n; + this.n -= n + 1; + return this.inner.nthBackItem(m); + } + if (len > 0) this.inner.nthBackItem(len - 1); + return DONE; + } + override len(): number { return Math.min(this.inner.len(), this.n); } @@ -1034,7 +1059,8 @@ export class RangeInclusive extends Iter { } contains(item: T): boolean { - return !lt(item, this.lo) && (this.exhausted ? lt(item, this.hi) : !lt(this.hi, item)); + // Exhausted, it may have wrapped past its end, and contains nothing, as std's bounds say. + return !this.exhausted && !lt(item, this.lo) && !lt(this.hi, item); } clone(): RangeInclusive { From b081a81c2e6bb26103c98678b047f968c6b32a88 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 24 Oct 2026 05:37:34 -0700 Subject: [PATCH 08/13] fix(ts): a zip of TrustedRandomAccess iterators reads no further than std's, and refuse one fold and next read apart Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- ts/fixtures/lang/expected/iters.ts | 108 +++++++++++++++++++++-------- ts/fixtures/lang/src/iters.rs | 28 ++++++++ ts/fixtures/reject/census.md | 13 +++- ts/fixtures/reject/expected.txt | 1 + ts/fixtures/reject/src/lib.rs | 6 ++ ts/fixtures/reject/ts.toml | 1 + ts/runtime/rt/iter.ts | 37 +++++++++- ts/src/census.rs | 2 + ts/src/lower/iter.rs | 67 +++++++++++++++++- ts/src/main.rs | 2 + 10 files changed, 230 insertions(+), 35 deletions(-) diff --git a/ts/fixtures/lang/expected/iters.ts b/ts/fixtures/lang/expected/iters.ts index b8192797c..1a6d9669b 100644 --- a/ts/fixtures/lang/expected/iters.ts +++ b/ts/fixtures/lang/expected/iters.ts @@ -217,16 +217,66 @@ export function consumers(kind: number, data: Uint8Array, out: rt.ByteVec): void out.push(rt.option.mapOr(position, 0xff, (p: number): number => { return p & 0xff; })); + const short = rt.slice.range(data, rt.usize.div(data.length, 2)); + const logged = (b9: number): number => { + return b9; + }; + const pairs = rt.iter.slice(data).map((arg9: number): number => { + const b10 = arg9; + log.push(b10); + return b10; + }).zip(rt.iter.slice(short), true).count(); + out.push(pairs & 0xff); + const walked = rt.iter.slice(data).map((arg10: number): number => { + const b11 = arg10; + log.push(~b11 & 0xff); + return b11; + }); + const iter = walked.zip(rt.iter.slice(short), true); + while (true) { + const item = iter.next(); + if (item === rt.DONE) { + break; + } + const a = item[0]; + const b12 = item[1]; + out.push(a ^ b12); + } + const iter2 = rt.iter.slice(data).inspect((arg11: number): void => { + const b13 = arg11; + log.push(b13); + }).map(logged).zip(rt.iter.slice(short)); + while (true) { + const item2 = iter2.next(); + if (item2 === rt.DONE) { + break; + } + const a2 = item2[0]; + const b14 = item2[1]; + out.push(a2 ^ b14); + } + let back: rt.Iter<[number, number]> = rt.iter.slice(data).map((arg12: number): number => { + const b15 = arg12; + return rotateFree(b15); + }).zip(rt.iter.slice(short), true); + const nextBack = rt.iter.toOption(back.nextBack()); + if (nextBack !== undefined) { + const a3 = nextBack[0]; + const b16 = nextBack[1]; + out.push(a3); + out.push(b16); + } + out.push(back.len() & 0xff); } else if (value === 2) { out.push(rt.option.unwrapOr(rt.iter.slice(data).max(), 0)); out.push(rt.option.unwrapOr(rt.iter.slice(data).min(), 0xff)); - out.push(rt.option.unwrapOr(rt.iter.slice(data).maxByKey((arg9: number): number => { - const b9 = arg9; - return rt.u8.rem(b9, 16); + out.push(rt.option.unwrapOr(rt.iter.slice(data).maxByKey((arg13: number): number => { + const b17 = arg13; + return rt.u8.rem(b17, 16); }), 0xee)); - out.push(rt.option.unwrapOr(rt.iter.slice(data).minByKey((arg10: number): number => { - const b10 = arg10; - return rt.u8.div(b10, 16); + out.push(rt.option.unwrapOr(rt.iter.slice(data).minByKey((arg14: number): number => { + const b18 = arg14; + return rt.u8.div(b18, 16); }), 0xee)); out.push(rt.option.unwrapOr(rt.iter.slice(data).last(), 0xdd)); out.push(rt.option.unwrapOr(rt.iter.slice(data).nth(3), 0xcc)); @@ -236,35 +286,35 @@ export function consumers(kind: number, data: Uint8Array, out: rt.ByteVec): void out.push(rt.option.unwrapOr(it.take(rt.usize.rem(data.length, 5)).nth(2), 0xbb)); out.push(it.len() & 0xff); } else if (value === 3) { - const wide = rt.iter.slice(data).map((arg11: number): bigint => { - const b11 = arg11; - return rt.u64.mul(BigInt(b11), 0x0101_0101_0101n); + const wide = rt.iter.slice(data).map((arg15: number): bigint => { + const b19 = arg15; + return rt.u64.mul(BigInt(b19), 0x0101_0101_0101n); }).sum(rt.u64.add, 0n); out.extendFromSlice(rt.u64.toBeBytes(wide)); - const folded = rt.iter.slice(data).fold(0, (acc: number, arg12: number): number => { - const b12 = arg12; - return rt.u32.wrappingAdd((acc << 3) >>> 0, (b12 ^ acc) >>> 0); + const folded = rt.iter.slice(data).fold(0, (acc: number, arg16: number): number => { + const b20 = arg16; + return rt.u32.wrappingAdd((acc << 3) >>> 0, (b20 ^ acc) >>> 0); }); out.extendFromSlice(rt.u32.toBeBytes(folded)); - rt.iter.slice(data).forEach((arg13: number): void => { - const b13 = arg13; - log.push(rotateFree(b13)); + rt.iter.slice(data).forEach((arg17: number): void => { + const b21 = arg17; + log.push(rotateFree(b21)); }); } else if (value === 4) { const total = rt.iter.slice(data).sum(rt.u8.add, 0); out.push(total); - const product = rt.iter.slice(data).map((arg14: number): number => { - const b14 = arg14; - return rt.u16.add(rt.u16.rem(b14, 4), 1); + const product = rt.iter.slice(data).map((arg18: number): number => { + const b22 = arg18; + return rt.u16.add(rt.u16.rem(b22, 4), 1); }).product(rt.u16.mul, 1); out.extendFromSlice(rt.u16.toBeBytes(product)); } else { - const parsed = rt.iter.slice(data).map((arg15: number): rt.Result => { - const b15 = arg15; - if (b15 < 0xf0) { - return rt.Ok(b15); + const parsed = rt.iter.slice(data).map((arg19: number): rt.Result => { + const b23 = arg19; + if (b23 < 0xf0) { + return rt.Ok(b23); } else { - return rt.Err(b15); + return rt.Err(b23); } }).collectResult((values__: rt.Iter) => values__.toByteVec()); if (parsed.ok) { @@ -274,12 +324,12 @@ export function consumers(kind: number, data: Uint8Array, out: rt.ByteVec): void const e = parsed.error; out.push(e); } - const some = rt.iter.slice(data).inspect((arg16: number): void => { - const b16 = arg16; - log.push(b16); - }).map((arg17: number): number | undefined => { - const b17 = arg17; - return rt.thenSome(b17 !== 0x20, b17); + const some = rt.iter.slice(data).inspect((arg20: number): void => { + const b24 = arg20; + log.push(b24); + }).map((arg21: number): number | undefined => { + const b25 = arg21; + return rt.thenSome(b25 !== 0x20, b25); }).collectOption((values__: rt.Iter) => values__.toArray()); out.push(rt.option.mapOr(some, 0xff, (v2: number[]): number => { return v2.length & 0xff; diff --git a/ts/fixtures/lang/src/iters.rs b/ts/fixtures/lang/src/iters.rs index 76db0de68..b88164303 100644 --- a/ts/fixtures/lang/src/iters.rs +++ b/ts/fixtures/lang/src/iters.rs @@ -135,6 +135,34 @@ pub fn consumers(kind: u8, data: &[u8], out: &mut Vec) { out.push(mapped.unwrap_or(0)); let position = data.iter().position(|&b| b == 0x40); out.push(position.map_or(0xff, |p| p as u8)); + // A zip of slices reads the longer side's closure only up to the shorter's length. + let short = &data[data.len() / 2..]; + let logged = |b: &u8| *b; + let pairs = data + .iter() + .map(|&b| { + log.push(b); + b + }) + .zip(short) + .count(); + out.push(pairs as u8); + let walked = data.iter().map(|&b| { + log.push(!b); + b + }); + for (a, b) in walked.zip(short.iter()) { + out.push(a ^ b); + } + for (a, &b) in data.iter().inspect(|&&b| log.push(b)).map(logged).zip(short.iter()) { + out.push(a ^ b); + } + let mut back = data.iter().map(|&b| b.rotate_free()).zip(short.iter().copied()); + if let Some((a, b)) = back.next_back() { + out.push(a); + out.push(b); + } + out.push(back.len() as u8); } 2 => { out.push(data.iter().copied().max().unwrap_or(0)); diff --git a/ts/fixtures/reject/census.md b/ts/fixtures/reject/census.md index beca8f559..15c4ec627 100644 --- a/ts/fixtures/reject/census.md +++ b/ts/fixtures/reject/census.md @@ -1,7 +1,7 @@ # Census of `reject-fixture` -- Refusals: 40 -- Function instances: 26 +- Refusals: 41 +- Function instances: 27 - Generic functions no instance reached, lowered over their type parameters: 2 ## By construct @@ -20,6 +20,7 @@ | 1 | `Pointer` formatting, as in `{:p}`, is not supported | fixtures/reject/src/lib.rs:68:3 | | 1 | `_` implements `Drop`, which is not supported yet | fixtures/reject/src/lib.rs:43:1 | | 1 | `_` is async, which is not supported yet | fixtures/reject/src/lib.rs:53:1 | +| 1 | a `zip` of `_`, whose closures std calls one item further through `next` than through `fold` | fixtures/reject/src/lib.rs:161:51 | | 1 | a cast from `_` | fixtures/reject/src/lib.rs:11:2 | | 1 | a cast to `_` | fixtures/reject/src/lib.rs:110:2 | | 1 | a closure that copies a mutable variable | fixtures/reject/src/lib.rs:80:18 | @@ -54,6 +55,7 @@ The type or module each refused type or path belongs to. | 1 | `Iterator` | fixtures/reject/src/lib.rs:128:2 | | 1 | `dyn std::ops::Fn(u8) -> u8` | fixtures/reject/src/lib.rs:14:16 | | 1 | `later` | fixtures/reject/src/lib.rs:53:1 | +| 1 | `std::iter::Chain` | fixtures/reject/src/lib.rs:161:51 | | 1 | `std::slice::Iter` | fixtures/reject/src/lib.rs:115:11 | | 1 | `std::slice` | fixtures/reject/src/lib.rs:36:4 | @@ -70,6 +72,7 @@ The type or module each refused type or path belongs to. | 1 | `_`: a JS value of `_` is a copy, so a reference can't change it | `&mut std::string::String`, `std::string::String` | fixtures/reject/src/lib.rs:73:16 | | 1 | `_`: a JS value of `_` is a copy, so a reference can't change it | `&mut u32`, `u32` | fixtures/reject/src/lib.rs:19:13 | | 1 | a `_` binding: a JS value of `_` is a copy, so it can't change | `&mut std::string::String`, `std::string::String` | fixtures/reject/src/lib.rs:73:16 | +| 1 | a `zip` of `_`, whose closures std calls one item further through `next` than through `fold` | `std::iter::Chain` | fixtures/reject/src/lib.rs:161:51 | | 1 | a cast from `_` | `char` | fixtures/reject/src/lib.rs:11:2 | | 1 | a cast to `_` | `f64` | fixtures/reject/src/lib.rs:110:2 | | 1 | a mutable reference to `_`, which is no object it can change in place | `std::string::String` | fixtures/reject/src/lib.rs:75:17 | @@ -169,6 +172,12 @@ The type or module each refused type or path belongs to.
+
a `zip` of `_`, whose closures std calls one item further through `next` than through `fold` (1) + +- fixtures/reject/src/lib.rs:161:51: a `zip` of `std::iter::Chain, {closure@fixtures/reject/src/lib.rs:161:15: 161:18}>, std::iter::Copied>>`, whose closures std calls one item further through `next` than through `fold` + +
+
a cast from `_` (1) - fixtures/reject/src/lib.rs:11:2: a cast from `char` diff --git a/ts/fixtures/reject/expected.txt b/ts/fixtures/reject/expected.txt index 3951554e2..694c6780f 100644 --- a/ts/fixtures/reject/expected.txt +++ b/ts/fixtures/reject/expected.txt @@ -36,5 +36,6 @@ replacing the value behind a reference to `Meters` at fixtures/reject/src/lib.rs replacing the value behind a reference to `Meters` at fixtures/reject/src/lib.rs:99:20 replacing the value behind a reference to `Meters` at fixtures/reject/src/lib.rs:103:25 unsupported call to ` as std::iter::Iterator>::cycle` at fixtures/reject/src/lib.rs:115:11 +a `zip` of `std::iter::Chain, {closure@fixtures/reject/src/lib.rs:161:15: 161:18}>, std::iter::Copied>>`, whose closures std calls one item further through `next` than through `fold` at fixtures/reject/src/lib.rs:161:51 overriding `Iterator::fold`, which std's adaptors call in place of `next` at fixtures/reject/src/lib.rs:128:2 an iterator of the crate implementing `DoubleEndedIterator`, which is not supported yet at fixtures/reject/src/lib.rs:148:1 diff --git a/ts/fixtures/reject/src/lib.rs b/ts/fixtures/reject/src/lib.rs index 329858bac..af0134385 100644 --- a/ts/fixtures/reject/src/lib.rs +++ b/ts/fixtures/reject/src/lib.rs @@ -154,3 +154,9 @@ impl DoubleEndedIterator for Backs { pub fn backs() -> usize { Backs.rev().count() } + +/// A zip that `count` reads through `fold`, which stops at the shorter side, and a `for` +/// through `next`, which calls the first side's closure once more. +pub fn zipped(a: &[u8], b: &[u8]) -> usize { + a.iter().map(|x| x + 1).chain(b.iter().copied()).zip(b).count() +} diff --git a/ts/fixtures/reject/ts.toml b/ts/fixtures/reject/ts.toml index 0d2733a58..091b4d603 100644 --- a/ts/fixtures/reject/ts.toml +++ b/ts/fixtures/reject/ts.toml @@ -21,6 +21,7 @@ roots = [ "cycled", "folds", "backs", + "zipped", "missing", ] printable = ["Meters"] diff --git a/ts/runtime/rt/iter.ts b/ts/runtime/rt/iter.ts index e09db2d2f..24d4347a6 100644 --- a/ts/runtime/rt/iter.ts +++ b/ts/runtime/rt/iter.ts @@ -57,9 +57,13 @@ export abstract class Iter implements Iterable { return new ChainIter(this, other); } - /** `zip`, with `other` already made an iterator, as std does when it is called. */ - zip(other: Iter): Iter<[T, U]> { - return new ZipIter(this, other); + /** + * `zip`, with `other` already made an iterator, as std does when it is called. A + * `trusted` zip is one of two `TrustedRandomAccess` iterators, which std reads by + * index up to the shorter length and never past it. + */ + zip(other: Iter, trusted = false): Iter<[T, U]> { + return trusted ? new TrustedZipIter(this, other) : new ZipIter(this, other); } rev(): Iter { @@ -552,6 +556,33 @@ class ZipIter extends Iter<[A, B]> { } } +/** std's `Zip` of two `TrustedRandomAccess` iterators, which counts the pairs it reads. */ +class TrustedZipIter extends ZipIter { + private index = 0; + private end: number; + + constructor(a: Iter, b: Iter) { + super(a, b); + this.end = Math.min(a.len(), b.len()); + } + + override next(): [A, B] | Done { + if (this.index >= this.end) return DONE; + this.index++; + return super.next(); + } + + override nextBack(): [A, B] | Done { + if (this.index >= this.end) return DONE; + this.end--; + return super.nextBack(); + } + + override len(): number { + return this.end - this.index; + } +} + class RevIter extends Iter { private readonly inner: Iter; diff --git a/ts/src/census.rs b/ts/src/census.rs index e17b92378..564ef1985 100644 --- a/ts/src/census.rs +++ b/ts/src/census.rs @@ -152,6 +152,7 @@ const VERBATIM: &[&str] = &[ "UpperExp", "bool", "break", + "fold", "for", "format_args!", "if", @@ -161,6 +162,7 @@ const VERBATIM: &[&str] = &[ "ref mut", "self", "undefined", + "zip", "{:.2}", "{:1$}", "{:E}", diff --git a/ts/src/lower/iter.rs b/ts/src/lower/iter.rs index 5290acf54..3f158e4d9 100644 --- a/ts/src/lower/iter.rs +++ b/ts/src/lower/iter.rs @@ -277,10 +277,26 @@ pub fn call<'tcx>( option_ok(b, item, span); method(&super::camel(name), rest) } - "chain" | "zip" => { + "chain" => { let other = into_iter(b, targ(1)?, rest.into_iter().next()?, span)?; method(name, vec![other]) } + "zip" => { + let other = into_iter(b, targ(1)?, rest.into_iter().next()?, span)?; + let sig = tcx.fn_sig(call.def_id).instantiate(tcx, gargs).skip_norm_wip(); + let zip = b.t.subst( + ty::List::empty(), + tcx.instantiate_bound_regions_with_erased(sig).output(), + ); + let ty::Adt(_, sides) = zip.kind() else { + return None; + }; + let mut args = vec![other]; + if zip_reads_by_index(b, sides.type_at(0), sides.type_at(1), span) { + args.push(Expr::lit("true")); + } + method(name, args) + } "flat_map" => { let mut rest = rest; rest.extend(into_fn(b, targ(1)?, span)?); @@ -323,6 +339,55 @@ pub fn call<'tcx>( } } +/// Whether std's `Zip` of `a` and `b` reads both by index, never past the shorter: when +/// both are `TrustedRandomAccess`. Refuse one that `fold` reads that way but `next` does +/// not, where the first calls a closure that would see the difference. +fn zip_reads_by_index<'tcx>(b: &mut Body<'_, '_, 'tcx>, a: Ty<'tcx>, other: Ty<'tcx>, span: Span) -> bool { + let both = |b: &mut Body<'_, '_, 'tcx>, name: &str| implements(b.t, a, name) && implements(b.t, other, name); + if both(b, "TrustedRandomAccess") { + return true; + } + if (both(b, "TrustedRandomAccessNoCoerce") || both(b, "TrustedLen")) && calls_closures(b.t.tcx, a) { + b.t.err( + span, + format!("a `zip` of `{a}`, whose closures std calls one item further through `next` than through `fold`"), + ); + } + false +} + +/// Whether `ty`, an iterator, has std's closure-calling adaptors in it, which std counts as +/// side effects. +fn calls_closures(tcx: ty::TyCtxt<'_>, ty: Ty<'_>) -> bool { + ty.walk().filter_map(|arg| arg.as_type()).any(|t| match t.kind() { + ty::Adt(adt, _) => matches!( + library::std_path(tcx, adt.did()).last().map(String::as_str), + Some("Map" | "Cloned" | "Inspect") + ), + _ => false, + }) +} + +/// Whether `ty` implements `core`'s trait `name`, one of the iterator markers std +/// specializes on and names nowhere else. +fn implements<'tcx>(t: &Translator<'_, 'tcx>, ty: Ty<'tcx>, name: &str) -> bool { + use rustc_infer::infer::TyCtxtInferExt; + use rustc_trait_selection::infer::InferCtxtExt; + let tcx = t.tcx; + let Some(&core) = tcx + .crates(()) + .iter() + .find(|&&c| tcx.crate_name(c) == rustc_span::sym::core) + else { + return false; + }; + let Some(&tr) = tcx.traits(core).iter().find(|&&tr| tcx.item_name(tr).as_str() == name) else { + return false; + }; + let (infcx, env) = tcx.infer_ctxt().build_with_typing_env(t.typing_env_pub()); + infcx.type_implements_trait(tr, [ty], env).must_apply_modulo_regions() +} + /// Whether `ty` compares in JS as Rust's `Ord` does: an integer or a `bool`. fn primitive(ty: Ty<'_>) -> bool { matches!(ty.kind(), ty::Bool | ty::Int(_) | ty::Uint(_)) diff --git a/ts/src/main.rs b/ts/src/main.rs index 231f3f3e7..b245fd7dc 100644 --- a/ts/src/main.rs +++ b/ts/src/main.rs @@ -14,10 +14,12 @@ extern crate rustc_data_structures; extern crate rustc_driver; extern crate rustc_hir; extern crate rustc_index; +extern crate rustc_infer; extern crate rustc_interface; extern crate rustc_middle; extern crate rustc_session; extern crate rustc_span; +extern crate rustc_trait_selection; mod census; mod cli; From 40fabdef8946dac1863bb9fb1a2ca2dd01d5340b Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 24 Oct 2026 05:45:17 -0700 Subject: [PATCH 09/13] fix(ts): Rev and Enumerate forward nth and nth_back as std's do Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- ts/fixtures/lang/expected/iters.ts | 26 +++++++++++++++++++------- ts/fixtures/lang/src/iters.rs | 9 +++++++++ ts/runtime/rt/iter.ts | 22 ++++++++++++++++++++++ 3 files changed, 50 insertions(+), 7 deletions(-) diff --git a/ts/fixtures/lang/expected/iters.ts b/ts/fixtures/lang/expected/iters.ts index 1a6d9669b..d86dffa4e 100644 --- a/ts/fixtures/lang/expected/iters.ts +++ b/ts/fixtures/lang/expected/iters.ts @@ -387,19 +387,31 @@ export function ranges(data: Uint8Array, out: rt.ByteVec): void { out.push(rt.option.unwrapOr(rt.iter.toOption(low.nthBackItem(rt.usize.rem(a, 16))), 0xee)); out.push(low.end()); out.push(low.contains(rt.u8.div(b, 2)) ? 1 : 0); + let reversed: rt.RangeInclusive = new rt.RangeInclusive(a, b, rt.u8); + out.push(rt.option.unwrapOr(reversed.rev().nth(a), 0xee)); + out.push(reversed.end()); + out.push(reversed.contains(a) ? 1 : 0); + let counted: rt.Iter<[number, number]> = new rt.RangeInclusive(a, b, rt.u8).enumerate(); + const [i3, n] = rt.option.unwrapOr(counted.nth(rt.usize.div(b, 4)), [0xdd, 0xdd] as [number, number]); + out.push(i3 & 0xff); + out.push(n); + out.push(rt.option.mapOr(rt.iter.toOption(counted.next()), 0xcc, (arg: [number, number]): number => { + const i4 = arg[0]; + return i4 & 0xff; + })); let stepped: rt.RangeInclusive = new rt.RangeInclusive(a, b, rt.u8); out.push(stepped.stepBy(3).count() & 0xff); out.push(stepped.start()); const signed = new rt.RangeInclusive((a << 24) >> 24, (b << 24) >> 24, rt.i8); - rt.iter.extend(out, signed.rev().map((i3: number): number => { - return i3 & 0xff; + rt.iter.extend(out, signed.rev().map((i5: number): number => { + return i5 & 0xff; })); let wide: rt.Range = new rt.Range(BigInt(a), rt.u64.mul(BigInt(b), 3n)); - out.push(wide.map((n: bigint): bigint => { - return rt.u64.rem(n, 5n); - }).filter((arg: bigint): boolean => { - const n2 = arg; - return n2 === 0n; + out.push(wide.map((n2: bigint): bigint => { + return rt.u64.rem(n2, 5n); + }).filter((arg2: bigint): boolean => { + const n3 = arg2; + return n3 === 0n; }).count() & 0xff); out.push(wide.isEmpty() ? 1 : 0); const total = new rt.Range(0n, BigInt(a)).sum(rt.u64.add, 0n); diff --git a/ts/fixtures/lang/src/iters.rs b/ts/fixtures/lang/src/iters.rs index b88164303..c692f5424 100644 --- a/ts/fixtures/lang/src/iters.rs +++ b/ts/fixtures/lang/src/iters.rs @@ -258,6 +258,15 @@ pub fn ranges(data: &[u8], out: &mut Vec) { out.push(low.nth_back(a as usize % 16).unwrap_or(0xee)); out.push(*low.end()); out.push(low.contains(&(b / 2)) as u8); + let mut reversed = a..=b; + out.push(reversed.by_ref().rev().nth(a as usize).unwrap_or(0xee)); + out.push(*reversed.end()); + out.push(reversed.contains(&a) as u8); + let mut counted = (a..=b).enumerate(); + let (i, n) = counted.nth(b as usize / 4).unwrap_or((0xdd, 0xdd)); + out.push(i as u8); + out.push(n); + out.push(counted.next().map_or(0xcc, |(i, _)| i as u8)); let mut stepped = a..=b; out.push(stepped.by_ref().step_by(3).count() as u8); out.push(*stepped.start()); diff --git a/ts/runtime/rt/iter.ts b/ts/runtime/rt/iter.ts index 24d4347a6..08aedb8a7 100644 --- a/ts/runtime/rt/iter.ts +++ b/ts/runtime/rt/iter.ts @@ -486,6 +486,20 @@ class EnumerateIter extends Iter<[number, T]> { return item === DONE ? DONE : [this.index + this.inner.len(), item]; } + /** std's `nth`, which counts only when it finds an item. */ + override nthItem(n: number): [number, T] | Done { + const item = this.inner.nthItem(n); + if (item === DONE) return DONE; + const i = this.index + n; + this.index = i + 1; + return [i, item]; + } + + override nthBackItem(n: number): [number, T] | Done { + const item = this.inner.nthBackItem(n); + return item === DONE ? DONE : [this.index + this.inner.len(), item]; + } + override len(): number { return this.inner.len(); } @@ -599,6 +613,14 @@ class RevIter extends Iter { return this.inner.next(); } + override nthItem(n: number): T | Done { + return this.inner.nthBackItem(n); + } + + override nthBackItem(n: number): T | Done { + return this.inner.nthItem(n); + } + override len(): number { return this.inner.len(); } From 7607264a2aa3429034e9b7ff4319c2f8ec869ba0 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 24 Oct 2026 05:47:04 -0700 Subject: [PATCH 10/13] test(ts): two instances of a generic own iterator on one class Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- ts/fixtures/lang/expected/iters.ts | 29 +++++++++++++++++++++++ ts/fixtures/lang/src/iters.rs | 37 ++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/ts/fixtures/lang/expected/iters.ts b/ts/fixtures/lang/expected/iters.ts index d86dffa4e..2a371ff2d 100644 --- a/ts/fixtures/lang/expected/iters.ts +++ b/ts/fixtures/lang/expected/iters.ts @@ -597,6 +597,35 @@ export function own(data: Uint8Array, out: rt.ByteVec): void { const firsts = rt.iter.from(byRef, (it: Evens) => it.next()).take(2).toByteVec(); out.extendFromSlice(firsts.view()); out.push(rt.iter.from(byRef, (it: Evens) => it.next()).count() & 0xff); + const halves = rt.iter.from(new Halving({ value: left }), (it: Halving) => it.next$Iterator()).toByteVec(); + out.extendFromSlice(halves.view()); + const total2 = rt.iter.from(new Halving({ value: rt.u64.mul(BigInt(left), 0x1_0000_0001n) }), (it: Halving) => it.next$Iterator$2()).sum(rt.u64.add, 0n); + out.extendFromSlice(rt.u64.toBeBytes(total2)); +} + +/** A value halved until it is zero: generic, with an `impl Iterator` per instance. */ +export class Halving { + value: T; + + constructor(fields: { value: T }) { + this.value = fields.value; + } + + next$Iterator(this: Halving): number | rt.Done { + const value = this.value; + this.value = rt.u8.div(this.value, 2); + return rt.iter.fromOption(rt.thenSome(value !== 0, value)); + } + + next$Iterator$2(this: Halving): bigint | rt.Done { + if (this.value === 0n) { + return rt.DONE; + } else { + const value = this.value; + this.value = value >> 1n; + return value; + } + } } /** diff --git a/ts/fixtures/lang/src/iters.rs b/ts/fixtures/lang/src/iters.rs index c692f5424..a7f310c96 100644 --- a/ts/fixtures/lang/src/iters.rs +++ b/ts/fixtures/lang/src/iters.rs @@ -409,6 +409,43 @@ pub fn own(data: &[u8], out: &mut Vec) { let firsts: Vec = by_ref.by_ref().take(2).collect(); out.extend_from_slice(&firsts); out.push(by_ref.count() as u8); + // Two instances of one generic iterator, each with its own `next` on one class. + let halves: Vec = Halving { value: left }.collect(); + out.extend_from_slice(&halves); + let total: u64 = Halving { + value: left as u64 * 0x1_0000_0001, + } + .sum(); + out.extend_from_slice(&total.to_be_bytes()); +} + +/// A value halved until it is zero: generic, with an `impl Iterator` per instance. +struct Halving { + value: T, +} + +impl Iterator for Halving { + type Item = u8; + + fn next(&mut self) -> Option { + let value = self.value; + self.value /= 2; + (value != 0).then_some(value) + } +} + +impl Iterator for Halving { + type Item = u64; + + fn next(&mut self) -> Option { + match self.value { + 0 => None, + value => { + self.value = value >> 1; + Some(value) + } + } + } } /// The last two bytes of `data`, as a `slice::Iter` made from the second argument: a From deb695d9c18b5d0b27b89f2c1ac33a3673cdfa00 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 24 Oct 2026 05:55:11 -0700 Subject: [PATCH 11/13] fix(ts): cloned calls an own Clone, integer ranges only compare, and size_hint overrides are refused Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- ts/fixtures/lang/expected/iters.ts | 25 +++++++++++++++++ ts/fixtures/lang/src/iters.rs | 14 ++++++++++ ts/fixtures/reject/census.md | 45 +++++++++++++++++------------- ts/fixtures/reject/expected.txt | 4 ++- ts/fixtures/reject/src/lib.rs | 27 ++++++++++++++++++ ts/fixtures/reject/ts.toml | 2 ++ ts/src/lower/iter.rs | 34 +++++++++++++++++++--- ts/src/lower/library.rs | 17 ++++++++--- 8 files changed, 139 insertions(+), 29 deletions(-) diff --git a/ts/fixtures/lang/expected/iters.ts b/ts/fixtures/lang/expected/iters.ts index 2a371ff2d..fdfb42330 100644 --- a/ts/fixtures/lang/expected/iters.ts +++ b/ts/fixtures/lang/expected/iters.ts @@ -691,6 +691,31 @@ export function values(data: Uint8Array, out: rt.ByteVec): void { } out.extendFromSlice(tag2.bytes.view()); } + const bumps = rt.iter.slice(data).map((arg4: number): Bumped => { + const b4 = arg4; + return new Bumped(b4); + }).toArray(); + rt.iter.extend(out, rt.iter.slice(bumps).map((a02: Bumped): Bumped => { + return a02.clone(); + }).map((b5: Bumped): number => { + return b5._0; + })); + rt.iter.extend(out, rt.iter.slice(bumps).map(rt.copy).map((b6: Bumped): number => { + return b6._0; + })); +} + +/** A `Copy` type whose `Clone` does more than copy. */ +export class Bumped { + _0: number; + + constructor(_0: number) { + this._0 = _0; + } + + clone(): Bumped { + return new Bumped(rt.u8.wrappingAdd(this._0, 1)); + } } /** A `Clone` that is not `Copy`, which `cloned` calls for each item. */ diff --git a/ts/fixtures/lang/src/iters.rs b/ts/fixtures/lang/src/iters.rs index a7f310c96..e44024b40 100644 --- a/ts/fixtures/lang/src/iters.rs +++ b/ts/fixtures/lang/src/iters.rs @@ -485,6 +485,20 @@ pub fn values(data: &[u8], out: &mut Vec) { for tag in tags.iter().chain(cloned.iter()) { out.extend_from_slice(&tag.bytes); } + // A `Copy` type's own `Clone`, which `cloned` calls and `copied` does not. + let bumps: Vec = data.iter().map(|&b| Bumped(b)).collect(); + out.extend(bumps.iter().cloned().map(|b| b.0)); + out.extend(bumps.iter().copied().map(|b| b.0)); +} + +/// A `Copy` type whose `Clone` does more than copy. +#[derive(Copy)] +struct Bumped(u8); + +impl Clone for Bumped { + fn clone(&self) -> Self { + Bumped(self.0.wrapping_add(1)) + } } /// A `Clone` that is not `Copy`, which `cloned` calls for each item. diff --git a/ts/fixtures/reject/census.md b/ts/fixtures/reject/census.md index 15c4ec627..73bc886e0 100644 --- a/ts/fixtures/reject/census.md +++ b/ts/fixtures/reject/census.md @@ -1,7 +1,7 @@ # Census of `reject-fixture` -- Refusals: 41 -- Function instances: 27 +- Refusals: 43 +- Function instances: 31 - Generic functions no instance reached, lowered over their type parameters: 2 ## By construct @@ -9,11 +9,12 @@ | Count | Construct | First | | ---: | --- | --- | | 5 | unsupported type `_` | fixtures/reject/src/lib.rs:6:1 | +| 4 | unsupported call to `_` | fixtures/reject/src/lib.rs:36:4 | | 3 | a `_` binding: a JS value of `_` is a copy, so it can't change | fixtures/reject/src/lib.rs:19:13 | | 3 | replacing the value behind a reference to `_` | fixtures/reject/src/lib.rs:99:17 | -| 3 | unsupported call to `_` | fixtures/reject/src/lib.rs:36:4 | | 2 | `_` is not supported: its `None` and `Some` would both be `undefined` | fixtures/reject/src/lib.rs:31:15 | | 2 | `_`: a JS value of `_` is a copy, so a reference can't change it | fixtures/reject/src/lib.rs:19:13 | +| 2 | overriding `_`, which std's adaptors call in place of `next` or beside it | fixtures/reject/src/lib.rs:128:2 | | 1 | `Binary` formatting, as in `{:b}`, is not supported | fixtures/reject/src/lib.rs:63:3 | | 1 | `LowerExp` formatting, as in `{:e}`, is not supported | fixtures/reject/src/lib.rs:65:3 | | 1 | `Octal` formatting, as in `{:o}`, is not supported | fixtures/reject/src/lib.rs:64:3 | @@ -32,7 +33,6 @@ | 1 | an or-pattern with bindings | fixtures/reject/src/lib.rs:87:3 | | 1 | arithmetic on `_` | fixtures/reject/src/lib.rs:7:2 | | 1 | hex `Debug`, as in `{:x?}`, is not supported | fixtures/reject/src/lib.rs:66:3 | -| 1 | overriding `_`, which std's adaptors call in place of `next` | fixtures/reject/src/lib.rs:128:2 | | 1 | the `+` flag of a format spec is not supported | fixtures/reject/src/lib.rs:61:3 | | 1 | the `-` flag of a format spec is not supported | fixtures/reject/src/lib.rs:62:3 | | 1 | unsupported expression: StaticRef | fixtures/reject/src/lib.rs:94:2 | @@ -48,14 +48,15 @@ The type or module each refused type or path belongs to. | 4 | `std::string::String` | fixtures/reject/src/lib.rs:73:16 | | 3 | `Meters` | fixtures/reject/src/lib.rs:99:17 | | 3 | `u32` | fixtures/reject/src/lib.rs:19:13 | +| 2 | `Iterator` | fixtures/reject/src/lib.rs:128:2 | | 2 | `Option` | fixtures/reject/src/lib.rs:31:15 | | 2 | `char` | fixtures/reject/src/lib.rs:10:18 | | 1 | `DoubleEndedIterator` | fixtures/reject/src/lib.rs:148:1 | | 1 | `Guard` | fixtures/reject/src/lib.rs:43:1 | -| 1 | `Iterator` | fixtures/reject/src/lib.rs:128:2 | | 1 | `dyn std::ops::Fn(u8) -> u8` | fixtures/reject/src/lib.rs:14:16 | | 1 | `later` | fixtures/reject/src/lib.rs:53:1 | | 1 | `std::iter::Chain` | fixtures/reject/src/lib.rs:161:51 | +| 1 | `std::ops::Range` | fixtures/reject/src/lib.rs:169:8 | | 1 | `std::slice::Iter` | fixtures/reject/src/lib.rs:115:11 | | 1 | `std::slice` | fixtures/reject/src/lib.rs:36:4 | @@ -78,9 +79,11 @@ The type or module each refused type or path belongs to. | 1 | a mutable reference to `_`, which is no object it can change in place | `std::string::String` | fixtures/reject/src/lib.rs:75:17 | | 1 | an iterator of the crate implementing `_`, which is not supported yet | `DoubleEndedIterator` | fixtures/reject/src/lib.rs:148:1 | | 1 | arithmetic on `_` | `f64` | fixtures/reject/src/lib.rs:7:2 | -| 1 | overriding `_`, which std's adaptors call in place of `next` | `Iterator::fold` | fixtures/reject/src/lib.rs:128:2 | +| 1 | overriding `_`, which std's adaptors call in place of `next` or beside it | `Iterator::fold` | fixtures/reject/src/lib.rs:128:2 | +| 1 | overriding `_`, which std's adaptors call in place of `next` or beside it | `Iterator::size_hint` | fixtures/reject/src/lib.rs:182:2 | | 1 | unsupported call to `_` | `::cycle` | fixtures/reject/src/lib.rs:115:11 | | 1 | unsupported call to `_` | `::write_fmt` | fixtures/reject/src/lib.rs:75:10 | +| 1 | unsupported call to `_` | `std::ops::Range::contains` | fixtures/reject/src/lib.rs:169:8 | | 1 | unsupported call to `_` | `std::slice::sort` | fixtures/reject/src/lib.rs:36:4 | | 1 | unsupported literal of type `_` | `f64` | fixtures/reject/src/lib.rs:7:6 | | 1 | unsupported type `_` | `char` | fixtures/reject/src/lib.rs:10:18 | @@ -98,6 +101,15 @@ The type or module each refused type or path belongs to.
+
unsupported call to `_` (4) + +- fixtures/reject/src/lib.rs:36:4: unsupported call to `std::slice::::sort` +- fixtures/reject/src/lib.rs:75:10: unsupported call to `::write_fmt` +- fixtures/reject/src/lib.rs:115:11: unsupported call to ` as std::iter::Iterator>::cycle` +- fixtures/reject/src/lib.rs:169:8: unsupported call to `std::ops::Range::::contains::` + +
+
a `_` binding: a JS value of `_` is a copy, so it can't change (3) - fixtures/reject/src/lib.rs:19:13: a `&mut u32` binding: a JS value of `u32` is a copy, so it can't change @@ -114,14 +126,6 @@ The type or module each refused type or path belongs to.
-
unsupported call to `_` (3) - -- fixtures/reject/src/lib.rs:36:4: unsupported call to `std::slice::::sort` -- fixtures/reject/src/lib.rs:75:10: unsupported call to `::write_fmt` -- fixtures/reject/src/lib.rs:115:11: unsupported call to ` as std::iter::Iterator>::cycle` - -
-
`_` is not supported: its `None` and `Some` would both be `undefined` (2) - fixtures/reject/src/lib.rs:31:15: `Option>` is not supported: its `None` and `Some` would both be `undefined` @@ -136,6 +140,13 @@ The type or module each refused type or path belongs to.
+
overriding `_`, which std's adaptors call in place of `next` or beside it (2) + +- fixtures/reject/src/lib.rs:128:2: overriding `Iterator::fold`, which std's adaptors call in place of `next` or beside it +- fixtures/reject/src/lib.rs:182:2: overriding `Iterator::size_hint`, which std's adaptors call in place of `next` or beside it + +
+
`Binary` formatting, as in `{:b}`, is not supported (1) - fixtures/reject/src/lib.rs:63:3: `Binary` formatting, as in `{:b}`, is not supported @@ -244,12 +255,6 @@ The type or module each refused type or path belongs to.
-
overriding `_`, which std's adaptors call in place of `next` (1) - -- fixtures/reject/src/lib.rs:128:2: overriding `Iterator::fold`, which std's adaptors call in place of `next` - -
-
the `+` flag of a format spec is not supported (1) - fixtures/reject/src/lib.rs:61:3: the `+` flag of a format spec is not supported diff --git a/ts/fixtures/reject/expected.txt b/ts/fixtures/reject/expected.txt index 694c6780f..f247f5047 100644 --- a/ts/fixtures/reject/expected.txt +++ b/ts/fixtures/reject/expected.txt @@ -37,5 +37,7 @@ replacing the value behind a reference to `Meters` at fixtures/reject/src/lib.rs replacing the value behind a reference to `Meters` at fixtures/reject/src/lib.rs:103:25 unsupported call to ` as std::iter::Iterator>::cycle` at fixtures/reject/src/lib.rs:115:11 a `zip` of `std::iter::Chain, {closure@fixtures/reject/src/lib.rs:161:15: 161:18}>, std::iter::Copied>>`, whose closures std calls one item further through `next` than through `fold` at fixtures/reject/src/lib.rs:161:51 -overriding `Iterator::fold`, which std's adaptors call in place of `next` at fixtures/reject/src/lib.rs:128:2 +unsupported call to `std::ops::Range::::contains::` at fixtures/reject/src/lib.rs:169:8 +overriding `Iterator::fold`, which std's adaptors call in place of `next` or beside it at fixtures/reject/src/lib.rs:128:2 an iterator of the crate implementing `DoubleEndedIterator`, which is not supported yet at fixtures/reject/src/lib.rs:148:1 +overriding `Iterator::size_hint`, which std's adaptors call in place of `next` or beside it at fixtures/reject/src/lib.rs:182:2 diff --git a/ts/fixtures/reject/src/lib.rs b/ts/fixtures/reject/src/lib.rs index af0134385..30f299999 100644 --- a/ts/fixtures/reject/src/lib.rs +++ b/ts/fixtures/reject/src/lib.rs @@ -160,3 +160,30 @@ pub fn backs() -> usize { pub fn zipped(a: &[u8], b: &[u8]) -> usize { a.iter().map(|x| x + 1).chain(b.iter().copied()).zip(b).count() } + +/// JS compares objects by identity, not by `PartialOrd`. +#[derive(PartialEq, PartialOrd)] +pub struct Key(pub u8); + +pub fn keyed(range: std::ops::Range) -> bool { + range.contains(&Key(2)) +} + +/// An iterator whose `size_hint` std's `collect` calls beside its `next`. +pub struct Hinted; + +impl Iterator for Hinted { + type Item = u8; + + fn next(&mut self) -> Option { + None + } + + fn size_hint(&self) -> (usize, Option) { + (0, Some(0)) + } +} + +pub fn hinted() -> Vec { + Hinted.collect() +} diff --git a/ts/fixtures/reject/ts.toml b/ts/fixtures/reject/ts.toml index 091b4d603..f00885e2c 100644 --- a/ts/fixtures/reject/ts.toml +++ b/ts/fixtures/reject/ts.toml @@ -22,6 +22,8 @@ roots = [ "folds", "backs", "zipped", + "keyed", + "hinted", "missing", ] printable = ["Meters"] diff --git a/ts/src/lower/iter.rs b/ts/src/lower/iter.rs index 3f158e4d9..280ee271d 100644 --- a/ts/src/lower/iter.rs +++ b/ts/src/lower/iter.rs @@ -57,8 +57,9 @@ fn own_next<'tcx>(b: &Body<'_, '_, 'tcx>, ty: Ty<'tcx>) -> Option } /// Refuse an `impl Iterator` of the crate, whose `next` is `next_fn`, that std's adaptors -/// would read other than through `next`: one that overrides `fold` or `nth`, which they -/// call in its place, or that has a `next_back` or `len`, which this protocol lacks. +/// would read other than through `next`: one that overrides `fold`, `nth`, or `size_hint`, +/// which they call in its place or beside it, or that has a `next_back` or `len`, which +/// this protocol lacks. pub fn check_impl(t: &mut Translator<'_, '_>, next_fn: DefId) { let tcx = t.tcx; let Some(imp) = tcx.impl_of_assoc(next_fn) else { @@ -66,10 +67,10 @@ pub fn check_impl(t: &mut Translator<'_, '_>, next_fn: DefId) { }; for item in tcx.associated_items(imp).in_definition_order() { let name = item.name(); - if item.is_fn() && name != rustc_span::sym::next && name.as_str() != "size_hint" { + if item.is_fn() && name != rustc_span::sym::next { t.err( tcx.def_span(item.def_id), - format!("overriding `Iterator::{name}`, which std's adaptors call in place of `next`"), + format!("overriding `Iterator::{name}`, which std's adaptors call in place of `next` or beside it"), ); } } @@ -310,7 +311,14 @@ pub fn call<'tcx>( let ty::Ref(_, inner, _) = item.kind() else { return None; }; + // A `Clone` of the crate's own, unless derived, may do other than copy. + let own_clone = name == "cloned" && own_clone(b, *inner); match () { + _ if own_clone => { + let clone = tcx.lang_items().clone_fn()?; + let clone = b.fn_value(clone, tcx.mk_args(&[(*inner).into()]), span); + method("map", vec![clone]) + } _ if b.needs_copy(*inner) => method("map", vec![Expr::Rt("copy")]), // A JS string is a value already. _ if b.t.is_copy(*inner) @@ -388,6 +396,24 @@ fn implements<'tcx>(t: &Translator<'_, 'tcx>, ty: Ty<'tcx>, name: &str) -> bool infcx.type_implements_trait(tr, [ty], env).must_apply_modulo_regions() } +/// Whether `ty`'s `Clone` is the crate's own and written by hand, so it need not copy. +fn own_clone<'tcx>(b: &Body<'_, '_, 'tcx>, ty: Ty<'tcx>) -> bool { + let tcx = b.t.tcx; + let Some(clone) = tcx.lang_items().clone_fn() else { + return false; + }; + let Ok(Some(instance)) = Instance::try_resolve(tcx, b.t.typing_env_pub(), clone, tcx.mk_args(&[ty.into()])) else { + return false; + }; + let ty::InstanceKind::Item(did) = instance.def else { + return false; + }; + did.is_local() + && tcx + .impl_of_assoc(did) + .is_some_and(|imp| !tcx.is_automatically_derived(imp)) +} + /// Whether `ty` compares in JS as Rust's `Ord` does: an integer or a `bool`. fn primitive(ty: Ty<'_>) -> bool { matches!(ty.kind(), ty::Bool | ty::Int(_) | ty::Uint(_)) diff --git a/ts/src/lower/library.rs b/ts/src/lower/library.rs index ece6c82c0..39479d97c 100644 --- a/ts/src/lower/library.rs +++ b/ts/src/lower/library.rs @@ -218,6 +218,14 @@ pub fn call<'tcx>(b: &mut Body<'_, '_, 'tcx>, call: &Call<'tcx>, args: Args<'_>, }; let is_u8 = |t: Ty<'tcx>| matches!(t.kind(), ty::Uint(ty::UintTy::U8)); let range = matches!(inherent_std(tcx, call.def_id), Some(Std::Range | Std::RangeInclusive)); + // A range of integers, whose comparisons and clones JS makes as Rust does. + let int_range = |t: Ty<'tcx>| match t.peel_refs().kind() { + ty::Adt(adt, args) => { + matches!(Std::of(tcx, adt.did()), Some(Std::Range | Std::RangeInclusive)) + && Int::of(args.type_at(0)).is_some() + } + _ => false, + }; let e = match (owner.as_str(), name.as_str()) { // Conversions. @@ -323,9 +331,7 @@ pub fn call<'tcx>(b: &mut Body<'_, '_, 'tcx>, call: &Call<'tcx>, args: Args<'_>, .call(vec![value.method("view", vec![])]) } ty::Adt(adt, _) if Std::of(tcx, adt.did()) == Some(Std::String) => value, - ty::Adt(adt, _) if matches!(Std::of(tcx, adt.did()), Some(Std::Range | Std::RangeInclusive)) => { - value.method("clone", vec![]) - } + ty::Adt(..) if int_range(ty) => value.method("clone", vec![]), _ if b.t.is_copy(ty) => b.copied(value, ty), _ => return None, } @@ -384,7 +390,10 @@ pub fn call<'tcx>(b: &mut Body<'_, '_, 'tcx>, call: &Call<'tcx>, args: Args<'_>, values.extend(self_ty().and_then(Int::of).map(|int| Expr::Rt(int.rt()))); Expr::New(Box::new(Expr::Rt("RangeInclusive")), values) } - ("Range" | "RangeInclusive", "is_empty" | "contains" | "start" | "end") if range => { + // JS compares integers as Rust does, and other endpoints by identity. + ("Range" | "RangeInclusive", "start" | "end" | "is_empty" | "contains") + if range && (matches!(name.as_str(), "start" | "end") || self_ty().and_then(Int::of).is_some()) => + { let mut values = b.args(args, out); let recv = values.remove(0); recv.method(&super::camel(&name), values) From ab084123fa2a408a61a2d101a938eeedd4a42707 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 24 Oct 2026 06:04:49 -0700 Subject: [PATCH 12/13] fix(ts): refuse an own FusedIterator, which fuse reads past its end Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- ts/fixtures/reject/census.md | 21 ++++++++++++--------- ts/fixtures/reject/expected.txt | 1 + ts/fixtures/reject/src/lib.rs | 17 +++++++++++++++++ ts/fixtures/reject/ts.toml | 1 + ts/src/lower/iter.rs | 14 ++++++++------ 5 files changed, 39 insertions(+), 15 deletions(-) diff --git a/ts/fixtures/reject/census.md b/ts/fixtures/reject/census.md index 73bc886e0..cb28a3c5c 100644 --- a/ts/fixtures/reject/census.md +++ b/ts/fixtures/reject/census.md @@ -1,7 +1,7 @@ # Census of `reject-fixture` -- Refusals: 43 -- Function instances: 31 +- Refusals: 44 +- Function instances: 33 - Generic functions no instance reached, lowered over their type parameters: 2 ## By construct @@ -14,6 +14,7 @@ | 3 | replacing the value behind a reference to `_` | fixtures/reject/src/lib.rs:99:17 | | 2 | `_` is not supported: its `None` and `Some` would both be `undefined` | fixtures/reject/src/lib.rs:31:15 | | 2 | `_`: a JS value of `_` is a copy, so a reference can't change it | fixtures/reject/src/lib.rs:19:13 | +| 2 | an iterator of the crate implementing `_`, which is not supported yet | fixtures/reject/src/lib.rs:148:1 | | 2 | overriding `_`, which std's adaptors call in place of `next` or beside it | fixtures/reject/src/lib.rs:128:2 | | 1 | `Binary` formatting, as in `{:b}`, is not supported | fixtures/reject/src/lib.rs:63:3 | | 1 | `LowerExp` formatting, as in `{:e}`, is not supported | fixtures/reject/src/lib.rs:65:3 | @@ -29,7 +30,6 @@ | 1 | a precision, as in `{:.2}`, is not supported | fixtures/reject/src/lib.rs:60:3 | | 1 | a printable type must implement `Display` or `Debug` | fixtures/reject/src/lib.rs:106:1 | | 1 | a width or precision from an argument, as in `{:1$}`, is not supported | fixtures/reject/src/lib.rs:67:3 | -| 1 | an iterator of the crate implementing `_`, which is not supported yet | fixtures/reject/src/lib.rs:148:1 | | 1 | an or-pattern with bindings | fixtures/reject/src/lib.rs:87:3 | | 1 | arithmetic on `_` | fixtures/reject/src/lib.rs:7:2 | | 1 | hex `Debug`, as in `{:x?}`, is not supported | fixtures/reject/src/lib.rs:66:3 | @@ -52,6 +52,7 @@ The type or module each refused type or path belongs to. | 2 | `Option` | fixtures/reject/src/lib.rs:31:15 | | 2 | `char` | fixtures/reject/src/lib.rs:10:18 | | 1 | `DoubleEndedIterator` | fixtures/reject/src/lib.rs:148:1 | +| 1 | `FusedIterator` | fixtures/reject/src/lib.rs:202:1 | | 1 | `Guard` | fixtures/reject/src/lib.rs:43:1 | | 1 | `dyn std::ops::Fn(u8) -> u8` | fixtures/reject/src/lib.rs:14:16 | | 1 | `later` | fixtures/reject/src/lib.rs:53:1 | @@ -78,6 +79,7 @@ The type or module each refused type or path belongs to. | 1 | a cast to `_` | `f64` | fixtures/reject/src/lib.rs:110:2 | | 1 | a mutable reference to `_`, which is no object it can change in place | `std::string::String` | fixtures/reject/src/lib.rs:75:17 | | 1 | an iterator of the crate implementing `_`, which is not supported yet | `DoubleEndedIterator` | fixtures/reject/src/lib.rs:148:1 | +| 1 | an iterator of the crate implementing `_`, which is not supported yet | `FusedIterator` | fixtures/reject/src/lib.rs:202:1 | | 1 | arithmetic on `_` | `f64` | fixtures/reject/src/lib.rs:7:2 | | 1 | overriding `_`, which std's adaptors call in place of `next` or beside it | `Iterator::fold` | fixtures/reject/src/lib.rs:128:2 | | 1 | overriding `_`, which std's adaptors call in place of `next` or beside it | `Iterator::size_hint` | fixtures/reject/src/lib.rs:182:2 | @@ -140,6 +142,13 @@ The type or module each refused type or path belongs to.
+
an iterator of the crate implementing `_`, which is not supported yet (2) + +- fixtures/reject/src/lib.rs:148:1: an iterator of the crate implementing `DoubleEndedIterator`, which is not supported yet +- fixtures/reject/src/lib.rs:202:1: an iterator of the crate implementing `FusedIterator`, which is not supported yet + +
+
overriding `_`, which std's adaptors call in place of `next` or beside it (2) - fixtures/reject/src/lib.rs:128:2: overriding `Iterator::fold`, which std's adaptors call in place of `next` or beside it @@ -231,12 +240,6 @@ The type or module each refused type or path belongs to.
-
an iterator of the crate implementing `_`, which is not supported yet (1) - -- fixtures/reject/src/lib.rs:148:1: an iterator of the crate implementing `DoubleEndedIterator`, which is not supported yet - -
-
an or-pattern with bindings (1) - fixtures/reject/src/lib.rs:87:3: an or-pattern with bindings diff --git a/ts/fixtures/reject/expected.txt b/ts/fixtures/reject/expected.txt index f247f5047..2e550a9eb 100644 --- a/ts/fixtures/reject/expected.txt +++ b/ts/fixtures/reject/expected.txt @@ -41,3 +41,4 @@ unsupported call to `std::ops::Range::::contains::` at fixtures/reject overriding `Iterator::fold`, which std's adaptors call in place of `next` or beside it at fixtures/reject/src/lib.rs:128:2 an iterator of the crate implementing `DoubleEndedIterator`, which is not supported yet at fixtures/reject/src/lib.rs:148:1 overriding `Iterator::size_hint`, which std's adaptors call in place of `next` or beside it at fixtures/reject/src/lib.rs:182:2 +an iterator of the crate implementing `FusedIterator`, which is not supported yet at fixtures/reject/src/lib.rs:202:1 diff --git a/ts/fixtures/reject/src/lib.rs b/ts/fixtures/reject/src/lib.rs index 30f299999..6b6ba014a 100644 --- a/ts/fixtures/reject/src/lib.rs +++ b/ts/fixtures/reject/src/lib.rs @@ -187,3 +187,20 @@ impl Iterator for Hinted { pub fn hinted() -> Vec { Hinted.collect() } + +/// An iterator that `fuse` reads on past its end, since it says it stays ended. +pub struct Fused; + +impl Iterator for Fused { + type Item = u8; + + fn next(&mut self) -> Option { + None + } +} + +impl std::iter::FusedIterator for Fused {} + +pub fn fused() -> usize { + Fused.fuse().count() +} diff --git a/ts/fixtures/reject/ts.toml b/ts/fixtures/reject/ts.toml index f00885e2c..2c44978e7 100644 --- a/ts/fixtures/reject/ts.toml +++ b/ts/fixtures/reject/ts.toml @@ -24,6 +24,7 @@ roots = [ "zipped", "keyed", "hinted", + "fused", "missing", ] printable = ["Meters"] diff --git a/ts/src/lower/iter.rs b/ts/src/lower/iter.rs index 280ee271d..6bdfa6018 100644 --- a/ts/src/lower/iter.rs +++ b/ts/src/lower/iter.rs @@ -58,8 +58,8 @@ fn own_next<'tcx>(b: &Body<'_, '_, 'tcx>, ty: Ty<'tcx>) -> Option /// Refuse an `impl Iterator` of the crate, whose `next` is `next_fn`, that std's adaptors /// would read other than through `next`: one that overrides `fold`, `nth`, or `size_hint`, -/// which they call in its place or beside it, or that has a `next_back` or `len`, which -/// this protocol lacks. +/// which they call in its place or beside it, that has a `next_back` or `len`, which this +/// protocol lacks, or that is a `FusedIterator`, which `fuse` reads on past its end. pub fn check_impl(t: &mut Translator<'_, '_>, next_fn: DefId) { let tcx = t.tcx; let Some(imp) = tcx.impl_of_assoc(next_fn) else { @@ -79,10 +79,12 @@ pub fn check_impl(t: &mut Translator<'_, '_>, next_fn: DefId) { }; for (&tr, impls) in tcx.all_local_trait_impls(()) { let path = library::std_path(tcx, tr); - let Some(name) = path - .last() - .filter(|n| matches!(n.as_str(), "DoubleEndedIterator" | "ExactSizeIterator")) - else { + let Some(name) = path.last().filter(|n| { + matches!( + n.as_str(), + "DoubleEndedIterator" | "ExactSizeIterator" | "FusedIterator" + ) + }) else { continue; }; for other in impls { From 50e5ec2ce538a3895033d52129d5e2d546708fa9 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 24 Oct 2026 06:13:57 -0700 Subject: [PATCH 13/13] fix(ts): clone only copies where no hand-written Clone runs, fields included Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- ts/fixtures/lang/expected/iters.ts | 21 ++++++++++++++++++++ ts/fixtures/lang/src/iters.rs | 6 ++++++ ts/src/lower/iter.rs | 24 +++-------------------- ts/src/lower/library.rs | 31 ++++++++++++++++++++++++++++-- 4 files changed, 59 insertions(+), 23 deletions(-) diff --git a/ts/fixtures/lang/expected/iters.ts b/ts/fixtures/lang/expected/iters.ts index fdfb42330..a0b61f92c 100644 --- a/ts/fixtures/lang/expected/iters.ts +++ b/ts/fixtures/lang/expected/iters.ts @@ -703,6 +703,27 @@ export function values(data: Uint8Array, out: rt.ByteVec): void { rt.iter.extend(out, rt.iter.slice(bumps).map(rt.copy).map((b6: Bumped): number => { return b6._0; })); + const wrapped = rt.iter.slice(data).map((arg5: number): Wrapper => { + const b7 = arg5; + return new Wrapper(new Bumped(b7)); + }).toArray(); + rt.iter.extend(out, rt.iter.slice(wrapped).map((a03: Wrapper): Wrapper => { + return a03.clone(); + }).map((w: Wrapper): number => { + return w._0._0; + })); +} + +export class Wrapper { + _0: T; + + constructor(_0: T) { + this._0 = _0; + } + + clone(this: Wrapper): Wrapper { + return new Wrapper(this._0.clone()); + } } /** A `Copy` type whose `Clone` does more than copy. */ diff --git a/ts/fixtures/lang/src/iters.rs b/ts/fixtures/lang/src/iters.rs index e44024b40..c4cc37d97 100644 --- a/ts/fixtures/lang/src/iters.rs +++ b/ts/fixtures/lang/src/iters.rs @@ -489,8 +489,14 @@ pub fn values(data: &[u8], out: &mut Vec) { let bumps: Vec = data.iter().map(|&b| Bumped(b)).collect(); out.extend(bumps.iter().cloned().map(|b| b.0)); out.extend(bumps.iter().copied().map(|b| b.0)); + // A derived `Clone` over a type parameter clones the field, by its own `Clone`. + let wrapped: Vec> = data.iter().map(|&b| Wrapper(Bumped(b))).collect(); + out.extend(wrapped.iter().cloned().map(|w| w.0.0)); } +#[derive(Clone, Copy)] +struct Wrapper(T); + /// A `Copy` type whose `Clone` does more than copy. #[derive(Copy)] struct Bumped(u8); diff --git a/ts/src/lower/iter.rs b/ts/src/lower/iter.rs index 6bdfa6018..bb36e6e82 100644 --- a/ts/src/lower/iter.rs +++ b/ts/src/lower/iter.rs @@ -313,10 +313,10 @@ pub fn call<'tcx>( let ty::Ref(_, inner, _) = item.kind() else { return None; }; - // A `Clone` of the crate's own, unless derived, may do other than copy. - let own_clone = name == "cloned" && own_clone(b, *inner); + // A `Clone` written by hand may do other than copy, even for a `Copy` type. + let clones = name == "cloned" && !library::clone_copies(b.t, *inner); match () { - _ if own_clone => { + _ if clones => { let clone = tcx.lang_items().clone_fn()?; let clone = b.fn_value(clone, tcx.mk_args(&[(*inner).into()]), span); method("map", vec![clone]) @@ -398,24 +398,6 @@ fn implements<'tcx>(t: &Translator<'_, 'tcx>, ty: Ty<'tcx>, name: &str) -> bool infcx.type_implements_trait(tr, [ty], env).must_apply_modulo_regions() } -/// Whether `ty`'s `Clone` is the crate's own and written by hand, so it need not copy. -fn own_clone<'tcx>(b: &Body<'_, '_, 'tcx>, ty: Ty<'tcx>) -> bool { - let tcx = b.t.tcx; - let Some(clone) = tcx.lang_items().clone_fn() else { - return false; - }; - let Ok(Some(instance)) = Instance::try_resolve(tcx, b.t.typing_env_pub(), clone, tcx.mk_args(&[ty.into()])) else { - return false; - }; - let ty::InstanceKind::Item(did) = instance.def else { - return false; - }; - did.is_local() - && tcx - .impl_of_assoc(did) - .is_some_and(|imp| !tcx.is_automatically_derived(imp)) -} - /// Whether `ty` compares in JS as Rust's `Ord` does: an integer or a `bool`. fn primitive(ty: Ty<'_>) -> bool { matches!(ty.kind(), ty::Bool | ty::Int(_) | ty::Uint(_)) diff --git a/ts/src/lower/library.rs b/ts/src/lower/library.rs index 39479d97c..ddbac0c02 100644 --- a/ts/src/lower/library.rs +++ b/ts/src/lower/library.rs @@ -7,7 +7,7 @@ use rustc_hir::def_id::DefId; use rustc_middle::thir::{ExprId, ExprKind}; -use rustc_middle::ty::{self, GenericArgsRef, Ty, TyCtxt}; +use rustc_middle::ty::{self, GenericArgsRef, Instance, Ty, TyCtxt}; use rustc_span::Span; use super::body::Body; @@ -119,6 +119,33 @@ fn is_std(tcx: TyCtxt<'_>, did: DefId) -> bool { matches!(tcx.crate_name(did.krate).as_str(), "core" | "alloc" | "std") } +/// Whether `Clone` of `ty`, a `Copy` type, only copies: no `Clone` written by hand runs +/// for it or anything in it, as a derived or std `Clone` clones each field. +pub fn clone_copies<'tcx>(t: &super::Translator<'_, 'tcx>, ty: Ty<'tcx>) -> bool { + let tcx = t.tcx; + match ty.kind() { + ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Ref(..) | ty::FnPtr(..) | ty::Never => true, + ty::Tuple(fields) => fields.iter().all(|field| clone_copies(t, field)), + ty::Array(elem, _) => clone_copies(t, *elem), + ty::Adt(adt, args) if !adt.did().is_local() => args.types().all(|arg| clone_copies(t, arg)), + ty::Adt(adt, args) => { + let Some(clone) = tcx.lang_items().clone_fn() else { + return false; + }; + let derived = Instance::try_resolve(tcx, t.typing_env_pub(), clone, tcx.mk_args(&[ty.into()])) + .ok() + .flatten() + .and_then(|instance| tcx.impl_of_assoc(instance.def_id())) + .is_some_and(|imp| tcx.is_automatically_derived(imp)); + derived + && adt + .all_fields() + .all(|field| clone_copies(t, t.subst(args, field.ty(tcx, args).skip_norm_wip()))) + } + _ => false, + } +} + /// The standard type an inherent method is of, such as `Range` for `Range::contains`. pub fn inherent_std(tcx: TyCtxt<'_>, def_id: DefId) -> Option { let imp = tcx.inherent_impl_of_assoc(def_id)?; @@ -332,7 +359,7 @@ pub fn call<'tcx>(b: &mut Body<'_, '_, 'tcx>, call: &Call<'tcx>, args: Args<'_>, } ty::Adt(adt, _) if Std::of(tcx, adt.did()) == Some(Std::String) => value, ty::Adt(..) if int_range(ty) => value.method("clone", vec![]), - _ if b.t.is_copy(ty) => b.copied(value, ty), + _ if b.t.is_copy(ty) && clone_copies(b.t, ty) => b.copied(value, ty), _ => return None, } }