diff --git a/quest/m3/README.md b/quest/m3/README.md index a5cca2bcd..64af53a64 100644 --- a/quest/m3/README.md +++ b/quest/m3/README.md @@ -20,6 +20,5 @@ 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 - [An inclusive range folded through an adaptor](/quest/m3/ts-range-fold.md) - a `RangeInclusive` folded through `map`, `filter`, or `rev` ends in core's state -- [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 - [Drop order in std's folds and iterators](/quest/m3/ts-drop-order.md) - the values that drop still refused in std's folds, zips, flattens, and collects drop as std's do diff --git a/quest/m3/ts-bool-core.md b/quest/m3/ts-bool-core.md deleted file mode 100644 index 31deeec14..000000000 --- a/quest/m3/ts-bool-core.md +++ /dev/null @@ -1,17 +0,0 @@ -# [XS] bool's combinators from core's MIR - -## Goal - -rust2-ts lowers `bool::then` and `then_some` from core's MIR, as -[#129](https://github.com/kixelated/rust2/pull/129) does `Option`'s and `Result`'s, and -deletes `rt.boolThen` and its dropper, with drops exact, proven by differential tests -against native Rust and the codec's bundle no larger. - -## Plan - -Found by [#115](https://github.com/kixelated/rust2/pull/115) on 2026-10-27: `rt.boolThen` -is the last runtime combinator that takes a closure dropper, and `rt.once` may go with it. -Decided with the maintainer on 2026-10-27 to plan it, since it deletes runtime code; -integer methods stay in the runtime, as core-mir decided. - -Public API: the translator's output and the runtime. diff --git a/ts/MIGRATE.md b/ts/MIGRATE.md index 19b33fc93..3dbea9f58 100644 --- a/ts/MIGRATE.md +++ b/ts/MIGRATE.md @@ -25,6 +25,8 @@ `ownedStepBy`: `iterNth`, `iterNthBack`, `iterSkip`, `iterTake`, and `iterStepBy` take what drops an item last. `rt.Iter`'s `nthItem` and `nthBackItem` are `nth` and `nthBack`, which return `rt.DONE` past the end. +- `bool::then` and `then_some` lower from core's MIR into `core/bool.ts`, so the runtime + no longer exports `boolThen` or `thenSome`. - Translation builds std from source for `wasm32-unknown-unknown`, so the toolchain needs its `rust-src` component, as the flake's has, and the first run compiles std. - `Option`'s and `Result`'s combinators lower from core's MIR into `core/option.ts` and diff --git a/ts/README.md b/ts/README.md index 523149f39..43df151eb 100644 --- a/ts/README.md +++ b/ts/README.md @@ -222,11 +222,11 @@ elaboration put it. A panic unwinds: a function with cleanup to run catches `rt. runs the cleanup blocks MIR has under `-C panic=unwind`, and throws it on, so what a panic drops matches native Rust's. A value without `[Symbol.dispose]` of its own, such as an enum or a tuple, drops through glue lowered from rustc's `drop_in_place` shim, a -function of its type's own (`drop$Held`). `Option`'s, `Result`'s, and `Poll`'s methods lower -from core's MIR, so they drop what core's do, where core's do. The runtime functions for -std's collections, `Arc`, and the std functions that may discard an operand -(`then_some`, a map's `insert`) take what drops each value they may drop, and drop it -where std's do. So do iterators: one that owns its items, as a `Vec`'s `into_iter` or a +function of its type's own (`drop$Held`). `bool`'s, `Option`'s, `Result`'s, and `Poll`'s +methods lower from core's MIR, so they drop what core's do, where core's do. The runtime +functions for std's collections, `Arc`, and the std functions that may discard an operand +(`OnceCell::get_or_init`, a map's `insert`) take what drops each value they may drop, and +drop it where std's do. So do iterators: one that owns its items, as a `Vec`'s `into_iter` or a map's `drain` does, drops those it still holds by `[Symbol.dispose]`, an adaptor drops what it skips or rejects, and a consumer that takes an iterator by value drops it, a panic included: through each `rt.Iter`'s own `fold` where std's folds, which drops each @@ -490,8 +490,8 @@ a pointer's address (`{:p}`), writing to a what it captured, `mem::swap`, `replace`, or `take` of a reference's whole pointee, assigning an `Arc`, a waker, a `MutexGuard`, a pinned reference, or a future through a reference, a switch on or a field of another crate's type without a mapping, and std -functions without a mapping in `src/lower/library.rs`, but for `Option`'s and `Result`'s -methods that take no `&mut`, which lower from core's MIR. +functions without a mapping in `src/lower/library.rs`, but for `bool`'s, `Option`'s, +`Result`'s, and `Poll`'s methods that take no `&mut`, which lower from core's MIR. ## Tests diff --git a/ts/fixtures/codec/expected/core/bool.ts b/ts/fixtures/codec/expected/core/bool.ts new file mode 100644 index 000000000..9dc9784b6 --- /dev/null +++ b/ts/fixtures/codec/expected/core/bool.ts @@ -0,0 +1,12 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "../rt.ts"; + +export function thenSome$bool_u64(self: boolean, t: rt.u64): rt.u64 | undefined { + if (!self) { + return undefined; + } + return t; +} + +//# sourceMappingURL=bool.ts.map diff --git a/ts/fixtures/codec/expected/harness.ts b/ts/fixtures/codec/expected/harness.ts index 8694c6cb2..d68c7b067 100644 --- a/ts/fixtures/codec/expected/harness.ts +++ b/ts/fixtures/codec/expected/harness.ts @@ -10,6 +10,7 @@ import { size, zigzag as varint_zigzag, unzigzag } from "./coding/varint.ts"; import * as BoundsExceeded from "./coding/varint.BoundsExceeded.ts"; import * as Form from "./coding/varint.Form.ts"; import * as Versions from "./coding/version.Versions.ts"; +import { thenSome$bool_u64 } from "./core/bool.ts"; import type * as SessionError from "./lib.SessionError.ts"; import * as Group from "./lite/group.Group.ts"; import * as Probe from "./lite/probe.Probe.ts"; @@ -492,7 +493,7 @@ export function encodeMessages(data: rt.ByteSlice, out: rt.ByteVec): void { const bitrate: rt.u64 = _19[0]; const rtt: rt.u64 = takeU64(_19[1])[0]; encodeMessage$Group({ subscribe, sequence, frameStart: rt.u64Rem(frameStart, 4) }, version, out); - encodeMessage$Probe({ bitrate: rt.u64CheckedSub(bitrate, 1), rtt: rt.thenSome(rt.u64Rem(rtt, 3) !== 0, rtt) }, version, out); + encodeMessage$Probe({ bitrate: rt.u64CheckedSub(bitrate, 1), rtt: thenSome$bool_u64(rt.u64Rem(rtt, 3) !== 0, rtt) }, version, out); const versions: Versions.Versions = Versions.from$From_Versions([{ _0: subscribe }, { _0: sequence }]); const buf: rt.ByteVec = new rt.ByteVec(); const _45: Form.Form = Form.from$From_Version(version); diff --git a/ts/fixtures/lang/expected/atomics.ts b/ts/fixtures/lang/expected/atomics.ts index bd94dc5a0..e283527c6 100644 --- a/ts/fixtures/lang/expected/atomics.ts +++ b/ts/fixtures/lang/expected/atomics.ts @@ -2,6 +2,7 @@ import * as rt from "./rt.ts"; import * as Stats from "./atomics.Stats.ts"; +import { thenSome$bool_i16, thenSome$bool_i32, thenSome$bool_i64, thenSome$bool_i8, thenSome$bool_isize, thenSome$bool_u16, thenSome$bool_u32$2, thenSome$bool_u64, thenSome$bool_u8, thenSome$bool_usize } from "./core/bool.ts"; import { isSomeAnd$Option_u8_bools_closure0 } from "./core/option.ts"; import { unwrapOr$Result_i16_i16, unwrapOr$Result_i32_i32, unwrapOr$Result_i64_i64, unwrapOr$Result_i8_i8, unwrapOr$Result_isize_isize, unwrapOr$Result_u16_u16, unwrapOr$Result_u32_u32, unwrapOr$Result_u64_u64, unwrapOr$Result_u8_u8, unwrapOr$Result_usize_usize } from "./core/result.ts"; @@ -120,7 +121,7 @@ export function i16s(data: rt.ByteSlice, out: rt.ByteVec): void { }); } else if (_60 === 14) { old = unwrapOr$Result_i16_i16(rt.atomicTryUpdate(atomic, any, failure, (x4: rt.i16): rt.i16 | undefined => { - return rt.thenSome(x4 !== v, v); + return thenSome$bool_i16(x4 !== v, v); }), v); } else { const _116: rt.Place = atomic; @@ -233,7 +234,7 @@ export function i32s(data: rt.ByteSlice, out: rt.ByteVec): void { }); } else if (_60 === 14) { old = unwrapOr$Result_i32_i32(rt.atomicTryUpdate(atomic, any, failure, (x4: rt.i32): rt.i32 | undefined => { - return rt.thenSome(x4 !== v, v); + return thenSome$bool_i32(x4 !== v, v); }), v); } else { const _116: rt.Place = atomic; @@ -346,7 +347,7 @@ export function i64s(data: rt.ByteSlice, out: rt.ByteVec): void { }, rt.i64Eq); } else if (_60 === 14) { old = unwrapOr$Result_i64_i64(rt.atomicTryUpdate(atomic, any, failure, (x4: rt.i64): rt.i64 | undefined => { - return rt.thenSome(rt.i64Ne(x4, v), v); + return thenSome$bool_i64(rt.i64Ne(x4, v), v); }, rt.i64Eq), v); } else { const _116: rt.Place = atomic; @@ -459,7 +460,7 @@ export function i8s(data: rt.ByteSlice, out: rt.ByteVec): void { }); } else if (_60 === 14) { old = unwrapOr$Result_i8_i8(rt.atomicTryUpdate(atomic, any, failure, (x4: rt.i8): rt.i8 | undefined => { - return rt.thenSome(x4 !== v, v); + return thenSome$bool_i8(x4 !== v, v); }), v); } else { const _116: rt.Place = atomic; @@ -572,7 +573,7 @@ export function isizes(data: rt.ByteSlice, out: rt.ByteVec): void { }); } else if (_60 === 14) { old = unwrapOr$Result_isize_isize(rt.atomicTryUpdate(atomic, any, failure, (x4: rt.isize): rt.isize | undefined => { - return rt.thenSome(x4 !== v, v); + return thenSome$bool_isize(x4 !== v, v); }), v); } else { const _116: rt.Place = atomic; @@ -685,7 +686,7 @@ export function u16s(data: rt.ByteSlice, out: rt.ByteVec): void { }); } else if (_60 === 14) { old = unwrapOr$Result_u16_u16(rt.atomicTryUpdate(atomic, any, failure, (x4: rt.u16): rt.u16 | undefined => { - return rt.thenSome(x4 !== v, v); + return thenSome$bool_u16(x4 !== v, v); }), v); } else { const _116: rt.Place = atomic; @@ -798,7 +799,7 @@ export function u32s(data: rt.ByteSlice, out: rt.ByteVec): void { }); } else if (_60 === 14) { old = unwrapOr$Result_u32_u32(rt.atomicTryUpdate(atomic, any, failure, (x4: rt.u32): rt.u32 | undefined => { - return rt.thenSome(x4 !== v, v); + return thenSome$bool_u32$2(x4 !== v, v); }), v); } else { const _116: rt.Place = atomic; @@ -911,7 +912,7 @@ export function u64s(data: rt.ByteSlice, out: rt.ByteVec): void { }, rt.u64Eq); } else if (_60 === 14) { old = unwrapOr$Result_u64_u64(rt.atomicTryUpdate(atomic, any, failure, (x4: rt.u64): rt.u64 | undefined => { - return rt.thenSome(rt.u64Ne(x4, v), v); + return thenSome$bool_u64(rt.u64Ne(x4, v), v); }, rt.u64Eq), v); } else { const _116: rt.Place = atomic; @@ -1024,7 +1025,7 @@ export function u8s(data: rt.ByteSlice, out: rt.ByteVec): void { }); } else if (_60 === 14) { old = unwrapOr$Result_u8_u8(rt.atomicTryUpdate(atomic, any, failure, (x4: rt.u8): rt.u8 | undefined => { - return rt.thenSome(x4 !== v, v); + return thenSome$bool_u8(x4 !== v, v); }), v); } else { const _116: rt.Place = atomic; @@ -1137,7 +1138,7 @@ export function usizes(data: rt.ByteSlice, out: rt.ByteVec): void { }); } else if (_60 === 14) { old = unwrapOr$Result_usize_usize(rt.atomicTryUpdate(atomic, any, failure, (x4: rt.usize): rt.usize | undefined => { - return rt.thenSome(x4 !== v, v); + return thenSome$bool_usize(x4 !== v, v); }), v); } else { const _116: rt.Place = atomic; diff --git a/ts/fixtures/lang/expected/cells.ts b/ts/fixtures/lang/expected/cells.ts index 84780d9fa..9a9251624 100644 --- a/ts/fixtures/lang/expected/cells.ts +++ b/ts/fixtures/lang/expected/cells.ts @@ -1,6 +1,7 @@ // Generated by rust2-ts. Do not edit. import * as rt from "./rt.ts"; +import { thenSome$bool_u8 } from "./core/bool.ts"; /** Steps of an operation and an operand on one cell, each putting what it read. */ export function cellOption(data: rt.ByteSlice, out: rt.ByteVec): void { @@ -1017,7 +1018,7 @@ export function wide(v: rt.u8): rt.u64 { /** `None` for every third byte. */ export function maybe(v: rt.u8): rt.u8 | undefined { - return rt.thenSome(rt.u8Rem(v, 3) !== 0, v); + return thenSome$bool_u8(rt.u8Rem(v, 3) !== 0, v); } export function putMaybe(out: rt.ByteVec, t: rt.u8 | undefined): void { diff --git a/ts/fixtures/lang/expected/core/bool.ts b/ts/fixtures/lang/expected/core/bool.ts new file mode 100644 index 000000000..c5f3e4db0 --- /dev/null +++ b/ts/fixtures/lang/expected/core/bool.ts @@ -0,0 +1,124 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "../rt.ts"; + +export function thenSome$bool_Duration(self: boolean, t: rt.Duration): rt.Duration | undefined { + if (!self) { + return undefined; + } + return t; +} + +export function thenSome$bool_i16(self: boolean, t: rt.i16): rt.i16 | undefined { + if (!self) { + return undefined; + } + return t; +} + +export function thenSome$bool_i32(self: boolean, t: rt.i32): rt.i32 | undefined { + if (!self) { + return undefined; + } + return t; +} + +export function thenSome$bool_i64(self: boolean, t: rt.i64): rt.i64 | undefined { + if (!self) { + return undefined; + } + return t; +} + +export function thenSome$bool_i8(self: boolean, t: rt.i8): rt.i8 | undefined { + if (!self) { + return undefined; + } + return t; +} + +export function thenSome$bool_isize(self: boolean, t: rt.isize): rt.isize | undefined { + if (!self) { + return undefined; + } + return t; +} + +export function thenSome$bool_u16(self: boolean, t: rt.u16): rt.u16 | undefined { + if (!self) { + return undefined; + } + return t; +} + +export function thenSome$bool_u32(self: boolean, t: rt.Place): rt.Place | undefined { + if (!self) { + return undefined; + } + return t; +} + +export function thenSome$bool_u32$2(self: boolean, t: rt.u32): rt.u32 | undefined { + if (!self) { + return undefined; + } + return t; +} + +export function thenSome$bool_u64(self: boolean, t: rt.u64): rt.u64 | undefined { + if (!self) { + return undefined; + } + return t; +} + +export function thenSome$bool_u8(self: boolean, t: rt.u8): rt.u8 | undefined { + if (!self) { + return undefined; + } + return t; +} + +export function thenSome$bool_unit(self: boolean, _: undefined): undefined | rt.None { + if (!self) { + return rt.None; + } + return undefined; +} + +export function thenSome$bool_usize(self: boolean, t: rt.usize): rt.usize | undefined { + if (!self) { + return undefined; + } + return t; +} + +export function then$bool_Option_u8_iters_closure2(self: boolean, f: () => rt.u8 | undefined): rt.u8 | undefined | rt.None { + if (!self) { + return rt.None; + } + return f(); +} + +export function then$bool_Vec_u8_new(self: boolean, _: () => rt.ByteVec): rt.ByteVec | undefined { + if (!self) { + return undefined; + } + return new rt.ByteVec(); +} + +export function then$bool_str_slices_closure1_closure0(self: boolean, f: () => string): string | undefined { + if (!self) { + return undefined; + } + return f(); +} + +export function then$bool_u8_library_closure4(self: boolean, f: () => rt.u8): rt.u8 | undefined { + if (!self) { + return undefined; + } + return f(); +} + +//# sourceMappingURL=bool.ts.map diff --git a/ts/fixtures/lang/expected/durations.ts b/ts/fixtures/lang/expected/durations.ts index 73adaa985..a82959594 100644 --- a/ts/fixtures/lang/expected/durations.ts +++ b/ts/fixtures/lang/expected/durations.ts @@ -2,6 +2,7 @@ import * as rt from "./rt.ts"; import { copy$Window } from "./lib.ts"; +import { thenSome$bool_Duration } from "./core/bool.ts"; import type * as Twice from "./durations.Twice.ts"; import * as Window from "./durations.Window.ts"; import { put } from "./fmt.ts"; @@ -225,7 +226,7 @@ export function durations(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): voi putDuration(out, { secs: 1, nanos: 500000000 }); putDuration(out, { secs: 0, nanos: 0 }); putOpt(out, rt.durationCheckedAdd({ secs: { hi: 0xffffffff, lo: 0xffffffff } as rt.u64, nanos: 999999999 }, rt.durationFromNanos(1))); - const _379: rt.Duration | undefined = rt.thenSome(rt.durationCmp(b, { secs: 1, nanos: 500000000 }) === 1, b); + const _379: rt.Duration | undefined = thenSome$bool_Duration(rt.durationCmp(b, { secs: 1, nanos: 500000000 }) === 1, b); const w2: Window.Window = { start: a, len: _379 }; const _388: Window.Window = Window.default$Default(); rt.byteVecPush(out, Window.eq$PartialEq(w2, _388) ? 1 : 0); diff --git a/ts/fixtures/lang/expected/iters.Halving.ts b/ts/fixtures/lang/expected/iters.Halving.ts index 1d2d62e4c..b4170bd1b 100644 --- a/ts/fixtures/lang/expected/iters.Halving.ts +++ b/ts/fixtures/lang/expected/iters.Halving.ts @@ -1,6 +1,7 @@ // Generated by rust2-ts. Do not edit. import * as rt from "./rt.ts"; +import { thenSome$bool_u8 } from "./core/bool.ts"; /** A value halved until it is zero: generic, with an `impl Iterator` per instance. */ export class Halving { @@ -14,7 +15,7 @@ export class Halving { export function next$Iterator_u8(self: Halving): rt.u8 | rt.Done { const value: rt.u8 = self.value; self.value = rt.u8Div(self.value, 2); - return rt.iterFromOption(rt.thenSome(value !== 0, value)); + return rt.iterFromOption(thenSome$bool_u8(value !== 0, value)); } export function next$Iterator_u64(self: Halving): rt.u64 | rt.Done { diff --git a/ts/fixtures/lang/expected/iters.ts b/ts/fixtures/lang/expected/iters.ts index 27740c7ed..29ed41ba5 100644 --- a/ts/fixtures/lang/expected/iters.ts +++ b/ts/fixtures/lang/expected/iters.ts @@ -2,6 +2,7 @@ import * as rt from "./rt.ts"; import { copy$Bumped, copy$u8_u8 } from "./lib.ts"; +import { thenSome$bool_u16, thenSome$bool_u8 } from "./core/bool.ts"; import { unwrapOr$Option_NonZero_u64, unwrapOr$Option_i64, unwrapOr$Option_u8, unwrapOr$Option_usize_u8, map$Option_u8_u8_elements_closure8, mapOr$Option_Vec_u16_u8_consumers_closure23, mapOr$Option_u8_u8_elements_closure3, mapOr$Option_u8_u8_u8_btree_maps_closure0, mapOr$Option_u8_usize_iters_closure1, mapOr$Option_u8s_u8_skips_closure44, mapOr$Option_usize_u8_consumers_closure8, mapOr$Option_usize_u8_u8_ranges_closure0 } from "./core/option.ts"; import * as Bumped from "./iters.Bumped.ts"; import * as Countdown from "./iters.Countdown.ts"; @@ -25,7 +26,7 @@ export function adaptors(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void })))); for (const _18 of rt.iterToArray(rt.iterFilterMap(rt.iterSlice(rt.byteSliceView(data)), (_218: rt.u8): rt.u16 | undefined => { const b12: rt.u8 = _218; - return rt.thenSome(b12 < 100, rt.u16Mul(b12 as rt.u16, 200)); + return thenSome$bool_u16(b12 < 100, rt.u16Mul(b12 as rt.u16, 200)); }))) { rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u16ToBeBytes(_18))); } @@ -298,7 +299,7 @@ export function consumers(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): voi rt.byteVecPush(log, _24); }), (_29: rt.u8): rt.u16 | undefined => { const b5: rt.u8 = _29; - return rt.thenSome(b5 !== 32, b5 as rt.u16); + return thenSome$bool_u16(b5 !== 32, b5 as rt.u16); }), (values__: rt.Iter) => rt.iterToArray(values__)), 255, (v2: rt.u16[]): rt.u8 => { return v2.length & 0xff; })); @@ -924,13 +925,13 @@ export function options(data: rt.ByteSlice, out: rt.ByteVec): void { let n: rt.u8 = 0; rt.byteVecExtendFromSlice(out, rt.byteVecSlice(rt.iterToByteVec(rt.iterFromFn((): rt.u8 | undefined => { n = rt.u8Add(n, 1); - return rt.thenSome(n < 4, n); + return thenSome$bool_u8(n < 4, n); })))); let k: rt.u8 = 0; rt.iterEmpty(); const _70: rt.Iter = rt.iterFromFn((): rt.u8 | undefined => { k = rt.u8Add(k, 1); - return rt.thenSome(k !== 2, k); + return thenSome$bool_u8(k !== 2, k); }); const gappy: rt.Iter = rt.iterChain(rt.iterEmpty(), _70); rt.byteVecPush(out, rt.iterCount(rt.iterBorrow(gappy)) & 0xff); diff --git a/ts/fixtures/lang/expected/nested.ts b/ts/fixtures/lang/expected/nested.ts index 3bf8ce4ca..2832a157a 100644 --- a/ts/fixtures/lang/expected/nested.ts +++ b/ts/fixtures/lang/expected/nested.ts @@ -1,6 +1,7 @@ // Generated by rust2-ts. Do not edit. import * as rt from "./rt.ts"; +import { thenSome$bool_u8, thenSome$bool_unit, then$bool_Option_u8_iters_closure2 } from "./core/bool.ts"; import { unwrapOr$Option_u8, map$Option_Option_u8_Option_u8_made_closure0, map$Option_u8_Option_u8_made_closure1_closure0, map$Option_u8_u8_elements_closure8, map$Option_unit_Option_u8_made_closure3, mapOr$Option_u8_u8_elements_closure3, mapOr$Option_u8_usize_iters_closure1, mapOr$Option_unit_u8_made_closure4, andThen$Option_Option_u8_Option_u8_made_closure1, filter$Option_Option_u8_made_closure2, or$Option_Option_u8, xor$Option_Option_u8, flatten$Option_Option_u8 } from "./core/option.ts"; import * as Halves from "./nested.Halves.ts"; import type * as Slot from "./nested.Slot.ts"; @@ -37,7 +38,7 @@ export function pick(b: rt.u8): rt.u8 | undefined | rt.None { /** `Some(())` for an even byte. */ export function unit(b: rt.u8): undefined | rt.None { - return rt.thenSome(rt.u8Rem(b, 2) === 0, undefined, undefined, rt.None); + return thenSome$bool_unit(rt.u8Rem(b, 2) === 0, undefined); } /** Which of the three `o` is, and its byte. */ @@ -177,22 +178,22 @@ export function keys(data: rt.ByteSlice, out: rt.ByteVec): void { } export function halve(n: rt.u8): rt.u8 | undefined | rt.None { - return rt.thenSome(rt.u8Rem(n, 2) === 0, rt.u8Div(n, 2)); + return thenSome$bool_u8(rt.u8Rem(n, 2) === 0, rt.u8Div(n, 2)); } export function iters(data: rt.ByteSlice, out: rt.ByteVec): void { const items: Array = rt.iterToArray(rt.iterMap(rt.iterSlice(rt.byteSliceView(data)), (_2: rt.u8): rt.u8 | undefined => { const b: rt.u8 = _2; - return rt.thenSome(rt.u8Rem(b, 2) === 0, b); + return thenSome$bool_u8(rt.u8Rem(b, 2) === 0, b); })); const at: rt.usize = mapOr$Option_u8_usize_iters_closure1(rt.byteSliceFirst(data), 0, (b2: rt.u8): rt.usize => { return rt.usizeRem(b2 as rt.usize, 4); }); rt.byteVecPush(out, code(rt.sliceGet(items, at, rt.None))); rt.byteVecPush(out, code(rt.sliceFirst(items, rt.None))); - rt.byteVecPush(out, code(rt.boolThen(at > 1, (): rt.u8 | undefined => { + rt.byteVecPush(out, code(then$bool_Option_u8_iters_closure2(at > 1, (): rt.u8 | undefined => { return flatten$Option_Option_u8(rt.sliceGet(items, at, rt.None)); - }, undefined, rt.None))); + }))); const it: rt.Iter = rt.iterSlice(items); rt.byteVecPush(out, code(rt.iterToOption(it.next(), rt.None))); rt.byteVecPush(out, code(rt.iterToOption(it.nextBack(), rt.None))); diff --git a/ts/fixtures/lang/expected/refs.ts b/ts/fixtures/lang/expected/refs.ts index e5d6c9a60..8129ca14f 100644 --- a/ts/fixtures/lang/expected/refs.ts +++ b/ts/fixtures/lang/expected/refs.ts @@ -2,6 +2,7 @@ import * as rt from "./rt.ts"; import { copy$Option_u8_u8, copy$u32s } from "./lib.ts"; +import { thenSome$bool_u32 } from "./core/bool.ts"; import { unwrapOr$Option_u32, unwrapOr$Option_u8, unwrapOr$Option_u8_u8, unwrapOrDefault$Option_String, map$Option_u8_u8_elements_closure8, map$Option_u8_u8_u8_elements_closure9, map$Option_u8_u8_u8_u8_lent_closure2, mapOr$Option_u32_u32_lent_closure4, mapOr$Option_u8_bool_take_largest_closure0, mapOr$Option_u8_u8_elements_closure3, mapOr$Option_u8_u8_u8_btree_maps_closure0, andThen$Option_u32_u32_lent_closure3, filter$Option_u8_u8_lent_closure1, or$Option_u8 } from "./core/option.ts"; import * as Counts from "./refs.Counts.ts"; import * as Cursor from "./refs.Cursor.ts"; @@ -373,7 +374,7 @@ export function lent(data: rt.ByteSlice, out: rt.ByteVec): void { } const _61: rt.Place = rt.place(() => seen, (value8) => seen = value8); rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u32ToBeBytes(mapOr$Option_u32_u32_lent_closure4(andThen$Option_u32_u32_lent_closure3(_61.v === undefined ? undefined : rt.place(() => _61.v as rt.u32, (value9) => _61.v = value9), (x4: rt.Place): rt.Place | undefined => { - return rt.thenSome(rt.u32Rem(x4.v, 3) === 0, x4); + return thenSome$bool_u32(rt.u32Rem(x4.v, 3) === 0, x4); }), 0, (x5: rt.Place): rt.u32 => { x5.v = rt.u32Mul(x5.v, 2); return x5.v; diff --git a/ts/fixtures/lang/expected/shapes.ts b/ts/fixtures/lang/expected/shapes.ts index 4cbd63d7c..a293353a7 100644 --- a/ts/fixtures/lang/expected/shapes.ts +++ b/ts/fixtures/lang/expected/shapes.ts @@ -2,6 +2,7 @@ import * as rt from "./rt.ts"; import { copy$ControlFlow_Arc_u8_u8s, copy$ControlFlow_unit_u8s, copy$Gauge, copy$Held, copy$Lens, copy$Level, copy$Mutex_u8_u8, copy$Mutex_u8s, copy$Option_Mutex_u8_u8, copy$Option_Shape, copy$Point, copy$Point_u8, copy$Points, copy$Poll_u8s, copy$Result_Mutex_u8_u8_u8, copy$Shape, copy$u8_u8, copy$u8s } from "./lib.ts"; +import { then$bool_Vec_u8_new, then$bool_u8_library_closure4 } from "./core/bool.ts"; import { unwrapOr$Option_u8, map$Option_Dir_Shape_rematched_closure1, map$Option_Dir_u8_from, mapOr$Option_Meters_u64_copies_closure0, mapOr$Option_Mutex_u8_u8_u8_shares_closure0, mapOr$Option_Point_u8_known_closure0_closure0, mapOr$Option_Shape_u8_rematched_closure0, mapOr$Option_Vec_u8_u8_library_closure5, mapOr$Option_u8_usize_iters_closure1, okOr$Option_u8_u8 } from "./core/option.ts"; import { map$Result_u8_str_u8_library_closure1, mapErr$Result_u8_str_u8_library_closure2 } from "./core/result.ts"; import * as Pair from "./fmt.Pair.ts"; @@ -2106,11 +2107,11 @@ export function library(data: rt.ByteSlice, out: rt.ByteVec): void { break; } const x: rt.u8 = _66; - rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(unwrapOr$Option_u8(rt.boolThen(rt.u8Rem(x, 2) === 0, (): rt.u8 => { + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(unwrapOr$Option_u8(then$bool_u8_library_closure4(rt.u8Rem(x, 2) === 0, (): rt.u8 => { calls = rt.u8Add(calls, 1); return rt.u8Div(x, 2); }), 238), calls))); - rt.byteVecPush(out, mapOr$Option_Vec_u8_u8_library_closure5(rt.boolThen(x > 128, (): rt.ByteVec => { + rt.byteVecPush(out, mapOr$Option_Vec_u8_u8_library_closure5(then$bool_Vec_u8_new(x > 128, (): rt.ByteVec => { return new rt.ByteVec(); }), 2, (v: rt.ByteVec): rt.u8 => { return v.length & 0xff; diff --git a/ts/fixtures/lang/expected/strs.ts b/ts/fixtures/lang/expected/strs.ts index 7d63fb879..6e10f6620 100644 --- a/ts/fixtures/lang/expected/strs.ts +++ b/ts/fixtures/lang/expected/strs.ts @@ -1,6 +1,7 @@ // Generated by rust2-ts. Do not edit. import * as rt from "./rt.ts"; +import { then$bool_str_slices_closure1_closure0 } from "./core/bool.ts"; import { unwrapOr$Option_Cow_str, map$Option_usize_str_str_slices_closure2, mapOr$Option_usize_str_slices_closure1_closure0_closure0, mapOr$Option_usize_str_usize_slices_closure3 } from "./core/option.ts"; import { put } from "./fmt.ts"; import * as Named from "./strs.Named.ts"; @@ -327,7 +328,7 @@ export function slices(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { if (_8 === 3) { const iter3: rt.Iter = rt.iterSuccessors(s, (path: string): string | undefined => { const _73: string = path; - return rt.boolThen(!((path.length === 0)), (): string => { + return then$bool_str_slices_closure1_closure0(!((path.length === 0)), (): string => { return mapOr$Option_usize_str_slices_closure1_closure0_closure0(rt.strRfind(_73, "/"), "", (slash: rt.usize): string => { return rt.strSlice(_73, 0, slash); }); diff --git a/ts/fixtures/reject/census.md b/ts/fixtures/reject/census.md index a7c37dc99..877c7576e 100644 --- a/ts/fixtures/reject/census.md +++ b/ts/fixtures/reject/census.md @@ -1,7 +1,7 @@ # Census of `reject-fixture` - Refusals: 73 -- Function instances: 81 +- Function instances: 83 - Generic functions no instance reached, lowered over their type parameters: 6 ## By construct diff --git a/ts/fixtures/task/expected/core/bool.ts b/ts/fixtures/task/expected/core/bool.ts new file mode 100644 index 000000000..5f2f91d27 --- /dev/null +++ b/ts/fixtures/task/expected/core/bool.ts @@ -0,0 +1,78 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "../rt.ts"; +import type * as Guard from "../model.Guard.ts"; + +export function thenSome$bool_Guard(self: boolean, t: Guard.Guard): Guard.Guard | undefined { + let _0!: Guard.Guard | undefined; + let _4: boolean = false; + _4 = true; + if (!self) { + _0 = undefined; + } else { + _4 = false; + _0 = t; + } + if (_4) { + rt.drop(t); + return _0; + } + return _0; +} + +export function thenSome$bool_u32(self: boolean, t: rt.u32): rt.u32 | undefined { + if (!self) { + return undefined; + } + return t; +} + +export function then$bool_Guard_folded_closure55_closure0(self: boolean, f: () => Guard.Guard): Guard.Guard | undefined { + if (!self) { + return undefined; + } + return f(); +} + +export function then$bool_u32_discarded_closure14(self: boolean, f: () => rt.u32): rt.u32 | undefined { + let _0!: rt.u32 | undefined; + let _5: boolean = false; + _5 = true; + if (!self) { + _0 = undefined; + } else { + _5 = false; + _0 = f(); + } + if (_5) { + rt.drop(f); + return _0; + } + return _0; +} + +export function then$bool_u32_discarded_closure18(self: boolean, f: () => rt.u32): rt.u32 | undefined { + let _0!: rt.u32 | undefined; + let _5: boolean = false; + _5 = true; + if (!self) { + _0 = undefined; + } else { + _5 = false; + _0 = rt.callOnce(f); + } + if (_5) { + rt.drop(f); + return _0; + } + return _0; +} + +export function then$bool_u32_folded_closure48_closure0(self: boolean, f: () => rt.u32): rt.u32 | undefined { + if (!self) { + return undefined; + } + return f(); +} + +//# sourceMappingURL=bool.ts.map diff --git a/ts/fixtures/task/expected/drops.ts b/ts/fixtures/task/expected/drops.ts index b6b192b9c..8383244b4 100644 --- a/ts/fixtures/task/expected/drops.ts +++ b/ts/fixtures/task/expected/drops.ts @@ -2,6 +2,7 @@ import * as rt from "./rt.ts"; import { drop$Arc_Wrap_Pair_Held, drop$Arc_str_Arc_str, drop$Bomb_Guard, drop$Guard_Entries_Bomb_Guard_Held, drop$Held, drop$Key_Key, drop$Mutex_Held, drop$Option_Box_Held, drop$Option_Guard_Guard, drop$Option_Held, drop$Option_Result_Guard_u8, drop$Pair_Held, drop$Result_Guard_Guard, drop$Result_Guard_u8, drop$Result_u8_Key, drop$Wrap_Arc_Held, drop$Wrap_Guard, drop$Wrap_Held, drop$Wrap_Pair_Held, drop$Wrap_Vec_Wrap_Held } from "./lib.ts"; +import { thenSome$bool_Guard } from "./core/bool.ts"; import { isSomeAnd$Option_Guard_options_closure7, unwrapOr$Option_Guard, unwrapOr$Option_u64, mapOr$Option_u32_Guard_options_closure2, okOr$Option_Guard_Guard, filter$Option_Guard_options_closure4, orElse$Option_Guard_options_closure6, zip$Option_Guard_Guard, clone$Clone_Option_Arc_Guard } from "./core/option.ts"; import { ok$Result_Guard_Guard, err$Result_Guard_Guard, asRef$Result_Guard_u8, mapOr$Result_Guard_u8_u32_id, mapOrElse$Result_Guard_Guard_u32_options_closure9_options_closure10, inspectErr$Result_u8_Key_tripped_closure6, andThen$Result_Guard_Guard_Guard_options_closure8, unwrapOr$Result_Guard_Guard } from "./core/result.ts"; import { scoped, discarded, iterated, blasted } from "./drops/closures.ts"; @@ -1777,9 +1778,9 @@ export function options(log: Log.Log, id: rt.u32, at: rt.u8): void { const args8: rt.Fmt[] = [(f: rt.Formatter) => rt.u32Display(_157, f), (f: rt.Formatter) => rt.u32Display(_160, f)]; Log.note(log, rt.fmtFormat(["map_or ", args8[0], " ", args8[1]])); check(4); - then = rt.thenSome(rt.u8Rem(at, 2) === 0, guard(17), rt.drop); + then = thenSome$bool_Guard(rt.u8Rem(at, 2) === 0, guard(17)); unwind = 203; - never = rt.thenSome(false, guard(18), rt.drop); + never = thenSome$bool_Guard(false, guard(18)); unwind = 202; const _187: boolean = then !== undefined; const _190: boolean = never !== undefined; diff --git a/ts/fixtures/task/expected/drops/closures.ts b/ts/fixtures/task/expected/drops/closures.ts index 3c5fd9397..40e74c02d 100644 --- a/ts/fixtures/task/expected/drops/closures.ts +++ b/ts/fixtures/task/expected/drops/closures.ts @@ -1,6 +1,7 @@ // Generated by rust2-ts. Do not edit. import * as rt from "../rt.ts"; +import { thenSome$bool_u32, then$bool_Guard_folded_closure55_closure0, then$bool_u32_discarded_closure14, then$bool_u32_discarded_closure18 } from "../core/bool.ts"; import { map$Option_Guard_u32_folded_closure100, map$Option_u32_u32_blasted_closure3, map$Option_u32_u32_discarded_closure16, mapOr$Option_u32_u32_discarded_closure12 } from "../core/option.ts"; import { map$Result_u32_u32_u32_discarded_closure4, mapErr$Result_u32_u32_u32_discarded_closure6, inspectErr$Result_u32_u32_discarded_closure10, andThen$Result_u32_u32_u32_discarded_closure8 } from "../core/result.ts"; import * as Holder from "../drops/closures.Holder.ts"; @@ -751,7 +752,7 @@ export function discarded(log: Log.Log, id: rt.u32, at: rt.u8): void { const yes: boolean = some !== undefined; _269 = false; const captured17 = m; - const _159: rt.u32 | undefined = rt.boolThen(yes, rt.closure((): rt.u32 => { + const _159: rt.u32 | undefined = then$bool_u32_discarded_closure14(yes, rt.closure((): rt.u32 => { let unwind14: number = 0; try { unwind14 = 4; @@ -768,10 +769,10 @@ export function discarded(log: Log.Log, id: rt.u32, at: rt.u8): void { } }, (): void => { rt.drop(captured17); - }), rt.drop); + })); _268 = false; const captured18 = n; - const part4 = [_159, rt.boolThen(!yes, rt.closure((): rt.u32 => { + const part4 = [_159, then$bool_u32_discarded_closure14(!yes, rt.closure((): rt.u32 => { let unwind15: number = 0; try { unwind15 = 4; @@ -788,7 +789,7 @@ export function discarded(log: Log.Log, id: rt.u32, at: rt.u8): void { } }, (): void => { rt.drop(captured18); - }), rt.drop)] as [rt.u32 | undefined, rt.u32 | undefined]; + }))] as [rt.u32 | undefined, rt.u32 | undefined]; const args10: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtTuple([(f: rt.Formatter) => rt.fmtOption(part4[0], f, rt.u32Debug), (f: rt.Formatter) => rt.fmtOption(part4[1], f, rt.u32Debug)], f)]; Log.note(log, rt.fmtFormat(["then ", args10[0]])); _177 = guard(15); @@ -839,13 +840,13 @@ export function discarded(log: Log.Log, id: rt.u32, at: rt.u8): void { }, (): void => { rt.drop(captured23); }); - const _219: rt.u32 | undefined = rt.boolThen(yes, rt.once(rt.closure((): rt.u32 => { + const _219: rt.u32 | undefined = then$bool_u32_discarded_closure18(yes, rt.closure((): rt.u32 => { return Guard.id(captured22); }, (): void => { rt.drop(captured22); - })), rt.drop); + })); _262 = false; - const _221: rt.u32 | undefined = rt.boolThen(!yes, rt.once(skipped), rt.drop); + const _221: rt.u32 | undefined = then$bool_u32_discarded_closure18(!yes, skipped); const args14: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtOption(_219, f, rt.u32Debug), (f: rt.Formatter) => rt.fmtOption(_221, f, rt.u32Debug)]; Log.note(log, rt.fmtFormat(["then by ref ", args14[0], " ", args14[1]])); const captured24 = guard(17); @@ -1357,7 +1358,7 @@ export function iterated(log: Log.Log, id: rt.u32, at: rt.u8): void { _555 = false; const captured10 = f; const _176: rt.Iter = rt.iterFilterMap(_177, rt.closure((n4: rt.u32): rt.u32 | undefined => { - return rt.thenSome(n4 !== 2, rt.u32Add(n4, Guard.id(captured10))); + return thenSome$bool_u32(n4 !== 2, rt.u32Add(n4, Guard.id(captured10))); }, (): void => { rt.drop(captured10); })); @@ -1409,7 +1410,7 @@ export function iterated(log: Log.Log, id: rt.u32, at: rt.u8): void { const captured13 = h; const whiles: rt.u32[] = rt.iterToArray(rt.iterMapWhile(_228, rt.closure((_26: rt.u32): rt.u32 | undefined => { const n7: rt.u32 = _26; - return rt.thenSome(n7 < 3, rt.u32Add(n7, Guard.id(captured13))); + return thenSome$bool_u32(n7 < 3, rt.u32Add(n7, Guard.id(captured13))); }, (): void => { rt.drop(captured13); })), "next"); @@ -1437,7 +1438,7 @@ export function iterated(log: Log.Log, id: rt.u32, at: rt.u8): void { const captured16 = j; const walked: rt.u32 = rt.iterSum(rt.iterSuccessors(1, rt.closure((_27: rt.u32): rt.u32 | undefined => { const n8: rt.u32 = _27; - return rt.thenSome(n8 < 3, rt.u32Sub(rt.u32Add(rt.u32Add(n8, 1), Guard.id(captured16)), Guard.id(captured16))); + return thenSome$bool_u32(n8 < 3, rt.u32Sub(rt.u32Add(rt.u32Add(n8, 1), Guard.id(captured16)), Guard.id(captured16))); }, (): void => { rt.drop(captured16); })), rt.u32Add, 0, true); @@ -1453,7 +1454,7 @@ export function iterated(log: Log.Log, id: rt.u32, at: rt.u8): void { if (Guard.id(g10) === rt.u32Add(captured17, 41)) { captured18(8); } - return rt.boolThen(rt.u32Add(Guard.id(g10), 1) < Guard.id(captured19), (): Guard.Guard => { + return then$bool_Guard_folded_closure55_closure0(rt.u32Add(Guard.id(g10), 1) < Guard.id(captured19), (): Guard.Guard => { return Guard.new(rt.u32Add(Guard.id(g10), 1), Log.clone$Clone(log)); }); }, (): void => { diff --git a/ts/fixtures/task/expected/drops/folds.ts b/ts/fixtures/task/expected/drops/folds.ts index d64353976..50010cdf9 100644 --- a/ts/fixtures/task/expected/drops/folds.ts +++ b/ts/fixtures/task/expected/drops/folds.ts @@ -2,6 +2,7 @@ import * as rt from "../rt.ts"; import { drop$Guard_u64, drop$u32_Guard, drop$usize_Guard } from "../lib.ts"; +import { thenSome$bool_Guard, then$bool_Guard_folded_closure55_closure0, then$bool_u32_folded_closure48_closure0 } from "../core/bool.ts"; import { map$Option_Guard_u32_folded_closure100, map$Option_usize_Guard_usize_u32_folded_closure103 } from "../core/option.ts"; import * as Bomb from "../drops.Bomb.ts"; import * as Log from "../log.Log.ts"; @@ -258,7 +259,7 @@ export function folded(log: Log.Log, id: rt.u32, arg: rt.u8): void { captured122(g91); const _7111: boolean = rt.u32Rem(Guard.id(g91), 3) !== rt.u32Rem(Guard.id(captured123), 3); _1717 = false; - return rt.thenSome(_7111, g91, rt.drop); + return thenSome$bool_Guard(_7111, g91); } catch (e73) { if (unwind72 === 0) { throw e73; @@ -719,7 +720,7 @@ export function folded(log: Log.Log, id: rt.u32, arg: rt.u8): void { captured94(g73); const _7102: boolean = Guard.id(g73) !== Guard.id(captured95); _1323 = false; - return rt.thenSome(_7102, g73, rt.drop); + return thenSome$bool_Guard(_7102, g73); } catch (e59) { if (unwind58 === 0) { throw e59; @@ -1149,7 +1150,7 @@ export function folded(log: Log.Log, id: rt.u32, arg: rt.u8): void { try { unwind40 = 8; captured63(g47); - const _051: rt.u32 | undefined = rt.boolThen(Guard.id(g47) !== rt.u32Sub(Guard.id(captured64), 7), (): rt.u32 => { + const _051: rt.u32 | undefined = then$bool_u32_folded_closure48_closure0(Guard.id(g47) !== rt.u32Sub(Guard.id(captured64), 7), (): rt.u32 => { return rt.u32Sub(Guard.id(g47), captured65); }); unwind40 = 0; @@ -1387,7 +1388,7 @@ export function folded(log: Log.Log, id: rt.u32, arg: rt.u8): void { const captured51 = d; const m8: rt.u32 = rt.iterFold(rt.iterSuccessors(_634, rt.closure((g41: Guard.Guard): Guard.Guard | undefined => { captured49(g41); - return rt.boolThen(Guard.id(g41) < rt.u32Add(captured50, 7), (): Guard.Guard => { + return then$bool_Guard_folded_closure55_closure0(Guard.id(g41) < rt.u32Add(captured50, 7), (): Guard.Guard => { return Guard.new(rt.u32Sub(rt.u32Add(rt.u32Add(Guard.id(g41), 1), Guard.id(captured51)), Guard.id(captured51)), Log.clone$Clone(log)); }); }, (): void => { @@ -1893,7 +1894,7 @@ export function folded(log: Log.Log, id: rt.u32, arg: rt.u8): void { captured20(g16); const _740: boolean = Guard.id(g16) !== rt.u32Sub(Guard.id(captured21), 5); _1510 = false; - return rt.thenSome(_740, g16, rt.drop); + return thenSome$bool_Guard(_740, g16); } catch (e13) { if (unwind12 === 0) { throw e13; @@ -2524,7 +2525,7 @@ export function rfolded(log: Log.Log, id: rt.u32, arg: rt.u8): void { captured35(g26); const _722: boolean = Guard.id(g26) !== Guard.id(captured36); _1313 = false; - return rt.thenSome(_722, g26, rt.drop); + return thenSome$bool_Guard(_722, g26); } catch (e24) { if (unwind23 === 0) { throw e24; diff --git a/ts/fixtures/task/expected/drops/once.ts b/ts/fixtures/task/expected/drops/once.ts index bef170376..146d5aa74 100644 --- a/ts/fixtures/task/expected/drops/once.ts +++ b/ts/fixtures/task/expected/drops/once.ts @@ -19,91 +19,102 @@ export function run(log: Log.Log, id: rt.u32, at: rt.u8): void { let _64!: Guard.Guard; let cell2!: rt.OnceLock; let other!: rt.OnceCell; + let fresh!: rt.OnceCell; + let first!: Guard.Guard; + let second!: Guard.Guard; + let _142!: Guard.Guard; + let second2!: () => Guard.Guard; let cell3!: rt.OnceCell; let taken!: Guard.Guard | undefined; - let _166!: Guard.Guard | undefined; + let _201!: Guard.Guard | undefined; let cell4!: rt.OnceCell; let cell5!: rt.OnceCell; let _probe!: CellProbe.CellProbe; let lock3!: rt.OnceLock; let _probe2!: LockProbe.LockProbe; let shared!: rt.Arc; - let _232!: rt.OnceLock; + let _267!: rt.OnceLock; let other2!: rt.Arc; let cells!: rt.OnceCell[]; let v!: rt.OnceCell[]; let cell6!: rt.OnceCell; - let _310!: rt.Result; - let _325!: rt.Result; - let _405!: boolean; - let _406!: boolean; - let _407!: boolean; - let _408!: boolean; - let _409!: boolean; - let _410!: boolean; - let _411!: boolean; - let _412!: boolean; + let _345!: rt.Result; + let _360!: rt.Result; + let _442!: boolean; + let _443!: boolean; + let _444!: boolean; + let _445!: boolean; + let _446!: boolean; + let _447!: boolean; + let _448!: boolean; + let _449!: boolean; + let _450!: boolean; + let _451!: boolean; + let _452!: boolean; let unwind: number = 0; try { - _412 = false; - _411 = false; - _410 = false; - _409 = false; - _408 = false; - _407 = false; - _406 = false; - _405 = false; + _452 = false; + _451 = false; + _450 = false; + _449 = false; + _448 = false; + _447 = false; + _446 = false; + _445 = false; + _444 = false; + _443 = false; + _442 = false; const guard: (a0: rt.u32) => Guard.Guard = (n: rt.u32): Guard.Guard => { return Guard.new(rt.u32Add(id, n), Log.clone$Clone(log)); }; const _6: rt.u8 = rt.u8Rem(at, 8); if (_6 === 0) { - _412 = true; + _452 = true; cell = new rt.OnceCell(undefined); - unwind = 234; + unwind = 258; const _19: Guard.Guard = guard(1); _17 = rt.onceSet(cell, _19, rt.drop); - unwind = 217; + unwind = 237; const part13 = _17.ok; const args2: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtBool(part13, f)]; const _11: string = rt.fmtFormat(["set ", args2[0]]); - unwind = 234; + unwind = 258; drop$Result_unit_Guard(_17); Log.note(log, _11); const _28: Guard.Guard = guard(2); refused = rt.onceSet(cell, _28, rt.drop); - unwind = 232; - _411 = true; + unwind = 256; + _451 = true; const part14 = !refused.ok; const args4: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtBool(part14, f)]; Log.note(log, rt.fmtFormat(["refused ", args4[0]])); - _411 = false; + _451 = false; drop$Result_unit_Guard(refused); const part15 = mapOr$Option_Guard_u32_drained_closure4(cell.v, 0, (g2: Guard.Guard): rt.u32 => { return rt.u32Sub(Guard.id(g2), id); }); const args6: rt.Fmt[] = [(f: rt.Formatter) => rt.u32Display(part15, f)]; Log.note(log, rt.fmtFormat(["held ", args6[0]])); - _412 = false; + _452 = false; rt.drop(cell); _64 = guard(3); - unwind = 216; + unwind = 236; const _67: Guard.Guard = guard(4); - unwind = 232; + unwind = 256; lock = new rt.OnceLock({ tag: "Two", _0: _64, _1: _67 } as Held.Held); - unwind = 215; + unwind = 235; const _72 = { tag: "One", _0: guard(5) } as Held.Held; drop$Result_unit_Held(rt.onceSet(lock, _72, drop$Held)); const part16 = lock.v !== undefined; const args8: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtBool(part16, f)]; Log.note(log, rt.fmtFormat(["kept ", args8[0]])); - unwind = 232; + unwind = 256; drop$Option_Held(lock.v); - _411 = false; - _412 = false; + _451 = false; + _452 = false; } else if (_6 === 1) { cell2 = new rt.OnceLock(undefined); - unwind = 214; + unwind = 234; const captured7 = guard(11); const captured8 = guard; const part11 = rt.u32Sub(Guard.id(rt.onceGetOrInit(cell2, rt.closure((): Guard.Guard => { @@ -151,8 +162,8 @@ export function run(log: Log.Log, id: rt.u32, at: rt.u8): void { const args12: rt.Fmt[] = [(f: rt.Formatter) => rt.u32Display(part12, f)]; Log.note(log, rt.fmtFormat(["kept ", args12[0]])); other = new rt.OnceCell(undefined); - unwind = 230; - _410 = true; + unwind = 254; + _450 = true; const captured11 = guard(15); const captured12 = guard; rt.onceGetOrInit(other, rt.closure((): Held.Held => { @@ -172,35 +183,74 @@ export function run(log: Log.Log, id: rt.u32, at: rt.u8): void { }, (): void => { rt.drop(captured11); }), drop$Held, rt.drop); - _410 = false; + _450 = false; drop$Option_Held(other.v); + fresh = new rt.OnceCell(undefined); + unwind = 233; + _142 = guard(17); + unwind = 232; + const _145: Guard.Guard = guard(18); + unwind = 250; + _449 = true; + first = _142; + _448 = true; + second = _145; + _449 = false; + const captured13 = first; + const captured14 = guard; + const first2: () => Guard.Guard = rt.closure((): Guard.Guard => { + Guard.note(captured13, "init"); + return captured14(19); + }, (): void => { + rt.drop(captured13); + }); + _448 = false; + _447 = true; + const captured15 = second; + const captured16 = guard; + second2 = rt.closure((): Guard.Guard => { + Guard.note(captured15, "init"); + return captured16(20); + }, (): void => { + rt.drop(captured15); + }); + const made2: rt.u32 = rt.u32Sub(Guard.id(rt.onceGetOrInit(fresh, rt.once(first2), rt.drop, rt.drop)), id); + _447 = false; + const _161: rt.u32 = rt.u32Sub(Guard.id(rt.onceGetOrInit(fresh, rt.once(second2), rt.drop, rt.drop)), id); + const args14: rt.Fmt[] = [(f: rt.Formatter) => rt.u32Display(made2, f), (f: rt.Formatter) => rt.u32Display(_161, f)]; + Log.note(log, rt.fmtFormat(["by ref ", args14[0], " ", args14[1]])); + unwind = 254; + _447 = false; + _448 = false; + _449 = false; + rt.drop(fresh); unwind = 0; - _410 = false; + _450 = false; rt.drop(cell2); } else if (_6 === 2) { cell3 = new rt.OnceCell(guard(21)); - unwind = 228; - _409 = true; + unwind = 248; + _446 = true; taken = rt.onceTake(cell3); - unwind = 226; - _408 = true; + unwind = 246; + _445 = true; const part8 = taken !== undefined; - const args14: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtBool(part8, f)]; - Log.note(log, rt.fmtFormat(["taken ", args14[0]])); - _408 = false; + const args16: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtBool(part8, f)]; + Log.note(log, rt.fmtFormat(["taken ", args16[0]])); + _445 = false; rt.drop(taken); - _166 = rt.onceTake(cell3); - unwind = 213; - const part9 = _166 === undefined; - const args16: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtBool(part9, f)]; - const _160: string = rt.fmtFormat(["empty ", args16[0]]); - unwind = 226; - rt.drop(_166); - Log.note(log, _160); - const _174: Guard.Guard = guard(22); - drop$Result_unit_Guard(rt.onceSet(cell3, _174, rt.drop)); + _201 = rt.onceTake(cell3); + unwind = 229; + const part9 = _201 === undefined; + const args18: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtBool(part9, f)]; + const _195: string = rt.fmtFormat(["empty ", args18[0]]); + unwind = 246; + rt.drop(_201); + Log.note(log, _195); + const _209: Guard.Guard = guard(22); + drop$Result_unit_Guard(rt.onceSet(cell3, _209, rt.drop)); rt.drop(new rt.OnceLock(guard(23)).v); - _409 = false; + _446 = false; const part10 = mapOr$Option_Guard_u32_drained_closure4(cell3.v, 0, (g: Guard.Guard): rt.u32 => { let unwind5: number = 0; try { @@ -217,13 +267,13 @@ export function run(log: Log.Log, id: rt.u32, at: rt.u8): void { throw e5; } }); - const args18: rt.Fmt[] = [(f: rt.Formatter) => rt.u32Display(part10, f)]; - Log.note(log, rt.fmtFormat(["inner ", args18[0]])); - _408 = false; - _409 = false; + const args20: rt.Fmt[] = [(f: rt.Formatter) => rt.u32Display(part10, f)]; + Log.note(log, rt.fmtFormat(["inner ", args20[0]])); + _445 = false; + _446 = false; } else if (_6 === 3) { cell4 = new rt.OnceCell(undefined); - unwind = 212; + unwind = 228; const owned3: Guard.Guard = guard(31); const captured5 = guard; const captured6 = owned3; @@ -251,10 +301,10 @@ export function run(log: Log.Log, id: rt.u32, at: rt.u8): void { rt.drop(cell4); } else if (_6 === 4) { cell5 = new rt.OnceCell(undefined); - unwind = 211; - const _209: Guard.Guard = guard(40); - unwind = 210; - _probe = new CellProbe.CellProbe(cell5, _209); + unwind = 227; + const _244: Guard.Guard = guard(40); + unwind = 226; + _probe = new CellProbe.CellProbe(cell5, _244); const captured3 = guard(42); const captured4 = id; rt.onceGetOrInit(cell5, rt.closure((): Guard.Guard => { @@ -262,8 +312,8 @@ export function run(log: Log.Log, id: rt.u32, at: rt.u8): void { try { unwind3 = 5; Guard.note(captured3, "init"); - const args38: rt.Fmt[] = [(f: rt.Formatter) => rt.u32Display(captured4, f)]; - rt.panicFmt(["d", args38[0], " init"]); + const args40: rt.Fmt[] = [(f: rt.Formatter) => rt.u32Display(captured4, f)]; + rt.panicFmt(["d", args40[0], " init"]); } catch (e3) { if (unwind3 === 0) { throw e3; @@ -274,16 +324,16 @@ export function run(log: Log.Log, id: rt.u32, at: rt.u8): void { }, (): void => { rt.drop(captured3); }), rt.drop, rt.drop); - unwind = 211; + unwind = 227; rt.drop(_probe); unwind = 0; rt.drop(cell5); } else if (_6 === 5) { lock3 = new rt.OnceLock(undefined); - unwind = 209; - const _221: Guard.Guard = guard(50); - unwind = 208; - _probe2 = new LockProbe.LockProbe(lock3, _221); + unwind = 225; + const _256: Guard.Guard = guard(50); + unwind = 224; + _probe2 = new LockProbe.LockProbe(lock3, _256); const captured = guard(52); const captured2 = id; rt.onceGetOrInit(lock3, rt.closure((): Guard.Guard => { @@ -291,8 +341,8 @@ export function run(log: Log.Log, id: rt.u32, at: rt.u8): void { try { unwind2 = 5; Guard.note(captured, "init"); - const args36: rt.Fmt[] = [(f: rt.Formatter) => rt.u32Display(captured2, f)]; - rt.panicFmt(["d", args36[0], " init"]); + const args38: rt.Fmt[] = [(f: rt.Formatter) => rt.u32Display(captured2, f)]; + rt.panicFmt(["d", args38[0], " init"]); } catch (e2) { if (unwind2 === 0) { throw e2; @@ -303,88 +353,88 @@ export function run(log: Log.Log, id: rt.u32, at: rt.u8): void { }, (): void => { rt.drop(captured); }), rt.drop, rt.drop); - unwind = 209; + unwind = 225; rt.drop(_probe2); unwind = 0; rt.drop(lock3); } else if (_6 === 6) { - _232 = new rt.OnceLock(undefined); - unwind = 207; - const _233: Guard.Guard = guard(61); + _267 = new rt.OnceLock(undefined); + unwind = 223; + const _268: Guard.Guard = guard(61); unwind = 0; - shared = new rt.Arc(new Cached.Cached({ sweep: _232, guard: _233 })); - unwind = 224; - _407 = true; + shared = new rt.Arc(new Cached.Cached({ sweep: _267, guard: _268 })); + unwind = 244; + _444 = true; other2 = rt.arcClone(shared); - unwind = 222; - _406 = true; + unwind = 242; + _443 = true; rt.onceGetOrInit(shared.value.sweep, (): Held.Held => { return { tag: "One", _0: guard(62) } as Held.Held; }, drop$Held); - _407 = false; + _444 = false; rt.drop(shared); const part6 = other2.value.sweep.v !== undefined; - const args20: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtBool(part6, f)]; - Log.note(log, rt.fmtFormat(["swept ", args20[0]])); - _406 = false; + const args22: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtBool(part6, f)]; + Log.note(log, rt.fmtFormat(["swept ", args22[0]])); + _443 = false; rt.drop(other2); v = []; - unwind = 206; - const _268: rt.OnceCell = new rt.OnceCell(guard(63)); - v.push(_268); - const _274: rt.OnceCell = new rt.OnceCell(undefined); - v.push(_274); - const _277: rt.OnceCell = new rt.OnceCell(guard(64)); - v.push(_277); - unwind = 220; - _405 = true; + unwind = 222; + const _303: rt.OnceCell = new rt.OnceCell(guard(63)); + v.push(_303); + const _309: rt.OnceCell = new rt.OnceCell(undefined); + v.push(_309); + const _312: rt.OnceCell = new rt.OnceCell(guard(64)); + v.push(_312); + unwind = 240; + _442 = true; cells = v; const part7 = cells.length; - const args22: rt.Fmt[] = [(f: rt.Formatter) => rt.usizeDisplay(part7, f)]; - Log.note(log, rt.fmtFormat(["cells ", args22[0]])); - _405 = false; + const args24: rt.Fmt[] = [(f: rt.Formatter) => rt.usizeDisplay(part7, f)]; + Log.note(log, rt.fmtFormat(["cells ", args24[0]])); + _442 = false; rt.dropAll(cells); rt.drop(new rt.OnceLock(guard(65))); - _405 = false; - _406 = false; - _407 = false; + _442 = false; + _443 = false; + _444 = false; } else { cell6 = new rt.OnceCell(rt.None); - unwind = 205; - _310 = rt.onceSet(cell6, undefined as Guard.Guard | undefined, rt.drop, rt.None); - unwind = 204; - const part = _310.ok; - const args24: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtBool(part, f)]; - const _304: string = rt.fmtFormat(["none ", args24[0]]); - unwind = 205; - drop$Result_unit_Option_Guard(_310); - Log.note(log, _304); - const _327: Guard.Guard | undefined = guard(71); - _325 = rt.onceSet(cell6, _327, rt.drop, rt.None); - unwind = 203; - const part2 = !_325.ok; - const args26: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtBool(part2, f)]; - const _319: string = rt.fmtFormat(["full ", args26[0]]); - unwind = 205; - drop$Result_unit_Option_Guard(_325); - Log.note(log, _319); + unwind = 221; + _345 = rt.onceSet(cell6, undefined as Guard.Guard | undefined, rt.drop, rt.None); + unwind = 220; + const part = _345.ok; + const args26: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtBool(part, f)]; + const _339: string = rt.fmtFormat(["none ", args26[0]]); + unwind = 221; + drop$Result_unit_Option_Guard(_345); + Log.note(log, _339); + const _362: Guard.Guard | undefined = guard(71); + _360 = rt.onceSet(cell6, _362, rt.drop, rt.None); + unwind = 219; + const part2 = !_360.ok; + const args28: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtBool(part2, f)]; + const _354: string = rt.fmtFormat(["full ", args28[0]]); + unwind = 221; + drop$Result_unit_Option_Guard(_360); + Log.note(log, _354); const unit: rt.OnceLock = new rt.OnceLock(rt.None); - unwind = 202; + unwind = 218; const part3 = rt.onceSet(unit, undefined, undefined, rt.None).ok; - const args28: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtBool(part3, f)]; - Log.note(log, rt.fmtFormat(["unit ", args28[0]])); + const args30: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtBool(part3, f)]; + Log.note(log, rt.fmtFormat(["unit ", args30[0]])); const part4 = !rt.onceSet(unit, undefined, undefined, rt.None).ok; - const args30: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtBool(part4, f)]; - Log.note(log, rt.fmtFormat(["again ", args30[0]])); + const args32: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtBool(part4, f)]; + Log.note(log, rt.fmtFormat(["again ", args32[0]])); const part5 = cell6.v !== rt.None; - const args32: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtBool(part5, f)]; - Log.note(log, rt.fmtFormat(["got ", args32[0]])); + const args34: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtBool(part5, f)]; + Log.note(log, rt.fmtFormat(["got ", args34[0]])); unwind = 0; rt.drop(cell6); } unwind = 0; - const args34: rt.Fmt[] = [(f: rt.Formatter) => rt.u32Display(id, f)]; - Log.note(log, rt.fmtFormat(["d", args34[0], " cells done"])); + const args36: rt.Fmt[] = [(f: rt.Formatter) => rt.u32Display(id, f)]; + Log.note(log, rt.fmtFormat(["d", args36[0], " cells done"])); } catch (e) { b1: { b2: { @@ -396,113 +446,137 @@ export function run(log: Log.Log, id: rt.u32, at: rt.u8): void { b8: { b9: { b10: { - if (unwind === 0) { - throw e; - } - if (unwind === 202) { - break b2; - } - if (unwind === 203) { - drop$Result_unit_Option_Guard(_325); - break b2; - } - if (unwind === 204) { - drop$Result_unit_Option_Guard(_310); - break b2; - } - if (unwind === 205) { - break b2; - } - if (unwind === 206) { - rt.dropAll(v); - break b4; - } - if (unwind === 207) { - drop$Option_Held(_232.v); - break b1; - } - if (unwind === 208) { - rt.drop(_probe2); - break b5; - } - if (unwind === 209) { - break b5; - } - if (unwind === 210) { - rt.drop(_probe); - break b6; - } - if (unwind === 211) { - break b6; - } - if (unwind === 212) { - rt.drop(cell4); - break b1; - } - if (unwind === 213) { - rt.drop(_166); - break b8; - } - if (unwind === 214) { - break b9; - } - if (unwind === 215) { - drop$Option_Held(lock.v); - } else if (unwind === 216) { - rt.drop(_64); - } else { - if (unwind === 217) { - drop$Result_unit_Guard(_17); - break b10; - } - if (unwind === 220) { - if (!_405) { + b11: { + b12: { + if (unwind === 0) { + throw e; + } + if (unwind === 218) { + break b2; + } + if (unwind === 219) { + drop$Result_unit_Option_Guard(_360); + break b2; + } + if (unwind === 220) { + drop$Result_unit_Option_Guard(_345); + break b2; + } + if (unwind === 221) { + break b2; + } + if (unwind === 222) { + rt.dropAll(v); break b4; } - rt.dropAll(cells); - break b4; - } - if (unwind === 222) { - break b4; - } - if (unwind === 224) { - break b3; - } - if (unwind === 226) { - break b8; - } - if (unwind === 228) { - break b7; - } - if (unwind === 230) { - if (!_410) { + if (unwind === 223) { + drop$Option_Held(_267.v); + break b1; + } + if (unwind === 224) { + rt.drop(_probe2); + break b5; + } + if (unwind === 225) { + break b5; + } + if (unwind === 226) { + rt.drop(_probe); + break b6; + } + if (unwind === 227) { + break b6; + } + if (unwind === 228) { + rt.drop(cell4); + break b1; + } + if (unwind === 229) { + rt.drop(_201); + break b8; + } + if (unwind === 232) { + rt.drop(_142); + break b11; + } + if (unwind === 233) { + break b11; + } + if (unwind === 234) { break b9; } - drop$Option_Held(other.v); - break b9; + if (unwind === 235) { + drop$Option_Held(lock.v); + } else if (unwind === 236) { + rt.drop(_64); + } else { + if (unwind === 237) { + drop$Result_unit_Guard(_17); + break b12; + } + if (unwind === 240) { + if (!_442) { + break b4; + } + rt.dropAll(cells); + break b4; + } + if (unwind === 242) { + break b4; + } + if (unwind === 244) { + break b3; + } + if (unwind === 246) { + break b8; + } + if (unwind === 248) { + break b7; + } + if (unwind === 250) { + if (_447) { + rt.drop(second2); + } + if (_448) { + rt.drop(second); + } + if (!_449) { + break b11; + } + rt.drop(first); + break b11; + } + if (unwind === 254) { + break b10; + } + if (unwind !== 256) { + break b12; + } + } + if (_451) { + drop$Result_unit_Guard(refused); + } } - if (unwind !== 232) { - break b10; + if (!_452) { + break b1; } + rt.drop(cell); + break b1; } - if (_411) { - drop$Result_unit_Guard(refused); - } + rt.drop(fresh); } - if (!_412) { - break b1; + if (_450) { + drop$Option_Held(other.v); } - rt.drop(cell); - break b1; } rt.drop(cell2); break b1; } - if (_408) { + if (_445) { rt.drop(taken); } } - if (!_409) { + if (!_446) { break b1; } rt.drop(cell3); @@ -514,11 +588,11 @@ export function run(log: Log.Log, id: rt.u32, at: rt.u8): void { rt.drop(lock3); break b1; } - if (_406) { + if (_443) { rt.drop(other2); } } - if (!_407) { + if (!_444) { break b1; } rt.drop(shared); diff --git a/ts/fixtures/task/src/drops/once.rs b/ts/fixtures/task/src/drops/once.rs index d660a0838..6b2f2cb82 100644 --- a/ts/fixtures/task/src/drops/once.rs +++ b/ts/fixtures/task/src/drops/once.rs @@ -78,6 +78,20 @@ pub fn run(log: &Log, id: u32, at: u8) { let owned = guard(15); other.get_or_init(move || Held::Two(owned, guard(16))); drop(other); + // Ones that `Fn` calls, which `get_or_init` consumes by `FnOnce`: called, then + // dropped, or dropped uncalled. + let fresh = OnceCell::new(); + let (first, second) = (guard(17), guard(18)); + let first = move || { + first.note("init"); + guard(19) + }; + let second = move || { + second.note("init"); + guard(20) + }; + let made = fresh.get_or_init(first).id() - id; + log.note(format!("by ref {made} {}", fresh.get_or_init(second).id() - id)); } 2 => { let mut cell = OnceCell::from(guard(21)); diff --git a/ts/runtime/rt.ts b/ts/runtime/rt.ts index cb8510d09..0efdec340 100644 --- a/ts/runtime/rt.ts +++ b/ts/runtime/rt.ts @@ -197,25 +197,6 @@ export function optionExpect(o: T | undefined | None, message: string, none?: return o as T; } -/** `bool::then_some`: `value` if `cond`, which is evaluated either way, and else drops. */ -export function thenSome(cond: boolean, value: T, drop?: (value: T) => void): T | undefined; -export function thenSome(cond: boolean, value: T, drop: ((value: T) => void) | undefined, none: None): T | None; -export function thenSome(cond: boolean, value: T, drop?: (value: T) => void, none?: None): T | undefined | None { - if (cond) return value; - drop?.(value); - return none; -} - -// Not `then`, which would make the module's namespace a thenable that `await` calls. -/** `bool::then`: `f()` if `cond`, which is called only then, and else `f` drops. */ -export function boolThen(cond: boolean, f: () => T, drop?: (f: () => T) => void): T | undefined; -export function boolThen(cond: boolean, f: () => T, drop: ((f: () => T) => void) | undefined, none: None): T | None; -export function boolThen(cond: boolean, f: () => T, drop?: (f: () => T) => void, none?: None): T | undefined | None { - if (cond) return f(); - drop?.(f); - return none; -} - // Slices: arrays, but for bytes, which are `ByteSlice`s. /** A bounds-checked index into a slice. */ diff --git a/ts/src/lower/library.rs b/ts/src/lower/library.rs index 6222ecf21..90e0a157e 100644 --- a/ts/src/lower/library.rs +++ b/ts/src/lower/library.rs @@ -469,10 +469,10 @@ pub fn inherent_std(tcx: TyCtxt<'_>, def_id: DefId) -> Option { } /// Whether std's `instance` lowers from its own MIR, as the crate's functions do, rather -/// than mapping here: a method of `Option`, `Result`, or `Poll`, or their `Clone`. Not one taking a -/// `&mut` or a raw pointer, whose writes the crate's analysis of what it writes into -/// would miss, nor an `unsafe` one, nor one mapped below as one expression or as the -/// runtime's panic. +/// than mapping here: a method of `bool`, `Option`, `Result`, or `Poll`, or their `Clone`. +/// Not one taking a `&mut` or a raw pointer, whose writes the crate's analysis of what it +/// writes into would miss, nor an `unsafe` one, nor one mapped below as one expression or +/// as the runtime's panic. pub fn from_mir<'tcx>(t: &Translator<'_, 'tcx>, instance: Instance<'tcx>) -> bool { let tcx = t.tcx; let ty::InstanceKind::Item(did) = instance.def else { @@ -484,11 +484,14 @@ pub fn from_mir<'tcx>(t: &Translator<'_, 'tcx>, instance: Instance<'tcx>) -> boo let Some(imp) = tcx.impl_of_assoc(did) else { return false; }; - let ty::Adt(adt, _) = tcx.type_of(imp).instantiate_identity().skip_norm_wip().kind() else { - return false; - }; - let Some(std @ (Std::Option | Std::Result | Std::Poll)) = Std::of(tcx, adt.did()) else { - return false; + // `None` for `bool`'s. + let std = match tcx.type_of(imp).instantiate_identity().skip_norm_wip().kind() { + ty::Adt(adt, _) => match Std::of(tcx, adt.did()) { + std @ Some(Std::Option | Std::Result | Std::Poll) => std, + _ => return false, + }, + ty::Bool => None, + _ => return false, }; let sig = tcx.fn_sig(did).instantiate_identity().skip_norm_wip().skip_binder(); let writes = |ty: Ty<'tcx>| { @@ -510,9 +513,12 @@ pub fn from_mir<'tcx>(t: &Translator<'_, 'tcx>, instance: Instance<'tcx>) -> boo !matches!( (std, name.as_str()), ( - Std::Option, + Some(Std::Option), "unwrap" | "expect" | "is_some" | "is_none" | "as_ref" | "copied" | "cloned" | "iter" - ) | (Std::Result, "unwrap" | "expect" | "unwrap_err" | "is_ok" | "is_err") + ) | ( + Some(Std::Result), + "unwrap" | "expect" | "unwrap_err" | "is_ok" | "is_err" + ) ) } @@ -543,9 +549,6 @@ fn discards(owner: &str, name: &str) -> Option<&'static [usize]> { ("Result", "unwrap_err") => &[0], // `Vec` drops each element `retain` rejects and `dedup` removes. ("Vec", "retain" | "retain_mut" | "dedup" | "dedup_by" | "dedup_by_key") => &[0], - // `then_some::` drops its value when false, and `then::` its function. - ("bool", "then_some") => &[0], - ("bool", "then") => &[1], // `get_or_init::` drops its function uncalled if the cell is set or a // `OnceLock` is initializing, and what it made if it set the cell itself. ("OnceCell" | "OnceLock", "get_or_init") => &[0, 1], @@ -577,7 +580,7 @@ fn takes_none(owner: &str, name: &str) -> bool { fn none_last(owner: &str, name: &str) -> bool { matches!( (owner, name), - ("Option", "unwrap" | "expect") | ("slice", "first" | "get") | ("bool", "then" | "then_some") + ("Option", "unwrap" | "expect") | ("slice", "first" | "get") ) } @@ -1094,11 +1097,6 @@ fn mapping<'tcx>( }; bound(variant, field.map(|f| r.member(f)), ts) } - ("bool", "then_some") => { - let values = args; - Expr::rt("thenSome", values) - } - ("bool", "then") => Expr::rt("boolThen", args), // Slices, whose byte functions take an `rt.ByteSlice`. ("slice", "len") => super::mir::slice_member(args.pop()?, "length"),