Repository navigation
fix(ts): fold matches an if or guard leaves decided - #175
Conversation
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>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
kixelated
left a comment
There was a problem hiding this comment.
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.
|
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 Choice made without asking, from the body: an (written by Opus 5.5) |
Problem
#110's fold drops the dead arms of a match an enclosing match already decided. Its dataflow in
ts/src/lower/mir/decided.rsforgot everything it knew at a switch on anything but a variant, so anifor 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
boolor an integer writes no place, so each of its edges now keeps what is known, as aGotodoes. 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 ontoown()ints/src/lower/mir/mod.rs, whichwhole()had split off.Output size
just ts moq): byte-identical before and after.just ts benchmeasures are the same and there is nothing to time.shapes.ts,rematched).Tests
Lang fixture, differential against native Rust and type-checked, no new op.
rematchedgains:ifbetween a match onLineand a rematch that readsLine's payload,Dot if ..,Rect { width, .. } if width > 300) before rematches, including a guard's fallthrough into a later arm on the same variant,whileloop entered with the variant known, whose body changes the place, so itsbooltest 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, andquest check --base origin/mainpass locally. The rootjust check/just testrecipes for ffi, driver, and uniffi were not run; this touches onlyts/andquest/.Impact
ifor guard sits between a match and its rematch. Same behavior, verified by the differential test.Alternatives
if'sbool, not of integers: no simpler, since no switch writes a place, and an integermatchbetween the two matches would still lose them.Choices made without the maintainer
if": anifbetween the matches. Anifwhose condition is itself a variant test (if matches!(s, Dot) { match s { .. } }) still doesn't fold: thebooljoins two edges before theif, so folding it means tracking which variants eachboolstands for. Checked locally; it translates and type-checks, just with the dead arm.Follow-ups
booldecided (matches!,==of a crate enum's derivedPartialEq), by recording which variants aboollocal holdstruefor.The quest is deleted here (
quest/m3/ts-fold-guards.md).(written by Opus 5.5)
🤖 Generated with Claude Code