From eeaba57bdfc7282ef45188525a975c226cfb1828 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Mon, 26 Oct 2026 23:23:52 -0700 Subject: [PATCH 1/4] chore(quest): claim quest/m2/ts-std-gaps.md Quest-Claim: 356195bafdf2dea4 From ba780f8938051c97dc9cfacb24edf3f2b9f730e3 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Mon, 26 Oct 2026 23:41:29 -0700 Subject: [PATCH 2/4] feat(ts): translate bool::then, iter::successors, and From Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- ts/fixtures/lang/expected/collections.ts | 2 +- ts/fixtures/lang/expected/fmt.ts | 2 +- ts/fixtures/lang/expected/iters.ts | 48 +++++++---- ts/fixtures/lang/expected/shapes.ts | 72 ++++++++++++---- ts/fixtures/lang/expected/strs.ts | 105 ++++++++++++----------- ts/fixtures/lang/src/iters.rs | 16 ++++ ts/fixtures/lang/src/shapes.rs | 31 +++++++ ts/fixtures/lang/src/strs.rs | 10 +-- ts/fixtures/task/expected/drops.ts | 16 ++-- ts/runtime/rt.ts | 8 ++ ts/runtime/rt/iter.ts | 13 +++ ts/src/lower/iter.rs | 4 + ts/src/lower/library.rs | 10 ++- ts/src/lower/mir/call.rs | 6 ++ ts/src/lower/mir/mod.rs | 48 +++++++---- 15 files changed, 278 insertions(+), 113 deletions(-) diff --git a/ts/fixtures/lang/expected/collections.ts b/ts/fixtures/lang/expected/collections.ts index e84410d41..94ce2e90d 100644 --- a/ts/fixtures/lang/expected/collections.ts +++ b/ts/fixtures/lang/expected/collections.ts @@ -784,7 +784,7 @@ export function btreeMaps(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): voi const o: rt.Ordering = rt.orderingThen(rt.orderingReverse(Nibble.cmp$Ord(a2, b4)), (): rt.Ordering => { return rt.cmp(y, x); }, true); - rt.byteVecPush(out, (o < 0 ? 1 : 0) | (((o >= 0 ? 1 : 0) << 1) & 0xff) | (((rt.orderingThen(Nibble.cmp$Ord(a2, b4), -1) === 0 ? 1 : 0) << 2) & 0xff)); + rt.byteVecPush(out, (o < 0 ? 1 : 0) | (((o >= 0 ? 1 : 0) << 1) & 0xff) | (((rt.orderingThen(Nibble.cmp$Ord(a2, b4), -1 as rt.Ordering) === 0 ? 1 : 0) << 2) & 0xff)); if (o === -1) { _401 = 1; } else if (o === 0) { diff --git a/ts/fixtures/lang/expected/fmt.ts b/ts/fixtures/lang/expected/fmt.ts index 876639ad6..13b7f1d1c 100644 --- a/ts/fixtures/lang/expected/fmt.ts +++ b/ts/fixtures/lang/expected/fmt.ts @@ -360,7 +360,7 @@ export function panics(kind: rt.u8, rest: rt.ByteSlice, out: rt.ByteVec): void { } } else if (_7 === 4) { if (!(n === 7)) { - rt.assertFailed("==", (f: rt.Formatter) => rt.u8Display(n, f), (f: rt.Formatter) => rt.u8Display(7, f), undefined); + rt.assertFailed("==", (f: rt.Formatter) => rt.u8Display(n, f), (f: rt.Formatter) => rt.u8Display(7, f), undefined as rt.Arguments | undefined); } } else if (_7 === 5) { if (Pair.eq$PartialEq({ _0: n, _1: 1 }, { _0: n, _1: 1 })) { diff --git a/ts/fixtures/lang/expected/iters.ts b/ts/fixtures/lang/expected/iters.ts index 68d95eb89..bb62e7cf1 100644 --- a/ts/fixtures/lang/expected/iters.ts +++ b/ts/fixtures/lang/expected/iters.ts @@ -440,16 +440,22 @@ export function fullRange(out: rt.ByteVec): undefined { /** Iterators of `Option`, which keep each `None` apart from their end. */ export function options(data: rt.ByteSlice, out: rt.ByteVec): void { - const all: Array = rt.iterToArray(rt.iterMap(rt.iterSlice(rt.byteSliceView(data)), (b2: rt.u8): rt.u8 | undefined => { - if (!(rt.u8Rem(b2, 3) === 0)) { - return b2; + const _68: (a0: rt.u8, a1: rt.ByteVec) => rt.u8 | undefined = (b2: rt.u8, calls2: rt.ByteVec): rt.u8 | undefined => { + rt.byteVecPush(calls2, b2); + return rt.optionMap(rt.u8CheckedSub(b2, 1), (b3: rt.u8): rt.u8 => { + return rt.u8Div(b3, 2); + }); + }; + const all: Array = rt.iterToArray(rt.iterMap(rt.iterSlice(rt.byteSliceView(data)), (b4: rt.u8): rt.u8 | undefined => { + if (!(rt.u8Rem(b4, 3) === 0)) { + return b4; } return undefined; })); rt.byteVecPush(out, all.length & 0xff); - const iter: rt.Iter = rt.iterMap(rt.iterSlice(rt.byteSliceView(data)), (b3: rt.u8): rt.u8 | undefined => { - if (!(rt.u8Rem(b3, 3) === 0)) { - return b3; + const iter: rt.Iter = rt.iterMap(rt.iterSlice(rt.byteSliceView(data)), (b5: rt.u8): rt.u8 | undefined => { + if (!(rt.u8Rem(b5, 3) === 0)) { + return b5; } return undefined; }); @@ -465,18 +471,18 @@ export function options(data: rt.ByteSlice, out: rt.ByteVec): void { rt.byteVecPush(out, item); } } - rt.byteVecExtendFromSlice(out, rt.byteVecSlice(rt.iterToByteVec(rt.iterFlatten(rt.iterMap(rt.iterSlice(rt.byteSliceView(data)), (b4: rt.u8): rt.u8 | undefined => { - if (!(rt.u8Rem(b4, 3) === 0)) { - return b4; + rt.byteVecExtendFromSlice(out, rt.byteVecSlice(rt.iterToByteVec(rt.iterFlatten(rt.iterMap(rt.iterSlice(rt.byteSliceView(data)), (b6: rt.u8): rt.u8 | undefined => { + if (!(rt.u8Rem(b6, 3) === 0)) { + return b6; } return undefined; }), (value: rt.u8 | undefined) => rt.iterOption(value))))); rt.byteVecPush(out, rt.iterCount(rt.iterFilter(rt.iterSlice(all), (o: rt.u8 | undefined): boolean => { return o !== undefined; })) & 0xff); - rt.byteVecPush(out, rt.optionUnwrapOr(second$Map_Iter_u8_options_closure0(rt.iterMap(rt.iterSlice(rt.byteSliceView(data)), (b5: rt.u8): rt.u8 | undefined => { - if (!(rt.u8Rem(b5, 3) === 0)) { - return b5; + rt.byteVecPush(out, rt.optionUnwrapOr(second$Map_Iter_u8_options_closure0(rt.iterMap(rt.iterSlice(rt.byteSliceView(data)), (b7: rt.u8): rt.u8 | undefined => { + if (!(rt.u8Rem(b7, 3) === 0)) { + return b7; } return undefined; })), 205)); @@ -497,14 +503,24 @@ export function options(data: rt.ByteSlice, out: rt.ByteVec): void { n = rt.u8Add(n, 1); return rt.thenSome(n < 4, n); })))); + const calls: rt.ByteVec = new rt.ByteVec(); + const first2: rt.u8 | undefined = rt.byteSliceFirst(data); + rt.byteVecExtendFromSlice(out, rt.byteVecSlice(rt.iterToByteVec(rt.iterTake(rt.iterSuccessors(first2, (b8: rt.u8): rt.u8 | undefined => { + return _68(b8, calls); + }), rt.usizeRem(data.length, 4))))); + rt.byteVecExtendFromSlice(out, rt.byteVecSlice(calls)); + rt.byteVecPush(out, rt.iterCount(rt.iterSuccessors(first2, (b9: rt.u8): rt.u8 | undefined => { + return _68(b9, calls); + })) & 0xff); + rt.byteVecExtendFromSlice(out, rt.byteVecSlice(calls)); const iter3: Gaps.Gaps = { data, at: 0 }; while (true) { - const _70: rt.u8 | undefined | rt.Done = Gaps.next$Iterator(iter3); - if (_70 === rt.DONE) { + const _99: rt.u8 | undefined | rt.Done = Gaps.next$Iterator(iter3); + if (_99 === rt.DONE) { break; } - rt.byteVecPush(out, rt.optionMapOr(_70, 170, (b6: rt.u8): rt.u8 => { - return b6; + rt.byteVecPush(out, rt.optionMapOr(_99, 170, (b10: rt.u8): rt.u8 => { + return b10; })); } } diff --git a/ts/fixtures/lang/expected/shapes.ts b/ts/fixtures/lang/expected/shapes.ts index a65b779de..31827f6ac 100644 --- a/ts/fixtures/lang/expected/shapes.ts +++ b/ts/fixtures/lang/expected/shapes.ts @@ -1627,27 +1627,27 @@ export function library(data: rt.ByteSlice, out: rt.ByteVec): void { const _3: (a0: rt.u8, a1: rt.u8) => rt.u8 | undefined = (a2: rt.u8, b2: rt.u8): rt.u8 | undefined => { const _4: rt.u8 | undefined = rt.u8CheckedAdd(a2, b2); if (_4 !== undefined) { - const _82: rt.u8 | undefined = rt.u8CheckedMul(_4, 2); - if (_82 !== undefined) { - return rt.u8Sub(_82, 1); + const _83: rt.u8 | undefined = rt.u8CheckedMul(_4, 2); + if (_83 !== undefined) { + return rt.u8Sub(_83, 1); } return undefined; } return undefined; }; const _30: (a0: rt.u8) => rt.Poll> = (a3: rt.u8): rt.Poll> => { - let half!: rt.Result; + let half2!: rt.Result; const _43: Dir.Dir | undefined = dir(a3); b0: { if (_43 !== undefined) { if (rt.u8Rem(a3, 2) === 1) { - half = rt.Err(_43); + half2 = rt.Err(_43); break b0; } } - half = rt.Ok(rt.u8Div(a3, 2)); + half2 = rt.Ok(rt.u8Div(a3, 2)); } - const _132: rt.Result = half; + const _132: rt.Result = half2; if (_132.ok) { return rt.Ok(_132.value); } @@ -1661,10 +1661,10 @@ export function library(data: rt.ByteSlice, out: rt.ByteVec): void { } else { parsed = rt.Ok(a); } - const mapped: rt.Result = rt.resultMapErr(rt.resultMap(parsed, (n5: rt.u8): rt.u8 => { - return rt.u8Div(n5, 2); - }), (e2: string): rt.u8 => { - return rt.stringLen(e2) & 0xff; + const mapped: rt.Result = rt.resultMapErr(rt.resultMap(parsed, (n7: rt.u8): rt.u8 => { + return rt.u8Div(n7, 2); + }), (e4: string): rt.u8 => { + return rt.stringLen(e4) & 0xff; }); if (mapped.ok) { rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(0, mapped.value))); @@ -1681,15 +1681,57 @@ export function library(data: rt.ByteSlice, out: rt.ByteVec): void { } else { rt.byteVecPush(out, 4); } - const _45: rt.Result = rt.stringFromUtf8(rt.byteSliceToByteVec(data)); - if (_45.ok) { - const s: string = _45.value; + const _46: rt.Result = rt.stringFromUtf8(rt.byteSliceToByteVec(data)); + if (_46.ok) { + const s: string = _46.value; rt.byteVecPush(out, 1); rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u32ToBeBytes(rt.stringLen(s) as rt.u32))); rt.byteVecExtendFromSlice(out, rt.stringAsBytes(s)); } else { rt.byteVecPush(out, 0); } + let calls: rt.u8 = 0; + const iter: rt.Iter = rt.iterTake(rt.iterSlice(rt.byteSliceView(data)), 6); + while (true) { + const _66: rt.u8 | rt.Done = iter.next(); + if (_66 === rt.DONE) { + break; + } + const x: rt.u8 = _66; + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(rt.optionUnwrapOr(rt.boolThen(rt.u8Rem(x, 2) === 0, (): rt.u8 => { + calls = rt.u8Add(calls, 1); + return rt.u8Div(x, 2); + }), 238), calls))); + rt.byteVecPush(out, rt.optionMapOr(rt.boolThen(x > 128, (): rt.ByteVec => { + return new rt.ByteVec(); + }), 2, (v: rt.ByteVec): rt.u8 => { + return v.length & 0xff; + })); + const flag: boolean = rt.u8Rem(x, 3) === 0; + rt.byteVecPush(out, rt.u8Add(rt.u8Add(flag ? 1 : 0, ((flag ? 1 : 0)) & 0xff), ((flag ? 1 : 0)) & 0xff)); + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(flag ? 1 : 0))); + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u128ToBeBytes(flag ? 1 : 0))); + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.i64ToBeBytes(flag ? 1 : 0))); + } + const _117: rt.Result = rt.optionOkOr(undefined as rt.u8 | undefined, a); + if (_117.ok) { + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(5, _117.value))); + } else { + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(6, _117.error))); + } + const iter2: rt.Iter> = rt.iterSlice([rt.optionOkOr(undefined as rt.u8 | undefined, b), rt.optionOkOr(undefined as rt.u8 | undefined, a)]); + while (true) { + const _134: rt.Result | rt.Done = iter2.next(); + if (_134 === rt.DONE) { + break; + } + const result: rt.Result = _134; + if (result.ok) { + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(5, result.value))); + } else { + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(6, result.error))); + } + } } /** Steps a total by `x`: breaks with what it can't take, or continues with the new total. */ @@ -1794,7 +1836,7 @@ export function flows(data: rt.ByteSlice, out: rt.ByteVec): void { rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(flow3.value)); } } - const slot: rt.Mutex = new rt.Mutex(undefined); + const slot: rt.Mutex = new rt.Mutex(undefined as rt.u8 | undefined); for (const x2 of rt.byteSliceView(data)) { const held: rt.Mutex = slot; const _114: rt.u8 | undefined = held.value; diff --git a/ts/fixtures/lang/expected/strs.ts b/ts/fixtures/lang/expected/strs.ts index 87f756b2f..f46c757c7 100644 --- a/ts/fixtures/lang/expected/strs.ts +++ b/ts/fixtures/lang/expected/strs.ts @@ -273,7 +273,7 @@ export function pattern(kind: rt.u8, s: string): string { * one matches before each char and after the last. */ export function slices(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { - let _211!: [rt.usize, rt.usize]; + let _209!: [rt.usize, rt.usize]; let sliced!: string; const s: string = text(data); const p: string = pattern(kind, s); @@ -322,59 +322,66 @@ export function slices(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { return rt.charIsAsciiDigit(c); }) ? 1 : 0); putOffset(out, rt.strRfind(s, p)); - } else if (_8 === 3) { - let path: string = s; - while (true) { - const _125: rt.usize | undefined = rt.strRfind(path, "/"); - if (_125 !== undefined) { - path = rt.strSlice(path, 0, _125); - put(out, path); - } else { - break; - } - } - put(out, path); - } else if (_8 === 4) { - const keep: rt.usize = rt.usizeAdd(rt.u8Rem(rt.u8Div(kind, 8), 4) as rt.usize, 1); - put(out, rt.optionUnwrapOr(rt.optionMap(rt.iterNth(rt.strMatchIndices(s, "/"), rt.usizeSub(keep, 1)), (_2: [rt.usize, string]): string => { - return rt.strSlice(s, 0, _2[0]); - }), "none")); - const part5 = rt.optionMapOr(rt.iterNth(rt.strMatchIndices(s, "/"), rt.usizeSub(keep, 1)), rt.stringLen(s), (_22: [rt.usize, string]): rt.usize => { - return rt.usizeAdd(_22[0], 1); - }); - put(out, rt.strSlice(s, part5)); - } else if (_8 === 5) { - const parts: string[] = rt.iterToArray(rt.strSplit(s, "/")); - put(out, parts.join(p)); - put(out, rt.iterToArray(rt.iterMap(rt.iterSlice(parts), (part3: string): string => { - return part3; - })).join(p)); - put(out, ["a", "", s].join("/")); } else { - const at2: (a0: rt.u8) => rt.usize = (b2: rt.u8): rt.usize => { - if ((kind & 32) === 0) { - return rt.usizeRem(b2 as rt.usize, rt.usizeAdd(rt.stringLen(s), 2)); + 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 rt.optionMapOr(rt.strRfind(_73, "/"), "", (slash: rt.usize): string => { + return rt.strSlice(_73, 0, slash); + }); + }); + }); + while (true) { + const _129: string | rt.Done = iter3.next(); + if (_129 === rt.DONE) { + break b0; + } + put(out, _129); } - return rt.usizeSaturatingSub(rt.usizeAdd(rt.stringLen(s), 1), rt.usizeRem(b2 as rt.usize, 4)); - }; - if (!(data.length >= 2)) { - _211 = [0, 0] as [rt.usize, rt.usize]; - } else { - _211 = [at2(data.data[data.start]), at2(data.data[data.start + 1])] as [rt.usize, rt.usize]; } - const a: rt.usize = _211[0]; - const b: rt.usize = _211[1]; - const _225: rt.u8 = rt.u8Rem(rt.u8Div(kind, 8), 4); - if (_225 === 0) { - sliced = rt.strSlice(s, a, b); - } else if (_225 === 1) { - sliced = rt.strSlice(s, a); - } else if (_225 === 2) { - sliced = rt.strSlice(s, 0, b); + if (_8 === 4) { + const keep: rt.usize = rt.usizeAdd(rt.u8Rem(rt.u8Div(kind, 8), 4) as rt.usize, 1); + put(out, rt.optionUnwrapOr(rt.optionMap(rt.iterNth(rt.strMatchIndices(s, "/"), rt.usizeSub(keep, 1)), (_2: [rt.usize, string]): string => { + return rt.strSlice(s, 0, _2[0]); + }), "none")); + const part5 = rt.optionMapOr(rt.iterNth(rt.strMatchIndices(s, "/"), rt.usizeSub(keep, 1)), rt.stringLen(s), (_22: [rt.usize, string]): rt.usize => { + return rt.usizeAdd(_22[0], 1); + }); + put(out, rt.strSlice(s, part5)); + } else if (_8 === 5) { + const parts: string[] = rt.iterToArray(rt.strSplit(s, "/")); + put(out, parts.join(p)); + put(out, rt.iterToArray(rt.iterMap(rt.iterSlice(parts), (part3: string): string => { + return part3; + })).join(p)); + put(out, ["a", "", s].join("/")); } else { - sliced = rt.strSlice(s, 0); + const at2: (a0: rt.u8) => rt.usize = (b2: rt.u8): rt.usize => { + if ((kind & 32) === 0) { + return rt.usizeRem(b2 as rt.usize, rt.usizeAdd(rt.stringLen(s), 2)); + } + return rt.usizeSaturatingSub(rt.usizeAdd(rt.stringLen(s), 1), rt.usizeRem(b2 as rt.usize, 4)); + }; + if (!(data.length >= 2)) { + _209 = [0, 0] as [rt.usize, rt.usize]; + } else { + _209 = [at2(data.data[data.start]), at2(data.data[data.start + 1])] as [rt.usize, rt.usize]; + } + const a: rt.usize = _209[0]; + const b: rt.usize = _209[1]; + const _223: rt.u8 = rt.u8Rem(rt.u8Div(kind, 8), 4); + if (_223 === 0) { + sliced = rt.strSlice(s, a, b); + } else if (_223 === 1) { + sliced = rt.strSlice(s, a); + } else if (_223 === 2) { + sliced = rt.strSlice(s, 0, b); + } else { + sliced = rt.strSlice(s, 0); + } + put(out, sliced); } - put(out, sliced); } } } diff --git a/ts/fixtures/lang/src/iters.rs b/ts/fixtures/lang/src/iters.rs index d63ef607d..a049156ff 100644 --- a/ts/fixtures/lang/src/iters.rs +++ b/ts/fixtures/lang/src/iters.rs @@ -397,6 +397,22 @@ pub fn options(data: &[u8], out: &mut Vec) { }) .collect(); out.extend_from_slice(&counted); + // `successors` calls its closure on each item as it yields it, and never again once + // it returned `None`. + let mut calls = Vec::new(); + let halving = |b: &u8, calls: &mut Vec| { + calls.push(*b); + b.checked_sub(1).map(|b| b / 2) + }; + let first = data.first().copied(); + let taken: Vec = std::iter::successors(first, |b| halving(b, &mut calls)) + .take(data.len() % 4) + .collect(); + out.extend_from_slice(&taken); + out.extend_from_slice(&calls); + let count = std::iter::successors(first, |b| halving(b, &mut calls)).count(); + out.push(count as u8); + out.extend_from_slice(&calls); // The crate's own iterator, whose items are options. let gaps = Gaps { data, at: 0 }; for gap in gaps { diff --git a/ts/fixtures/lang/src/shapes.rs b/ts/fixtures/lang/src/shapes.rs index 6b1e98798..622337e69 100644 --- a/ts/fixtures/lang/src/shapes.rs +++ b/ts/fixtures/lang/src/shapes.rs @@ -1471,6 +1471,37 @@ pub fn library(data: &[u8], out: &mut Vec) { } Err(_) => out.push(0), } + + // `then` calls its closure only when true, a function as well as a closure. + let mut calls = 0u8; + for &x in data.iter().take(6) { + let half = (x % 2 == 0).then(|| { + calls += 1; + x / 2 + }); + out.extend_from_slice(&[half.unwrap_or(0xee), calls]); + let empty = (x > 0x80).then(Vec::::new); + out.push(empty.map_or(2, |v| v.len() as u8)); + let flag = x % 3 == 0; + out.push(u8::from(flag) + i8::from(flag) as u8 + usize::from(flag) as u8); + out.extend_from_slice(&u64::from(flag).to_be_bytes()); + out.extend_from_slice(&u128::from(flag).to_be_bytes()); + out.extend_from_slice(&i64::from(flag).to_be_bytes()); + } + + // `ok_or` of a local TypeScript sees is `undefined`. + let none: Option = None; + match none.ok_or(a) { + Ok(n) => out.extend_from_slice(&[5, n]), + Err(e) => out.extend_from_slice(&[6, e]), + } + const NONE: Option = None; + for result in [None.ok_or(b), NONE.ok_or(a)] { + match result { + Ok(n) => out.extend_from_slice(&[5, n]), + Err(e) => out.extend_from_slice(&[6, e]), + } + } } /// Steps a total by `x`: breaks with what it can't take, or continues with the new total. diff --git a/ts/fixtures/lang/src/strs.rs b/ts/fixtures/lang/src/strs.rs index becee85b3..3dae639d1 100644 --- a/ts/fixtures/lang/src/strs.rs +++ b/ts/fixtures/lang/src/strs.rs @@ -275,13 +275,13 @@ pub fn slices(kind: u8, data: &[u8], out: &mut Vec) { put_offset(out, s.rfind(p.as_str())); } 3 => { - // Each parent of a path, as moq-net's discovery walks them. - let mut path = s.as_str(); - while let Some(slash) = path.rfind('/') { - path = &path[..slash]; + // A path and each parent of it, as moq-net's discovery walks them. + let covering = std::iter::successors(Some(s.as_str()), |path| { + (!path.is_empty()).then(|| path.rfind('/').map_or("", |slash| &path[..slash])) + }); + for path in covering { put(out, path); } - put(out, path); } 4 => { // A path cut after its `keep`th part, and the rest, as moq-net's compression diff --git a/ts/fixtures/task/expected/drops.ts b/ts/fixtures/task/expected/drops.ts index f20d7d77c..587efbba1 100644 --- a/ts/fixtures/task/expected/drops.ts +++ b/ts/fixtures/task/expected/drops.ts @@ -105,7 +105,7 @@ export function run(log: Log.Log, id: rt.u32, arg: rt.u8): void { } else if (_13 === 0) { v = []; unwind = 197; - const _19: Held.Held = { tag: "One", _0: guard(1) }; + const _19 = { tag: "One", _0: guard(1) } as Held.Held; v.push(_19); v.push({ tag: "Empty" } as Held.Held); _29 = guard(2); @@ -129,7 +129,7 @@ export function run(log: Log.Log, id: rt.u32, arg: rt.u8): void { check(2); v2 = []; unwind = 193; - const _67: Held.Held = { tag: "One", _0: guard(5) }; + const _67 = { tag: "One", _0: guard(5) } as Held.Held; v2.push(_67); unwind = 194; mutex = new rt.Mutex(v2); @@ -540,7 +540,7 @@ export function bail(log: Log.Log, id: rt.u32, at: rt.u8): rt.Result 8)) { _217 = true; - _18 = undefined; + _18 = undefined as Guard.Guard | undefined; } else { unwind = 113; const _20: Guard.Guard = guard(0); @@ -1638,7 +1638,7 @@ export function options(log: Log.Log, id: rt.u32, at: rt.u8): void { unwind = 120; _216 = false; _215 = true; - _92 = undefined; + _92 = undefined as Guard.Guard | undefined; const _93: Guard.Guard = guard(9); _215 = false; fallback = rt.optionUnwrapOr(_92, _93, rt.drop); @@ -1678,7 +1678,7 @@ export function options(log: Log.Log, id: rt.u32, at: rt.u8): void { return Guard.new(rt.u32Add(n2, 15), Log.clone$Clone(log)); }, rt.drop); unwind = 104; - unmapped = rt.optionMapOr(undefined, guard(16), (n3: rt.u32): Guard.Guard => { + unmapped = rt.optionMapOr(undefined as rt.u32 | undefined, guard(16), (n3: rt.u32): Guard.Guard => { return Guard.new(n3, Log.clone$Clone(log)); }, rt.drop); unwind = 103; @@ -1945,7 +1945,7 @@ export function arcs(log: Log.Log, id: rt.u32, at: rt.u8): void { check(3); tags = rt.btreeNew(rt.cmp) as rt.BTreeMap; unwind = 88; - const _123: Held.Held = { tag: "One", _0: Guard.new(rt.u32Add(id, 1), Log.clone$Clone(log)) }; + const _123 = { tag: "One", _0: Guard.new(rt.u32Add(id, 1), Log.clone$Clone(log)) } as Held.Held; drop$Option_Held(rt.btreeInsert(tags, 1, _123, undefined, drop$Held)); const place2 = rt.entryOrInsertMut(rt.entry(tags, 1), { tag: "One", _0: Guard.new(rt.u32Add(id, 2), Log.clone$Clone(log)) } as Held.Held, drop$Held); if (place2.v.tag === "One") { diff --git a/ts/runtime/rt.ts b/ts/runtime/rt.ts index 1d32dbcf8..00dbb0840 100644 --- a/ts/runtime/rt.ts +++ b/ts/runtime/rt.ts @@ -198,6 +198,14 @@ export function thenSome(cond: boolean, value: T, drop?: (value: T) => void): return undefined; } +// 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 { + if (cond) return f(); + drop?.(f); + return undefined; +} + // Slices: arrays, but for bytes, which are `ByteSlice`s. /** A bounds-checked index into a slice. */ diff --git a/ts/runtime/rt/iter.ts b/ts/runtime/rt/iter.ts index 4bc45aaa4..8e022b989 100644 --- a/ts/runtime/rt/iter.ts +++ b/ts/runtime/rt/iter.ts @@ -87,6 +87,19 @@ export function iterFromFn(f: () => T | undefined): Iter { return new FromNext(f, (f) => iterFromOption(f())); } +/** `iter::successors`: `first`, then `succ` of each item, called as that item is yielded. */ +export function iterSuccessors(first: T | undefined, succ: (item: T) => T | undefined): Iter { + let next = first; + return iterFromFn(() => { + const item = next; + if (item === undefined) return undefined; + // Taken before `succ` runs, so a panic in it leaves the iterator done, as std's. + next = undefined; + next = succ(item); + return item; + }); +} + /** An `impl Iterator` of the translated crate, which `next` calls the `next` of. */ export function iterFrom(source: S, next: (source: S) => T | Done): Iter { return new FromNext(source, next); diff --git a/ts/src/lower/iter.rs b/ts/src/lower/iter.rs index c6c2078fe..143e67f55 100644 --- a/ts/src/lower/iter.rs +++ b/ts/src/lower/iter.rs @@ -218,6 +218,10 @@ pub fn call<'tcx>( } ["iter", "sources", "empty", "empty"] => return Some(Expr::rt("iterEmpty", Vec::new())), ["iter", "sources", "from_fn", "from_fn"] => return Some(Expr::rt("iterFromFn", args)), + ["iter", "sources", "successors", "successors"] => { + no_drops(b, targ(0)?, span); + return Some(Expr::rt("iterSuccessors", args)); + } _ => {} } diff --git a/ts/src/lower/library.rs b/ts/src/lower/library.rs index 74279f234..3c8cde5a3 100644 --- a/ts/src/lower/library.rs +++ b/ts/src/lower/library.rs @@ -237,7 +237,7 @@ pub fn std_path(tcx: TyCtxt<'_>, did: DefId) -> Vec { .collect() } -fn is_std(tcx: TyCtxt<'_>, did: DefId) -> bool { +pub fn is_std(tcx: TyCtxt<'_>, did: DefId) -> bool { matches!(tcx.crate_name(did.krate).as_str(), "core" | "alloc" | "std") } @@ -402,7 +402,9 @@ fn discards(owner: &str, name: &str) -> Option<&'static [usize]> { // `inspect_err` its error if its closure panics. ("Result", "unwrap" | "expect" | "inspect_err") => &[1], ("Result", "unwrap_err") => &[0], + // `then_some::` drops its value when false, and `then::` its function. ("bool", "then_some") => &[0], + ("bool", "then") => &[1], _ => return None, }) } @@ -523,9 +525,10 @@ pub fn call<'tcx>(b: &mut Body<'_, '_, 'tcx>, call: &Call<'tcx>, args: Vec if to == from { return Some(value); } - // Each `From` between integers and `char` keeps the value, as `as` then does. + // Each `From` between integers and `char`, or from `bool`, keeps the value, as + // `as` then does. let scalar = |t: Ty<'tcx>| Int::of(t).is_some() || t.is_char(); - if !scalar(to) || !scalar(from) { + if !scalar(to) || !(scalar(from) || from.is_bool()) { return None; } let operand = Operand { @@ -682,6 +685,7 @@ pub fn call<'tcx>(b: &mut Body<'_, '_, 'tcx>, call: &Call<'tcx>, args: Vec let values = args; Expr::rt("thenSome", values) } + ("bool", "then") => Expr::rt("boolThen", args), // Slices, whose byte functions take an `rt.ByteSlice`. ("slice", "len") => args.pop()?.member("length"), diff --git a/ts/src/lower/mir/call.rs b/ts/src/lower/mir/call.rs index 84758c547..1f21f0f4b 100644 --- a/ts/src/lower/mir/call.rs +++ b/ts/src/lower/mir/call.rs @@ -152,6 +152,12 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { for arg in args { let value = self.operand_frozen(&arg.node, span, out); values.push(match &arg.node { + // Its type, rather than its variant's, for std's runtime function to infer + // its type parameters from, as `widen` gives a local's. + Operand::Constant(c) if !translated && self.b.t.narrows(c.ty()) => { + let ty = self.b.t.ty(c.ty(), span); + super::whole(value, ty).unwrap_or_else(|value| value) + } Operand::Copy(p) | Operand::Move(p) if self.moved_from(&arg.node).is_none() && self.keeps(translated, args, p.ty(&self.mir.local_decls, self.tcx).ty, result) => diff --git a/ts/src/lower/mir/mod.rs b/ts/src/lower/mir/mod.rs index b074ec263..06a24bc3b 100644 --- a/ts/src/lower/mir/mod.rs +++ b/ts/src/lower/mir/mod.rs @@ -227,7 +227,8 @@ pub struct Fx<'m, 'b, 'a, 'tcx> { /// The paths a switch lowered so far tests the variant of, which `tsc` narrows in /// its arms. switched: FxHashSet, - /// Locals whose variant some switch tests, directly or through a reference to them. + /// Locals whose variant some switch tests, or that std takes, directly or through a + /// reference to them. tested: IndexVec, /// The names of those that hold a value TypeScript would narrow by what it is assigned. widened: FxHashSet, @@ -1113,11 +1114,19 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { } } - /// Mark the locals whose variant a switch tests, or that a closure or async block - /// captures and could test, and what references to them borrow. + /// Mark the locals whose variant a switch tests, that a closure or async block + /// captures and could test, or that std takes, whose runtime function infers its type + /// parameters from them, and what references to them borrow. fn find_tested(&mut self) { + let tcx = self.tcx; let mut work = Vec::new(); for data in self.mir.basic_blocks.iter() { + if let TerminatorKind::Call { func, args, .. } = &data.terminator().kind + && let ty::FnDef(def_id, _) = *func.ty(&self.mir.local_decls, tcx).kind() + && crate::lower::library::is_std(tcx, def_id) + { + work.extend(args.iter().filter_map(|arg| arg.node.place().map(|p| p.local))); + } for stmt in &data.statements { let StatementKind::Assign(assign) = &stmt.kind else { continue; @@ -1154,9 +1163,10 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { } /// `value`, the `rvalue` assigned to `place`, as a value of its whole type where - /// something tests `place`'s variant: TypeScript narrows a variable to the type of - /// what it was assigned, as a `Poll` made `Ready` to its payload's, and would refuse a - /// test for another variant. + /// something tests `place`'s variant or std takes it: TypeScript narrows a variable to + /// the type of what it was assigned, as a `Poll` made `Ready` to its payload's, and + /// would refuse a test for another variant, or infer a `None`'s `Option` as of + /// `T = undefined`. fn widen(&mut self, place: Place<'tcx>, rvalue: &Rvalue<'tcx>, value: Expr, span: Span) -> Expr { let ty = place.ty(&self.mir.local_decls, self.tcx).ty; // What is made here, rather than read from a place declared with its type. @@ -1180,15 +1190,9 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { Some(local) => self.decl_ty(local), None => self.b.t.ty(ty, span), }; - let mut inner = &value; - while let Expr::Located(_, e) = inner { - inner = e; - } - let value = match inner { - Expr::As(_, t) if *t == ts_ty => value, - // `Ok` and `Err` are of the whole type. - Expr::Call(f, _) if matches!(&**f, Expr::Rt(n) if n == "Ok" || n == "Err") => return value, - _ => Expr::As(Box::new(value.typed()), ts_ty), + let value = match whole(value, ts_ty) { + Ok(value) => value, + Err(value) => return value, }; // Its declaration keeps the `as`. if let Some(local) = local @@ -2643,6 +2647,20 @@ impl<'tcx> Fx<'_, '_, '_, 'tcx> { /// Whether `place` is part of its local's own value, rather than reached through a /// reference or pointer. +/// `value` as a value of `ty`, its JS union type: `Ok` of it, or `Err` of an `Ok` or `Err`, +/// which TypeScript types as the whole union already. +pub(super) fn whole(value: Expr, ty: Type) -> Result { + let mut inner = &value; + while let Expr::Located(_, e) = inner { + inner = e; + } + match inner { + Expr::As(_, t) if *t == ty => Ok(value), + Expr::Call(f, _) if matches!(&**f, Expr::Rt(n) if n == "Ok" || n == "Err") => Err(value), + _ => Ok(Expr::As(Box::new(value.typed()), ty)), + } +} + pub(super) fn own(place: Place<'_>) -> bool { place .projection From 3eb03f3215878c09cb42869bdda11593c0c0dbd6 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Mon, 26 Oct 2026 23:52:55 -0700 Subject: [PATCH 3/4] chore(quest): finish ts-std-gaps Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- quest/m2/README.md | 1 - quest/m2/ts-std-gaps.md | 19 ------------------- 2 files changed, 20 deletions(-) delete mode 100644 quest/m2/ts-std-gaps.md diff --git a/quest/m2/README.md b/quest/m2/README.md index e0a6fa9ad..9cb151105 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -35,7 +35,6 @@ The fork's feature quests folded into the [exporter](/quest/m1/ffi-export.md); - [Shared ownership](/quest/m2/ts-shared.md) - `Arc`/`Rc`/`Weak` keep exact counts, and locks and atomics become plain values - [Share what the crate never changes in place](/quest/m2/ts-shared-read.md) - copies of `Copy` aggregates no one writes into in place go away, recovering #83's decode cost - [Matches TypeScript has already narrowed](/quest/m2/ts-narrow-test.md) - an inner match on a value an outer match decided passes `tsc` -- [Small std gaps](/quest/m2/ts-std-gaps.md) - `bool::then`, `iter::successors`, `usize::from(bool)`, and `None.ok_or` narrowing - [Copy-on-write strings](/quest/m2/ts-cow-str.md) - `Cow` translates, finishing `ietf/namespace.rs` - [Translate moq-net's model to TypeScript](/quest/m2/ts-model.md) - the driver grows from moq-net's codec to its model, for moq to compare against WASM - [Waiting: Swift 6.4 in nixpkgs-unstable](/quest/m2/uniffi-swift-arm.md) - the nightly UniFFI fixtures move back to arm diff --git a/quest/m2/ts-std-gaps.md b/quest/m2/ts-std-gaps.md deleted file mode 100644 index 1db902cb2..000000000 --- a/quest/m2/ts-std-gaps.md +++ /dev/null @@ -1,19 +0,0 @@ -# [S] Small std gaps - -## Goal - -rust2-ts translates `bool::then`, `bool::then_some`, `iter::successors`, and -`usize::from(bool)`, and `None.ok_or(e)` passes `tsc` where TypeScript narrowed the -value to `undefined`, so `discovery.rs`'s parent walk translates, proven by differential -tests against native Rust. - -## Plan - -Found by [#87](https://github.com/kixelated/rust2/pull/87) and -[#106](https://github.com/kixelated/rust2/pull/106) on 2026-10-27. - -- `then` calls its closure only when true, and `successors` calls its closure only for - the next item it yields; cover both laziness points in the differential tests. -- Report moq-net's census before and after. - -Public API: the translator's output. From 911485b13b27950d58473b2b0836627a1c279fe6 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Tue, 27 Oct 2026 00:03:15 -0700 Subject: [PATCH 4/4] fix(ts): refuse successors and from_fn closures that own a value that drops Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- ts/fixtures/reject/census.md | 41 ++++++++++++++++++--------------- ts/fixtures/reject/expected.txt | 3 +++ ts/fixtures/reject/src/lib.rs | 10 ++++++++ ts/fixtures/reject/ts.toml | 1 + ts/src/lower/iter.rs | 7 +++++- 5 files changed, 43 insertions(+), 19 deletions(-) diff --git a/ts/fixtures/reject/census.md b/ts/fixtures/reject/census.md index 24c84fff1..fd9d8fdd6 100644 --- a/ts/fixtures/reject/census.md +++ b/ts/fixtures/reject/census.md @@ -1,7 +1,7 @@ # Census of `reject-fixture` -- Refusals: 74 -- Function instances: 61 +- Refusals: 77 +- Function instances: 62 - Generic functions no instance reached, lowered over their type parameters: 2 ## By construct @@ -12,13 +12,14 @@ | 7 | unsupported call to `_` | fixtures/reject/src/lib.rs:32:4 | | 6 | `_` is not supported: its `None` and `Some` would both be `undefined` | fixtures/reject/src/lib.rs:27:15 | | 6 | `_`: a JS value of `_` is a copy, so a reference can't change it | fixtures/reject/src/lib.rs:15:13 | +| 5 | an iterator that owns `_`, which drops, which is not supported yet | fixtures/reject/src/lib.rs:55:2 | | 4 | replacing the value behind a reference to `_` | fixtures/reject/src/lib.rs:107:2 | | 4 | unsupported type `_` | fixtures/reject/src/lib.rs:6:14 | -| 3 | an iterator that owns `_`, which drops, which is not supported yet | fixtures/reject/src/lib.rs:55:2 | | 2 | `_` is not supported: it would be pending two ways with one value | fixtures/reject/src/lib.rs:127:20 | | 2 | a field of `_` | fixtures/reject/src/lib.rs:289:7 | | 2 | an iterator of the crate implementing `_`, which is not supported yet | fixtures/reject/src/lib.rs:186:1 | | 2 | constructing `_` from another crate | fixtures/reject/src/lib.rs:172:23 | +| 2 | dropping a closure that owns a value that drops, which is not supported yet | fixtures/reject/src/lib.rs:50:1 | | 2 | overriding `_`, which std's adaptors call in place of `next` or beside it | fixtures/reject/src/lib.rs:166:2 | | 1 | `Binary` formatting, as in `{:b}`, is not supported | fixtures/reject/src/lib.rs:84:3 | | 1 | `LowerExp` formatting, as in `{:e}`, is not supported | fixtures/reject/src/lib.rs:86:3 | @@ -42,7 +43,6 @@ | 1 | a zero-sized constant of type `_` | fixtures/reject/src/lib.rs:74:2 | | 1 | arithmetic on `_` | fixtures/reject/src/lib.rs:7:2 | | 1 | comparing `_` as a key, which is not supported yet | fixtures/reject/src/lib.rs:299:52 | -| 1 | dropping a closure that owns a value that drops, which is not supported yet | fixtures/reject/src/lib.rs:50:1 | | 1 | hashing `_` as a key, which is not supported yet | fixtures/reject/src/lib.rs:299:52 | | 1 | hex `Debug`, as in `{:x?}`, is not supported | fixtures/reject/src/lib.rs:87:3 | | 1 | the `+` flag of a format spec is not supported | fixtures/reject/src/lib.rs:82:3 | @@ -68,6 +68,7 @@ The type or module each refused type or path belongs to. | 2 | `std::option::Option` | fixtures/reject/src/lib.rs:294:6 | | 2 | `std::pin::Pin` | fixtures/reject/src/lib.rs:143:2 | | 2 | `std::slice` | fixtures/reject/src/lib.rs:32:4 | +| 2 | `{closure}` | fixtures/reject/src/lib.rs:391:15 | | 1 | `DoubleEndedIterator` | fixtures/reject/src/lib.rs:186:1 | | 1 | `FusedIterator` | fixtures/reject/src/lib.rs:240:1 | | 1 | `HashMap` | fixtures/reject/src/lib.rs:317:6 | @@ -102,6 +103,7 @@ The type or module each refused type or path belongs to. | 2 | an assignment through a reference to `_` | `std::pin::Pin` | fixtures/reject/src/lib.rs:143:2 | | 2 | an assignment through a reference to `_` | `u8` | fixtures/reject/src/lib.rs:65:3 | | 2 | an iterator that owns `_`, which drops, which is not supported yet | `std::vec::Vec` | fixtures/reject/src/lib.rs:55:2 | +| 2 | an iterator that owns `_`, which drops, which is not supported yet | `{closure}` | fixtures/reject/src/lib.rs:391:15 | | 2 | replacing the value behind a reference to `_` | `Meters` | fixtures/reject/src/lib.rs:107:2 | | 1 | `_` holds `_`, which is `undefined` as `None` is, which is not supported | `std::collections::HashMap`, `std::option::Option` | fixtures/reject/src/lib.rs:294:6 | | 1 | `_` of `_` by `_`, which is not supported yet | `HashMap::contains_key`, `std::collections::HashMap`, `u8` | fixtures/reject/src/lib.rs:317:6 | @@ -189,6 +191,16 @@ The type or module each refused type or path belongs to. +
an iterator that owns `_`, which drops, which is not supported yet (5) + +- fixtures/reject/src/lib.rs:55:2: an iterator that owns `std::vec::Vec`, which drops, which is not supported yet +- fixtures/reject/src/lib.rs:248:4: an iterator that owns `std::vec::Vec`, which drops, which is not supported yet +- fixtures/reject/src/lib.rs:253:20: an iterator that owns `Noisy`, which drops, which is not supported yet +- fixtures/reject/src/lib.rs:391:15: an iterator that owns `{closure@fixtures/reject/src/lib.rs:391:48: 391:56}`, which drops, which is not supported yet +- fixtures/reject/src/lib.rs:392:13: an iterator that owns `{closure@fixtures/reject/src/lib.rs:392:32: 392:39}`, which drops, which is not supported yet + +
+
replacing the value behind a reference to `_` (4) - fixtures/reject/src/lib.rs:107:2: replacing the value behind a reference to `Meters` @@ -207,14 +219,6 @@ The type or module each refused type or path belongs to.
-
an iterator that owns `_`, which drops, which is not supported yet (3) - -- fixtures/reject/src/lib.rs:55:2: an iterator that owns `std::vec::Vec`, which drops, which is not supported yet -- fixtures/reject/src/lib.rs:248:4: an iterator that owns `std::vec::Vec`, which drops, which is not supported yet -- fixtures/reject/src/lib.rs:253:20: an iterator that owns `Noisy`, which drops, which is not supported yet - -
-
`_` is not supported: it would be pending two ways with one value (2) - fixtures/reject/src/lib.rs:127:20: `Poll>>` is not supported: it would be pending two ways with one value @@ -243,6 +247,13 @@ The type or module each refused type or path belongs to.
+
dropping a closure that owns a value that drops, which is not supported yet (2) + +- fixtures/reject/src/lib.rs:50:1: dropping a closure that owns a value that drops, which is not supported yet +- fixtures/reject/src/lib.rs:389:18: dropping a closure that owns a value that drops, which is not supported yet + +
+
overriding `_`, which std's adaptors call in place of `next` or beside it (2) - fixtures/reject/src/lib.rs:166:2: overriding `Iterator::fold`, which std's adaptors call in place of `next` or beside it @@ -382,12 +393,6 @@ The type or module each refused type or path belongs to.
-
dropping a closure that owns a value that drops, which is not supported yet (1) - -- fixtures/reject/src/lib.rs:50:1: dropping a closure that owns a value that drops, which is not supported yet - -
-
hashing `_` as a key, which is not supported yet (1) - fixtures/reject/src/lib.rs:299:52: hashing `std::vec::Vec` as a key, which is not supported yet diff --git a/ts/fixtures/reject/expected.txt b/ts/fixtures/reject/expected.txt index 582e45745..4fd0f97df 100644 --- a/ts/fixtures/reject/expected.txt +++ b/ts/fixtures/reject/expected.txt @@ -64,6 +64,9 @@ a coercion to `std::sync::Arc<[u8]>` at fixtures/reject/src/lib.rs:372:2 `&mut std::string::String`: a JS value of `std::string::String` is a copy, so a reference can't change it at fixtures/reject/src/lib.rs:376:15 replacing the value behind a reference to `std::string::String` at fixtures/reject/src/lib.rs:377:4 unsupported call to `core::str::::split::<[char; 2]>` at fixtures/reject/src/lib.rs:383:4 +dropping a closure that owns a value that drops, which is not supported yet at fixtures/reject/src/lib.rs:389:18 +an iterator that owns `{closure@fixtures/reject/src/lib.rs:391:48: 391:56}`, which drops, which is not supported yet at fixtures/reject/src/lib.rs:391:15 +an iterator that owns `{closure@fixtures/reject/src/lib.rs:392:32: 392:39}`, which drops, which is not supported yet at fixtures/reject/src/lib.rs:392:13 overriding `Iterator::fold`, which std's adaptors call in place of `next` or beside it at fixtures/reject/src/lib.rs:166:2 an iterator of the crate implementing `DoubleEndedIterator`, which is not supported yet at fixtures/reject/src/lib.rs:186:1 overriding `Iterator::size_hint`, which std's adaptors call in place of `next` or beside it at fixtures/reject/src/lib.rs:220:2 diff --git a/ts/fixtures/reject/src/lib.rs b/ts/fixtures/reject/src/lib.rs index a239af3d4..e16191f78 100644 --- a/ts/fixtures/reject/src/lib.rs +++ b/ts/fixtures/reject/src/lib.rs @@ -382,3 +382,13 @@ pub fn pushed(s: &mut String) { pub fn split_chars(s: &str) -> usize { s.split(['a', 'b']).count() } + +/// Each drops its closure, and the `Noisy` it owns, when it is done with it. +pub fn owning_lazy(cond: bool) -> usize { + let (a, b, c) = (Noisy(1), Noisy(2), Noisy(3)); + let then = cond.then(move || a).is_some() as usize; + // Through a reference, each owns its `Noisy` whole rather than a copy of its field. + let walked = std::iter::successors(Some(0u8), move |_| Some((&b).0)).take(2).count(); + let made = std::iter::from_fn(move || Some((&c).0)).take(2).count(); + then + walked + made +} diff --git a/ts/fixtures/reject/ts.toml b/ts/fixtures/reject/ts.toml index d055212eb..b0cb1f00e 100644 --- a/ts/fixtures/reject/ts.toml +++ b/ts/fixtures/reject/ts.toml @@ -46,6 +46,7 @@ roots = [ "shared", "pushed", "split_chars", + "owning_lazy", "missing", ] printable = ["Meters"] diff --git a/ts/src/lower/iter.rs b/ts/src/lower/iter.rs index 143e67f55..af8a82354 100644 --- a/ts/src/lower/iter.rs +++ b/ts/src/lower/iter.rs @@ -217,9 +217,14 @@ pub fn call<'tcx>( return Some(Expr::rt("iterOnce", args)); } ["iter", "sources", "empty", "empty"] => return Some(Expr::rt("iterEmpty", Vec::new())), - ["iter", "sources", "from_fn", "from_fn"] => return Some(Expr::rt("iterFromFn", args)), + // Each owns its closure, `F`, which it drops with itself. + ["iter", "sources", "from_fn", "from_fn"] => { + no_drops(b, targ(1)?, span); + return Some(Expr::rt("iterFromFn", args)); + } ["iter", "sources", "successors", "successors"] => { no_drops(b, targ(0)?, span); + no_drops(b, targ(1)?, span); return Some(Expr::rt("iterSuccessors", args)); } _ => {}