diff --git a/quest/m3/README.md b/quest/m3/README.md index 4bb8362b0..6f3c2d093 100644 --- a/quest/m3/README.md +++ b/quest/m3/README.md @@ -20,7 +20,6 @@ generic std gap is planned here even when moq never calls it. - [Trait objects](/quest/m3/ts-dyn.md) - `dyn Trait` and `Box` translate, so a consumer neither rewrites nor binds them - [Waiting: type-check in process with tsrs](/quest/m3/ts-tsrs.md) - rust2-ts checks bindings through tsrs instead of `tsc` once it is a stable library - [Fold matches across tests that aren't variant switches](/quest/m3/ts-fold-guards.md) - a match an `if` or guard decided drops its dead arms -- [A crate's own comparison operators](/quest/m3/ts-partial-ord.md) - an overridden `lt`/`le`/`gt`/`ge` is called where std calls it - [An inclusive range drained by a fold](/quest/m3/ts-range-fold.md) - a folded `RangeInclusive` ends in core's state - [bool's combinators from core's MIR](/quest/m3/ts-bool-core.md) - `bool::then` lowers from core, retiring `rt.boolThen` - [Binding gaps](/quest/m3/ts-bind-gaps.md) - bound types as keys, unit returns checked, renamed dependencies diff --git a/quest/m3/ts-partial-ord.md b/quest/m3/ts-partial-ord.md deleted file mode 100644 index afb206257..000000000 --- a/quest/m3/ts-partial-ord.md +++ /dev/null @@ -1,15 +0,0 @@ -# [S] A crate's own comparison operators - -## Goal - -rust2-ts translates `<`, `<=`, `>`, `>=`, sorts, and `Ord::max`, `min`, and `clamp` on a -type whose `PartialOrd` overrides `lt`, `le`, `gt`, or `ge`, calling the crate's override -where std does, proven by differential tests against native Rust. - -## Plan - -Found by [#124](https://github.com/kixelated/rust2/pull/124) on 2026-10-27: default sorts -and comparisons now go through `partial_cmp` in std's operand order, and a comparison that -reaches the crate's own `lt`, `le`, `gt`, or `ge` is refused with its span. - -Public API: the translator's output. diff --git a/ts/MIGRATE.md b/ts/MIGRATE.md index eeefa10f0..7479f0e0a 100644 --- a/ts/MIGRATE.md +++ b/ts/MIGRATE.md @@ -2,6 +2,10 @@ ## Unreleased +- `rt.ordMax`, `rt.ordMin`, and `rt.sliceSortByKey` take a `PartialOrd::lt` rather than a + `partial_cmp`, and `rt.ordClamp` takes `le`, `lt`, and `gt`; `rt.cmpLt` makes one from a + `partial_cmp`. `rt.sliceSortBy` takes only an `Ord::cmp`-like comparator: sort by an + `lt` with `rt.sliceSort`. - A trait object in a function's suffix is named for its trait and the types it binds: `show$dynstdfmtDebug` is `show$dyn_Debug`, and `poll$dyn_Future_u64` is a `dyn Future`'s. diff --git a/ts/fixtures/lang/expected/cows.ts b/ts/fixtures/lang/expected/cows.ts index 8c07877e7..5d92481d0 100644 --- a/ts/fixtures/lang/expected/cows.ts +++ b/ts/fixtures/lang/expected/cows.ts @@ -95,7 +95,7 @@ export function cows(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { } rt.byteVecPush(out, _189 ? 1 : 0); } - rt.byteVecPush(out, rt.ordMax(a, b, rt.cmpStr) === b ? 1 : 0); + rt.byteVecPush(out, rt.ordMax(a, b, rt.cmpLt(rt.cmpStr)) === b ? 1 : 0); } else if (_14 === 3) { const n: Named.Named = Named.new(first, kind); const m2: Named.Named = Named.clone$Clone(n); diff --git a/ts/fixtures/lang/expected/durations.ts b/ts/fixtures/lang/expected/durations.ts index 8818d68df..73adaa985 100644 --- a/ts/fixtures/lang/expected/durations.ts +++ b/ts/fixtures/lang/expected/durations.ts @@ -124,8 +124,8 @@ export function durations(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): voi } rt.byteVecPush(out, _144 ? 1 : 0); } - putDuration(out, rt.ordMax(a, b, rt.durationCmp)); - putDuration(out, rt.ordMin(a, b, rt.durationCmp)); + putDuration(out, rt.ordMax(a, b, rt.cmpLt(rt.durationCmp))); + putDuration(out, rt.ordMin(a, b, rt.cmpLt(rt.durationCmp))); const seen: rt.BTreeMap = rt.btreeNew(rt.durationCmp) as rt.BTreeMap; const iter2: rt.Iter<[rt.usize, rt.Duration]> = rt.iterEnumerate(rt.iterSlice([a, b, { secs: 1, nanos: 500000000 }, a, { secs: 0, nanos: 0 }])); while (true) { @@ -176,7 +176,7 @@ export function durations(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): voi w.start = rt.durationSub(w.start, b); putDuration(out, w.start); if (w.len !== undefined) { - const _283: rt.Duration = rt.ordMin(a, b, rt.durationCmp); + const _283: rt.Duration = rt.ordMin(a, b, rt.cmpLt(rt.durationCmp)); w.len = rt.durationSub(w.len, _283); } putOpt(out, w.len); diff --git a/ts/fixtures/lang/expected/edits.ts b/ts/fixtures/lang/expected/edits.ts index 36f3fa6c5..138fe9dfe 100644 --- a/ts/fixtures/lang/expected/edits.ts +++ b/ts/fixtures/lang/expected/edits.ts @@ -102,7 +102,7 @@ export function edits(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { rt.byteVecPush(out, names2.length & 0xff); } else if (_8 === 2) { const v3: rt.u16[] = words.slice(); - rt.sliceSortBy(v3, rt.cmp); + rt.sliceSort(v3, rt.cmpLt(rt.cmp)); putU16s(out, v3); const v4: rt.u16[] = words.slice(); rt.sliceSortBy(v4, (a: rt.u16, b4: rt.u16): rt.Ordering => { @@ -114,7 +114,7 @@ export function edits(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { })); rt.sliceSortByKey(pairs2, (p2: [rt.u16, rt.u16]): rt.u16 => { return p2[0]; - }, rt.cmp); + }, rt.cmpLt(rt.cmp)); for (const _159 of pairs2) { putU16s(out, [_159[0], _159[1]]); } @@ -122,7 +122,7 @@ export function edits(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { const a2: rt.u16 = _213[0]; const b5: rt.u16 = _213[1]; return [rt.u16Rem(b5, 3), a2, b5] as [rt.u16, rt.u16, rt.u16]; - }, rt.cmpTuple([rt.cmp, rt.cmp, rt.cmp])); + }, rt.cmpLt(rt.cmpTuple([rt.cmp, rt.cmp, rt.cmp]))); for (const _176 of pairs2) { putU16s(out, [_176[0], _176[1]]); } @@ -139,7 +139,7 @@ export function edits(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { } return undefined; })); - rt.sliceSortBy(options, rt.cmpOption(rt.cmp)); + rt.sliceSort(options, rt.cmpLt(rt.cmpOption(rt.cmp))); for (const _215 of options) { putU16s(out, [mapOr$Option_u16_u16_edits_closure21(_215, 65535, (o: rt.u16): rt.u16 => { return o; @@ -149,7 +149,7 @@ export function edits(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { const b6: rt.u8 = _216; return text(rt.byteSliceFrom(Uint8Array.of(b6, rt.u8Div(b6, 16)))); })); - rt.sliceSortBy(names3, rt.cmpStr); + rt.sliceSort(names3, rt.cmpLt(rt.cmpStr)); for (const _234 of names3) { put(out, _234); } @@ -213,8 +213,8 @@ export function edits(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { return rt.u16Add(i4 as rt.u16, b3 as rt.u16); })); })); - const _424: rt.u16[][] = rt.vecClone(nested, (a014: rt.u16[]): rt.u16[] => { - return a014.slice(); + const _424: rt.u16[][] = rt.vecClone(nested, (a018: rt.u16[]): rt.u16[] => { + return a018.slice(); }); rt.byteVecPush(out, rt.eqSlice(rt.eqSlice(rt.same))(_424, nested) ? 1 : 0); const pair: [string, rt.u8] = [text(data), at(0)]; @@ -255,16 +255,16 @@ export function edits(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { const x: Stamp.Stamp = _529[0]; const lo: Stamp.Stamp = _529[1]; const hi: Stamp.Stamp = _529[2]; - rt.byteVecPush(out, rt.ordMax(x, lo, Stamp.partialCmp$PartialOrd)._0); - rt.byteVecPush(out, rt.ordMin(x, lo, Stamp.partialCmp$PartialOrd)._0); - rt.byteVecPush(out, rt.ordClamp(x, lo, hi, Stamp.partialCmp$PartialOrd)._0); + rt.byteVecPush(out, rt.ordMax(x, lo, rt.cmpLt(Stamp.partialCmp$PartialOrd))._0); + rt.byteVecPush(out, rt.ordMin(x, lo, rt.cmpLt(Stamp.partialCmp$PartialOrd))._0); + rt.byteVecPush(out, rt.ordClamp(x, lo, hi, rt.cmpLe(Stamp.partialCmp$PartialOrd), rt.cmpLt(Stamp.partialCmp$PartialOrd), rt.cmpGt(Stamp.partialCmp$PartialOrd))._0); const _554: [Lean.Lean, Lean.Lean, Lean.Lean] = [{ _0: at(0) }, { _0: at(1) }, { _0: at(2) }]; const p: Lean.Lean = _554[0]; const q: Lean.Lean = _554[1]; const r: Lean.Lean = _554[2]; - rt.byteVecPush(out, rt.ordMax(p, q, Lean.partialCmp$PartialOrd)._0); - rt.byteVecPush(out, rt.ordMin(p, q, Lean.partialCmp$PartialOrd)._0); - rt.byteVecPush(out, rt.ordClamp(p, q, r, Lean.partialCmp$PartialOrd)._0); + rt.byteVecPush(out, rt.ordMax(p, q, rt.cmpLt(Lean.partialCmp$PartialOrd))._0); + rt.byteVecPush(out, rt.ordMin(p, q, rt.cmpLt(Lean.partialCmp$PartialOrd))._0); + rt.byteVecPush(out, rt.ordClamp(p, q, r, rt.cmpLe(Lean.partialCmp$PartialOrd), rt.cmpLt(Lean.partialCmp$PartialOrd), rt.cmpGt(Lean.partialCmp$PartialOrd))._0); } else { const s: string = text(data); const sep: rt.char[] = [47, 97]; diff --git a/ts/fixtures/lang/expected/harness.ts b/ts/fixtures/lang/expected/harness.ts index 6561fd200..9b29c82da 100644 --- a/ts/fixtures/lang/expected/harness.ts +++ b/ts/fixtures/lang/expected/harness.ts @@ -14,7 +14,7 @@ import { ints, strings, derived, written, counts, rounding, panics } from "./fmt import { methods } from "./generics.ts"; import { u8Op, u16Op, u32Op, u64Op, u128Op, usizeOp, i8Op, i16Op, i32Op, i64Op, i128Op, isizeOp, i8Neg, i16Neg, i32Neg, i64Neg, i128Neg, isizeNeg, cast as ints_cast, convert } from "./ints.ts"; import { adaptors, consumers, skips, ranges, options, own, values } from "./iters.ts"; -import { keys } from "./keys.ts"; +import { keys, overrides } from "./keys.ts"; import { nested } from "./nested.ts"; import { refs } from "./refs.ts"; import { copies, order, discriminants, loops, patterns, closures, library, flows } from "./shapes.ts"; @@ -74,7 +74,7 @@ export function cases(): [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) counts(data22, out22); }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::rounding", (_232: rt.u8, _310: rt.ByteSlice, out23: rt.ByteVec): void => { rounding(out23); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::skips", skips] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["wrappers::wrappers", wrappers] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["edits::edits", edits] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["utf8::decodes", decodes] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["floats::floats", floats] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["atomics::atomics", atomics] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void]]; + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::skips", skips] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["wrappers::wrappers", wrappers] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["edits::edits", edits] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["utf8::decodes", decodes] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["floats::floats", floats] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["atomics::atomics", atomics] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["keys::overrides", overrides] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void]]; } /** Run one case. */ diff --git a/ts/fixtures/lang/expected/keys.Lopsided.ts b/ts/fixtures/lang/expected/keys.Lopsided.ts new file mode 100644 index 000000000..bce19bd87 --- /dev/null +++ b/ts/fixtures/lang/expected/keys.Lopsided.ts @@ -0,0 +1,43 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; + +export function eq$PartialEq(self: Lopsided, other: Lopsided): boolean { + return self._0 === other._0; +} + +export function cmp$Ord(self: Lopsided, other: Lopsided): rt.Ordering { + return rt.cmp(self._0, other._0); +} + +/** + * An `Ord` by value whose own `<`, `<=`, `>`, and `>=` each disagree with it: `<` and `>` + * reversed, `<=` by quarter and reversed, and `>=` by the value mod 4, so a test that std + * calls and the translation computes from `partial_cmp` shows. `<` is still a total + * order, which sorts need. + */ +export interface Lopsided { + _0: rt.u8; +} + +export function partialCmp$PartialOrd(self: Lopsided, other: Lopsided): rt.Ordering | undefined { + return cmp$Ord(self, other); +} + +export function lt$PartialOrd(self: Lopsided, other: Lopsided): boolean { + return self._0 > other._0; +} + +export function le$PartialOrd(self: Lopsided, other: Lopsided): boolean { + return rt.u8Div(self._0, 4) >= rt.u8Div(other._0, 4); +} + +export function gt$PartialOrd(self: Lopsided, other: Lopsided): boolean { + return self._0 < other._0; +} + +export function ge$PartialOrd(self: Lopsided, other: Lopsided): boolean { + return rt.u8Rem(self._0, 4) >= rt.u8Rem(other._0, 4); +} + +//# sourceMappingURL=keys.Lopsided.ts.map diff --git a/ts/fixtures/lang/expected/keys.ts b/ts/fixtures/lang/expected/keys.ts index a8ff381f4..e4d6fc693 100644 --- a/ts/fixtures/lang/expected/keys.ts +++ b/ts/fixtures/lang/expected/keys.ts @@ -1,11 +1,12 @@ // Generated by rust2-ts. Do not edit. import * as rt from "./rt.ts"; -import { copy$Hop, copy$Horizon, copy$u8_Horizon } from "./lib.ts"; +import { copy$Hop, copy$Horizon, copy$u8_Horizon, drop$u8_Reverse_Arc_Lopsided } from "./lib.ts"; import { unwrapOr$Option_NonZero_u64, unwrapOr$Option_u16, unwrapOr$Option_u8, map$Option_Hop_u64_defaults_closure0, map$Option_Hop_u64_u64_options_closure0, map$Option_Way_bool_defaults_closure2, map$Option_u8_Side_side, mapOr$Option_Hop_u64_options_closure2, mapOr$Option_Ordering_u8_order } from "./core/option.ts"; import * as Hop from "./keys.Hop.ts"; import * as Horizon from "./keys.Horizon.ts"; import * as Liar from "./keys.Liar.ts"; +import * as Lopsided from "./keys.Lopsided.ts"; import * as Name from "./keys.Name.ts"; import * as Never from "./keys.Never.ts"; import * as Picky from "./keys.Picky.ts"; @@ -201,6 +202,11 @@ export function keys(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { } } +/** `!=`, `<`, `<=`, `>`, and `>=` of each of `l` and `r` with the other, as bits. */ +export function compare$Arc_Lopsided(l: rt.Arc, r: rt.Arc): rt.u8 { + return (!rt.eqArc(Lopsided.eq$PartialEq, true)(l, r) ? 1 : 0) | (((rt.cmpArc(Lopsided.lt$PartialOrd)(l, r) ? 1 : 0) << 1) & 0xff) | (((rt.cmpArc(Lopsided.le$PartialOrd)(l, r) ? 1 : 0) << 2) & 0xff) | (((rt.cmpArc(Lopsided.gt$PartialOrd)(l, r) ? 1 : 0) << 3) & 0xff) | (((rt.cmpArc(Lopsided.ge$PartialOrd)(l, r) ? 1 : 0) << 4) & 0xff); +} + /** `!=`, `<`, `<=`, `>`, and `>=` of each of `l` and `r` with the other, as bits. */ export function compare$Horizon(l: Horizon.Horizon, r: Horizon.Horizon): rt.u8 { const _5: rt.u8 = (!Horizon.eq$PartialEq(l, r) ? 1 : 0) | (((Horizon.partialCmp$PartialOrd(l, r) === -1 ? 1 : 0) << 1) & 0xff); @@ -210,6 +216,20 @@ export function compare$Horizon(l: Horizon.Horizon, r: Horizon.Horizon): rt.u8 { return _3 | (((order2 !== undefined && order2 >= 0 ? 1 : 0) << 4) & 0xff); } +/** `!=`, `<`, `<=`, `>`, and `>=` of each of `l` and `r` with the other, as bits. */ +export function compare$Lopsided(l: Lopsided.Lopsided, r: Lopsided.Lopsided): rt.u8 { + return (!Lopsided.eq$PartialEq(l, r) ? 1 : 0) | (((Lopsided.lt$PartialOrd(l, r) ? 1 : 0) << 1) & 0xff) | (((Lopsided.le$PartialOrd(l, r) ? 1 : 0) << 2) & 0xff) | (((Lopsided.gt$PartialOrd(l, r) ? 1 : 0) << 3) & 0xff) | (((Lopsided.ge$PartialOrd(l, r) ? 1 : 0) << 4) & 0xff); +} + +/** `!=`, `<`, `<=`, `>`, and `>=` of each of `l` and `r` with the other, as bits. */ +export function compare$Lopsided_u8(l: [Lopsided.Lopsided, rt.u8], r: [Lopsided.Lopsided, rt.u8]): rt.u8 { + const _5: rt.u8 = (!rt.eqTuple([Lopsided.eq$PartialEq, rt.same])(l, r) ? 1 : 0) | (((rt.cmpTuple([Lopsided.partialCmp$PartialOrd, rt.cmp])(l, r) === -1 ? 1 : 0) << 1) & 0xff); + const order_ = rt.cmpTuple([Lopsided.partialCmp$PartialOrd, rt.cmp])(l, r); + const _3: rt.u8 = _5 | (((order_ !== undefined && order_ <= 0 ? 1 : 0) << 2) & 0xff) | (((rt.cmpTuple([Lopsided.partialCmp$PartialOrd, rt.cmp])(l, r) === 1 ? 1 : 0) << 3) & 0xff); + const order2 = rt.cmpTuple([Lopsided.partialCmp$PartialOrd, rt.cmp])(l, r); + return _3 | (((order2 !== undefined && order2 >= 0 ? 1 : 0) << 4) & 0xff); +} + /** `!=`, `<`, `<=`, `>`, and `>=` of each of `l` and `r` with the other, as bits. */ export function compare$Name(l: Name.Name, r: Name.Name): rt.u8 { const _5: rt.u8 = (!Name.eq$PartialEq(l, r) ? 1 : 0) | (((Name.partialCmp$PartialOrd(l, r) === -1 ? 1 : 0) << 1) & 0xff); @@ -228,6 +248,15 @@ export function compare$Option_Hop(l: Hop.Hop | undefined, r: Hop.Hop | undefine return _3 | (((order2 !== undefined && order2 >= 0 ? 1 : 0) << 4) & 0xff); } +/** `!=`, `<`, `<=`, `>`, and `>=` of each of `l` and `r` with the other, as bits. */ +export function compare$Option_Lopsided(l: Lopsided.Lopsided | undefined, r: Lopsided.Lopsided | undefined): rt.u8 { + const _5: rt.u8 = (!rt.eqOption(Lopsided.eq$PartialEq)(l, r) ? 1 : 0) | (((rt.cmpOption(Lopsided.partialCmp$PartialOrd)(l, r) === -1 ? 1 : 0) << 1) & 0xff); + const order_ = rt.cmpOption(Lopsided.partialCmp$PartialOrd)(l, r); + const _3: rt.u8 = _5 | (((order_ !== undefined && order_ <= 0 ? 1 : 0) << 2) & 0xff) | (((rt.cmpOption(Lopsided.partialCmp$PartialOrd)(l, r) === 1 ? 1 : 0) << 3) & 0xff); + const order2 = rt.cmpOption(Lopsided.partialCmp$PartialOrd)(l, r); + return _3 | (((order2 !== undefined && order2 >= 0 ? 1 : 0) << 4) & 0xff); +} + /** `!=`, `<`, `<=`, `>`, and `>=` of each of `l` and `r` with the other, as bits. */ export function compare$Option_Rough(l: Rough.Rough | undefined, r: Rough.Rough | undefined): rt.u8 { const _5: rt.u8 = (!rt.eqOption(Rough.eq$PartialEq)(l, r) ? 1 : 0) | (((rt.cmpOption(Rough.partialCmp$PartialOrd)(l, r) === -1 ? 1 : 0) << 1) & 0xff); @@ -237,6 +266,11 @@ export function compare$Option_Rough(l: Rough.Rough | undefined, r: Rough.Rough return _3 | (((order2 !== undefined && order2 >= 0 ? 1 : 0) << 4) & 0xff); } +/** `!=`, `<`, `<=`, `>`, and `>=` of each of `l` and `r` with the other, as bits. */ +export function compare$Reverse_Lopsided(l: Lopsided.Lopsided, r: Lopsided.Lopsided): rt.u8 { + return (!Lopsided.eq$PartialEq(l, r) ? 1 : 0) | (((rt.cmpReverse(Lopsided.lt$PartialOrd)(l, r) ? 1 : 0) << 1) & 0xff) | (((rt.cmpReverse(Lopsided.le$PartialOrd)(l, r) ? 1 : 0) << 2) & 0xff) | (((rt.cmpReverse(Lopsided.gt$PartialOrd)(l, r) ? 1 : 0) << 3) & 0xff) | (((rt.cmpReverse(Lopsided.ge$PartialOrd)(l, r) ? 1 : 0) << 4) & 0xff); +} + /** `!=`, `<`, `<=`, `>`, and `>=` of each of `l` and `r` with the other, as bits. */ export function compare$Rough(l: Rough.Rough, r: Rough.Rough): rt.u8 { const _5: rt.u8 = (!Rough.eq$PartialEq(l, r) ? 1 : 0) | (((Rough.partialCmp$PartialOrd(l, r) === -1 ? 1 : 0) << 1) & 0xff); @@ -264,6 +298,21 @@ export function compare$Skewed(l: Skewed.Skewed, r: Skewed.Skewed): rt.u8 { return _3 | (((order2 !== undefined && order2 >= 0 ? 1 : 0) << 4) & 0xff); } +/** `!=`, `<`, `<=`, `>`, and `>=` of each of `l` and `r` with the other, as bits. */ +export function compare$u8_Lopsided(l: [rt.u8, Lopsided.Lopsided], r: [rt.u8, Lopsided.Lopsided]): rt.u8 { + return (!rt.eqTuple([rt.same, Lopsided.eq$PartialEq])(l, r) ? 1 : 0) | (((rt.testTuple([rt.cmp], Lopsided.lt$PartialOrd, -1)(l, r) ? 1 : 0) << 1) & 0xff) | (((rt.testTuple([rt.cmp], Lopsided.le$PartialOrd, -1)(l, r) ? 1 : 0) << 2) & 0xff) | (((rt.testTuple([rt.cmp], Lopsided.gt$PartialOrd, 1)(l, r) ? 1 : 0) << 3) & 0xff) | (((rt.testTuple([rt.cmp], Lopsided.ge$PartialOrd, 1)(l, r) ? 1 : 0) << 4) & 0xff); +} + +/** `!=`, `<`, `<=`, `>`, and `>=` of each of `l` and `r` with the other, as bits. */ +export function compare$u8_Reverse_Arc_Lopsided(l: [rt.u8, rt.Arc], r: [rt.u8, rt.Arc]): rt.u8 { + return (!rt.eqTuple([rt.same, rt.eqArc(Lopsided.eq$PartialEq, true)])(l, r) ? 1 : 0) | (((rt.testTuple([rt.cmp], rt.cmpReverse(rt.cmpArc(Lopsided.lt$PartialOrd)), -1)(l, r) ? 1 : 0) << 1) & 0xff) | (((rt.testTuple([rt.cmp], rt.cmpReverse(rt.cmpArc(Lopsided.le$PartialOrd)), -1)(l, r) ? 1 : 0) << 2) & 0xff) | (((rt.testTuple([rt.cmp], rt.cmpReverse(rt.cmpArc(Lopsided.gt$PartialOrd)), 1)(l, r) ? 1 : 0) << 3) & 0xff) | (((rt.testTuple([rt.cmp], rt.cmpReverse(rt.cmpArc(Lopsided.ge$PartialOrd)), 1)(l, r) ? 1 : 0) << 4) & 0xff); +} + +/** `!=`, `<`, `<=`, `>`, and `>=` of each of `l` and `r` with the other, as bits. */ +export function compare$u8_u8_Lopsided(l: [[rt.u8, rt.u8], Lopsided.Lopsided], r: [[rt.u8, rt.u8], Lopsided.Lopsided]): rt.u8 { + return (!rt.eqTuple([rt.eqTuple([rt.same, rt.same]), Lopsided.eq$PartialEq])(l, r) ? 1 : 0) | (((rt.testTuple([rt.cmpTuple([rt.cmp, rt.cmp])], Lopsided.lt$PartialOrd, -1)(l, r) ? 1 : 0) << 1) & 0xff) | (((rt.testTuple([rt.cmpTuple([rt.cmp, rt.cmp])], Lopsided.le$PartialOrd, -1)(l, r) ? 1 : 0) << 2) & 0xff) | (((rt.testTuple([rt.cmpTuple([rt.cmp, rt.cmp])], Lopsided.gt$PartialOrd, 1)(l, r) ? 1 : 0) << 3) & 0xff) | (((rt.testTuple([rt.cmpTuple([rt.cmp, rt.cmp])], Lopsided.ge$PartialOrd, 1)(l, r) ? 1 : 0) << 4) & 0xff); +} + /** * The comparisons a type's `PartialEq` and `PartialOrd` inherit: `!=` as `!eq`, and * the orderings from `partial_cmp`, false when it is `None`, whatever `eq` says. @@ -371,6 +420,155 @@ export function defaults(data: rt.ByteSlice, out: rt.ByteVec): void { } } +/** + * The comparisons std makes through a type's own `<`, `<=`, `>`, and `>=`: directly, as + * the last field of a tuple, in an `Arc`, and in a `Reverse`, and in `max`, `min`, + * `clamp`, and sorts. A field before a tuple's last, and an `Option`, compare by + * `partial_cmp`. The first three bytes clamp last, which panics unless `min <= max`. + */ +export function overrides(_1: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { + let _8!: rt.usize; + let _90!: rt.Arc; + let _92!: rt.Arc; + let _108!: [rt.u8, rt.Arc]; + let _114!: [rt.u8, rt.Arc]; + let n!: rt.Arc; + let _142!: rt.Arc; + let _200!: boolean; + let unwind: number = 0; + try { + _200 = false; + let iterStart: rt.usize = 1; + const iterEnd: rt.usize = data.length; + while ((_8 = iterStart++) < iterEnd) { + const i: rt.usize = _8; + const _16: rt.usize = rt.usizeSub(i, 1); + rt.idx(data, _16); + const _15: rt.u8 = data.data[data.start + _16]; + rt.idx(data, i); + const _14: [rt.u8, rt.u8] = [_15, data.data[data.start + i]]; + const c: rt.u8 = _15; + const d: rt.u8 = _14[1]; + const _25: [Lopsided.Lopsided, Lopsided.Lopsided] = [{ _0: c }, { _0: d }]; + const a: Lopsided.Lopsided = _25[0]; + const b: Lopsided.Lopsided = _25[1]; + rt.byteVecPush(out, compare$Lopsided(a, b)); + const _34: rt.u8 = compare$u8_Lopsided([rt.u8Rem(c, 2), a] as [rt.u8, Lopsided.Lopsided], [rt.u8Rem(d, 2), b] as [rt.u8, Lopsided.Lopsided]); + const _48: [rt.u8, Lopsided.Lopsided] = [rt.u8Rem(c, 2), a]; + const _55: [rt.u8, Lopsided.Lopsided] = [rt.u8Rem(d, 2), b]; + rt.byteVecPush(out, _34 | (((rt.testTuple([rt.cmp], Lopsided.lt$PartialOrd, -1)(_48, _55) ? 1 : 0) << 5) & 0xff)); + rt.byteVecPush(out, compare$u8_u8_Lopsided([[rt.u8Rem(c, 2), rt.u8Rem(c, 3)] as [rt.u8, rt.u8], a] as [[rt.u8, rt.u8], Lopsided.Lopsided], [[rt.u8Rem(d, 2), rt.u8Rem(d, 3)] as [rt.u8, rt.u8], b] as [[rt.u8, rt.u8], Lopsided.Lopsided])); + const _80: [Lopsided.Lopsided, rt.u8] = [a, rt.u8Rem(c, 2)]; + unwind = 0; + const _78: rt.u8 = compare$Lopsided_u8(_80, [b, rt.u8Rem(d, 2)] as [Lopsided.Lopsided, rt.u8]); + unwind = 0; + rt.byteVecPush(out, _78); + _90 = new rt.Arc(a); + unwind = 100; + _92 = new rt.Arc(b); + unwind = 99; + rt.byteVecPush(out, compare$Arc_Lopsided(_90, _92)); + unwind = 100; + rt.drop(_92); + unwind = 0; + rt.drop(_90); + rt.byteVecPush(out, compare$Reverse_Lopsided(a, b)); + rt.byteVecPush(out, compare$Option_Lopsided(a, b)); + const _109: rt.u8 = rt.u8Rem(c, 2); + const _112: rt.Arc = new rt.Arc(a); + unwind = 98; + _108 = [_109, _112] as [rt.u8, rt.Arc]; + const _115: rt.u8 = rt.u8Rem(d, 2); + const _118: rt.Arc = new rt.Arc(b); + unwind = 97; + _114 = [_115, _118] as [rt.u8, rt.Arc]; + rt.byteVecPush(out, compare$u8_Reverse_Arc_Lopsided(_108, _114)); + unwind = 98; + drop$u8_Reverse_Arc_Lopsided(_114); + unwind = 0; + drop$u8_Reverse_Arc_Lopsided(_108); + rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(rt.ordMax(a, b, Lopsided.lt$PartialOrd)._0, rt.ordMin(a, b, Lopsided.lt$PartialOrd)._0, rt.ordMax(a, b, Lopsided.lt$PartialOrd)._0))); + const _132: [rt.u8, Lopsided.Lopsided] = rt.ordMax([rt.u8Rem(c, 2), a] as [rt.u8, Lopsided.Lopsided], [rt.u8Rem(d, 2), b] as [rt.u8, Lopsided.Lopsided], rt.testTuple([rt.cmp], rt.cmpReverse(Lopsided.lt$PartialOrd), -1)); + _200 = true; + _142 = new rt.Arc(a); + unwind = 103; + const _143: rt.Arc = new rt.Arc(b); + _200 = false; + const _141: rt.Arc = rt.ordMin(_142, _143, rt.cmpArc(Lopsided.lt$PartialOrd), rt.drop); + unwind = 96; + _200 = false; + const m: [rt.u8, Lopsided.Lopsided] = _132; + n = _141; + rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(m[0], m[1]._0, n.value._0))); + const x: Lopsided.Lopsided = { _0: c ^ d }; + if (Lopsided.le$PartialOrd(a, b)) { + rt.byteVecPush(out, rt.ordClamp(x, a, b, Lopsided.le$PartialOrd, Lopsided.lt$PartialOrd, Lopsided.gt$PartialOrd)._0); + } + unwind = 0; + rt.drop(n); + } + const v: Lopsided.Lopsided[] = rt.iterToArray(rt.iterMap(rt.iterSlice(rt.byteSliceView(data)), (_2: rt.u8): Lopsided.Lopsided => { + return { _0: _2 }; + })); + rt.sliceSort(v, Lopsided.lt$PartialOrd); + rt.iterExtendBytes(out, rt.iterMap(rt.iterSlice(v), (x2: Lopsided.Lopsided): rt.u8 => { + return x2._0; + })); + rt.sliceSortByKey(v, (_23: Lopsided.Lopsided): [rt.u8, Lopsided.Lopsided] => { + const x3: Lopsided.Lopsided = _23; + return [rt.u8Rem(x3._0, 3), x3] as [rt.u8, Lopsided.Lopsided]; + }, rt.testTuple([rt.cmp], rt.cmpReverse(Lopsided.lt$PartialOrd), -1)); + rt.iterExtendBytes(out, rt.iterMap(rt.iterSlice(v), (x4: Lopsided.Lopsided): rt.u8 => { + return x4._0; + })); + const pairs: [rt.u8, Lopsided.Lopsided][] = rt.iterToArray(rt.iterMap(rt.iterSlice(rt.byteSliceView(data)), (_24: rt.u8): [rt.u8, Lopsided.Lopsided] => { + const c4: rt.u8 = _24; + return [rt.u8Rem(c4, 4), { _0: c4 }] as [rt.u8, Lopsided.Lopsided]; + })); + rt.sliceSort(pairs, rt.testTuple([rt.cmp], Lopsided.lt$PartialOrd, -1)); + rt.iterExtendBytes(out, rt.iterMap(rt.iterSlice(pairs), (p: [rt.u8, Lopsided.Lopsided]): rt.u8 => { + return p[1]._0; + })); + if (data.length >= 3) { + const _196: Lopsided.Lopsided = rt.ordClamp({ _0: data.data[data.start] }, { _0: data.data[data.start + 1] }, { _0: data.data[data.start + 2] }, Lopsided.le$PartialOrd, Lopsided.lt$PartialOrd, Lopsided.gt$PartialOrd); + unwind = 0; + rt.byteVecPush(out, _196._0); + } + } catch (e2) { + b4: { + b5: { + if (unwind === 0) { + throw e2; + } + if (unwind === 96) { + rt.drop(n); + break b4; + } + if (unwind === 97) { + drop$u8_Reverse_Arc_Lopsided(_114); + break b5; + } + if (unwind === 98) { + break b5; + } + if (unwind === 99) { + rt.drop(_92); + } else if (unwind !== 100) { + if (!_200) { + break b4; + } + rt.drop(_142); + break b4; + } + rt.drop(_90); + break b4; + } + drop$u8_Reverse_Arc_Lopsided(_108); + } + throw e2; + } +} + /** One of `Seen`'s variants, by `k`. */ export function seen(k: rt.u8, hop_: Hop.Hop | undefined, horizon: Horizon.Horizon, name_: Name.Name, mine: Hop.Hop): Seen.Seen { const _6: rt.u8 = rt.u8Rem(k, 5); @@ -495,9 +693,9 @@ export function options(data: rt.ByteSlice, out: rt.ByteVec): void { const a: Hop.Hop | undefined = _27[0]; const b: Hop.Hop | undefined = _27[1]; rt.byteVecPush(out, (rt.eqOption(Hop.eq$PartialEq)(a, b) ? 1 : 0) | (((!rt.eqOption(Hop.eq$PartialEq)(a, b) ? 1 : 0) << 1) & 0xff) | ((order(rt.cmpOption(Hop.cmp$Ord)(a, b)) << 2) & 0xff)); - const _56: Hop.Hop | undefined = rt.ordMax(a, b, rt.cmpOption(Hop.partialCmp$PartialOrd)); + const _56: Hop.Hop | undefined = rt.ordMax(a, b, rt.cmpLt(rt.cmpOption(Hop.partialCmp$PartialOrd))); const _53: rt.u8 = rt.eqOption(Hop.eq$PartialEq)(_56, b) ? 1 : 0; - const _62: Hop.Hop | undefined = rt.ordMin(a, b, rt.cmpOption(Hop.partialCmp$PartialOrd)); + const _62: Hop.Hop | undefined = rt.ordMin(a, b, rt.cmpLt(rt.cmpOption(Hop.partialCmp$PartialOrd))); rt.byteVecPush(out, _53 | (((rt.eqOption(Hop.eq$PartialEq)(_62, a) ? 1 : 0) << 1) & 0xff)); rt.byteVecPush(out, rt.u64Eq(hashOf$Option_Hop(a), hashOf$Option_Hop(b)) ? 1 : 0); const _75: [rt.u8 | undefined, rt.u8 | undefined] = [small(c), small(d)]; @@ -515,7 +713,7 @@ export function options(data: rt.ByteSlice, out: rt.ByteVec): void { }), map$Option_Hop_u64_u64_options_closure0(b, (h3: Hop.Hop): [rt.u64, rt.u64] => { return [h3._0, c as rt.u64] as [rt.u64, rt.u64]; })]; - const _119: [rt.u64, rt.u64] | undefined = rt.ordMax(pairs[0], pairs[1], rt.cmpOption(rt.cmpTuple([rt.cmp, rt.cmp]))); + const _119: [rt.u64, rt.u64] | undefined = rt.ordMax(pairs[0], pairs[1], rt.cmpLt(rt.cmpOption(rt.cmpTuple([rt.cmp, rt.cmp])))); rt.byteVecPush(out, rt.eqOption(rt.eqTuple([rt.u64Eq, rt.u64Eq]))(_119, pairs[1]) ? 1 : 0); const _125: [Skew.Skew | undefined, Skew.Skew | undefined] = [{ _0: rt.u8Rem(c, 4) }, { _0: rt.u8Rem(d, 4) }]; const p: Skew.Skew | undefined = _125[0]; @@ -646,8 +844,8 @@ export function arcs(data: rt.ByteSlice, out: rt.ByteVec): void { _165 = true; b = _20; rt.byteVecPush(out, order(rt.cmpArc(rt.cmpStr)(a.text, b.text)) | ((order(Name.cmp$Ord(a, b)) << 2) & 0xff)); - const _40: rt.Arc = rt.ordMax(a.text, b.text, rt.cmpArc(rt.cmpStr)); - const _43: rt.Arc = rt.ordMin(a.text, b.text, rt.cmpArc(rt.cmpStr)); + const _40: rt.Arc = rt.ordMax(a.text, b.text, rt.cmpLt(rt.cmpArc(rt.cmpStr))); + const _43: rt.Arc = rt.ordMin(a.text, b.text, rt.cmpLt(rt.cmpArc(rt.cmpStr))); rt.byteVecPush(out, (_40 === b.text ? 1 : 0) | (((_43 === a.text ? 1 : 0) << 1) & 0xff)); rt.byteVecPush(out, (rt.eqArc(rt.same, true)(a.text, b.text) ? 1 : 0) | (((Name.eq$PartialEq(a, b) ? 1 : 0) << 1) & 0xff) | (((rt.eqArc(rt.same, true)(_40, b.text) ? 1 : 0) << 2) & 0xff)); rt.byteVecPush(out, (rt.u64Eq(hashOf$Arc_str(a.text), hashOf$Arc_str(b.text)) ? 1 : 0) | (((rt.u64Eq(hashOf$Name(a), hashOf$Name(b)) ? 1 : 0) << 1) & 0xff)); diff --git a/ts/fixtures/lang/expected/lib.ts b/ts/fixtures/lang/expected/lib.ts index a2c3b3dfe..30531276c 100644 --- a/ts/fixtures/lang/expected/lib.ts +++ b/ts/fixtures/lang/expected/lib.ts @@ -8,6 +8,7 @@ import * as Wrap from "./generics.Wrap.ts"; import type * as Bumped from "./iters.Bumped.ts"; import type * as Hop from "./keys.Hop.ts"; import type * as Horizon from "./keys.Horizon.ts"; +import type * as Lopsided from "./keys.Lopsided.ts"; import type * as Gauge from "./shapes.Gauge.ts"; import type * as Held from "./shapes.Held.ts"; import type * as Lens from "./shapes.Lens.ts"; @@ -196,4 +197,8 @@ export function copy$u8s_u8s(v: [rt.ByteSlice, rt.ByteSlice]): [rt.ByteSlice, rt return [v[0], v[1]]; } +export function drop$u8_Reverse_Arc_Lopsided(_1: [rt.u8, rt.Arc]): void { + rt.drop(_1[1]); +} + //# sourceMappingURL=lib.ts.map diff --git a/ts/fixtures/lang/expected/nested.ts b/ts/fixtures/lang/expected/nested.ts index 48db7d34b..b756d7ae6 100644 --- a/ts/fixtures/lang/expected/nested.ts +++ b/ts/fixtures/lang/expected/nested.ts @@ -152,7 +152,7 @@ export function keys(data: rt.ByteSlice, out: rt.ByteVec): void { const a: rt.u8 | undefined | rt.None = _16; const b: rt.u8 | undefined | rt.None = _15[1]; rt.byteVecPush(out, (a === b ? 1 : 0) | (((a !== b ? 1 : 0) << 1) & 0xff) | (((rt.cmpOption(rt.cmpOption(rt.cmp), rt.None)(a, b) === -1 ? 1 : 0) << 2) & 0xff) | ((order(rt.cmpOption(rt.cmpOption(rt.cmp), rt.None)(a, b)) << 3) & 0xff)); - rt.byteVecPush(out, (rt.ordMax(a, b, rt.cmpOption(rt.cmpOption(rt.cmp), rt.None)) === b ? 1 : 0) | (((rt.u64Eq(hashOf$Option_Option_u8(a), hashOf$Option_Option_u8(b)) ? 1 : 0) << 1) & 0xff)); + rt.byteVecPush(out, (rt.ordMax(a, b, rt.cmpLt(rt.cmpOption(rt.cmpOption(rt.cmp), rt.None))) === b ? 1 : 0) | (((rt.u64Eq(hashOf$Option_Option_u8(a), hashOf$Option_Option_u8(b)) ? 1 : 0) << 1) & 0xff)); rt.idx(data, i); const _75: [rt.u8, rt.u8 | undefined | rt.None] = [rt.u8Rem(data.data[data.start + i], 2), a]; rt.byteVecPush(out, unwrapOr$Option_u8(rt.hashInsert(hashed, _75, i & 0xff), 204)); diff --git a/ts/fixtures/lang/expected/wrappers.ts b/ts/fixtures/lang/expected/wrappers.ts index 094af34ef..a641472f0 100644 --- a/ts/fixtures/lang/expected/wrappers.ts +++ b/ts/fixtures/lang/expected/wrappers.ts @@ -360,7 +360,7 @@ export function wrappers(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void rt.byteVecPush(out, rt.cmpReverse(rt.cmp)(x2, y) & 0xff); rt.byteVecPush(out, rt.cmpReverse(rt.cmp)(x2, y) === -1 ? 1 : 0); rt.byteVecPush(out, rt.cmpReverse(rt.cmpOption(rt.cmp))(x2, undefined) === 1 ? 1 : 0); - rt.byteVecPush(out, rt.ordMax(x2, y, rt.cmpReverse(rt.cmp))); + rt.byteVecPush(out, rt.ordMax(x2, y, rt.cmpLt(rt.cmpReverse(rt.cmp)))); const order = rt.cmpTuple([rt.cmpReverse(rt.cmp), rt.cmp])([x2, y] as [rt.u8, rt.u8], [y, x2] as [rt.u8, rt.u8]); rt.byteVecPush(out, order !== undefined && order <= 0 ? 1 : 0); const _302: rt.Iter<[rt.u8, rt.usize]> = rt.iterMap(rt.iterEnumerate(rt.iterSlice(rt.byteSliceView(data))), (_2: [rt.usize, rt.u8]): [rt.u8, rt.usize] => { @@ -380,7 +380,7 @@ export function wrappers(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void })); rt.sliceSortByKey(ranked, (_25: [rt.u8, rt.usize]): [rt.u8, rt.usize] => { return [rt.u8Rem(_25[0], 4), _25[1]] as [rt.u8, rt.usize]; - }, rt.cmpTuple([rt.cmpReverse(rt.cmp), rt.cmp])); + }, rt.cmpLt(rt.cmpTuple([rt.cmpReverse(rt.cmp), rt.cmp]))); for (const _328 of ranked) { const d: rt.u8 = _328[0]; const i2: rt.usize = _328[1]; diff --git a/ts/fixtures/lang/src/harness.rs b/ts/fixtures/lang/src/harness.rs index 0922af242..c84551946 100644 --- a/ts/fixtures/lang/src/harness.rs +++ b/ts/fixtures/lang/src/harness.rs @@ -82,6 +82,7 @@ fn cases() -> Vec<(&'static str, Case)> { ("utf8::decodes", utf8::decodes), ("floats::floats", floats::floats), ("atomics::atomics", atomics::atomics), + ("keys::overrides", keys::overrides), ] } diff --git a/ts/fixtures/lang/src/keys.rs b/ts/fixtures/lang/src/keys.rs index a0aeb23b8..e4f82af5d 100644 --- a/ts/fixtures/lang/src/keys.rs +++ b/ts/fixtures/lang/src/keys.rs @@ -1,10 +1,10 @@ //! Keys a map compares, orders, and hashes through std's impls: `Option`s, which order //! `None` first, and `Arc`s, which compare as what they hold; lookups through `Borrow`; //! the references to values a map holds that JS copies, changed in place; and the `!=`, -//! `<`, `<=`, `>`, and `>=` a type's `PartialEq` and `PartialOrd` inherit. +//! `<`, `<=`, `>`, and `>=` a type's `PartialEq` and `PartialOrd` inherit or override. use std::borrow::Borrow; -use std::cmp::Ordering; +use std::cmp::{Ordering, Reverse}; use std::collections::{BTreeMap, HashMap, btree_map, hash_map}; use std::hash::{DefaultHasher, Hash, Hasher}; use std::sync::Arc; @@ -74,6 +74,35 @@ impl PartialOrd for Skew { } } +/// An `Ord` by value whose own `<`, `<=`, `>`, and `>=` each disagree with it: `<` and `>` +/// reversed, `<=` by quarter and reversed, and `>=` by the value mod 4, so a test that std +/// calls and the translation computes from `partial_cmp` shows. `<` is still a total +/// order, which sorts need. +#[derive(Clone, Copy, PartialEq, Eq, Ord)] +struct Lopsided(u8); + +impl PartialOrd for Lopsided { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } + + fn lt(&self, other: &Self) -> bool { + self.0 > other.0 + } + + fn le(&self, other: &Self) -> bool { + self.0 / 4 >= other.0 / 4 + } + + fn gt(&self, other: &Self) -> bool { + self.0 < other.0 + } + + fn ge(&self, other: &Self) -> bool { + self.0 % 4 >= other.0 % 4 + } +} + /// A `ne` of its own that disagrees with `eq`, which an `Option`'s `!=` never calls. struct Picky(u8); @@ -313,6 +342,46 @@ fn defaults(data: &[u8], out: &mut Vec) { } } +/// The comparisons std makes through a type's own `<`, `<=`, `>`, and `>=`: directly, as +/// the last field of a tuple, in an `Arc`, and in a `Reverse`, and in `max`, `min`, +/// `clamp`, and sorts. A field before a tuple's last, and an `Option`, compare by +/// `partial_cmp`. The first three bytes clamp last, which panics unless `min <= max`. +pub fn overrides(_: u8, data: &[u8], out: &mut Vec) { + for i in 1..data.len() { + let (c, d) = (data[i - 1], data[i]); + let (a, b) = (Lopsided(c), Lopsided(d)); + out.push(compare(&a, &b)); + out.push(compare(&(c % 2, a), &(d % 2, b)) | ((&(c % 2, &a) < &(d % 2, &b)) as u8) << 5); + out.push(compare(&((c % 2, c % 3), a), &((d % 2, d % 3), b))); + out.push(compare(&(a, c % 2), &(b, d % 2))); + out.push(compare(&Arc::new(a), &Arc::new(b))); + out.push(compare(&Reverse(a), &Reverse(b))); + out.push(compare(&Some(a), &Some(b))); + out.push(compare(&(c % 2, Reverse(Arc::new(a))), &(d % 2, Reverse(Arc::new(b))))); + out.extend([a.max(b).0, a.min(b).0, (&a).max(&b).0]); + let (m, n) = ( + (c % 2, Reverse(a)).max((d % 2, Reverse(b))), + Arc::new(a).min(Arc::new(b)), + ); + out.extend([m.0, m.1.0.0, n.0]); + let x = Lopsided(c ^ d); + if a <= b { + out.push(x.clamp(a, b).0); + } + } + let mut v: Vec = data.iter().map(|&c| Lopsided(c)).collect(); + v.sort(); + out.extend(v.iter().map(|x| x.0)); + v.sort_unstable_by_key(|&x| (x.0 % 3, Reverse(x))); + out.extend(v.iter().map(|x| x.0)); + let mut pairs: Vec<(u8, Lopsided)> = data.iter().map(|&c| (c % 4, Lopsided(c))).collect(); + pairs.sort_unstable(); + out.extend(pairs.iter().map(|p| p.1.0)); + if let [c, d, e, ..] = *data { + out.push(Lopsided(c).clamp(Lopsided(d), Lopsided(e)).0); + } +} + /// One of `Seen`'s variants, by `k`. fn seen<'a>(k: u8, hop: &'a Option, horizon: &'a &'a Horizon, name: &'a Name, mine: &'a mut Hop) -> Seen<'a> { match k % 5 { diff --git a/ts/fixtures/reject/census.md b/ts/fixtures/reject/census.md index f82bc1b30..a5d6361d7 100644 --- a/ts/fixtures/reject/census.md +++ b/ts/fixtures/reject/census.md @@ -1,7 +1,7 @@ # Census of `reject-fixture` -- Refusals: 77 -- Function instances: 84 +- Refusals: 74 +- Function instances: 81 - Generic functions no instance reached, lowered over their type parameters: 6 ## By construct @@ -9,11 +9,10 @@ | Count | Construct | First | | ---: | --- | --- | | 11 | unsupported call to `_` | fixtures/reject/src/lib.rs:28:4 | -| 4 | a variant of `_`, which is not supported: either is the same JS string | fixtures/reject/src/lib.rs:424:2 | +| 4 | a variant of `_`, which is not supported: either is the same JS string | fixtures/reject/src/lib.rs:403:2 | | 4 | an assignment through a reference to `_` | fixtures/reject/src/lib.rs:117:2 | | 4 | unsupported type `_` | fixtures/reject/src/lib.rs:7:14 | | 3 | `_` with a mutable reference to a value JS copies, which its mapping would copy | fixtures/reject/src/lib.rs:17:2 | -| 3 | comparing `_` through its own `_`, which is not supported yet | fixtures/reject/src/lib.rs:381:4 | | 3 | replacing the value behind a reference to `_` | fixtures/reject/src/lib.rs:101:2 | | 2 | `_` is not supported: it would be pending two ways with one value | fixtures/reject/src/lib.rs:121:20 | | 2 | `_` is not supported: its `None` and `Some` would both be `rt.None` | fixtures/reject/src/lib.rs:23:15 | @@ -23,16 +22,16 @@ | 2 | an iterator of the crate implementing `_`, which is not supported yet | fixtures/reject/src/lib.rs:175:1 | | 2 | constructing `_` from another crate | fixtures/reject/src/lib.rs:161:23 | | 2 | overriding `_`, which std's adaptors call in place of `next` or beside it | fixtures/reject/src/lib.rs:155:2 | -| 2 | passing `_`, a closure that owns a value that drops, to `_`, which is not supported yet | fixtures/reject/src/lib.rs:411:13 | +| 2 | passing `_`, a closure that owns a value that drops, to `_`, which is not supported yet | fixtures/reject/src/lib.rs:390:13 | | 1 | `Pointer` formatting, as in `{:p}`, is not supported | fixtures/reject/src/lib.rs:84:2 | -| 1 | `_` is not supported yet | fixtures/reject/src/lib.rs:435:18 | +| 1 | `_` is not supported yet | fixtures/reject/src/lib.rs:414:18 | | 1 | `_` is not supported: its field `_` is `_`, typed `_`, whose `None` would be `rt.None` as `_` | fixtures/reject/src/lib.rs:38:2 | | 1 | `_` of `_` by `_`, which is not supported yet | fixtures/reject/src/lib.rs:307:6 | | 1 | `_` of `_`, which drops `_`, which is not supported yet | fixtures/reject/src/lib.rs:69:13 | | 1 | `_` of `_`, which folds a closure that drops beside what else drops, which is not supported yet | fixtures/reject/src/lib.rs:63:23 | | 1 | `_` through a held type's own, which is not supported yet | fixtures/reject/src/lib.rs:339:2 | | 1 | `_`, which is not supported yet | fixtures/reject/src/lib.rs:315:9 | -| 1 | `f64::mul_add`, which rounds once, where JS would round twice | fixtures/reject/src/lib.rs:498:4 | +| 1 | `f64::mul_add`, which rounds once, where JS would round twice | fixtures/reject/src/lib.rs:477:4 | | 1 | `fold` of `_`, which folds a `flatten` whose source and iterators both drop, which is not supported yet | fixtures/reject/src/lib.rs:237:26 | | 1 | a `zip` of `_` and `_`, which std reads by index, of items that drop, which is not supported yet | fixtures/reject/src/lib.rs:242:30 | | 1 | a `zip` of `_`, whose closures std calls one item further through `next` than through `fold` | fixtures/reject/src/lib.rs:188:51 | @@ -40,17 +39,17 @@ | 1 | a constant of type `_` | fixtures/reject/src/lib.rs:96:2 | | 1 | a copy of a closure that changes what it captured | fixtures/reject/src/lib.rs:278:14 | | 1 | a mutable borrow of a subslice of `_`, which JS would copy, having no views | fixtures/reject/src/lib.rs:247:13 | -| 1 | a mutable reference into a `Result`'s payload, passed on or kept | fixtures/reject/src/lib.rs:467:12 | +| 1 | a mutable reference into a `Result`'s payload, passed on or kept | fixtures/reject/src/lib.rs:446:12 | | 1 | a printable type must implement `Display` or `Debug` | fixtures/reject/src/lib.rs:108:1 | | 1 | a scalar constant of type `_` | fixtures/reject/src/lib.rs:8:2 | | 1 | a zero-sized constant of type `_` | fixtures/reject/src/lib.rs:78:2 | | 1 | arithmetic on `_` | fixtures/reject/src/lib.rs:8:2 | | 1 | collecting `_`, whose `None` is `rt.None`, which is not supported yet | fixtures/reject/src/lib.rs:43:43 | | 1 | comparing `_` as a key, which is not supported yet | fixtures/reject/src/lib.rs:289:52 | -| 1 | constructing `_` | fixtures/reject/src/lib.rs:442:53 | +| 1 | constructing `_` | fixtures/reject/src/lib.rs:421:53 | | 1 | hashing `_` as a key, which is not supported yet | fixtures/reject/src/lib.rs:289:52 | -| 1 | hashing `_` through `_`'s own `Hash::hash_slice`, which is not supported yet | fixtures/reject/src/lib.rs:402:16 | -| 1 | passing `_`, which drops, to `_`, which is not supported yet | fixtures/reject/src/lib.rs:419:4 | +| 1 | hashing `_` through `_`'s own `Hash::hash_slice`, which is not supported yet | fixtures/reject/src/lib.rs:381:16 | +| 1 | passing `_`, which drops, to `_`, which is not supported yet | fixtures/reject/src/lib.rs:398:4 | ## By API @@ -58,30 +57,27 @@ The type or module each refused type or path belongs to. | Count | API | First | | ---: | --- | --- | -| 7 | `std::borrow::Cow` | fixtures/reject/src/lib.rs:424:2 | +| 7 | `std::borrow::Cow` | fixtures/reject/src/lib.rs:403:2 | | 6 | `Option` | fixtures/reject/src/lib.rs:23:15 | | 6 | `f32` | fixtures/reject/src/lib.rs:7:14 | | 4 | `Iterator` | fixtures/reject/src/lib.rs:48:42 | | 4 | `std::sync::Arc` | fixtures/reject/src/lib.rs:117:2 | -| 3 | `PartialOrd` | fixtures/reject/src/lib.rs:381:4 | | 3 | `std::iter::Map` | fixtures/reject/src/lib.rs:63:23 | | 3 | `std::slice` | fixtures/reject/src/lib.rs:28:4 | | 3 | `std::vec::Vec` | fixtures/reject/src/lib.rs:289:52 | | 3 | `u8` | fixtures/reject/src/lib.rs:96:2 | -| 3 | `{closure}` | fixtures/reject/src/lib.rs:411:13 | +| 3 | `{closure}` | fixtures/reject/src/lib.rs:390:13 | | 2 | `HashMap` | fixtures/reject/src/lib.rs:284:6 | -| 2 | `Lopsided` | fixtures/reject/src/lib.rs:381:4 | | 2 | `Meters` | fixtures/reject/src/lib.rs:101:2 | | 2 | `Poll` | fixtures/reject/src/lib.rs:121:20 | | 2 | `std::collections::HashMap` | fixtures/reject/src/lib.rs:69:13 | | 2 | `std::iter::Iterator` | fixtures/reject/src/lib.rs:268:16 | | 2 | `std::ops::Range` | fixtures/reject/src/lib.rs:196:8 | | 2 | `std::pin::Pin` | fixtures/reject/src/lib.rs:132:2 | -| 2 | `std::vec::IntoIter` | fixtures/reject/src/lib.rs:493:17 | -| 1 | `(i32, &Lopsided)` | fixtures/reject/src/lib.rs:382:13 | -| 1 | `Batched` | fixtures/reject/src/lib.rs:402:16 | +| 2 | `std::vec::IntoIter` | fixtures/reject/src/lib.rs:472:17 | +| 1 | `Batched` | fixtures/reject/src/lib.rs:381:16 | | 1 | `DoubleEndedIterator` | fixtures/reject/src/lib.rs:175:1 | -| 1 | `Entry` | fixtures/reject/src/lib.rs:411:13 | +| 1 | `Entry` | fixtures/reject/src/lib.rs:390:13 | | 1 | `FusedIterator` | fixtures/reject/src/lib.rs:229:1 | | 1 | `Hasher` | fixtures/reject/src/lib.rs:315:9 | | 1 | `Maybe` | fixtures/reject/src/lib.rs:38:2 | @@ -90,18 +86,18 @@ The type or module each refused type or path belongs to. | 1 | `[u16; 1]` | fixtures/reject/src/lib.rs:261:22 | | 1 | `count` | fixtures/reject/src/lib.rs:63:23 | | 1 | `dyn std::ops::Fn(u8) -> u8` | fixtures/reject/src/lib.rs:11:16 | -| 1 | `f64` | fixtures/reject/src/lib.rs:503:2 | +| 1 | `f64` | fixtures/reject/src/lib.rs:482:2 | | 1 | `into_keys` | fixtures/reject/src/lib.rs:69:13 | -| 1 | `std::collections::hash_map::Entry` | fixtures/reject/src/lib.rs:457:15 | +| 1 | `std::collections::hash_map::Entry` | fixtures/reject/src/lib.rs:436:15 | | 1 | `std::iter::Chain` | fixtures/reject/src/lib.rs:188:51 | | 1 | `std::iter::Flatten` | fixtures/reject/src/lib.rs:237:26 | | 1 | `std::iter::IntoIterator` | fixtures/reject/src/lib.rs:17:2 | | 1 | `std::iter::Rev` | fixtures/reject/src/lib.rs:182:14 | -| 1 | `std::option::Option` | fixtures/reject/src/lib.rs:474:2 | +| 1 | `std::option::Option` | fixtures/reject/src/lib.rs:453:2 | | 1 | `std::slice::Iter` | fixtures/reject/src/lib.rs:142:11 | | 1 | `std::string::String` | fixtures/reject/src/lib.rs:90:10 | | 1 | `std::sync::MutexGuard` | fixtures/reject/src/lib.rs:137:2 | -| 1 | `str` | fixtures/reject/src/lib.rs:419:4 | +| 1 | `str` | fixtures/reject/src/lib.rs:398:4 | | 1 | `u32` | fixtures/reject/src/lib.rs:247:13 | | 1 | `u64` | fixtures/reject/src/lib.rs:112:2 | | 1 | `value` | fixtures/reject/src/lib.rs:38:2 | @@ -111,16 +107,15 @@ The type or module each refused type or path belongs to. | Count | Construct | Type or path | First | | ---: | --- | --- | --- | -| 4 | a variant of `_`, which is not supported: either is the same JS string | `std::borrow::Cow` | fixtures/reject/src/lib.rs:424:2 | +| 4 | a variant of `_`, which is not supported: either is the same JS string | `std::borrow::Cow` | fixtures/reject/src/lib.rs:403:2 | | 3 | unsupported type `_` | `f32` | fixtures/reject/src/lib.rs:7:14 | | 2 | `_` is not supported: it would be pending two ways with one value | `Poll` | fixtures/reject/src/lib.rs:121:20 | | 2 | `_` is not supported: its `None` and `Some` would both be `rt.None` | `Option` | fixtures/reject/src/lib.rs:23:15 | | 2 | a coercion to `_` | `std::sync::Arc` | fixtures/reject/src/lib.rs:362:2 | | 2 | an assignment through a reference to `_` | `std::pin::Pin` | fixtures/reject/src/lib.rs:132:2 | -| 2 | comparing `_` through its own `_`, which is not supported yet | `Lopsided`, `PartialOrd::lt` | fixtures/reject/src/lib.rs:381:4 | | 2 | replacing the value behind a reference to `_` | `Meters` | fixtures/reject/src/lib.rs:101:2 | -| 2 | unsupported call to `_` | `::sum` | fixtures/reject/src/lib.rs:493:17 | -| 1 | `_` is not supported yet | `std::borrow::Cow` | fixtures/reject/src/lib.rs:435:18 | +| 2 | unsupported call to `_` | `::sum` | fixtures/reject/src/lib.rs:472:17 | +| 1 | `_` is not supported yet | `std::borrow::Cow` | fixtures/reject/src/lib.rs:414:18 | | 1 | `_` is not supported: its field `_` is `_`, typed `_`, whose `None` would be `rt.None` as `_` | `Maybe`, `value`, `Option`, `T \| undefined`, `Option` | fixtures/reject/src/lib.rs:38:2 | | 1 | `_` of `_` by `_`, which is not supported yet | `HashMap::contains_key`, `std::collections::HashMap`, `u8` | fixtures/reject/src/lib.rs:307:6 | | 1 | `_` of `_`, which drops `_`, which is not supported yet | `into_keys`, `std::collections::HashMap`, `Noisy` | fixtures/reject/src/lib.rs:69:13 | @@ -130,9 +125,9 @@ The type or module each refused type or path belongs to. | 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:268: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:268:16 | | 1 | `_` through a held type's own, which is not supported yet | `::ne` | fixtures/reject/src/lib.rs:339:2 | -| 1 | `_` with a mutable reference to a value JS copies, which its mapping would copy | `std::collections::hash_map::Entry::and_modify` | fixtures/reject/src/lib.rs:457:15 | +| 1 | `_` with a mutable reference to a value JS copies, which its mapping would copy | `std::collections::hash_map::Entry::and_modify` | fixtures/reject/src/lib.rs:436:15 | | 1 | `_` with a mutable reference to a value JS copies, which its mapping would copy | `std::iter::IntoIterator::into_iter` | fixtures/reject/src/lib.rs:17:2 | -| 1 | `_` with a mutable reference to a value JS copies, which its mapping would copy | `std::slice::last_mut` | fixtures/reject/src/lib.rs:450:21 | +| 1 | `_` with a mutable reference to a value JS copies, which its mapping would copy | `std::slice::last_mut` | fixtures/reject/src/lib.rs:429:21 | | 1 | `_`, which is not supported yet | `Hasher::write` | fixtures/reject/src/lib.rs:315:9 | | 1 | `fold` of `_`, which folds a `flatten` whose source and iterators both drop, which is not supported yet | `std::iter::Flatten` | fixtures/reject/src/lib.rs:237:26 | | 1 | a `zip` of `_` and `_`, which std reads by index, of items that drop, which is not supported yet | `std::iter::Map`, `std::ops::Range` | fixtures/reject/src/lib.rs:242:30 | @@ -149,24 +144,23 @@ The type or module each refused type or path belongs to. | 1 | arithmetic on `_` | `f32` | fixtures/reject/src/lib.rs:8:2 | | 1 | collecting `_`, whose `None` is `rt.None`, which is not supported yet | `Option` | fixtures/reject/src/lib.rs:43:43 | | 1 | comparing `_` as a key, which is not supported yet | `std::vec::Vec` | fixtures/reject/src/lib.rs:289:52 | -| 1 | comparing `_` through its own `_`, which is not supported yet | `(i32, &Lopsided)`, `PartialOrd::lt` | fixtures/reject/src/lib.rs:382:13 | -| 1 | constructing `_` | `std::borrow::Cow` | fixtures/reject/src/lib.rs:442:53 | +| 1 | constructing `_` | `std::borrow::Cow` | fixtures/reject/src/lib.rs:421:53 | | 1 | constructing `_` from another crate | `std::iter::Map` | fixtures/reject/src/lib.rs:161:23 | | 1 | constructing `_` from another crate | `std::iter::Rev` | fixtures/reject/src/lib.rs:182:14 | | 1 | hashing `_` as a key, which is not supported yet | `std::vec::Vec` | fixtures/reject/src/lib.rs:289:52 | -| 1 | hashing `_` through `_`'s own `Hash::hash_slice`, which is not supported yet | `std::vec::Vec`, `Batched` | fixtures/reject/src/lib.rs:402:16 | +| 1 | hashing `_` through `_`'s own `Hash::hash_slice`, which is not supported yet | `std::vec::Vec`, `Batched` | fixtures/reject/src/lib.rs:381:16 | | 1 | overriding `_`, which std's adaptors call in place of `next` or beside it | `Iterator::fold` | fixtures/reject/src/lib.rs:155:2 | | 1 | overriding `_`, which std's adaptors call in place of `next` or beside it | `Iterator::size_hint` | fixtures/reject/src/lib.rs:209:2 | -| 1 | passing `_`, a closure that owns a value that drops, to `_`, which is not supported yet | `{closure}`, `Entry::or_insert_with` | fixtures/reject/src/lib.rs:411:13 | -| 1 | passing `_`, a closure that owns a value that drops, to `_`, which is not supported yet | `{closure}`, `Iterator::for_each` | fixtures/reject/src/lib.rs:412:16 | -| 1 | passing `_`, which drops, to `_`, which is not supported yet | `{closure}`, `str::contains` | fixtures/reject/src/lib.rs:419:4 | +| 1 | passing `_`, a closure that owns a value that drops, to `_`, which is not supported yet | `{closure}`, `Entry::or_insert_with` | fixtures/reject/src/lib.rs:390:13 | +| 1 | passing `_`, a closure that owns a value that drops, to `_`, which is not supported yet | `{closure}`, `Iterator::for_each` | fixtures/reject/src/lib.rs:391:16 | +| 1 | passing `_`, which drops, to `_`, which is not supported yet | `{closure}`, `str::contains` | fixtures/reject/src/lib.rs:398:4 | | 1 | replacing the value behind a reference to `_` | `[u16; 1]` | fixtures/reject/src/lib.rs:261:22 | -| 1 | unsupported call to `_` | `::fmt` | fixtures/reject/src/lib.rs:503:2 | -| 1 | unsupported call to `_` | `::deref` | fixtures/reject/src/lib.rs:436:2 | -| 1 | unsupported call to `_` | `::eq` | fixtures/reject/src/lib.rs:474:2 | +| 1 | unsupported call to `_` | `::fmt` | fixtures/reject/src/lib.rs:482:2 | +| 1 | unsupported call to `_` | `::deref` | fixtures/reject/src/lib.rs:415:2 | +| 1 | unsupported call to `_` | `::eq` | fixtures/reject/src/lib.rs:453:2 | | 1 | unsupported call to `_` | `::cycle` | fixtures/reject/src/lib.rs:142:11 | | 1 | unsupported call to `_` | `::write_fmt` | fixtures/reject/src/lib.rs:90:10 | -| 1 | unsupported call to `_` | `::hash` | fixtures/reject/src/lib.rs:393:10 | +| 1 | unsupported call to `_` | `::hash` | fixtures/reject/src/lib.rs:372:10 | | 1 | unsupported call to `_` | `std::ops::Range::contains` | fixtures/reject/src/lib.rs:196:8 | | 1 | unsupported call to `_` | `std::slice::copy_within` | fixtures/reject/src/lib.rs:357:4 | | 1 | unsupported call to `_` | `std::slice::sort` | fixtures/reject/src/lib.rs:28:4 | @@ -181,21 +175,21 @@ The type or module each refused type or path belongs to. - fixtures/reject/src/lib.rs:142:11: unsupported call to ` as std::iter::Iterator>::cycle` - fixtures/reject/src/lib.rs:196:8: unsupported call to `std::ops::Range::::contains::` - fixtures/reject/src/lib.rs:357:4: unsupported call to `core::slice::::copy_within::>` -- fixtures/reject/src/lib.rs:393:10: unsupported call to `::hash::` -- fixtures/reject/src/lib.rs:436:2: unsupported call to ` as std::ops::Deref>::deref` -- fixtures/reject/src/lib.rs:474:2: unsupported call to ` as std::cmp::PartialEq>::eq` -- fixtures/reject/src/lib.rs:493:17: unsupported call to ` as std::iter::Iterator>::sum::` -- fixtures/reject/src/lib.rs:493:38: unsupported call to ` as std::iter::Iterator>::sum::` -- fixtures/reject/src/lib.rs:503:2: unsupported call to `::fmt` +- fixtures/reject/src/lib.rs:372:10: unsupported call to `::hash::` +- fixtures/reject/src/lib.rs:415:2: unsupported call to ` as std::ops::Deref>::deref` +- fixtures/reject/src/lib.rs:453:2: unsupported call to ` as std::cmp::PartialEq>::eq` +- fixtures/reject/src/lib.rs:472:17: unsupported call to ` as std::iter::Iterator>::sum::` +- fixtures/reject/src/lib.rs:472:38: unsupported call to ` as std::iter::Iterator>::sum::` +- fixtures/reject/src/lib.rs:482:2: unsupported call to `::fmt`
a variant of `_`, which is not supported: either is the same JS string (4) -- fixtures/reject/src/lib.rs:424:2: a variant of `std::borrow::Cow<'_, str>`, which is not supported: either is the same JS string -- fixtures/reject/src/lib.rs:425:30: a variant of `std::borrow::Cow<'_, str>`, which is not supported: either is the same JS string -- fixtures/reject/src/lib.rs:426:27: a variant of `std::borrow::Cow<'_, str>`, which is not supported: either is the same JS string -- fixtures/reject/src/lib.rs:431:2: a variant of `std::borrow::Cow<'_, str>`, which is not supported: either is the same JS string +- fixtures/reject/src/lib.rs:403:2: a variant of `std::borrow::Cow<'_, str>`, which is not supported: either is the same JS string +- fixtures/reject/src/lib.rs:404:30: a variant of `std::borrow::Cow<'_, str>`, which is not supported: either is the same JS string +- fixtures/reject/src/lib.rs:405:27: a variant of `std::borrow::Cow<'_, str>`, which is not supported: either is the same JS string +- fixtures/reject/src/lib.rs:410:2: a variant of `std::borrow::Cow<'_, str>`, which is not supported: either is the same JS string
@@ -220,16 +214,8 @@ The type or module each refused type or path belongs to.
`_` with a mutable reference to a value JS copies, which its mapping would copy (3) - fixtures/reject/src/lib.rs:17:2: `std::iter::IntoIterator::into_iter` with a mutable reference to a value JS copies, which its mapping would copy -- fixtures/reject/src/lib.rs:450:21: `core::slice::::last_mut` with a mutable reference to a value JS copies, which its mapping would copy -- fixtures/reject/src/lib.rs:457:15: `std::collections::hash_map::Entry::<'a, K, V, A>::and_modify` with a mutable reference to a value JS copies, which its mapping would copy - -
- -
comparing `_` through its own `_`, which is not supported yet (3) - -- fixtures/reject/src/lib.rs:381:4: comparing `Lopsided` through its own `PartialOrd::lt`, which is not supported yet -- fixtures/reject/src/lib.rs:382:13: comparing `(i32, &Lopsided)` through its own `PartialOrd::lt`, which is not supported yet -- fixtures/reject/src/lib.rs:383:12: comparing `Lopsided` through its own `PartialOrd::lt`, which is not supported yet +- fixtures/reject/src/lib.rs:429:21: `core::slice::::last_mut` with a mutable reference to a value JS copies, which its mapping would copy +- fixtures/reject/src/lib.rs:436:15: `std::collections::hash_map::Entry::<'a, K, V, A>::and_modify` with a mutable reference to a value JS copies, which its mapping would copy
@@ -272,7 +258,7 @@ The type or module each refused type or path belongs to.
a coercion to `_` (2) - fixtures/reject/src/lib.rs:362:2: a coercion to `std::sync::Arc<[u8]>` -- fixtures/reject/src/lib.rs:509:2: a coercion to `std::sync::Arc>` +- fixtures/reject/src/lib.rs:488:2: a coercion to `std::sync::Arc>`
@@ -299,8 +285,8 @@ The type or module each refused type or path belongs to.
passing `_`, a closure that owns a value that drops, to `_`, which is not supported yet (2) -- fixtures/reject/src/lib.rs:411:13: passing `{closure@fixtures/reject/src/lib.rs:411:28: 411:35}`, a closure that owns a value that drops, to `Entry::or_insert_with`, which is not supported yet -- fixtures/reject/src/lib.rs:412:16: passing `{closure@fixtures/reject/src/lib.rs:412:25: 412:33}`, a closure that owns a value that drops, to `Iterator::for_each`, which is not supported yet +- fixtures/reject/src/lib.rs:390:13: passing `{closure@fixtures/reject/src/lib.rs:390:28: 390:35}`, a closure that owns a value that drops, to `Entry::or_insert_with`, which is not supported yet +- fixtures/reject/src/lib.rs:391:16: passing `{closure@fixtures/reject/src/lib.rs:391:25: 391:33}`, a closure that owns a value that drops, to `Iterator::for_each`, which is not supported yet
@@ -312,7 +298,7 @@ The type or module each refused type or path belongs to.
`_` is not supported yet (1) -- fixtures/reject/src/lib.rs:435:18: `std::borrow::Cow<'_, [u8]>` is not supported yet +- fixtures/reject/src/lib.rs:414:18: `std::borrow::Cow<'_, [u8]>` is not supported yet
@@ -354,7 +340,7 @@ The type or module each refused type or path belongs to.
`f64::mul_add`, which rounds once, where JS would round twice (1) -- fixtures/reject/src/lib.rs:498:4: `f64::mul_add`, which rounds once, where JS would round twice +- fixtures/reject/src/lib.rs:477:4: `f64::mul_add`, which rounds once, where JS would round twice
@@ -402,7 +388,7 @@ The type or module each refused type or path belongs to.
a mutable reference into a `Result`'s payload, passed on or kept (1) -- fixtures/reject/src/lib.rs:467:12: a mutable reference into a `Result`'s payload, passed on or kept +- fixtures/reject/src/lib.rs:446:12: a mutable reference into a `Result`'s payload, passed on or kept
@@ -444,7 +430,7 @@ The type or module each refused type or path belongs to.
constructing `_` (1) -- fixtures/reject/src/lib.rs:442:53: constructing `std::borrow::Cow<'_, std::sync::Arc>` +- fixtures/reject/src/lib.rs:421:53: constructing `std::borrow::Cow<'_, std::sync::Arc>`
@@ -456,12 +442,12 @@ The type or module each refused type or path belongs to.
hashing `_` through `_`'s own `Hash::hash_slice`, which is not supported yet (1) -- fixtures/reject/src/lib.rs:402:16: hashing `std::vec::Vec` through `Batched`'s own `Hash::hash_slice`, which is not supported yet +- fixtures/reject/src/lib.rs:381:16: hashing `std::vec::Vec` through `Batched`'s own `Hash::hash_slice`, which is not supported yet
passing `_`, which drops, to `_`, which is not supported yet (1) -- fixtures/reject/src/lib.rs:419:4: passing `{closure@fixtures/reject/src/lib.rs:419:13: 419:27}`, which drops, to `str::contains`, which is not supported yet +- fixtures/reject/src/lib.rs:398:4: passing `{closure@fixtures/reject/src/lib.rs:398:13: 398:27}`, which drops, to `str::contains`, which is not supported yet
diff --git a/ts/fixtures/reject/expected.txt b/ts/fixtures/reject/expected.txt index fe102d4f0..3584a2b31 100644 --- a/ts/fixtures/reject/expected.txt +++ b/ts/fixtures/reject/expected.txt @@ -1,4 +1,5 @@ a root must not be generic at fixtures/reject/src/lib.rs:110:1 +no item `lopsided` in the crate no item `missing` in the crate a printable type must implement `Display` or `Debug` at fixtures/reject/src/lib.rs:108:1 unsupported type `f32` at fixtures/reject/src/lib.rs:7:14 @@ -24,7 +25,7 @@ replacing the value behind a reference to `Meters` at fixtures/reject/src/lib.rs an assignment through a reference to `std::sync::Arc` at fixtures/reject/src/lib.rs:117:2 `Poll>>` is not supported: it would be pending two ways with one value at fixtures/reject/src/lib.rs:121:20 `Poll>` is not supported: it would be pending two ways with one value at fixtures/reject/src/lib.rs:126:54 -a coercion to `std::sync::Arc>` at fixtures/reject/src/lib.rs:509:2 +a coercion to `std::sync::Arc>` at fixtures/reject/src/lib.rs:488:2 an assignment through a reference to `std::pin::Pin>` at fixtures/reject/src/lib.rs:132:2 an assignment through a reference to `std::sync::MutexGuard<'_, u8>` at fixtures/reject/src/lib.rs:137:2 unsupported call to ` as std::iter::Iterator>::cycle` at fixtures/reject/src/lib.rs:142:11 @@ -46,28 +47,25 @@ comparing `std::vec::Vec` as a key, which is not supported yet at fixtures/r `HashMap::contains_key` of `std::collections::HashMap` by `u8`, which is not supported yet at fixtures/reject/src/lib.rs:307:6 unsupported call to `core::slice::::copy_within::>` at fixtures/reject/src/lib.rs:357:4 a coercion to `std::sync::Arc<[u8]>` at fixtures/reject/src/lib.rs:362:2 -a variant of `std::borrow::Cow<'_, str>`, which is not supported: either is the same JS string at fixtures/reject/src/lib.rs:424:2 -a variant of `std::borrow::Cow<'_, str>`, which is not supported: either is the same JS string at fixtures/reject/src/lib.rs:426:27 -a variant of `std::borrow::Cow<'_, str>`, which is not supported: either is the same JS string at fixtures/reject/src/lib.rs:425:30 -a variant of `std::borrow::Cow<'_, str>`, which is not supported: either is the same JS string at fixtures/reject/src/lib.rs:431:2 -`std::borrow::Cow<'_, [u8]>` is not supported yet at fixtures/reject/src/lib.rs:435:18 -unsupported call to ` as std::ops::Deref>::deref` at fixtures/reject/src/lib.rs:436:2 -constructing `std::borrow::Cow<'_, std::sync::Arc>` at fixtures/reject/src/lib.rs:442:53 -comparing `Lopsided` through its own `PartialOrd::lt`, which is not supported yet at fixtures/reject/src/lib.rs:381:4 -comparing `(i32, &Lopsided)` through its own `PartialOrd::lt`, which is not supported yet at fixtures/reject/src/lib.rs:382:13 -comparing `Lopsided` through its own `PartialOrd::lt`, which is not supported yet at fixtures/reject/src/lib.rs:383:12 -hashing `std::vec::Vec` through `Batched`'s own `Hash::hash_slice`, which is not supported yet at fixtures/reject/src/lib.rs:402:16 -passing `{closure@fixtures/reject/src/lib.rs:411:28: 411:35}`, a closure that owns a value that drops, to `Entry::or_insert_with`, which is not supported yet at fixtures/reject/src/lib.rs:411:13 -passing `{closure@fixtures/reject/src/lib.rs:412:25: 412:33}`, a closure that owns a value that drops, to `Iterator::for_each`, which is not supported yet at fixtures/reject/src/lib.rs:412:16 -passing `{closure@fixtures/reject/src/lib.rs:419:13: 419:27}`, which drops, to `str::contains`, which is not supported yet at fixtures/reject/src/lib.rs:419:4 -unsupported call to ` as std::iter::Iterator>::sum::` at fixtures/reject/src/lib.rs:493:17 -unsupported call to ` as std::iter::Iterator>::sum::` at fixtures/reject/src/lib.rs:493:38 -`core::slice::::last_mut` with a mutable reference to a value JS copies, which its mapping would copy at fixtures/reject/src/lib.rs:450:21 -`std::collections::hash_map::Entry::<'a, K, V, A>::and_modify` with a mutable reference to a value JS copies, which its mapping would copy at fixtures/reject/src/lib.rs:457:15 -a mutable reference into a `Result`'s payload, passed on or kept at fixtures/reject/src/lib.rs:467:12 -unsupported call to ` as std::cmp::PartialEq>::eq` at fixtures/reject/src/lib.rs:474:2 -`f64::mul_add`, which rounds once, where JS would round twice at fixtures/reject/src/lib.rs:498:4 -unsupported call to `::fmt` at fixtures/reject/src/lib.rs:503:11 +a variant of `std::borrow::Cow<'_, str>`, which is not supported: either is the same JS string at fixtures/reject/src/lib.rs:403:2 +a variant of `std::borrow::Cow<'_, str>`, which is not supported: either is the same JS string at fixtures/reject/src/lib.rs:405:27 +a variant of `std::borrow::Cow<'_, str>`, which is not supported: either is the same JS string at fixtures/reject/src/lib.rs:404:30 +a variant of `std::borrow::Cow<'_, str>`, which is not supported: either is the same JS string at fixtures/reject/src/lib.rs:410:2 +`std::borrow::Cow<'_, [u8]>` is not supported yet at fixtures/reject/src/lib.rs:414:18 +unsupported call to ` as std::ops::Deref>::deref` at fixtures/reject/src/lib.rs:415:2 +constructing `std::borrow::Cow<'_, std::sync::Arc>` at fixtures/reject/src/lib.rs:421:53 +hashing `std::vec::Vec` through `Batched`'s own `Hash::hash_slice`, which is not supported yet at fixtures/reject/src/lib.rs:381:16 +passing `{closure@fixtures/reject/src/lib.rs:390:28: 390:35}`, a closure that owns a value that drops, to `Entry::or_insert_with`, which is not supported yet at fixtures/reject/src/lib.rs:390:13 +passing `{closure@fixtures/reject/src/lib.rs:391:25: 391:33}`, a closure that owns a value that drops, to `Iterator::for_each`, which is not supported yet at fixtures/reject/src/lib.rs:391:16 +passing `{closure@fixtures/reject/src/lib.rs:398:13: 398:27}`, which drops, to `str::contains`, which is not supported yet at fixtures/reject/src/lib.rs:398:4 +unsupported call to ` as std::iter::Iterator>::sum::` at fixtures/reject/src/lib.rs:472:17 +unsupported call to ` as std::iter::Iterator>::sum::` at fixtures/reject/src/lib.rs:472:38 +`core::slice::::last_mut` with a mutable reference to a value JS copies, which its mapping would copy at fixtures/reject/src/lib.rs:429:21 +`std::collections::hash_map::Entry::<'a, K, V, A>::and_modify` with a mutable reference to a value JS copies, which its mapping would copy at fixtures/reject/src/lib.rs:436:15 +a mutable reference into a `Result`'s payload, passed on or kept at fixtures/reject/src/lib.rs:446:12 +unsupported call to ` as std::cmp::PartialEq>::eq` at fixtures/reject/src/lib.rs:453:2 +`f64::mul_add`, which rounds once, where JS would round twice at fixtures/reject/src/lib.rs:477:4 +unsupported call to `::fmt` at fixtures/reject/src/lib.rs:482:11 overriding `Iterator::fold`, which std's adaptors call in place of `next` or beside it at fixtures/reject/src/lib.rs:155:2 an iterator of the crate implementing `DoubleEndedIterator`, which is not supported yet at fixtures/reject/src/lib.rs:175:1 overriding `Iterator::size_hint`, which std's adaptors call in place of `next` or beside it at fixtures/reject/src/lib.rs:209:2 diff --git a/ts/fixtures/reject/src/lib.rs b/ts/fixtures/reject/src/lib.rs index d4f133e17..5cd1fbe8c 100644 --- a/ts/fixtures/reject/src/lib.rs +++ b/ts/fixtures/reject/src/lib.rs @@ -362,27 +362,6 @@ pub fn shared(b: std::sync::Arc<[u8; 2]>) -> std::sync::Arc<[u8]> { b } -/// A `<` of its own, which `sort`, `max`, and a tuple's `<` would call where the -/// translation calls `partial_cmp`. -#[derive(PartialEq, Eq, Ord)] -pub struct Lopsided(u8); - -impl PartialOrd for Lopsided { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.0.cmp(&other.0)) - } - - fn lt(&self, other: &Self) -> bool { - self.0 > other.0 - } -} - -pub fn lopsided(v: &mut Vec, a: Lopsided, b: Lopsided) -> bool { - v.sort(); - let wins = (1, &a) < (2, &b); - wins && a.max(b).0 > 0 -} - /// A `hash_slice` of its own, which a `Vec`'s `Hash` calls where the translation hashes each /// element. #[derive(PartialEq, Eq)] diff --git a/ts/fixtures/task/expected/drops.ts b/ts/fixtures/task/expected/drops.ts index a1db9f3be..18f6f8b96 100644 --- a/ts/fixtures/task/expected/drops.ts +++ b/ts/fixtures/task/expected/drops.ts @@ -2140,7 +2140,7 @@ export function arcs(log: Log.Log, id: rt.u32, at: rt.u8): void { _196 = true; const _78: rt.Arc = rt.arcClone(other); _196 = false; - max = rt.ordMax(_76, _78, rt.cmpArc(rt.cmpStr), rt.drop); + max = rt.ordMax(_76, _78, rt.cmpLt(rt.cmpArc(rt.cmpStr)), rt.drop); unwind = 98; _195 = true; _196 = false; @@ -2149,7 +2149,7 @@ export function arcs(log: Log.Log, id: rt.u32, at: rt.u8): void { _194 = true; const _83: rt.Arc = rt.arcClone(other); _194 = false; - min = rt.ordMin(_81, _83, rt.cmpArc(rt.cmpStr), rt.drop); + min = rt.ordMin(_81, _83, rt.cmpLt(rt.cmpArc(rt.cmpStr)), rt.drop); unwind = 94; _193 = true; _194 = false; @@ -2742,7 +2742,7 @@ export function tripped(log: Log.Log, id: rt.u32, at: rt.u8, tree: boolean): voi _155 = true; const _114: rt.Arc = new rt.Arc(trip(4)); _155 = false; - const _109: rt.Arc = rt.ordMax(_110, _114, rt.cmpArc(Key.partialCmp$PartialOrd), rt.drop); + const _109: rt.Arc = rt.ordMax(_110, _114, rt.cmpLt(rt.cmpArc(Key.partialCmp$PartialOrd)), rt.drop); unwind = 100; _155 = false; rt.drop(_109); @@ -3412,7 +3412,7 @@ export function edited(log: Log.Log, id: rt.u32, at: rt.u8): void { const _78: Key.Key = fault(3, "Blows" as Fault.Fault); _451 = false; _450 = false; - const _71: Key.Key = rt.ordClamp(_72, _75, _78, Key.partialCmp$PartialOrd, rt.drop); + const _71: Key.Key = rt.ordClamp(_72, _75, _78, rt.cmpLe(Key.partialCmp$PartialOrd), rt.cmpLt(Key.partialCmp$PartialOrd), rt.cmpGt(Key.partialCmp$PartialOrd), rt.drop); unwind = 236; _450 = false; _451 = false; @@ -3491,7 +3491,7 @@ export function edited(log: Log.Log, id: rt.u32, at: rt.u8): void { const _194: Key.Key = key(1); _449 = false; _448 = false; - const _187: Key.Key = rt.ordClamp(_188, _191, _194, Key.partialCmp$PartialOrd, rt.drop); + const _187: Key.Key = rt.ordClamp(_188, _191, _194, rt.cmpLe(Key.partialCmp$PartialOrd), rt.cmpLt(Key.partialCmp$PartialOrd), rt.cmpGt(Key.partialCmp$PartialOrd), rt.drop); unwind = 230; _448 = false; _449 = false; @@ -3591,7 +3591,7 @@ export function edited(log: Log.Log, id: rt.u32, at: rt.u8): void { unwind = 252; const _343: Key.Key = key(2); _447 = false; - _339 = rt.ordMax(_340, _343, Key.partialCmp$PartialOrd, rt.drop); + _339 = rt.ordMax(_340, _343, rt.cmpLt(Key.partialCmp$PartialOrd), rt.drop); unwind = 211; _447 = false; const part = rt.u32Sub(_339.id, id); @@ -3605,7 +3605,7 @@ export function edited(log: Log.Log, id: rt.u32, at: rt.u8): void { unwind = 250; const _363: Key.Key = key(4); _446 = false; - _359 = rt.ordMin(_360, _363, Key.partialCmp$PartialOrd, rt.drop); + _359 = rt.ordMin(_360, _363, rt.cmpLt(Key.partialCmp$PartialOrd), rt.drop); unwind = 210; _446 = false; const part2 = rt.u32Sub(_359.id, id); @@ -3623,7 +3623,7 @@ export function edited(log: Log.Log, id: rt.u32, at: rt.u8): void { const _386: Key.Key = key(3); _445 = false; _444 = false; - _379 = rt.ordClamp(_380, _383, _386, Key.partialCmp$PartialOrd, rt.drop); + _379 = rt.ordClamp(_380, _383, _386, rt.cmpLe(Key.partialCmp$PartialOrd), rt.cmpLt(Key.partialCmp$PartialOrd), rt.cmpGt(Key.partialCmp$PartialOrd), rt.drop); unwind = 209; _444 = false; _445 = false; @@ -3642,7 +3642,7 @@ export function edited(log: Log.Log, id: rt.u32, at: rt.u8): void { const _409: Key.Key = key(3); _443 = false; _442 = false; - _402 = rt.ordClamp(_403, _406, _409, Key.partialCmp$PartialOrd, rt.drop); + _402 = rt.ordClamp(_403, _406, _409, rt.cmpLe(Key.partialCmp$PartialOrd), rt.cmpLt(Key.partialCmp$PartialOrd), rt.cmpGt(Key.partialCmp$PartialOrd), rt.drop); unwind = 208; _442 = false; _443 = false; @@ -3657,7 +3657,7 @@ export function edited(log: Log.Log, id: rt.u32, at: rt.u8): void { unwind = 240; const _422: Key.Key = fault(2, "Trips" as Fault.Fault); _441 = false; - const _418: Key.Key = rt.ordMax(_419, _422, Key.partialCmp$PartialOrd, rt.drop); + const _418: Key.Key = rt.ordMax(_419, _422, rt.cmpLt(Key.partialCmp$PartialOrd), rt.drop); unwind = 0; _441 = false; rt.drop(_418); diff --git a/ts/runtime/rt.ts b/ts/runtime/rt.ts index 69dae6e4f..f32eb1c55 100644 --- a/ts/runtime/rt.ts +++ b/ts/runtime/rt.ts @@ -302,27 +302,25 @@ export function sliceSwap(s: T[], a: number, b: number): void { s[b] = x; } -/** - * `sort` and `sort_by`, and the unstable sorts, which may order equal elements either way - * and here keep them in order: stable, by `cmp`, which moves an element before another - * only if it orders `Less`, as std's sorts test `lt`. If `cmp` panics, the slice holds each - * element once, in an order std leaves unspecified, as std's does. - */ -export function sliceSortBy(s: T[], cmp: (a: T, b: T) => Ordering | undefined): void { - sort(s, (a, b) => cmp(a, b) === -1); +/** `sort_by` and `sort_unstable_by`: by `cmp`, whether it orders `Less`, as `sliceSort` sorts. */ +export function sliceSortBy(s: T[], cmp: (a: T, b: T) => Ordering): void { + sliceSort(s, (a, b) => cmp(a, b) === -1); } -/** `sort_by_key` and `sort_unstable_by_key`: by `cmp` of each element's `key`, as `sliceSortBy` sorts. */ -export function sliceSortByKey(s: T[], key: (item: T) => K, cmp: (a: K, b: K) => Ordering | undefined): void { - sort(s, (a, b) => cmp(key(a), key(b)) === -1); +/** `sort_by_key` and `sort_unstable_by_key`: by `lt` of each element's `key`, as `sliceSort` sorts. */ +export function sliceSortByKey(s: T[], key: (item: T) => K, lt: (a: K, b: K) => boolean): void { + sliceSort(s, (a, b) => lt(key(a), key(b))); } /** - * A stable merge sort of `s` by `less`, over runs that insertion sorts first. It calls only - * `less`, never JS's `sort`, which moves `undefined`, a `None`, to the end without - * comparing it, and orders its calls differently in each engine. + * `sort` and `sort_unstable`, by `less`, the elements' `lt`, which std's sorts test: a + * stable merge sort over runs that insertion sorts first. The unstable sorts may order + * equal elements either way and here keep them in order. It calls only `less`, never + * JS's `sort`, which moves `undefined`, a `None`, to the end without comparing it, and + * orders its calls differently in each engine. If `less` panics, the slice holds each + * element once, in an order std leaves unspecified, as std's does. */ -function sort(s: T[], less: (a: T, b: T) => boolean): void { +export function sliceSort(s: T[], less: (a: T, b: T) => boolean): void { const n = s.length; const RUN = 16; for (let lo = 0; lo < n; lo += RUN) { diff --git a/ts/runtime/rt/collections.ts b/ts/runtime/rt/collections.ts index 7301885fd..0a785fb01 100644 --- a/ts/runtime/rt/collections.ts +++ b/ts/runtime/rt/collections.ts @@ -65,6 +65,51 @@ export function cmpTuple( + cmps: readonly ((a: never, b: never) => Ordering | undefined)[], + test: (a: never, b: never) => boolean, + order: Ordering, +): (a: T, b: T) => boolean { + return (a, b) => { + for (let i = 0; i < cmps.length; i++) { + const c = (cmps[i] as (a: unknown, b: unknown) => Ordering | undefined)(a[i], b[i]); + if (c !== 0) return c === order; + } + return (test as (a: unknown, b: unknown) => boolean)(a[cmps.length], b[cmps.length]); + }; +} + +/** `PartialOrd::lt` by `cmp`, a `partial_cmp`, as the trait's default tests it: whether it is `Less`. */ +export function cmpLt(cmp: (a: T, b: T) => Ordering | undefined): (a: T, b: T) => boolean { + return (a, b) => cmp(a, b) === -1; +} + +/** `PartialOrd::le` by `cmp`, a `partial_cmp`: whether it is `Less` or `Equal`. */ +export function cmpLe(cmp: (a: T, b: T) => Ordering | undefined): (a: T, b: T) => boolean { + return (a, b) => { + const c = cmp(a, b); + return c !== undefined && c <= 0; + }; +} + +/** `PartialOrd::gt` by `cmp`, a `partial_cmp`: whether it is `Greater`. */ +export function cmpGt(cmp: (a: T, b: T) => Ordering | undefined): (a: T, b: T) => boolean { + return (a, b) => cmp(a, b) === 1; +} + +/** `PartialOrd::ge` by `cmp`, a `partial_cmp`: whether it is `Greater` or `Equal`. */ +export function cmpGe(cmp: (a: T, b: T) => Ordering | undefined): (a: T, b: T) => boolean { + return (a, b) => { + const c = cmp(a, b); + return c !== undefined && c >= 0; + }; +} + /** `Ord::cmp` of `Option`s, or `PartialOrd::partial_cmp`, from their values': `None` first. */ export function cmpOption(cmp: (a: T, b: T) => O): (a: T | undefined, b: T | undefined) => O; export function cmpOption(cmp: (a: T, b: T) => O, none: None): (a: T | None, b: T | None) => O; @@ -75,8 +120,11 @@ export function cmpOption( return (a, b) => (a === none ? (b === none ? 0 : -1) : b === none ? 1 : cmp(a as T, b as T)) as O; } -/** `Ord::cmp` of `Reverse`s, or `PartialOrd::partial_cmp`: their values', the other way. */ -export function cmpReverse(cmp: (a: T, b: T) => O): (a: T, b: T) => O { +/** + * `Ord::cmp` of `Reverse`s, or `PartialOrd::partial_cmp` or one of its tests: their + * values', the other way. + */ +export function cmpReverse(cmp: (a: T, b: T) => O): (a: T, b: T) => O { return (a, b) => cmp(b, a); } @@ -95,18 +143,18 @@ export function cmpSlice(cmp: (a: T, b: T) => }; } -/** `Ord::cmp` of `Arc`s, or `PartialOrd::partial_cmp`: their values'. */ -export function cmpArc(cmp: (a: T, b: T) => O): (a: Arc, b: Arc) => O { +/** `Ord::cmp` of `Arc`s, or `PartialOrd::partial_cmp` or one of its tests: their values'. */ +export function cmpArc(cmp: (a: T, b: T) => O): (a: Arc, b: Arc) => O { return (a, b) => cmp(a.value, b.value); } /** - * `other < self` of `Ord::max` and `min`, by `cmp`, a `partial_cmp`, which drops `other` and - * then `self` if it panics, as their unwinding does. + * `other < self` of `Ord::max` and `min`, by `lt`, which drops `other` and then `self` if it + * panics, as their unwinding does. */ -function less(self: T, other: T, cmp: (a: T, b: T) => Ordering | undefined, drop?: (value: T) => void): boolean { +function less(self: T, other: T, lt: (a: T, b: T) => boolean, drop?: (value: T) => void): boolean { try { - return cmp(other, self) === -1; + return lt(other, self); } catch (e) { dropPair(other, self, drop, drop); throw e; @@ -114,16 +162,23 @@ function less(self: T, other: T, cmp: (a: T, b: T) => Ordering | undefined, d } /** - * `Ord::clamp`, by `cmp`, a `partial_cmp`: `min` if `a` is less, `max` if greater, or else - * `a`, once `min <= max` is asserted. The operands it does not return drop, the last - * first, as std's do. + * `Ord::clamp`, by `PartialOrd`'s `le`, `lt`, and `gt`: `min` if `a` is less, `max` if + * greater, or else `a`, once `min <= max` is asserted. The operands it does not return + * drop, the last first, as std's do. */ -export function ordClamp(a: T, min: T, max: T, cmp: (a: T, b: T) => Ordering | undefined, drop?: (value: T) => void): T { +export function ordClamp( + a: T, + min: T, + max: T, + le: (a: T, b: T) => boolean, + lt: (a: T, b: T) => boolean, + gt: (a: T, b: T) => boolean, + drop?: (value: T) => void, +): T { let out: 0 | 1 | 2 | undefined; try { - const order = cmp(min, max); - if (order === undefined || order > 0) panic("assertion failed: min <= max"); - out = cmp(a, min) === -1 ? 1 : cmp(a, max) === 1 ? 2 : 0; + if (!le(min, max)) panic("assertion failed: min <= max"); + out = lt(a, min) ? 1 : gt(a, max) ? 2 : 0; } finally { // A panic in one drop still drops the rest, as unwinding does. if (drop !== undefined) dropAll([max, min, a].filter((_, i) => 2 - i !== out), drop); @@ -131,9 +186,9 @@ export function ordClamp(a: T, min: T, max: T, cmp: (a: T, b: T) => Ordering return out === 1 ? min : out === 2 ? max : a; } -/** `Ord::max`, by `cmp`, a `partial_cmp`: `a` if `b < a`, or else `b`, and the other drops. */ -export function ordMax(a: T, b: T, cmp: (a: T, b: T) => Ordering | undefined, drop?: (value: T) => void): T { - if (less(a, b, cmp, drop)) { +/** `Ord::max`, by `lt`: `a` if `b < a`, or else `b`, and the other drops. */ +export function ordMax(a: T, b: T, lt: (a: T, b: T) => boolean, drop?: (value: T) => void): T { + if (less(a, b, lt, drop)) { drop?.(b); return a; } @@ -141,9 +196,9 @@ export function ordMax(a: T, b: T, cmp: (a: T, b: T) => Ordering | undefined, return b; } -/** `Ord::min`, by `cmp`, a `partial_cmp`: `b` if `b < a`, or else `a`, and the other drops. */ -export function ordMin(a: T, b: T, cmp: (a: T, b: T) => Ordering | undefined, drop?: (value: T) => void): T { - if (less(a, b, cmp, drop)) { +/** `Ord::min`, by `lt`: `b` if `b < a`, or else `a`, and the other drops. */ +export function ordMin(a: T, b: T, lt: (a: T, b: T) => boolean, drop?: (value: T) => void): T { + if (less(a, b, lt, drop)) { drop?.(a); return b; } diff --git a/ts/src/lower/collections.rs b/ts/src/lower/collections.rs index 099303415..43b8348fd 100644 --- a/ts/src/lower/collections.rs +++ b/ts/src/lower/collections.rs @@ -58,7 +58,7 @@ pub fn call<'tcx>( return Ok(Some(f.call(args))); } // `Ord`'s defaults, of a std type or the crate's, which compare by `PartialOrd`'s `<`, - // `<=`, and `>`, and so by `partial_cmp` while those are the defaults. + // `<=`, and `>`. ("Ord", "max" | "min" | "clamp", ..) if let Some(t) = self_ty => { let t = peel(b, t); let local = matches!(t.kind(), ty::Adt(adt, _) if b.t.own(adt.did())); @@ -66,11 +66,8 @@ pub fn call<'tcx>( return Err(args); } let tests: &[&str] = if name == "clamp" { &["le", "lt", "gt"] } else { &["lt"] }; - if let Some(test) = tests.iter().find(|test| own_test(b, t, test)) { - return Ok(Some(b.err(span, own_test_err(t, test)))); - } let mut values = args; - values.push(cmp_fn(b, t, true, span)); + values.extend(tests.iter().map(|test| test_fn(b, t, test, span))); // What drops the operands it does not return. let Some(droppers) = b.t.droppers(self_ty, span) else { return Ok(Some(Expr::undefined())); @@ -91,7 +88,7 @@ pub fn call<'tcx>( return Err(args); } if own_test(b, t, name) { - return Ok(Some(b.err(span, own_test_err(t, name)))); + return Ok(Some(test_fn(b, t, name, span).call(args))); } let order = cmp_fn(b, t, true, span).call(args); let test = match name { @@ -529,7 +526,7 @@ fn own_ne<'tcx>(b: &Body<'_, '_, 'tcx>, ty: Ty<'tcx>) -> bool { /// Whether `ty`'s `PartialOrd::{test}`, `lt` or another of its tests, reaches the crate's /// own `test` of a type it holds, which `partial_cmp` stands for only while that is the /// default: through a tuple's last element, `Arc`s, and `Reverse`s. -pub fn own_test<'tcx>(b: &Body<'_, '_, 'tcx>, ty: Ty<'tcx>, test: &str) -> bool { +fn own_test<'tcx>(b: &Body<'_, '_, 'tcx>, ty: Ty<'tcx>, test: &str) -> bool { let tcx = b.t.tcx; let ty = peel(b, ty); match ty.kind() { @@ -546,9 +543,37 @@ pub fn own_test<'tcx>(b: &Body<'_, '_, 'tcx>, ty: Ty<'tcx>, test: &str) -> bool } } -/// The refusal of a comparison that reaches the crate's own `test` of `ty`. -pub fn own_test_err(ty: Ty<'_>, test: &str) -> String { - format!("comparing `{ty}` through its own `PartialOrd::{test}`, which is not supported yet") +/// `PartialOrd::{test}` of `ty` as a function, `lt` or another of its tests: the crate's own +/// where `ty` reaches it, or else by `partial_cmp`, as the trait's defaults test it. +pub fn test_fn<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, test: &str, span: Span) -> Expr { + let tcx = b.t.tcx; + let ty = peel(b, ty); + if !own_test(b, ty, test) { + let by = match test { + "lt" => "cmpLt", + "le" => "cmpLe", + "gt" => "cmpGt", + _ => "cmpGe", + }; + return Expr::rt(by, vec![cmp_fn(b, ty, true, span)]); + } + match ty.kind() { + // The fields before the last chain by `partial_cmp`, which no crate's type overrides. + ty::Tuple(tys) if let Some((&last, init)) = tys.split_last() => { + let cmps = init.iter().map(|&t| cmp_fn(b, t, true, span)).collect(); + let order = if matches!(test, "lt" | "le") { "-1" } else { "1" }; + let last = test_fn(b, last, test, span); + Expr::rt("testTuple", vec![Expr::Array(cmps), last, Expr::lit(order)]) + } + ty::Adt(..) if let Some(inner) = arc_of(b, ty) => Expr::rt("cmpArc", vec![test_fn(b, inner, test, span)]), + ty::Adt(..) if let Some(inner) = reverse_of(b, ty) => { + Expr::rt("cmpReverse", vec![test_fn(b, inner, test, span)]) + } + _ => { + let f = trait_fn(tcx, "PartialOrd", test); + b.fn_value(f, tcx.mk_args(&[ty.into(), ty.into()]), span) + } + } } /// Whether `ty`'s `PartialEq` is derived, so it compares as JS compares its tags. diff --git a/ts/src/lower/library.rs b/ts/src/lower/library.rs index dfd4c18ba..226cdacf9 100644 --- a/ts/src/lower/library.rs +++ b/ts/src/lower/library.rs @@ -1136,21 +1136,16 @@ fn mapping<'tcx>( } } } - // Sorted by `lt` of `Ord`, or by a comparator, or by `lt` of the keys a function makes. + // Sorted by `PartialOrd::lt`, or by a comparator, or by `lt` of the keys a function makes. ( "slice", "sort" | "sort_unstable" | "sort_by" | "sort_unstable_by" | "sort_by_key" | "sort_unstable_by_key", ) if !is_u8(targ(gargs, 0)) => { let mut values = args; match name.as_str() { - // By `<`, which is `partial_cmp`'s while it is the default. "sort" | "sort_unstable" => { - let elem = targ(gargs, 0); - if super::collections::own_test(b, elem, "lt") { - return Some(b.err(span, super::collections::own_test_err(elem, "lt"))); - } - values.push(super::collections::cmp_fn(b, elem, true, span)); - Expr::rt("sliceSortBy", values) + values.push(super::collections::test_fn(b, targ(gargs, 0), "lt", span)); + Expr::rt("sliceSort", values) } "sort_by" | "sort_unstable_by" => Expr::rt("sliceSortBy", values), _ => { @@ -1161,10 +1156,7 @@ fn mapping<'tcx>( format!("`slice::{name}` by `{key}`, which drops, which is not supported yet"), )); } - if super::collections::own_test(b, key, "lt") { - return Some(b.err(span, super::collections::own_test_err(key, "lt"))); - } - values.push(super::collections::cmp_fn(b, key, true, span)); + values.push(super::collections::test_fn(b, key, "lt", span)); Expr::rt("sliceSortByKey", values) } }