From 6ff1bde3bb08ac334be1dd961f0144cde1bc6812 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Wed, 28 Oct 2026 06:17:37 -0700 Subject: [PATCH 1/2] chore(quest): claim quest/m3/ts-fold-guards.md Quest-Claim: 85b27edabc213cbc From 889d5926d5a11c18d3e4b2a6c8f806393444c8a2 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Wed, 28 Oct 2026 06:27:34 -0700 Subject: [PATCH 2/2] fix(ts): fold matches an if or guard leaves decided The dataflow behind #110's fold dropped what it knew at a switch on anything but a variant, so a match an enclosing match decided before an `if` or a match guard kept its dead arms, tested with a type-only widening. A switch on a `bool` or integer changes no place, so each edge now keeps what was known. Co-Authored-By: Claude Opus 5.5 --- quest/m3/README.md | 1 - quest/m3/ts-fold-guards.md | 21 ----------- ts/fixtures/lang/expected/shapes.ts | 55 +++++++++++++++++++++++++++++ ts/fixtures/lang/src/shapes.rs | 42 ++++++++++++++++++++++ ts/src/lower/mir/decided.rs | 7 +--- ts/src/lower/mir/mod.rs | 4 +-- 6 files changed, 100 insertions(+), 30 deletions(-) delete mode 100644 quest/m3/ts-fold-guards.md diff --git a/quest/m3/README.md b/quest/m3/README.md index 6f3c2d093..07fe0783e 100644 --- a/quest/m3/README.md +++ b/quest/m3/README.md @@ -19,7 +19,6 @@ generic std gap is planned here even when moq never calls it. - [f32](/quest/m3/ts-f32.md) - `f32` and `Duration`'s `f32` methods translate as `f64` does - [Trait objects](/quest/m3/ts-dyn.md) - `dyn Trait` and `Box` translate, so a consumer neither rewrites nor binds them - [Waiting: type-check in process with tsrs](/quest/m3/ts-tsrs.md) - rust2-ts checks bindings through tsrs instead of `tsc` once it is a stable library -- [Fold matches across tests that aren't variant switches](/quest/m3/ts-fold-guards.md) - a match an `if` or guard decided drops its dead arms - [An inclusive range drained by a fold](/quest/m3/ts-range-fold.md) - a folded `RangeInclusive` ends in core's state - [bool's combinators from core's MIR](/quest/m3/ts-bool-core.md) - `bool::then` lowers from core, retiring `rt.boolThen` - [Binding gaps](/quest/m3/ts-bind-gaps.md) - bound types as keys, unit returns checked, renamed dependencies diff --git a/quest/m3/ts-fold-guards.md b/quest/m3/ts-fold-guards.md deleted file mode 100644 index 8efdbede9..000000000 --- a/quest/m3/ts-fold-guards.md +++ /dev/null @@ -1,21 +0,0 @@ -# [S] Fold matches across tests that aren't variant switches - -## Goal - -rust2-ts folds a match that an enclosing test already decided when that test is an `if` -or a match guard, as [#110](https://github.com/kixelated/rust2/pull/110) does for a -switch on a variant, so the output drops the dead arms rather than widening them, proven -by differential tests against native Rust. - -## Plan - -Found by [#118](https://github.com/kixelated/rust2/pull/118) on 2026-10-27: the dataflow -in `ts/src/lower/mir/decided.rs` forgets what it knows at a switch on anything but a -variant, so a match decided before an `if` gets a type-only widening instead of folding. -`tsc` accepts it; this only shrinks the output. - -- Fix the doc comments in `ts/src/lower/mir/mod.rs` while there: `whole()` sits between - `own()`'s doc comment and `own()`. -- Measure the output size on the [TS benchmark](/ts/README.md#bench). - -Public API: the translator's output. diff --git a/ts/fixtures/lang/expected/shapes.ts b/ts/fixtures/lang/expected/shapes.ts index ec450c6bd..4cbd63d7c 100644 --- a/ts/fixtures/lang/expected/shapes.ts +++ b/ts/fixtures/lang/expected/shapes.ts @@ -1659,6 +1659,10 @@ export function rematched(data: rt.ByteSlice, out: rt.ByteVec): void { let _127!: rt.u8; let _131!: Shape.Shape; let _189!: rt.u8; + let _192!: rt.u8; + let lift!: rt.u8; + let _206!: rt.u8; + let _226!: rt.u8; const s: Shape.Shape = shape(data); if (s.tag === "Dot") { _5 = 2; @@ -1820,6 +1824,57 @@ export function rematched(data: rt.ByteSlice, out: rt.ByteVec): void { } rt.byteVecPush(out, _189); } + if (s.tag === "Line") { + const from3: Point.Point = s._0; + if (!(data.length > 2)) { + lift = 2; + } else { + lift = 1; + } + _192 = ((from3.y ^ s._1.x) & 0xff) ^ lift; + } else { + _192 = 49; + } + rt.byteVecPush(out, _192); + b30: { + if (s.tag === "Dot") { + if (!(data.length > 1)) { + _206 = 53; + break b30; + } + _206 = 51; + break b30; + } + if (s.tag === "Rect") { + if (s.width > 300) { + _206 = s.height & 0xff; + break b30; + } + } + _206 = 55; + } + rt.byteVecPush(out, _206); + b32: { + let w: Shape.Shape = s; + if (w.tag === "Dot") { + let i2: rt.usize = 0; + while (true) { + if (!(i2 < rt.min(data.length, 3))) { + break b32; + } + if (w.tag === "Dot") { + w = shape(rt.byteSliceFrom(Uint8Array.of(1))); + _226 = 56; + } else if (w.tag === "Line") { + _226 = 57; + } else { + _226 = 58; + } + rt.byteVecPush(out, _226); + i2 = rt.usizeAdd(i2, 1); + } + } + } reseated(data, out); } diff --git a/ts/fixtures/lang/src/shapes.rs b/ts/fixtures/lang/src/shapes.rs index bc56bca64..93bfcf01c 100644 --- a/ts/fixtures/lang/src/shapes.rs +++ b/ts/fixtures/lang/src/shapes.rs @@ -1604,6 +1604,48 @@ fn rematched(data: &[u8], out: &mut Vec) { _ => 26, }); } + // An `if` or a guard between the matches keeps what the outer one decided. + out.push(match s { + Shape::Line(from, _) => { + let lift = if data.len() > 2 { 1 } else { 2 }; + match s { + Shape::Line(_, to) => (from.y ^ to.x) as u8 ^ lift, + _ => 48, + } + } + _ => 49, + }); + out.push(match s { + Shape::Dot if data.len() > 1 => match s { + Shape::Line(..) => 50, + _ => 51, + }, + Shape::Rect { width, .. } if width > 300 => match s { + Shape::Rect { height, .. } => height as u8, + _ => 52, + }, + Shape::Dot => match s { + Shape::Dot => 53, + _ => 54, + }, + _ => 55, + }); + // A loop's test keeps only what every way around it knows. + let mut w = s; + if let Shape::Dot = w { + let mut i = 0; + while i < data.len().min(3) { + out.push(match w { + Shape::Dot => { + w = shape(&[1]); + 56 + } + Shape::Line(..) => 57, + _ => 58, + }); + i += 1; + } + } reseated(data, out); } diff --git a/ts/src/lower/mir/decided.rs b/ts/src/lower/mir/decided.rs index 441f88900..00fa2492b 100644 --- a/ts/src/lower/mir/decided.rs +++ b/ts/src/lower/mir/decided.rs @@ -589,13 +589,8 @@ impl<'tcx> Flow<'tcx> { } decided = Some((def, kept, otherwise)); } - // A test of anything else changes nothing. What is known stays with the - // switches on a variant, which fold by it. + // A test of anything else, as an `if` or a match guard, changes nothing. TerminatorKind::SwitchInt { targets, .. } => { - let facts = Facts { - known: Vec::new(), - narrowed: facts.narrowed.clone(), - }; for &to in targets.all_targets() { if !out.iter().any(|(t, _)| *t == to) { out.push((to, facts.clone())); diff --git a/ts/src/lower/mir/mod.rs b/ts/src/lower/mir/mod.rs index dec05bf28..fd4a8b014 100644 --- a/ts/src/lower/mir/mod.rs +++ b/ts/src/lower/mir/mod.rs @@ -3008,8 +3008,6 @@ 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 { @@ -3024,6 +3022,8 @@ pub(super) fn whole(value: Expr, ty: Type) -> Result { } } +/// Whether `place` is part of its local's own value, rather than reached through a +/// reference or pointer. pub(super) fn own(place: Place<'_>) -> bool { place .projection