Skip to content

fix(ts): fold matches an if or guard leaves decided - #175

Merged
kixelated merged 2 commits into
mainfrom
quest/m3/ts-fold-guards
Oct 28, 2026
Merged

kixelated merged 2 commits into
mainfrom
quest/m3/ts-fold-guards

Conversation

@kixelated

@kixelated kixelated commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

#110's fold drops the dead arms of a match an enclosing match already decided. Its dataflow in ts/src/lower/mir/decided.rs forgot everything it knew at a switch on anything but a variant, so an if or a match guard between the two matches undid the fold: the inner match kept its dead arms, and since #118 tested them through a type-only widening ((s as Shape.Shape).tag === "Line"). Found by #118.

Approach

A switch on a bool or an integer writes no place, so each of its edges now keeps what is known, as a Goto does. TypeScript's narrowing already flowed through such switches (#118); now Rust's facts do too. Writes, calls, borrows, and drops forget facts as before, and a loop head still joins what every way in knows.

Also moves own()'s doc comment back onto own() in ts/src/lower/mir/mod.rs, which whole() had split off.

Output size

  • moq-net (just ts moq): byte-identical before and after.
  • 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: the new cases lose their dead arms and widening casts (shapes.ts, rematched).

Tests

Lang fixture, differential against native Rust and type-checked, no new op. rematched gains:

  • an if between a match on Line and a rematch that reads Line's payload,
  • guards (Dot if .., Rect { width, .. } if width > 300) before rematches, including a guard's fallthrough into a later arm on the same variant,
  • a while loop entered with the variant known, whose body changes the place, so its bool test carries facts around the back edge and must forget them there.

On main the same source keeps the inner tests (if (s.tag === "Line") .. else _192 = 48) and widens one ((s as Shape.Shape).tag === "Line"). On this branch they fold.

just ts check, just ts test, and quest check --base origin/main pass locally. The root just check/just test recipes for ffi, driver, and uniffi were not run; this touches only ts/ and quest/.

Impact

  • Translator output: fewer dead arms where an if or guard sits between a match and its rematch. Same behavior, verified by the differential test.
  • No public API change.

Alternatives

  • Keep facts only across switches of a guard's or if's bool, not of integers: no simpler, since no switch writes a place, and an integer match between the two matches would still lose them.

Choices made without the maintainer

  • Took the quest's Plan reading of "a test that is an if": an if between the matches. An if whose condition is itself a variant test (if matches!(s, Dot) { match s { .. } }) still doesn't fold: the bool joins two edges before the if, so folding it means tracking which variants each bool stands for. Checked locally; it translates and type-checks, just with the dead arm.

Follow-ups

  • Fold a match a bool decided (matches!, == of a crate enum's derived PartialEq), by recording which variants a bool local holds true for.

The quest is deleted here (quest/m3/ts-fold-guards.md).

(written by Opus 5.5)

🤖 Generated with Claude Code

kixelated and others added 2 commits October 28, 2026 06:17
Quest-Claim: 85b27edabc213cbc
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 <noreply@anthropic.com>
@kixelated kixelated changed the title Fold matches across tests that aren't variant switches fix(ts): fold matches an if or guard leaves decided Oct 28, 2026
@kixelated
kixelated marked this pull request as ready for review October 28, 2026 13:28
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-28T13:29:58.729758Z 889d592 Draft marked ready
🔒 Security Review ✅ Completed 2026-10-28T13:32:07.500705Z 889d592 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@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: 889d592

No actionable bugs found. Preserving variant facts across a non-variant SwitchInt is the simpler, appropriate fix: the switch does not mutate its operands, existing write/borrow/call invalidation still runs before propagation, and joins conservatively account for loop back edges. The added fixtures exercise payload reads after an if, guard success/fallthrough, and mutation across a while-loop back edge; the generated snapshots retain the necessary loop rematch.

Verification: static inspection of the diff, the full decided.rs dataflow, and surrounding fixtures/snapshots only; I did not run tests or independently verify the reported output-size measurements.

@kixelated

Copy link
Copy Markdown
Collaborator Author

Landing at the reviewed head 889d592 by squash, with the maintainer's approval. Codex reviewed it with no findings and CI's Check passed. Main had not moved past the PR's base, and quest check --base origin/main passes.

Choice made without asking, from the body: an if whose condition is itself a variant test (if matches!(s, Dot)) still doesn't fold. That is a follow-up to plan.

(written by Opus 5.5)

@kixelated
kixelated merged commit 7e8b157 into main Oct 28, 2026
1 check passed
@kixelated
kixelated deleted the quest/m3/ts-fold-guards branch October 28, 2026 13:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant