Skip to content

feat(ts): fold a match a bool decided - #251

Merged
kixelated merged 3 commits into
mainfrom
quest/m3/ts-fold-bool
Oct 30, 2026
Merged

kixelated merged 3 commits into
mainfrom
quest/m3/ts-fold-bool

Conversation

@kixelated

@kixelated kixelated commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

#175 folds a match that an enclosing match, if, or guard already decided. Its dataflow in ts/src/lower/mir/decided.rs knew nothing about a bool. So a match that a bool decided kept its dead arms: if matches!(s, Shape::Dot) { match s { .. } }, where the bool joins two edges before the if, and if s == Shape::Dot { match s { .. } } with a derived PartialEq. Arms dropped this way could also widen a payload read. Closes the quest quest/m3/ts-fold-bool.md.

Approach

The dataflow now tracks, for each bool local a switch tests, the variants each of its values leaves a place:

  • b = const true/false records the value. Where two ways in join with different values, each way's known variants become what that value implies. A way that knows nothing of the place leaves its value's side open, so x && matches!(s, Rect) still decides the true side.
  • b = !c and b = c carry c's implications, swapped for !.
  • b = <E as PartialEq>::eq(&x, &y) or ne, when E is an enum whose PartialEq is #[automatically_derived]: on the equal side, both places hold a variant the other can hold. On the unequal side, a place is not the other's variant only when that variant is fieldless. The other side's variant also comes from a constant (== Shape::Dot promotes one) or from an enum a local's single write builds.
  • A switch on b narrows each place on each edge. An edge it can't take (the bool known to hold the other value, or a place left with no variant) folds away, just as a variant switch's untaken edge does. That keeps the CFG and the narrowing pass in agreement.
  • A bool that a raw pointer or two-phase borrow can write (escaped) is never tracked. The new fixture case shows the differential failing without this check.

A fold that drops arms while the switch's default is unreachable now also moves the last kept arm into the default, as it already did where no known variant reaches the default. Before, the lowering emitted that untested last arm on a type TypeScript had narrowed less than the narrowing pass assumed, which caused tsc errors like Property 'width' does not exist on type 'Dot | Rect'.

Output size

  • moq-net (just ts moq at the pinned revision): byte-identical before and after (610,242 bytes), and still type-checks.
  • Bench: the codec and task fixtures' snapshots don't change, so the bundles just ts bench measures are the same and there is nothing to time.
  • Lang fixture (diff of the second commit on the first's snapshot): shapes.ts 75,296 to 74,710 bytes, dyns.ts 30,762 to 30,731 bytes, +18/-64 lines. The new cases lose their dead arms. rematched's if order == Ordering::Less { match order { .. } } folds through Ordering's derived PartialEq and loses its widening cast. dyns::anys folds a drop-flag bool switch, which leaves typed with one reader, so the lowering holds it back as part7.
  • Translation time: the dataflow's own time across moq-net's translation went from about 26 ms to about 40 ms (instrumented locally, not kept), out of a 10 to 14 s translation.

Tests

The new shapes::decided_by_bool in the lang fixture is differential against native wasm32 and type-checked. The first commit adds it with the base translator's snapshot, so the second commit's snapshot diff is the shrink. It covers:

  • matches! with one variant and with two, !matches!, and a payload read from the arm left,
  • x && matches!(..), where one way in knows nothing of the place,
  • == Shape::Dot and != Shape::Dot (fieldless), == and != against a built Line (!= must keep Line's arm), and == of two places, one known by an if let,
  • what must not fold: the place changed after the test, the bool negated on one way in, a bool carried around a loop from the place's previous value, and a bool written through a raw pointer after its test.

just ci passes locally.

Impact

  • Translator output: fewer dead arms where a bool decided a match. Same behavior, verified by the differential test.
  • No public API change.

Alternatives

  • Track enum aggregates and constants as known facts for every place: tried it. It also folded matches on a value built in sight (the known fixture) into payload reads TypeScript hadn't narrowed, which tsc rejects. They now feed only the == operands.
  • Trust only is_builtin_derived: it answers only for the local crate, so dependencies' derived impls (moq's enums from another crate, std's Ordering) would never fold. is_automatically_derived matches what collections::derived_eq already trusts.

Choices made without the maintainer

  • Accept any #[automatically_derived] PartialEq on an enum, std's included, not only the crate's own.
  • Fold untakable bool edges, such as drop flags known at a point, beyond the quest's matches! and ==. This is required to keep the narrowing pass sound, and it only shrinks output.

Follow-ups

None.

Review

Round 1: Codex (quest review), gpt-6.1-sol, medium effort, on head 05de1abcc489a6a2fb4f82f9ccace58761e34b4f against origin/main (e51ecada). No findings.

(written by Claude Opus 5.5)

🤖 Generated with Claude Code

kixelated and others added 3 commits October 29, 2026 21:10
Quest-Claim: c9858410a12a4079
Record, per `bool` local, which variants each of its values leaves a place:
from `const` assignments joined before the switch on it (`matches!`), its
copies and negations, and an enum's derived `PartialEq::eq`/`ne`. A switch on
the `bool` narrows the place on each edge, and an edge it can't take folds
away as a variant switch's does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated kixelated changed the title Fold a match a bool decided feat(ts): fold a match a bool decided Oct 30, 2026
@kixelated
kixelated marked this pull request as ready for review October 30, 2026 05:06
@coderabbitai

coderabbitai Bot commented Oct 30, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 2 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 3ff3eddf-aac1-4e26-a84e-b855416c1d7a
Commits

Reviewing files that changed from the base of the PR and between e51ecad and 05de1ab.

Files selected for processing: 7
  • quest/m3/README.md
  • quest/m3/ts-fold-bool.md
  • ts/fixtures/lang/expected/dyns.ts
  • ts/fixtures/lang/expected/shapes.Shape.ts
  • ts/fixtures/lang/expected/shapes.ts
  • ts/fixtures/lang/src/shapes.rs
  • ts/src/lower/mir/decided.rs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@kixelated kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review by review (OpenAI)

Reviewed commit: 05de1ab.

No actionable correctness findings in this review. The boolean implications remain conservative across joins and invalidation; derived equality only excludes a variant on inequality for a singleton fieldless comparator, and escaped booleans are excluded. The fixture exercises changed enum/bool values, raw-pointer writes and loop-carried state, not just successful folds. An independent source review reached the same conclusion.

Direction: extending the existing MIR dataflow is preferable to syntax-specific rewrites or a second narrowing mechanism. Nonblocking coverage suggestion: add a hand-written enum PartialEq that equates different variants, so the negative derived-implementation gate has a direct differential regression.

Limits: static GitHub review of the dataflow, lowering integration and fixture changes. I did not run the translator, TypeScript checks or native differential tests, and did not independently reproduce the reported output-size or translation-time measurements.

(Written by OpenAI)

@kixelated
kixelated merged commit e63f718 into main Oct 30, 2026
2 checks passed
@kixelated
kixelated deleted the quest/m3/ts-fold-bool branch October 30, 2026 05:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant