diff --git a/quest/m3/README.md b/quest/m3/README.md index 955952119..d381f0115 100644 --- a/quest/m3/README.md +++ b/quest/m3/README.md @@ -27,9 +27,9 @@ generic std gap is planned here even when moq never calls it. - [A B-tree's drop order](/quest/m3/ts-btree-drop.md) - dropping a whole `BTreeMap` or `BTreeSet` drops its entries node by node, as std's does - [Drop order in std's folds and iterators](/quest/m3/ts-drop-order.md) - the values that drop still refused in std's consumers, zips, flattens, and collects drop as std's do - [References to byte arrays std takes](/quest/m3/ts-byte-refs-taken.md) - `extend` by references to arrays, adaptors' closures over held arrays, and a collection of both forms translate -- [Equality of std containers of unlike elements](/quest/m3/ts-eq-unlike.md) - `==` and `next_if_eq` of a `Vec` against a `&[Right]`, and of `VecDeque` against `Vec`, translate - [Waiting: a slice object for a mutable part of other values](/quest/m3/ts-slice-object.md) - waits on a moq profile showing the `Proxy` view's traps matter, then decides on an `rt.ByteSlice`-like form - [Port the workflow guard to kixelated/quest](/quest/m3/quest-workflow-guard.md) - quest's environment guard parses YAML as this repository's does +- [Equality through a held type's own ne](/quest/m3/ts-eq-own-ne.md) - `==` of slices of a type whose `ne` the crate overrides, and `!=` of a tuple or `Arc` of one, translate - [std iterators' Debug](/quest/m3/ts-iter-debug.md) - `{:?}` of std's iterators translates, writing std's text, a deque's two slices included - [LinkedList](/quest/m3/ts-linked-list.md) - `LinkedList` and its methods translate - [A parameter read only where lowering leaves nothing](/quest/m3/ts-unread-params.md) - an instance whose lowering drops every read of a parameter marks it unused, so `tsc` accepts it diff --git a/quest/m3/ts-eq-own-ne.md b/quest/m3/ts-eq-own-ne.md new file mode 100644 index 000000000..356b06cb9 --- /dev/null +++ b/quest/m3/ts-eq-own-ne.md @@ -0,0 +1,16 @@ +# [S] Equality through a held type's own ne + +## Goal + +`==` of slices, `Vec`s, and `VecDeque`s of a crate's type whose `PartialEq` overrides `ne`, +and `!=` of a tuple or an `Arc` holding one, translate, proven by a differential test +against native Rust. + +## Plan + +Refused today with "through a held type's own" at the comparison's span: the runtime's +`eqSlice`, `eqTuple`, and `eqArc` compare by `eq`, where std's slices call each item's +`ne`, and a tuple's and an `Arc`'s `ne` call what they hold's. Items of unlike types +already compare by `!ne` through the crate's impl (`item_eq` in the collections +lowering); items of one type could do the same, and a tuple's or `Arc`'s `ne` could +lower by its fields' `ne`. diff --git a/quest/m3/ts-eq-unlike.md b/quest/m3/ts-eq-unlike.md deleted file mode 100644 index 6d7a0d7ac..000000000 --- a/quest/m3/ts-eq-unlike.md +++ /dev/null @@ -1,18 +0,0 @@ -# [S] Equality of std containers of unlike elements - -## Goal - -`==` and `next_if_eq` of std containers whose elements are of other types that compare -by their own `PartialEq`, as a `Vec` against a `&[Right]` does, and of -`VecDeque` against `Vec`, slices, and arrays, translate, proven by a differential test -against native Rust. - -## Plan - -Refused at their spans today, as the reject fixture's `unlike_elements` and -`peeked_deque` are: the collections mapping compares only containers whose elements -compare alike. Each element pair could lower through `>::eq` as -`next_if_eq` of other types does, lending a `[u8; N]` element as its `rt.ByteSlice`, with -`ne` by the elements' own `ne` where a crate overrides it. - -Public API: the translator's output. diff --git a/ts/README.md b/ts/README.md index 758dab7a9..799c9ff9d 100644 --- a/ts/README.md +++ b/ts/README.md @@ -299,7 +299,9 @@ finds any other key, a 64-bit one past 2^53 included, through its translated `Ha `Option` orders `None` first, an `Arc` compares, orders, and hashes as what it holds, so a map of `Arc` finds a `&str`, and byte arrays, slices, and vectors order by their bytes and then their length, and compare and hash alike whatever their form, so a map of -`Vec` finds a `&[u8]`. A collection's reference to a byte array it holds, as `keys`, +`Vec` finds a `&[u8]`. Slices, arrays, `Vec`s, and `VecDeque`s compare against each +other as std's do, by their lengths and then each pair of items in order, through the +items' own `PartialEq`, and its `ne` where the crate overrides it. A collection's reference to a byte array it holds, as `keys`, `HashSet::get`, and `retain`'s closure get one, is an `rt.ByteSlice` of its bytes, as a `&[u8; N]` is elsewhere; a `&mut` writes where the collection holds them. A `&mut` to a value a map holds that JS copies, such as a number, from `get_mut` or the entry API, is an `rt.Place` whose `v` reads and @@ -696,7 +698,8 @@ built natively for wasm32 and run beside its translation on the same inputs by closures that log what each adaptor reads, `Option`'s and `Result`'s combinators, with closures that log each call, and std's collections, over keys whose own `Hash`, `Eq`, and `Ord` - disagree with JS's, `vec![x; n]` of types whose `Clone` counts its clones, `&mut` + disagree with JS's, sequences compared across forms and item types by an `eq` and `ne` + that disagree, `vec![x; n]` of types whose `Clone` counts its clones, `&mut` to values JS copies, of locals, fields, elements, and a map's values, passed on, returned, kept in a struct, and captured, a `Vec`'s edits and sorts, with closures that log what each is called with, std's `Bound`, `Reverse`, and `NonZero`, and raw diff --git a/ts/fixtures/lang/caller.ts b/ts/fixtures/lang/caller.ts index e48668adc..a564143b8 100644 --- a/ts/fixtures/lang/caller.ts +++ b/ts/fixtures/lang/caller.ts @@ -544,4 +544,14 @@ const setOps = [ const zerosOp = op("zeros::zeros"); const zeros = Array.from({ length: 7 * 4 }, (_, i) => Uint8Array.of(zerosOp, i % 7, ...Array.from({ length: Math.floor(i / 7) }, (_, b) => b))); -export const cases: Uint8Array[] = [...loops, ...durations, ...ints(), ...utf8, ...floats, ...formats, ...parsed, ...singlesCases, ...singlesFormats, ...singlesParsed, ...dyns, ...cells, ...shared, ...locks, ...onces, ...bounds, ...unsafeCells, ...edits, ...subslices, ...consumers, ...closures, ...elements, ...ranges, ...reducers, ...sliceEdits, ...drains, ...iters, ...hinted, ...peekedOthers, ...hints, ...reserves, ...setOps, ...zeros, ...floatsMulAdds, ...singlesMulAdds]; +/** + * `collections::unlike_eqs` of every pair of positions where `eq` and `ne` disagree and each + * length the right side may lose, over no items, one, two, a wrapped deque's four, and nine, + * whose positions are all within it. + */ +const unlikeEqsOp = op("collections::unlike_eqs"); +const unlikeEqs = [[], [5], [1, 2], [0, 7, 2, 9], [3, 3, 3, 3, 3, 3, 3, 3, 3]].flatMap((bytes) => + Array.from({ length: 128 }, (_, kind) => Uint8Array.of(unlikeEqsOp, kind, ...bytes)), +); + +export const cases: Uint8Array[] = [...loops, ...durations, ...ints(), ...utf8, ...floats, ...formats, ...parsed, ...singlesCases, ...singlesFormats, ...singlesParsed, ...dyns, ...cells, ...shared, ...locks, ...onces, ...bounds, ...unsafeCells, ...edits, ...subslices, ...consumers, ...closures, ...elements, ...ranges, ...reducers, ...sliceEdits, ...drains, ...iters, ...hinted, ...peekedOthers, ...hints, ...reserves, ...setOps, ...zeros, ...floatsMulAdds, ...singlesMulAdds, ...unlikeEqs]; diff --git a/ts/fixtures/lang/expected/collections.Left.ts b/ts/fixtures/lang/expected/collections.Left.ts new file mode 100644 index 000000000..3663aa2e0 --- /dev/null +++ b/ts/fixtures/lang/expected/collections.Left.ts @@ -0,0 +1,35 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; +import type * as Right from "./collections.Right.ts"; + +/** + * An item that equals a `Right` of the same remainder by 4, but whose `ne` sees the + * remainder by 3, so what `!=` finds tells which std calls. Each call adds to the log, + * in order. + */ +export interface Left { + _0: rt.u16; + _1: rt.Cell; +} + +export function log(self: Left, by: rt.u16): void { + rt.cellSet(self._1, rt.u16WrappingAdd(rt.u16WrappingMul(self._1.v, 31), self._0 ^ by)); +} + +export function ne$PartialEq_Right(self: Left, other: Right.Right): boolean { + log(self, 512); + return rt.u16Rem(self._0, 3) !== rt.u16Rem(other._0, 3); +} + +export function eq$PartialEq_Right(self: Left, other: Right.Right): boolean { + log(self, 2048); + return rt.u16Rem(self._0, 5) === rt.u16Rem(other._0, 5); +} + +export function eq$PartialEq_Left(self: Left, other: Left): boolean { + log(self, 4096); + return rt.u16Rem(self._0, 2) === rt.u16Rem(other._0, 2); +} + +//# sourceMappingURL=collections.Left.ts.map diff --git a/ts/fixtures/lang/expected/collections.Odd.ts b/ts/fixtures/lang/expected/collections.Odd.ts new file mode 100644 index 000000000..a40eadcbc --- /dev/null +++ b/ts/fixtures/lang/expected/collections.Odd.ts @@ -0,0 +1,18 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; +import type * as Right from "./collections.Right.ts"; + +/** + * An item only a reference to compares, by the crate's impl of two references, which + * std's of references would otherwise forward from. + */ +export interface Odd { + _0: rt.u16; +} + +export function eq$PartialEq_Right(self: Odd, other: Right.Right): boolean { + return rt.u16Rem(self._0, 2) === rt.u16Rem(other._0, 2); +} + +//# sourceMappingURL=collections.Odd.ts.map diff --git a/ts/fixtures/lang/expected/collections.Right.ts b/ts/fixtures/lang/expected/collections.Right.ts new file mode 100644 index 000000000..215951243 --- /dev/null +++ b/ts/fixtures/lang/expected/collections.Right.ts @@ -0,0 +1,15 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; +import * as Left from "./collections.Left.ts"; + +export interface Right { + _0: rt.u16; +} + +export function eq$PartialEq_Left(self: Right, other: Left.Left): boolean { + Left.log(other, 1024); + return rt.u16Rem(self._0, 4) === rt.u16Rem(other._0, 4); +} + +//# sourceMappingURL=collections.Right.ts.map diff --git a/ts/fixtures/lang/expected/collections.ts b/ts/fixtures/lang/expected/collections.ts index 4cc8e88ab..68d76c85a 100644 --- a/ts/fixtures/lang/expected/collections.ts +++ b/ts/fixtures/lang/expected/collections.ts @@ -7,9 +7,12 @@ import * as Cloned from "./collections.Cloned.ts"; import * as Dir from "./collections.Dir.ts"; import * as Fussy from "./collections.Fussy.ts"; import type * as Holder from "./collections.Holder.ts"; +import * as Left from "./collections.Left.ts"; import * as Nibble from "./collections.Nibble.ts"; +import * as Odd from "./collections.Odd.ts"; import * as Pair from "./collections.Pair.ts"; import * as Rank from "./collections.Rank.ts"; +import * as Right from "./collections.Right.ts"; import * as Tally from "./collections.Tally.ts"; import { thenSome$0d5am30 } from "./core/bool.ts"; import { unwrapOr$1j4op5y, unwrapOr$0r2bmpr, map$09ua5et, map$0oc32h1, mapOr$0q0ymkh, mapOr$1xztl8h, mapOr$1to2ul4, mapOr$1ht002r, mapOr$099335t, mapOr$0z4mj8g, mapOr$1surhce, mapOr$0mmpeqx, mapOr$1nlkf9b, mapOr$1t0muvr, mapOr$1w1gbwl, mapOr$02k4ito, clone$14nprly, flatten$1nwzx42 } from "./core/option.ts"; @@ -3525,4 +3528,257 @@ export function putSorted$u8s(items: rt.ByteSlice[], put: (a0: rt.ByteSlice) => putAll$u8s_IntoIter_u8s(rt.iterSlice(items), put, out); } +export function eq$PartialEq_Right_u8(self: rt.u8, other: Right.Right): boolean { + return rt.u16Rem(self as rt.u16, 4) === rt.u16Rem(other._0, 4); +} + +export function eq$PartialEq_Right_u8s(self: rt.ByteSlice, other: Right.Right): boolean { + return rt.u16Rem(rt.u16Add(self.data[self.start] as rt.u16, self.data[self.start + 1] as rt.u16), 4) === rt.u16Rem(other._0, 4); +} + +/** Whether a comparison found its sides equal, then the log of the calls it made. */ +export function putEq(eq: boolean, log: rt.Cell, out: rt.ByteVec): void { + rt.byteVecPush(out, eq ? 1 : 0); + rt.iterExtendBytes(out, rt.iterSlice(rt.u16ToBeBytes(rt.cellReplace(log, 0)).reverse()), undefined, true); +} + +/** + * `==` and `!=` of sequences whose items compare by their own `PartialEq`, and of a + * `VecDeque` against each other sequence: of unequal lengths, empty, with items `eq` and + * `ne` disagree on at the positions `kind` picks, and a deque wrapped around its buffer. + * Then `next_if_eq` of such sequences, matching and missing. + */ +export function unlikeEqs(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { + const log: rt.Cell = new rt.Cell(0); + const lefts: () => Left.Left[] = (): Left.Left[] => { + return rt.iterToArray(rt.iterMap(rt.iterSlice(rt.byteSliceView(data)), (_25: rt.u8): Left.Left => { + return { _0: _25 as rt.u16, _1: log }; + }), undefined, 8, true); + }; + const _9: [rt.usize, rt.usize] = [(kind & 7) as rt.usize, ((kind >> 3) & 7) as rt.usize]; + const byNe: rt.usize = _9[0]; + const byEq: rt.usize = _9[1]; + const step: (a0: rt.usize) => rt.u16 = (i: rt.usize): rt.u16 => { + if (!(i === byEq)) { + if (!(i === byNe)) { + return 12; + } + return 4; + } + return 3; + }; + const rights: Right.Right[] = rt.iterToArray(rt.iterMap(rt.iterEnumerate(rt.iterSlice(rt.byteSliceView(data))), (_29: [rt.usize, rt.u8]): Right.Right => { + return { _0: rt.u16Add(_29[1] as rt.u16, step(_29[0])) }; + }), undefined, 2, true); + if ((kind & 64) !== 0) { + rights.pop(); + } + const at: (a0: rt.usize) => rt.u16 = (i3: rt.usize): rt.u16 => { + return unwrapOr$0r2bmpr(rt.byteSliceGet(data, i3), 0) as rt.u16; + }; + const pair: () => Right.Right[] = (): Right.Right[] => { + return [{ _0: rt.u16Add(at(0), 12) }, { _0: rt.u16Add(at(1), 12) }]; + }; + const mine: Left.Left[] = lefts(); + const _39: Right.Right[] = rights; + putEq(rt.eqSeq((a: Left.Left, b7: Right.Right): boolean => { + return !Left.ne$PartialEq_Right(a, b7); + })(mine, _39), log, out); + putEq(!rt.eqSeq((a2: Left.Left, b8: Right.Right): boolean => { + return !Left.ne$PartialEq_Right(a2, b8); + })(mine, rights), log, out); + const _49: Left.Left[] = rt.sliceRange(mine, 0); + const _51: Right.Right[] = rt.sliceRange(rights, 0); + putEq(rt.eqSeq((a3: Left.Left, b9: Right.Right): boolean => { + return !Left.ne$PartialEq_Right(a3, b9); + })(_49, _51), log, out); + putEq(rt.eqSeq(Right.eq$PartialEq_Left)(rights, mine), log, out); + const _63: Left.Left[] = mine; + putEq(rt.eqSeq((a4: Left.Left, b10: Right.Right): boolean => { + return !Left.ne$PartialEq_Right(a4, b10); + })(_63, rights), log, out); + const _71: Right.Right[] = pair(); + putEq(rt.eqSeq((a5: Left.Left, b11: Right.Right): boolean => { + return !Left.ne$PartialEq_Right(a5, b11); + })(mine, _71), log, out); + const _79: Right.Right[] = pair(); + putEq(rt.eqSeq((a6: Left.Left, b12: Right.Right): boolean => { + return !Left.ne$PartialEq_Right(a6, b12); + })(mine, _79), log, out); + const _85: Left.Left[] = mine; + const _88: Right.Right[] = pair(); + putEq(rt.eqSeq((a7: Left.Left, b13: Right.Right): boolean => { + return !Left.ne$PartialEq_Right(a7, b13); + })(_85, _88), log, out); + const two: Left.Left[] = [{ _0: at(0), _1: log }, { _0: at(1), _1: log }]; + const _106: Right.Right[] = pair(); + putEq(rt.eqSeq((a8: Left.Left, b14: Right.Right): boolean => { + return !Left.ne$PartialEq_Right(a8, b14); + })(two, _106), log, out); + const _113: Right.Right[] = pair(); + putEq(!rt.eqSeq((a9: Left.Left, b15: Right.Right): boolean => { + return !Left.ne$PartialEq_Right(a9, b15); + })(two, _113), log, out); + const _119: Right.Right[] = rt.sliceRange(rights, 0); + putEq(rt.eqSeq((a10: Left.Left, b16: Right.Right): boolean => { + return !Left.ne$PartialEq_Right(a10, b16); + })(two, _119), log, out); + const _126: Right.Right[] = rights; + putEq(rt.eqSeq((a11: Left.Left, b17: Right.Right): boolean => { + return !Left.ne$PartialEq_Right(a11, b17); + })(two, _126), log, out); + const _132: Left.Left[] = []; + const _134: Right.Right[] = []; + putEq(rt.eqSeq((a20: Left.Left, b18: Right.Right): boolean => { + return !Left.ne$PartialEq_Right(a20, b18); + })(_132, _134), log, out); + const _139: Left.Left[] = []; + putEq(rt.eqSeq((a21: Left.Left, b19: Right.Right): boolean => { + return !Left.ne$PartialEq_Right(a21, b19); + })(_139, rights), log, out); + const _145: Right.Right[] = []; + putEq(rt.eqSeq((a22: Left.Left, b20: Right.Right): boolean => { + return !Left.ne$PartialEq_Right(a22, b20); + })(mine, _145), log, out); + const _150: rt.ByteVec = rt.byteSliceToByteVec(data); + putEq(rt.eqSeq(eq$PartialEq_Right_u8)(_150, rights), log, out); + const _157: Right.Right[] = rights; + putEq(rt.eqSeq(eq$PartialEq_Right_u8)(data, _157), log, out); + const pairs: Uint8Array[] = rt.iterToArray(rt.iterMap(rt.byteSliceChunksExact(data, 2), (c: rt.ByteSlice): Uint8Array => { + rt.idx(c, 0); + const _35: rt.u8 = c.data[c.start]; + rt.idx(c, 1); + return Uint8Array.of(_35, c.data[c.start + 1]); + }), undefined, 2, true); + const _165: Uint8Array[] = rt.sliceRange(pairs, 0); + const _167: Right.Right[] = rt.sliceRange(rights, 0, rt.min(pairs.length, rights.length)); + putEq(rt.eqSeq((a23: Uint8Array, b21: Right.Right): boolean => { + return eq$PartialEq_Right_u8s(rt.byteSliceFrom(a23), b21); + })(_165, _167), log, out); + const _179: Left.Left[][] = [lefts(), lefts()]; + const _186: Right.Right[][] = [rights, rt.sliceRange(rights, 0)]; + putEq(rt.eqSeq(rt.eqSeq((a24: Left.Left, b22: Right.Right): boolean => { + return !Left.ne$PartialEq_Right(a24, b22); + }))(_179, _186), log, out); + const refs: Right.Right[] = rt.iterToArray(rt.iterSlice(rights), undefined, 4, true); + putEq(rt.eqSeq(Left.eq$PartialEq_Right)(mine, refs), log, out); + const _204: Left.Left[] = rt.iterToArray(rt.iterSlice(mine), undefined, 4, true); + putEq(rt.eqSeq((a25: Left.Left, b23: Right.Right): boolean => { + return !Left.ne$PartialEq_Right(a25, b23); + })(_204, refs), log, out); + const _216: Left.Left[] = rt.iterToArray(rt.iterSlice(lefts()), undefined, 4, true); + putEq(rt.eqSeq(Left.eq$PartialEq_Left)(mine, _216), log, out); + const _227: Odd.Odd[] = rt.iterToArray(rt.iterSlice(rt.iterToArray(rt.iterMap(rt.iterSlice(rt.byteSliceView(data)), (_211: rt.u8): Odd.Odd => { + return { _0: _211 as rt.u16 }; + }), undefined, 2, true)), undefined, 4, true); + putEq(rt.eqSeq(Odd.eq$PartialEq_Right)(_227, refs), log, out); + const half: rt.usize = rt.usizeDiv(data.length, 2); + const q: rt.VecDeque = rt.dequeNew() as rt.VecDeque; + const iter: rt.Iter = rt.iterRev(rt.iterSlice(rt.byteSliceView(rt.byteSliceRange(data, 0, half)))); + while (true) { + const _243: rt.u8 | rt.Done = iter.next(); + if (_243 === rt.DONE) { + break; + } + const _249: Left.Left = { _0: _243 as rt.u16, _1: log }; + rt.dequePushFront(q, _249); + } + for (const _256 of rt.byteSliceView(rt.byteSliceRange(data, half))) { + const _262: Left.Left = { _0: _256 as rt.u16, _1: log }; + rt.dequePushBack(q, _262); + } + const _269: Right.Right[] = rights; + putEq(rt.eqSeq((a26: Left.Left, b25: Right.Right): boolean => { + return !Left.ne$PartialEq_Right(a26, b25); + })(q, _269), log, out); + putEq(rt.eqSeq((a27: Left.Left, b26: Right.Right): boolean => { + return !Left.ne$PartialEq_Right(a27, b26); + })(q, rights), log, out); + const _281: Right.Right[] = rights; + putEq(!rt.eqSeq((a28: Left.Left, b27: Right.Right): boolean => { + return !Left.ne$PartialEq_Right(a28, b27); + })(q, _281), log, out); + const _289: Right.Right[] = rights; + putEq(rt.eqSeq((a29: Left.Left, b28: Right.Right): boolean => { + return !Left.ne$PartialEq_Right(a29, b28); + })(q, _289), log, out); + const _296: Right.Right[] = pair(); + putEq(rt.eqSeq((a30: Left.Left, b29: Right.Right): boolean => { + return !Left.ne$PartialEq_Right(a30, b29); + })(q, _296), log, out); + const _304: Right.Right[] = pair(); + putEq(rt.eqSeq((a31: Left.Left, b30: Right.Right): boolean => { + return !Left.ne$PartialEq_Right(a31, b30); + })(q, _304), log, out); + const _312: Right.Right[] = pair(); + putEq(rt.eqSeq((a32: Left.Left, b31: Right.Right): boolean => { + return !Left.ne$PartialEq_Right(a32, b31); + })(q, _312), log, out); + const bytes: rt.VecDeque = rt.dequeNew() as rt.VecDeque; + const iter3: rt.Iter = rt.iterRev(rt.iterSlice(rt.byteSliceView(rt.byteSliceRange(data, 0, half)))); + while (true) { + const _322: rt.u8 | rt.Done = iter3.next(); + if (_322 === rt.DONE) { + break; + } + rt.dequePushFront(bytes, _322); + } + const _330: rt.ByteSlice = rt.byteSliceRange(data, half); + rt.dequeExtend(bytes, rt.iterSlice(rt.byteSliceView(_330)), undefined, 1, true); + const stepped: rt.ByteVec = rt.iterToByteVec(rt.iterMap(rt.iterEnumerate(rt.iterSlice(rt.byteSliceView(data))), (_212: [rt.usize, rt.u8]): rt.u8 => { + return _212[1] ^ (_212[0] === byEq ? 1 : 0); + }), undefined, true); + putEq(rt.eqSeq(rt.same)(bytes, stepped), log, out); + putEq(rt.eqSeq(rt.same)(bytes, data), log, out); + const _351: rt.ByteSlice = rt.byteSliceFrom(Uint8Array.of(unwrapOr$0r2bmpr(rt.byteSliceFirst(data), 0), 0)); + putEq(rt.eqSeq(rt.same)(bytes, _351), log, out); + const _358: rt.Iter = rt.iterMap(rt.dequeIter(bytes), (_214: rt.u8): rt.u16 => { + return _214 as rt.u16; + }); + const wide_: rt.VecDeque = rt.dequeExtend(rt.dequeNew() as rt.VecDeque, _358, undefined, 2, true); + const wrapped: rt.VecDeque = rt.dequeNew() as rt.VecDeque; + const iter4: rt.Iter = rt.iterRev(rt.iterSlice(rt.byteSliceView(rt.byteVecSlice(stepped)))); + while (true) { + const _368: rt.u8 | rt.Done = iter4.next(); + if (_368 === rt.DONE) { + break; + } + rt.dequePushFront(wrapped, _368 as rt.u16); + } + putEq(rt.eqSeq(rt.same)(wide_, wrapped), log, out); + const _383: rt.VecDeque[] = [rt.dequeClone(wide_, undefined)]; + const _388: rt.VecDeque[] = [wrapped]; + putEq(rt.eqSeq(rt.eqSeq(rt.same))(_383, _388), log, out); + const it: rt.Peekable = rt.iterPeekable(rt.iterSlice([lefts(), lefts(), lefts()])); + const exact: Right.Right[] = rt.iterToArray(rt.iterMap(rt.iterSlice(rt.byteSliceView(data)), (_215: rt.u8): Right.Right => { + return { _0: _215 as rt.u16 }; + }), undefined, 2, true); + putEq(rt.peekableNextIfEq(it, exact, (a031: Left.Left[], a131: Right.Right[]): boolean => { + return rt.eqSeq((a33: Left.Left, b35: Right.Right): boolean => { + return !Left.ne$PartialEq_Right(a33, b35); + })(a031, a131); + }) !== undefined, log, out); + putEq(rt.peekableNextIfEq(it, rights, (a033: Left.Left[], a133: Right.Right[]): boolean => { + return rt.eqSeq((a34: Left.Left, b36: Right.Right): boolean => { + return !Left.ne$PartialEq_Right(a34, b36); + })(a033, a133); + }) !== undefined, log, out); + const _426: Right.Right[] = rt.sliceRange(exact, 0, half); + putEq(rt.peekableNextIfEq(it, _426, (a035: Left.Left[], a135: Right.Right[]): boolean => { + return rt.eqSeq((a35: Left.Left, b37: Right.Right): boolean => { + return !Left.ne$PartialEq_Right(a35, b37); + })(a035, a135); + }) !== undefined, log, out); + rt.byteVecPush(out, rt.iterCount(it) & 0xff); + const it2: rt.Peekable> = rt.iterPeekable(rt.iterSlice([rt.dequeClone(bytes, undefined), bytes])); + const _447: rt.ByteVec = rt.byteSliceToByteVec(data); + putEq(rt.peekableNextIfEq(it2, _447, (a037: rt.VecDeque, a137: rt.ByteVec): boolean => { + return rt.eqSeq(rt.same)(a037, a137); + }) !== undefined, log, out); + putEq(rt.peekableNextIfEq(it2, stepped, (a038: rt.VecDeque, a138: rt.ByteVec): boolean => { + return rt.eqSeq(rt.same)(a038, a138); + }) !== undefined, log, out); + rt.byteVecPush(out, rt.iterCount(it2) & 0xff); +} + //# sourceMappingURL=collections.ts.map diff --git a/ts/fixtures/lang/expected/harness.ts b/ts/fixtures/lang/expected/harness.ts index d9039548f..ac0c9c3d1 100644 --- a/ts/fixtures/lang/expected/harness.ts +++ b/ts/fixtures/lang/expected/harness.ts @@ -9,7 +9,7 @@ import { captures } from "./captures.ts"; import { cells } from "./cells.ts"; import { chars } from "./chars.ts"; import { chunks } from "./chunks.ts"; -import { hashMaps, sets, btreeMaps, deques, vecs, stores, optionals, setOps } from "./collections.ts"; +import { hashMaps, sets, btreeMaps, deques, vecs, stores, optionals, setOps, unlikeEqs } from "./collections.ts"; import { combinators } from "./combinators.ts"; import { arcs, clonedArcs, cyclicArcs, uniqueArcs, clonedRcs, cyclicRcs, rcs, uniqueRcs, copies } from "./counts.ts"; import { cows } from "./cows.ts"; @@ -194,7 +194,7 @@ export function cases(): [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) lent(data70, out71); }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["floats::mul_adds", mulAdds as (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["singles::mul_adds", singles_mulAdds as (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["fmt::collections", (_2171: rt.u8, data71: rt.ByteSlice, out72: rt.ByteVec): undefined => { collections(data71, out72); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined]]; + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["collections::unlike_eqs", unlikeEqs as (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined]]; } /** Run one case. */ diff --git a/ts/fixtures/lang/expected/iters.ts b/ts/fixtures/lang/expected/iters.ts index 50ec71d36..df73ed19c 100644 --- a/ts/fixtures/lang/expected/iters.ts +++ b/ts/fixtures/lang/expected/iters.ts @@ -533,7 +533,7 @@ export function peekedOthers(data: rt.ByteSlice, out: rt.ByteVec): void { return Uint8Array.of(_317, rt.u8Rem(c.data[c.start + 1], 2)); })); const _217: rt.ByteSlice = rt.byteSliceRange(rt.byteSliceFrom(Uint8Array.of(0, 1)), 0); - rt.byteVecPush(out, mapOr$0sl5app(rt.peekableNextIfEq(it9, _217, (a, b7): boolean => { + rt.byteVecPush(out, mapOr$0sl5app(rt.peekableNextIfEq(it9, _217, (a: Uint8Array, b7: rt.ByteSlice): boolean => { return ((a017: rt.ByteSlice, a113: rt.ByteSlice): boolean => { return rt.eqBytes(a017, a113); })(rt.byteSliceFrom(a), b7); @@ -543,7 +543,7 @@ export function peekedOthers(data: rt.ByteSlice, out: rt.ByteVec): void { rt.byteVecPush(out, mapOr$0sl5app(rt.iterToOption(it9.next()), 170, (p2: Uint8Array): rt.u8 => { return p2[0]; })); - rt.byteVecPush(out, mapOr$0sl5app(rt.peekableNextIfEq(it9, { _0: 1, _1: calls }, (a2, b8): boolean => { + rt.byteVecPush(out, mapOr$0sl5app(rt.peekableNextIfEq(it9, { _0: 1, _1: calls }, (a2: Uint8Array, b8: Quarter.Quarter): boolean => { return eq$PartialEq_Quarter_u8s(rt.byteSliceFrom(a2), b8); }), 170, (p3: Uint8Array): rt.u8 => { return p3[0]; diff --git a/ts/fixtures/lang/src/collections.rs b/ts/fixtures/lang/src/collections.rs index 5ae291115..5dfa50662 100644 --- a/ts/fixtures/lang/src/collections.rs +++ b/ts/fixtures/lang/src/collections.rs @@ -1172,3 +1172,184 @@ fn put_sorted(mut items: Vec<&T>, put: fn(&T) -> u8, out: &mut Vec) items.sort(); put_all(items.into_iter(), put, out); } + +/// An item that equals a `Right` of the same remainder by 4, but whose `ne` sees the +/// remainder by 3, so what `!=` finds tells which std calls. Each call adds to the log, +/// in order. +struct Left<'a>(u16, &'a std::cell::Cell); + +struct Right(u16); + +impl Left<'_> { + fn log(&self, by: u16) { + self.1.set(self.1.get().wrapping_mul(31).wrapping_add(self.0 ^ by)); + } +} + +impl PartialEq for Left<'_> { + fn eq(&self, other: &Right) -> bool { + self.log(0x100); + self.0 % 4 == other.0 % 4 + } + + fn ne(&self, other: &Right) -> bool { + self.log(0x200); + self.0 % 3 != other.0 % 3 + } +} + +/// Against a reference, by the remainder by 5, which no reference's impl forwards to. +impl PartialEq<&Right> for Left<'_> { + fn eq(&self, other: &&Right) -> bool { + self.log(0x800); + self.0 % 5 == other.0 % 5 + } +} + +/// Against a reference to another, by the remainder by 2. +impl<'a> PartialEq<&Left<'a>> for Left<'_> { + fn eq(&self, other: &&Left<'a>) -> bool { + self.log(0x1000); + self.0 % 2 == other.0 % 2 + } +} + +/// An item only a reference to compares, by the crate's impl of two references, which +/// std's of references would otherwise forward from. +struct Odd(u16); + +impl PartialEq<&Right> for &Odd { + fn eq(&self, other: &&Right) -> bool { + self.0 % 2 == other.0 % 2 + } +} + +/// The other way, by the remainder by 4, with `ne`'s default. +impl<'a> PartialEq> for Right { + fn eq(&self, other: &Left<'a>) -> bool { + other.log(0x400); + self.0 % 4 == other.0 % 4 + } +} + +/// A byte against a `Right`, read from a typed array. +impl PartialEq for u8 { + fn eq(&self, other: &Right) -> bool { + u16::from(*self) % 4 == other.0 % 4 + } +} + +/// A pair against a `Right` by its sum, which reads the array through the reference `eq` +/// takes. +impl PartialEq for [u8; 2] { + fn eq(&self, other: &Right) -> bool { + (u16::from(self[0]) + u16::from(self[1])) % 4 == other.0 % 4 + } +} + +/// Whether a comparison found its sides equal, then the log of the calls it made. +fn put_eq(eq: bool, log: &std::cell::Cell, out: &mut Vec) { + out.push(eq as u8); + out.extend(log.replace(0).to_le_bytes()); +} + +/// `==` and `!=` of sequences whose items compare by their own `PartialEq`, and of a +/// `VecDeque` against each other sequence: of unequal lengths, empty, with items `eq` and +/// `ne` disagree on at the positions `kind` picks, and a deque wrapped around its buffer. +/// Then `next_if_eq` of such sequences, matching and missing. +pub fn unlike_eqs(kind: u8, data: &[u8], out: &mut Vec) { + let log = std::cell::Cell::new(0); + let lefts = || data.iter().map(|&b| Left(b.into(), &log)).collect::>(); + // Of the same remainders by 3 and 4 but where only `ne`'s or only `eq`'s is. + let (by_ne, by_eq) = (usize::from(kind & 7), usize::from(kind >> 3 & 7)); + let step = |i: usize| match i { + _ if i == by_eq => 3, + _ if i == by_ne => 4, + _ => 12, + }; + let mut rights: Vec = data + .iter() + .enumerate() + .map(|(i, &b)| Right(u16::from(b) + step(i))) + .collect(); + if kind & 0x40 != 0 { + rights.pop(); + } + let at = |i: usize| u16::from(data.get(i).copied().unwrap_or(0)); + let pair = || [Right(at(0) + 12), Right(at(1) + 12)]; + + let mut mine = lefts(); + put_eq(mine == rights.as_slice(), &log, out); + put_eq(mine != rights, &log, out); + put_eq(mine[..] == rights[..], &log, out); + put_eq(rights == mine, &log, out); + put_eq(&mut mine[..] == rights, &log, out); + put_eq(mine == pair(), &log, out); + put_eq(mine == &pair(), &log, out); + put_eq(mine.as_slice() == pair(), &log, out); + let two = [Left(at(0), &log), Left(at(1), &log)]; + put_eq(two == pair(), &log, out); + put_eq(two != pair(), &log, out); + put_eq(two == rights[..], &log, out); + put_eq(two == rights.as_slice(), &log, out); + put_eq(Vec::::new() == Vec::::new(), &log, out); + put_eq(Vec::::new() == rights, &log, out); + put_eq(mine == [] as [Right; 0], &log, out); + put_eq(data.to_vec() == rights, &log, out); + put_eq(data == rights.as_slice(), &log, out); + let pairs: Vec<[u8; 2]> = data.chunks_exact(2).map(|c| [c[0], c[1]]).collect(); + put_eq(pairs[..] == rights[..pairs.len().min(rights.len())], &log, out); + put_eq(vec![lefts(), lefts()] == [rights.as_slice(), &rights[..]], &log, out); + // By the impls against references, and through references of both, by `Right`'s. + let refs: Vec<&Right> = rights.iter().collect(); + put_eq(mine == refs, &log, out); + put_eq(mine.iter().collect::>() == refs, &log, out); + let others = lefts(); + put_eq(mine == others.iter().collect::>(), &log, out); + let odds: Vec = data.iter().map(|&b| Odd(b.into())).collect(); + put_eq(odds.iter().collect::>() == refs, &log, out); + + // Wrapped around: the first half pushed to the front backwards, the rest to the back. + let half = data.len() / 2; + let mut q = std::collections::VecDeque::new(); + for &b in data[..half].iter().rev() { + q.push_front(Left(b.into(), &log)); + } + for &b in &data[half..] { + q.push_back(Left(b.into(), &log)); + } + put_eq(q == rights.as_slice(), &log, out); + put_eq(q == rights, &log, out); + put_eq(q != rights.as_slice(), &log, out); + put_eq(q == &mut rights[..], &log, out); + put_eq(q == pair(), &log, out); + put_eq(q == &pair(), &log, out); + put_eq(q == &mut pair(), &log, out); + let mut bytes = std::collections::VecDeque::new(); + for &b in data[..half].iter().rev() { + bytes.push_front(b); + } + bytes.extend(&data[half..]); + let stepped: Vec = data.iter().enumerate().map(|(i, &b)| b ^ (i == by_eq) as u8).collect(); + put_eq(bytes == stepped, &log, out); + put_eq(bytes == data, &log, out); + put_eq(bytes == [data.first().copied().unwrap_or(0), 0], &log, out); + let wide: std::collections::VecDeque = bytes.iter().map(|&b| u16::from(b)).collect(); + let mut wrapped = std::collections::VecDeque::new(); + for &b in stepped.iter().rev() { + wrapped.push_front(u16::from(b)); + } + put_eq(wide == wrapped, &log, out); + put_eq(vec![wide.clone()] == [wrapped], &log, out); + + let mut it = vec![lefts(), lefts(), lefts()].into_iter().peekable(); + let exact: Vec = data.iter().map(|&b| Right(b.into())).collect(); + put_eq(it.next_if_eq(exact.as_slice()).is_some(), &log, out); + put_eq(it.next_if_eq(&rights.as_slice()).is_some(), &log, out); + put_eq(it.next_if_eq(&exact[..half]).is_some(), &log, out); + out.push(it.count() as u8); + let mut it = vec![bytes.clone(), bytes].into_iter().peekable(); + put_eq(it.next_if_eq(&data.to_vec()).is_some(), &log, out); + put_eq(it.next_if_eq(&stepped).is_some(), &log, out); + out.push(it.count() as u8); +} diff --git a/ts/fixtures/lang/src/harness.rs b/ts/fixtures/lang/src/harness.rs index 212a0c6d0..878f91494 100644 --- a/ts/fixtures/lang/src/harness.rs +++ b/ts/fixtures/lang/src/harness.rs @@ -172,6 +172,7 @@ fn cases() -> Vec<(&'static str, Case)> { ("floats::mul_adds", floats::mul_adds), ("singles::mul_adds", singles::mul_adds), ("fmt::collections", |_, data, out| fmt::collections(data, out)), + ("collections::unlike_eqs", collections::unlike_eqs), ] } diff --git a/ts/fixtures/reject/census.md b/ts/fixtures/reject/census.md index d36414e8e..c003c0c65 100644 --- a/ts/fixtures/reject/census.md +++ b/ts/fixtures/reject/census.md @@ -1,14 +1,14 @@ # Census of `reject-fixture` -- Refusals: 121 -- Function instances: 147 +- Refusals: 119 +- Function instances: 144 - Generic functions no instance reached, lowered over their type parameters: 10 ## By construct | Count | Construct | First | | ---: | --- | --- | -| 17 | unsupported call to `_` | fixtures/reject/src/lib.rs:129:11 | +| 15 | unsupported call to `_` | fixtures/reject/src/lib.rs:129:11 | | 12 | a raw pointer to `_` in `_`, which the crate may put another object in place of | fixtures/reject/src/lib.rs:648:10 | | 8 | a raw pointer to `_` in a variable assigned again while it may point there | fixtures/reject/src/lib.rs:656:10 | | 4 | `_` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it | fixtures/reject/src/lib.rs:235:16 | @@ -82,13 +82,13 @@ The type or module each refused type or path belongs to. | 7 | `std::borrow::Cow` | fixtures/reject/src/lib.rs:369:2 | | 6 | `Option` | fixtures/reject/src/lib.rs:16:15 | | 6 | `f16` | fixtures/reject/src/lib.rs:7:14 | -| 5 | `Gap` | fixtures/reject/src/lib.rs:973:10 | +| 5 | `Gap` | fixtures/reject/src/lib.rs:953:10 | | 5 | `u32` | fixtures/reject/src/lib.rs:595:27 | | 4 | `Iterator` | fixtures/reject/src/lib.rs:42:4 | | 4 | `std::cell::UnsafeCell` | fixtures/reject/src/lib.rs:727:15 | | 4 | `std::pin::Pin` | fixtures/reject/src/lib.rs:103:2 | -| 4 | `std::vec::Vec` | fixtures/reject/src/lib.rs:348:16 | | 3 | `std::sync::Arc` | fixtures/reject/src/lib.rs:306:2 | +| 3 | `std::vec::Vec` | fixtures/reject/src/lib.rs:348:16 | | 3 | `u8` | fixtures/reject/src/lib.rs:274:6 | | 3 | `{closure}` | fixtures/reject/src/lib.rs:357:13 | | 2 | `Poll` | fixtures/reject/src/lib.rs:84:20 | @@ -102,7 +102,7 @@ The type or module each refused type or path belongs to. | 2 | `std::collections::BTreeSet` | fixtures/reject/src/lib.rs:256:75 | | 2 | `std::collections::HashMap` | fixtures/reject/src/lib.rs:274:6 | | 2 | `std::iter::Iterator` | fixtures/reject/src/lib.rs:235:16 | -| 2 | `std::mem::MaybeUninit` | fixtures/reject/src/lib.rs:987:19 | +| 2 | `std::mem::MaybeUninit` | fixtures/reject/src/lib.rs:967:19 | | 2 | `std::ops::Range` | fixtures/reject/src/lib.rs:183:8 | | 2 | `std::std` | fixtures/reject/src/lib.rs:607:11 | | 2 | `std::sync::MutexGuard` | fixtures/reject/src/lib.rs:95:2 | @@ -118,16 +118,16 @@ The type or module each refused type or path belongs to. | 1 | `HashMap` | fixtures/reject/src/lib.rs:274:6 | | 1 | `Hasher` | fixtures/reject/src/lib.rs:282:9 | | 1 | `Hits` | fixtures/reject/src/lib.rs:760:21 | -| 1 | `Lane` | fixtures/reject/src/lib.rs:937:19 | +| 1 | `Lane` | fixtures/reject/src/lib.rs:917:19 | | 1 | `Maybe` | fixtures/reject/src/lib.rs:31:2 | | 1 | `Only` | fixtures/reject/src/lib.rs:777:11 | -| 1 | `Plain` | fixtures/reject/src/lib.rs:891:10 | +| 1 | `Plain` | fixtures/reject/src/lib.rs:871:10 | | 1 | `Sink` | fixtures/reject/src/lib.rs:67:10 | | 1 | `Span2` | fixtures/reject/src/lib.rs:648:10 | | 1 | `String` | fixtures/reject/src/lib.rs:824:4 | | 1 | `T \| undefined` | fixtures/reject/src/lib.rs:31:2 | | 1 | `Tail` | fixtures/reject/src/lib.rs:524:30 | -| 1 | `Track` | fixtures/reject/src/lib.rs:917:19 | +| 1 | `Track` | fixtures/reject/src/lib.rs:897:19 | | 1 | `Waiting` | fixtures/reject/src/lib.rs:535:2 | | 1 | `Window` | fixtures/reject/src/lib.rs:709:2 | | 1 | `[u8; 4]` | fixtures/reject/src/lib.rs:514:26 | @@ -135,9 +135,8 @@ The type or module each refused type or path belongs to. | 1 | `poll` | fixtures/reject/src/lib.rs:535:2 | | 1 | `rt.Poll` | fixtures/reject/src/lib.rs:535:2 | | 1 | `std::any::Any` | fixtures/reject/src/lib.rs:578:4 | -| 1 | `std::collections::VecDeque` | fixtures/reject/src/lib.rs:861:27 | | 1 | `std::iter::Chain` | fixtures/reject/src/lib.rs:175:51 | -| 1 | `std::iter::Extend` | fixtures/reject/src/lib.rs:995:6 | +| 1 | `std::iter::Extend` | fixtures/reject/src/lib.rs:975:6 | | 1 | `std::iter::Map` | fixtures/reject/src/lib.rs:224:30 | | 1 | `std::net::AddrParseError` | fixtures/reject/src/lib.rs:471:32 | | 1 | `std::net::Ipv4Addr` | fixtures/reject/src/lib.rs:471:32 | @@ -160,13 +159,13 @@ The type or module each refused type or path belongs to. | ---: | --- | --- | --- | | 6 | a raw pointer to `_` in a variable assigned again while it may point there | `(u32, u32)` | fixtures/reject/src/lib.rs:656:10 | | 4 | a variant of `_`, which is not supported: either is the same JS string | `std::borrow::Cow` | fixtures/reject/src/lib.rs:369:2 | -| 3 | a raw pointer to `_` in `_`, which the crate may put another object in place of | `(u32, u32)`, `Gap` | fixtures/reject/src/lib.rs:973:10 | +| 3 | a raw pointer to `_` in `_`, which the crate may put another object in place of | `(u32, u32)`, `Gap` | fixtures/reject/src/lib.rs:953:10 | | 3 | unsupported type `_` | `f16` | fixtures/reject/src/lib.rs:7:14 | | 2 | `_` is not supported: its `None` and `Some` would both be `rt.None` | `Option` | fixtures/reject/src/lib.rs:16:15 | | 2 | `_` of `_`, whose `None` is `rt.None`, which is not supported yet | `Iterator::scan`, `Option` | fixtures/reject/src/lib.rs:42:4 | | 2 | `_`, a trait object of another crate's trait, which is not supported yet | `dyn std::iter::Iterator` | fixtures/reject/src/lib.rs:11:16 | | 2 | `_`, a trait object of the generic trait `_`, which is not supported yet | `dyn Scaled`, `Scaled` | fixtures/reject/src/lib.rs:497:23 | -| 2 | a raw pointer to `_` in `_`, which the crate may put another object in place of | `std::mem::MaybeUninit`, `Gap` | fixtures/reject/src/lib.rs:987:19 | +| 2 | a raw pointer to `_` in `_`, which the crate may put another object in place of | `std::mem::MaybeUninit`, `Gap` | fixtures/reject/src/lib.rs:967:19 | | 2 | a raw pointer to `_` in a variable assigned again while it may point there | `std::cell::UnsafeCell` | fixtures/reject/src/lib.rs:736:15 | | 2 | an assignment through a reference to `_` | `std::pin::Pin` | fixtures/reject/src/lib.rs:119:2 | | 2 | replacing the value behind a reference to `_` | `std::pin::Pin` | fixtures/reject/src/lib.rs:103:2 | @@ -183,7 +182,7 @@ The type or module each refused type or path belongs to. | 1 | `_` of `_`, whose `clone_from` is its own, which is not supported yet | `slice::clone_from_slice`, `Reused` | fixtures/reject/src/lib.rs:324:4 | | 1 | `_` of `_`, whose `clone_from` is its own, which is not supported yet | `slice::fill`, `Reused` | fixtures/reject/src/lib.rs:21:4 | | 1 | `_` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it | `std::collections::HashMap::into_keys` | fixtures/reject/src/lib.rs:856:6 | -| 1 | `_` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it | `std::iter::Extend::extend` | fixtures/reject/src/lib.rs:995:6 | +| 1 | `_` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it | `std::iter::Extend::extend` | fixtures/reject/src/lib.rs:975:6 | | 1 | `_` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it | `std::iter::Iterator::count` | fixtures/reject/src/lib.rs:235:38 | | 1 | `_` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it | `std::iter::Iterator::filter` | fixtures/reject/src/lib.rs:235:16 | | 1 | `_` through a held type's own, which is not supported yet | `::ne` | fixtures/reject/src/lib.rs:306:2 | @@ -202,10 +201,10 @@ The type or module each refused type or path belongs to. | 1 | a dangling pointer to `_`, a value of no bytes made of more than structs and tuples | `Only` | fixtures/reject/src/lib.rs:777:11 | | 1 | a future of `_`, which can be `rt.Pending`, as the future's own `Pending` is | `std::task::Poll` | fixtures/reject/src/lib.rs:89:34 | | 1 | a raw pointer to `_` behind a reference, which is the object and not where it is kept | `std::vec::Vec` | fixtures/reject/src/lib.rs:617:10 | -| 1 | a raw pointer to `_` in `_`, which the crate may put another object in place of | `(u32, u32)`, `Lane` | fixtures/reject/src/lib.rs:937:19 | -| 1 | a raw pointer to `_` in `_`, which the crate may put another object in place of | `(u32, u32)`, `Plain` | fixtures/reject/src/lib.rs:891:10 | +| 1 | a raw pointer to `_` in `_`, which the crate may put another object in place of | `(u32, u32)`, `Lane` | fixtures/reject/src/lib.rs:917:19 | +| 1 | a raw pointer to `_` in `_`, which the crate may put another object in place of | `(u32, u32)`, `Plain` | fixtures/reject/src/lib.rs:871:10 | | 1 | a raw pointer to `_` in `_`, which the crate may put another object in place of | `(u32, u32)`, `Span2` | fixtures/reject/src/lib.rs:648:10 | -| 1 | a raw pointer to `_` in `_`, which the crate may put another object in place of | `(u32, u32)`, `Track` | fixtures/reject/src/lib.rs:917:19 | +| 1 | a raw pointer to `_` in `_`, which the crate may put another object in place of | `(u32, u32)`, `Track` | fixtures/reject/src/lib.rs:897:19 | | 1 | a raw pointer to `_` in `_`, which the crate may put another object in place of | `(u32, u32)`, `Window` | fixtures/reject/src/lib.rs:709:2 | | 1 | a raw pointer to `_` in `_`, which the crate may put another object in place of | `(u32, u32)`, `std::cell::UnsafeCell` | fixtures/reject/src/lib.rs:727:15 | | 1 | a raw pointer to `_` in `_`, which the crate may put another object in place of | `std::cell::UnsafeCell`, `Hits` | fixtures/reject/src/lib.rs:760:21 | @@ -234,12 +233,10 @@ The type or module each refused type or path belongs to. | 1 | union `_`, whose fields overlap in `_` and `f64`, which JS keeps apart | `Bits`, `u32` | fixtures/reject/src/lib.rs:622:1 | | 1 | unsupported call to `_` | `::eq` | fixtures/reject/src/lib.rs:578:2 | | 1 | unsupported call to `_` | `::deref` | fixtures/reject/src/lib.rs:381:2 | -| 1 | unsupported call to `_` | `::eq` | fixtures/reject/src/lib.rs:861:27 | | 1 | unsupported call to `_` | `::eq` | fixtures/reject/src/lib.rs:407:2 | | 1 | unsupported call to `_` | `::cycle` | fixtures/reject/src/lib.rs:129:11 | | 1 | unsupported call to `_` | `::is_sorted` | fixtures/reject/src/lib.rs:806:15 | -| 1 | unsupported call to `_` | `::clone_from` | fixtures/reject/src/lib.rs:951:10 | -| 1 | unsupported call to `_` | `::eq` | fixtures/reject/src/lib.rs:876:27 | +| 1 | unsupported call to `_` | `::clone_from` | fixtures/reject/src/lib.rs:931:10 | | 1 | unsupported call to `_` | `::hash` | fixtures/reject/src/lib.rs:339:10 | | 1 | unsupported call to `_` | `std::any::TypeId::of` | fixtures/reject/src/lib.rs:578:17 | | 1 | unsupported call to `_` | `std::ops::Range::contains` | fixtures/reject/src/lib.rs:183:8 | @@ -252,7 +249,7 @@ The type or module each refused type or path belongs to. ## Spans -
unsupported call to `_` (17) +
unsupported call to `_` (15) - fixtures/reject/src/lib.rs:129:11: unsupported call to ` as std::iter::Iterator>::cycle` - fixtures/reject/src/lib.rs:183:8: unsupported call to `std::ops::Range::::contains::` @@ -268,9 +265,7 @@ The type or module each refused type or path belongs to. - fixtures/reject/src/lib.rs:607:11: unsupported call to `std::alloc::alloc` - fixtures/reject/src/lib.rs:610:3: unsupported call to `std::alloc::dealloc` - fixtures/reject/src/lib.rs:806:15: unsupported call to ` as std::iter::Iterator>::is_sorted` -- fixtures/reject/src/lib.rs:861:27: unsupported call to ` as std::cmp::PartialEq>>::eq` -- fixtures/reject/src/lib.rs:876:27: unsupported call to ` as std::cmp::PartialEq<&[Right]>>::eq` -- fixtures/reject/src/lib.rs:951:10: unsupported call to ` as std::clone::Clone>::clone_from` +- fixtures/reject/src/lib.rs:931:10: unsupported call to ` as std::clone::Clone>::clone_from`
@@ -280,14 +275,14 @@ The type or module each refused type or path belongs to. - fixtures/reject/src/lib.rs:709:2: a raw pointer to `(u32, u32)` in `Window`, which the crate may put another object in place of - fixtures/reject/src/lib.rs:727:15: a raw pointer to `(u32, u32)` in `std::cell::UnsafeCell<(u32, u32)>`, which the crate may put another object in place of - fixtures/reject/src/lib.rs:760:21: a raw pointer to `std::cell::UnsafeCell` in `Hits`, which the crate may put another object in place of -- fixtures/reject/src/lib.rs:891:10: a raw pointer to `(u32, u32)` in `Plain`, which the crate may put another object in place of -- fixtures/reject/src/lib.rs:917:19: a raw pointer to `(u32, u32)` in `Track`, which the crate may put another object in place of -- fixtures/reject/src/lib.rs:937:19: a raw pointer to `(u32, u32)` in `Lane`, which the crate may put another object in place of -- fixtures/reject/src/lib.rs:973:10: a raw pointer to `(u32, u32)` in `Gap`, which the crate may put another object in place of -- fixtures/reject/src/lib.rs:974:10: a raw pointer to `(u32, u32)` in `Gap`, which the crate may put another object in place of -- fixtures/reject/src/lib.rs:975:58: a raw pointer to `(u32, u32)` in `Gap`, which the crate may put another object in place of -- fixtures/reject/src/lib.rs:987:19: a raw pointer to `std::mem::MaybeUninit<(u32, u32)>` in `Gap`, which the crate may put another object in place of -- fixtures/reject/src/lib.rs:988:19: a raw pointer to `std::mem::MaybeUninit<(u32, u32)>` in `Gap`, which the crate may put another object in place of +- fixtures/reject/src/lib.rs:871:10: a raw pointer to `(u32, u32)` in `Plain`, which the crate may put another object in place of +- fixtures/reject/src/lib.rs:897:19: a raw pointer to `(u32, u32)` in `Track`, which the crate may put another object in place of +- fixtures/reject/src/lib.rs:917:19: a raw pointer to `(u32, u32)` in `Lane`, which the crate may put another object in place of +- fixtures/reject/src/lib.rs:953:10: a raw pointer to `(u32, u32)` in `Gap`, which the crate may put another object in place of +- fixtures/reject/src/lib.rs:954:10: a raw pointer to `(u32, u32)` in `Gap`, which the crate may put another object in place of +- fixtures/reject/src/lib.rs:955:58: a raw pointer to `(u32, u32)` in `Gap`, which the crate may put another object in place of +- fixtures/reject/src/lib.rs:967:19: a raw pointer to `std::mem::MaybeUninit<(u32, u32)>` in `Gap`, which the crate may put another object in place of +- fixtures/reject/src/lib.rs:968:19: a raw pointer to `std::mem::MaybeUninit<(u32, u32)>` in `Gap`, which the crate may put another object in place of
@@ -309,7 +304,7 @@ The type or module each refused type or path belongs to. - fixtures/reject/src/lib.rs:235:16: `std::iter::Iterator::filter` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it - fixtures/reject/src/lib.rs:235:38: `std::iter::Iterator::count` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it - fixtures/reject/src/lib.rs:856:6: `std::collections::HashMap::::into_keys` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it -- fixtures/reject/src/lib.rs:995:6: `std::iter::Extend::extend` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it +- fixtures/reject/src/lib.rs:975:6: `std::iter::Extend::extend` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it diff --git a/ts/fixtures/reject/expected.txt b/ts/fixtures/reject/expected.txt index a497a6173..a0b936a3b 100644 --- a/ts/fixtures/reject/expected.txt +++ b/ts/fixtures/reject/expected.txt @@ -102,17 +102,15 @@ comparing pointers to `u32` in an atomic, which JS has no address of at fixtures a constant of type `&u32`, a pointer into memory translation can't name at fixtures/reject/src/lib.rs:818:3 passing `{closure@fixtures/reject/src/lib.rs:824:11: 824:19}`, which drops, to `String::retain`, which is not supported yet at fixtures/reject/src/lib.rs:824:4 `std::collections::HashMap::::into_keys` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it at fixtures/reject/src/lib.rs:856:6 -unsupported call to ` as std::cmp::PartialEq>>::eq` at fixtures/reject/src/lib.rs:861:27 -unsupported call to ` as std::cmp::PartialEq<&[Right]>>::eq` at fixtures/reject/src/lib.rs:876:27 -a raw pointer to `(u32, u32)` in `Plain`, which the crate may put another object in place of at fixtures/reject/src/lib.rs:891:10 -a raw pointer to `(u32, u32)` in `Track`, which the crate may put another object in place of at fixtures/reject/src/lib.rs:917:19 -a raw pointer to `(u32, u32)` in `Lane`, which the crate may put another object in place of at fixtures/reject/src/lib.rs:937:19 -a raw pointer to `(u32, u32)` in `Gap`, which the crate may put another object in place of at fixtures/reject/src/lib.rs:973:10 -a raw pointer to `(u32, u32)` in `Gap`, which the crate may put another object in place of at fixtures/reject/src/lib.rs:974:10 -a raw pointer to `(u32, u32)` in `Gap`, which the crate may put another object in place of at fixtures/reject/src/lib.rs:975:58 -a raw pointer to `std::mem::MaybeUninit<(u32, u32)>` in `Gap`, which the crate may put another object in place of at fixtures/reject/src/lib.rs:987:19 -a raw pointer to `std::mem::MaybeUninit<(u32, u32)>` in `Gap`, which the crate may put another object in place of at fixtures/reject/src/lib.rs:988:19 -`std::iter::Extend::extend` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it at fixtures/reject/src/lib.rs:995:6 +a raw pointer to `(u32, u32)` in `Plain`, which the crate may put another object in place of at fixtures/reject/src/lib.rs:871:10 +a raw pointer to `(u32, u32)` in `Track`, which the crate may put another object in place of at fixtures/reject/src/lib.rs:897:19 +a raw pointer to `(u32, u32)` in `Lane`, which the crate may put another object in place of at fixtures/reject/src/lib.rs:917:19 +a raw pointer to `(u32, u32)` in `Gap`, which the crate may put another object in place of at fixtures/reject/src/lib.rs:953:10 +a raw pointer to `(u32, u32)` in `Gap`, which the crate may put another object in place of at fixtures/reject/src/lib.rs:954:10 +a raw pointer to `(u32, u32)` in `Gap`, which the crate may put another object in place of at fixtures/reject/src/lib.rs:955:58 +a raw pointer to `std::mem::MaybeUninit<(u32, u32)>` in `Gap`, which the crate may put another object in place of at fixtures/reject/src/lib.rs:967:19 +a raw pointer to `std::mem::MaybeUninit<(u32, u32)>` in `Gap`, which the crate may put another object in place of at fixtures/reject/src/lib.rs:968:19 +`std::iter::Extend::extend` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it at fixtures/reject/src/lib.rs:975:6 overriding `Iterator::fold`, which std's adaptors call in place of `next` or beside it at fixtures/reject/src/lib.rs:142:2 an iterator of the crate implementing `DoubleEndedIterator`, which is not supported yet at fixtures/reject/src/lib.rs:162:1 overriding `Iterator::size_hint`, which std's adaptors call in place of `next` or beside it at fixtures/reject/src/lib.rs:196:2 diff --git a/ts/fixtures/reject/src/lib.rs b/ts/fixtures/reject/src/lib.rs index a83a0e645..457c7130c 100644 --- a/ts/fixtures/reject/src/lib.rs +++ b/ts/fixtures/reject/src/lib.rs @@ -856,26 +856,6 @@ pub fn byte_key(map: std::collections::HashMap<&[u8; 2], [u8; 2]>) -> u8 { map.into_keys().next().map_or(0, |k| k[0]) } -/// `next_if_eq` compares by the item's `PartialEq>`, which is refused as `==` is. -pub fn peeked_deque(v: Vec>) -> bool { - v.into_iter().peekable().next_if_eq(&vec![1]).is_some() -} - -pub struct Left(pub u16); - -pub struct Right(pub u16); - -impl PartialEq for Left { - fn eq(&self, other: &Right) -> bool { - self.0 % 4 == other.0 % 4 - } -} - -/// `Vec`s compare by their elements' `PartialEq`, not by `Left`'s own. -pub fn unlike_elements(v: Vec>, w: &[Right]) -> bool { - v.into_iter().peekable().next_if_eq(&w).is_some() -} - pub struct Plain { pub at: (u32, u32), } diff --git a/ts/fixtures/reject/ts.toml b/ts/fixtures/reject/ts.toml index 4031ab9ac..8c1c619b8 100644 --- a/ts/fixtures/reject/ts.toml +++ b/ts/fixtures/reject/ts.toml @@ -90,8 +90,6 @@ roots = [ "bound_ranges", "string_retained", "byte_key", - "peeked_deque", - "unlike_elements", "put_replaced", "object_replaced", "closure_replaced", diff --git a/ts/runtime/rt/collections.ts b/ts/runtime/rt/collections.ts index cb972ac3e..5d78253da 100644 --- a/ts/runtime/rt/collections.ts +++ b/ts/runtime/rt/collections.ts @@ -14,7 +14,7 @@ // A function that may drop a key, value, or item std's would, rather than return or keep // it, takes what drops each that does, as `k`, `v`, or `f`, and drops it where std's does. -import { ByteSlice, type ByteVec, type None, type Place, panic, type RangeBound, rangeCheck } from "../rt.ts"; +import { ByteSlice, ByteVec, type None, type Place, panic, type RangeBound, rangeCheck } from "../rt.ts"; import type { Hashing } from "./hash.ts"; import { type Limbs, lt } from "./int.ts"; import { @@ -275,6 +275,31 @@ export function eqSlice(eq: (a: T, b: T) => boolean): (a: ArrayLike, b: Ar }; } +/** A slice, an array, or a `Vec`, of bytes in any form, or a `VecDeque`. */ +type Seq = ArrayLike | ByteSlice | ByteVec | VecDeque; + +/** The length of `s` and what reads its element at an index. */ +function seqItems(s: Seq): [number, (i: number) => T] { + if (s instanceof ByteSlice) return [s.length, (i) => s.data[s.start + i] as T]; + if (s instanceof ByteVec) return [s.length, (i) => s.data[i] as T]; + if (!("length" in s)) return [s.n, (i) => s.b[at(s, i)] as T]; + return [s.length, (i) => s[i] as T]; +} + +/** + * `PartialEq::eq` of sequences of any form against each other, as std's of slices, arrays, + * `Vec`s, and `VecDeque`s are: their lengths, then each pair in order by `eq`. + */ +export function eqSeq(eq: (a: T, b: U) => boolean): (a: Seq, b: Seq) => boolean { + return (a, b) => { + const [n, x] = seqItems(a); + const [m, y] = seqItems(b); + if (n !== m) return false; + for (let i = 0; i < n; i++) if (!eq(x(i), y(i))) return false; + return true; + }; +} + /** `PartialEq::eq` of `Option`s, from their values'. */ export function eqOption(eq: (a: T, b: T) => boolean): (a: T | undefined, b: T | undefined) => boolean; export function eqOption(eq: (a: T, b: T) => boolean, none: None): (a: T | None, b: T | None) => boolean; diff --git a/ts/src/lower/collections.rs b/ts/src/lower/collections.rs index c0b038bed..84984bf9b 100644 --- a/ts/src/lower/collections.rs +++ b/ts/src/lower/collections.rs @@ -115,6 +115,11 @@ pub fn call<'tcx>( let eq = Expr::rt("eqBytes", args); return Ok(Some(if name == "eq" { eq } else { eq.not() })); } + // Sequences of other forms or items, whose `ne` is `!eq`. + if let Some(eq) = seq_eq(b, t, rhs, span) { + let eq = eq.call(args); + return Ok(Some(if name == "eq" { eq } else { eq.not() })); + } let kind = b.t.std_of(t) == Some(Std::ErrorKind); if matches!(t.kind(), ty::Bool | ty::Int(_) | ty::Uint(_)) || bytes(b, t) @@ -493,6 +498,12 @@ fn same_key<'tcx>(b: &Body<'_, '_, 'tcx>, key: Ty<'tcx>, by: Ty<'tcx>) -> bool { /// Whether a `ty` compares to an `other` as to another `ty`: as keys do, as bytes do, or /// by elements that do. Else the impl of `PartialEq` is its own. fn alike<'tcx>(b: &Body<'_, '_, 'tcx>, ty: Ty<'tcx>, other: Ty<'tcx>) -> bool { + // A crate's type against a reference compares by an impl of the crate's own, which no + // reference's forwards to. + let (ty, other) = peel_pair(b, ty, other); + if (ty.is_ref() || other.is_ref()) && (local(b, peel(b, ty)) || local(b, peel(b, other))) { + return false; + } let (ty, other) = (peel(b, ty), peel(b, other)); same_key(b, ty, other) || (bytes(b, ty) && bytes(b, other)) @@ -1822,6 +1833,119 @@ pub fn eq_fn<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, span: Span) -> Expr } } +/// `PartialEq::eq` of sequences of types `t` and `u`, as a function, where the runtime's of +/// keys does not compare them: a `VecDeque` against another sequence, or items that are no +/// key or compare by their own `PartialEq`. Else `None`. +fn seq_eq<'tcx>(b: &mut Body<'_, '_, 'tcx>, t: Ty<'tcx>, u: Ty<'tcx>, span: Span) -> Option { + let (t, u) = (peel(b, t), peel(b, u)); + let (Some(ti), Some(ui)) = (seq_item(b, t), seq_item(b, u)) else { + return None; + }; + let deque = [t, u].into_iter().any(|t| b.t.std_of(t) == Some(Std::VecDeque)); + if !deque && alike(b, t, u) && std_key(b, t) { + return None; + } + Some(Expr::rt("eqSeq", vec![item_eq(b, t, ti, ui, span)])) +} + +/// What a slice, an array, a `Vec`, or a `VecDeque` of type `ty` holds, bytes included. +fn seq_item<'tcx>(b: &Body<'_, '_, 'tcx>, ty: Ty<'tcx>) -> Option> { + match ty.kind() { + ty::Slice(item) | ty::Array(item, _) => Some(*item), + ty::Adt(..) if matches!(b.t.std_of(ty), Some(Std::Vec | Std::VecDeque)) => Some(arg(ty, 0)), + _ => None, + } +} + +/// What std's `!=` of a `t` and a `u` that sequence `seq` holds finds equal, as a function: +/// `>::ne`, which `!eq` stands for unless the crate overrides it. +fn item_eq<'tcx>(b: &mut Body<'_, '_, 'tcx>, seq: Ty<'tcx>, t: Ty<'tcx>, u: Ty<'tcx>, span: Span) -> Expr { + let tcx = b.t.tcx; + if let Some(eq) = seq_eq(b, t, u, span) { + return eq; + } + let (pt, pu) = (peel(b, t), peel(b, u)); + if b.t.deferred(pt) || b.t.deferred(pu) { + return Expr::undefined(); + } + if alike(b, t, u) { + if own_ne(b, pt) { + return b.err( + span, + format!( + "comparing `{seq}` by `<{pt} as PartialEq>::ne` through a held type's own, which is not supported yet" + ), + ); + } + return eq_fn(b, pt, span); + } + let (pt, pu) = peel_pair(b, t, u); + let ne = trait_fn(tcx, "PartialEq", "ne"); + let own = Instance::try_resolve(tcx, b.t.typing_env_pub(), ne, tcx.mk_args(&[pt.into(), pu.into()])) + .ok() + .flatten() + .is_some_and(|instance| instance.def_id() != ne); + impl_eq(b, if own { "ne" } else { "eq" }, pt, pu, [t, u], span) +} + +/// `t` and `u` through the references std's `PartialEq` of two references forwards +/// through, one of each at a time, while JS holds both as what they point at. A crate's +/// own impl of two references is where they stop. +fn peel_pair<'tcx>(b: &Body<'_, '_, 'tcx>, t: Ty<'tcx>, u: Ty<'tcx>) -> (Ty<'tcx>, Ty<'tcx>) { + let tcx = b.t.tcx; + let forwards = || { + let eq = trait_fn(tcx, "PartialEq", "eq"); + Instance::try_resolve(tcx, b.t.typing_env_pub(), eq, tcx.mk_args(&[t.into(), u.into()])) + .ok() + .flatten() + .is_some_and(|instance| library::is_std(tcx, instance.def_id())) + }; + match (t.kind(), u.kind()) { + (ty::Ref(_, t_in, _), ty::Ref(_, u_in, _)) + if b.t.place_pointee(t).is_none() && b.t.place_pointee(u).is_none() && forwards() => + { + peel_pair(b, *t_in, *u_in) + } + _ => (t, u), + } +} + +/// `>::{name}`, `eq` or `ne`, as a function of what JS holds of types +/// `held`, references to `t` and `u` or they themselves: a `[u8; N]` held is a typed array, +/// lent as the `rt.ByteSlice` the impl's `&[u8; N]` is. A `ne` returns what `eq` would. +pub fn impl_eq<'tcx>( + b: &mut Body<'_, '_, 'tcx>, + name: &str, + t: Ty<'tcx>, + u: Ty<'tcx>, + held: [Ty<'tcx>; 2], + span: Span, +) -> Expr { + let tcx = b.t.tcx; + let f = trait_fn(tcx, "PartialEq", name); + let f = b.fn_value(f, tcx.mk_args(&[t.into(), u.into()]), span); + let ne = name == "ne"; + if !ne && !held.iter().any(|&ty| super::byte_array(ty)) { + return f; + } + let names = [b.fresh("a"), b.fresh("b")]; + let args = std::iter::zip(&names, held) + .map(|(name, ty)| match super::byte_array(ty) { + true => Expr::rt("byteSliceFrom", vec![Expr::ident(name)]), + false => Expr::ident(name), + }) + .collect(); + let call = f.call(args); + let params = std::iter::zip(names, held) + .map(|(name, ty)| Param { + name, + ty: Some(b.t.ty(ty, span)), + }) + .collect(); + let ret = StmtKind::Return(Some(if ne { call.not() } else { call })).at(None); + Expr::Arrow(params, crate::ts::Type::Builtin("boolean"), vec![ret]) +} + /// `Hash::hash` of `ty` into a `hasher`, as a function: the runtime's for a std type, or /// the crate's. fn hash_fn<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, hasher: Ty<'tcx>, span: Span) -> Expr { diff --git a/ts/src/lower/iter.rs b/ts/src/lower/iter.rs index 0a9eb067c..c94b453f0 100644 --- a/ts/src/lower/iter.rs +++ b/ts/src/lower/iter.rs @@ -12,7 +12,7 @@ use super::Translator; use super::body::Body; use super::library::{self, Call, Std}; use super::num::Int; -use crate::ts::{Expr, Param, StmtKind, Type}; +use crate::ts::{Expr, Param, Type}; /// `Iterator::next`, and the trait's `Item`. fn trait_items(tcx: ty::TyCtxt<'_>) -> Option<(DefId, DefId)> { @@ -497,29 +497,14 @@ pub fn call<'tcx>( // An item JS copies is lent as its place, and an object as itself. "peek_mut" => method(&camel, vec![Expr::lit(b.t.needs_place(item).to_string())]), // By the item's `PartialEq`: `eq_fn` of its own type, else the impl's `eq`, as a - // call to it lowers. + // call to it lowers, given a `&T`. "next_if_eq" => { let expected = targ(1)?; let eq = match expected == item { true => super::collections::eq_fn(b, item, span), false => { - let eq = super::collections::trait_fn(tcx, "PartialEq", "eq"); - let eq = b.fn_value(eq, tcx.mk_args(&[item.into(), expected.into()]), span); - match super::byte_array(item) { - // The `eq` takes a `&[u8; N]`, an `rt.ByteSlice` of the item's bytes. - true => { - let (a, c) = (b.fresh("a"), b.fresh("b")); - let lent = Expr::rt("byteSliceFrom", vec![Expr::ident(&a)]); - let call = eq.call(vec![lent, Expr::ident(&c)]); - let params = [a, c].map(|name| Param { name, ty: None }).into(); - Expr::Arrow( - params, - Type::Builtin("boolean"), - vec![StmtKind::Return(Some(call)).at(None)], - ) - } - false => eq, - } + let by = Ty::new_imm_ref(tcx, tcx.lifetimes.re_erased, expected); + super::collections::impl_eq(b, "eq", item, expected, [item, by], span) } }; method(&camel, [rest, vec![eq], none.into_iter().collect()].concat())