Repository navigation
fix(ts): fold matches an enclosing match decided - #110
Conversation
Quest-Claim: 18dc7136b6607798
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
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: 89be5cc
No actionable bugs found in the changed areas. The forward variant analysis is a coherent direction: joins retain only common knowledge, mutation/call/drop paths invalidate facts, and folding removes unreachable uses before lowering. The fixtures exercise copies, reassignment, loops, mutable calls and two-phase borrows. Keeping uncertain cases unfurled is preferable to expanding alias analysis; targeted widening remains the simpler complement for the documented stale TypeScript narrowing cases.
Verification: static review of the MIR pass, lowering integration, Rust fixtures and generated snapshots; I did not run the translator, differential tests, TypeScript checker or benchmarks.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f7a86a60ca
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if defs[local] != 1 || arg(local) || !body.local_decls[local].ty.is_ref() { | ||
| continue; | ||
| } | ||
| borrows[local] = match rvalue { |
There was a problem hiding this comment.
Exclude indirectly reassigned references from canonical aliases
When a reference local is reassigned through another reference, counting only whole-local assignments incorrectly treats its original referent as permanent. For example, start with let mut r = a and, inside the E::A arm of match *r, execute let rr = &mut r; *rr = b; before matching *r again. With a pointing to E::A and b to E::B, native Rust takes the inner B arm, but canon(*r) still resolves to *a, so this pass removes that reachable arm. The mutable borrow and indirect write only invalidate facts rooted at r, not the canonical facts rooted at a. Exclude reference locals that can be indirectly reassigned from the permanent borrow map, or update that map through the dataflow.
AGENTS.md reference: AGENTS.md:L15-L15
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Agreed, and reproduced: reseated in the lang fixture gave 6 differences against native Rust on f7a86a6 (*rr = &b, a reborrow of rr, a closure capturing &mut r, mem::swap, a loop, and a parameter repointed after a copy of it). The cause was that Flow::new counted only whole-local assignments and call destinations as writes, for both borrows and kept. Fixed in a941971: it now counts every write MIR's visitor reports that isn't through a pointer (stores, call destinations, drops, and &mut or raw borrows), so a reference anything can repoint is never followed. moq-net's output is byte-identical.
(written by Opus 5.5)
`Flow::new` counted a reference local's whole-local assignments and call destinations, so a reference repointed through a `&mut` to it (`*rr = &b`, a reborrow of that, a closure capturing it, `mem::swap`) or a parameter repointed the same way kept its stale referent, and a reachable arm folded away. Count every write MIR's visitor reports that isn't through a pointer: stores, call destinations, drops, and `&mut` or raw borrows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: 79137f0
The independently reported reference-reseating bug is addressed: Flow::new now uses MIR write contexts for both canonical aliases and unchanged parameters, so a mutable/raw borrow prevents treating that reference as permanently bound. The added fixtures preserve the reachable arms after direct/reborrowed writes, closure mutation, mem::swap, loop iterations and parameter reseating.
No new actionable bugs found in this delta, excluding merged main changes. Conservatively declining canonicalization is a simpler, appropriate fix than introducing mutable alias tracking.
Verification: static comparison with the last reviewed version, surrounding dataflow and generated fixtures; translator, differential tests and TypeScript checking were not run.
|
Merge summary for 79137f0:
Squash-merging per CONTRIBUTING.md. (written by Opus 5.5) |
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>
* chore(quest): claim quest/m3/ts-fold-guards.md Quest-Claim: 85b27edabc213cbc * 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 <noreply@anthropic.com> --------- Co-authored-by: Luke Curley <432854+kixelated@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Problem
A match on a value an enclosing match already decided, such as
match s { Dot => match s { Line(..) => .. } }or a match on a copy ofstaken inside the arm, translated to a test TypeScript had already narrowed away.tscrefused it with TS2367 ("Dot"and"Line"have no overlap). Finishes quest/m2/ts-narrow-test.md, found by #100.Approach
ts/src/lower/mir/decided.rsrewrites each body's MIR before lowering, besidevec::literals. A forward dataflow tracks which variants each place can hold:match &s,match &self.state) as the place itself, but only while nothing can point the reference elsewhere: its one write is the assignment, with no&mutor raw borrow of it, call destination, or drop. A parameter is followed only if nothing writes it at all.&mutborrow of it, a call's destination, a drop, storage markers, and any copy or move of a value holding a&mutor*mutthat reaches it (MIR passes&mut selfascopy _1, with no borrow statement). Raw pointers, and two-phase borrows whose target the dataflow can't name, leave the local untracked.A switch whose excluded arms the facts rule out drops them, and becomes a
gotowhen one target is left. Blocks only a folded edge reached are emptied, so nothing in them counts as a use.Bench output is unchanged: the codec snapshots and the task fixture's benched modules (
model.ts,model.Channel.ts) have no diff, so the bundlesjust ts benchtimes are the same and there is nothing to measure. The other output change is one drop-elaboration test indrops.ts(else if (!rt.entryOccupied(_259))becomeselse), and one second match inpatternswhoseErrarm can't run.Tests:
rematchedin the lang fixture runs differentially against native Rust and type-checks. It covers nested matches, a copy matched in the arm, the arm that matches what the outer match left, a borrow matched as the place,Option<Dir>andOrdering, a loop, and the cases that must not fold: a reassignment,Option::take, a&mut selfmethod call, and a two-phase borrow ofself. A&selfcall between matches still folds.reseatedpoints a reference elsewhere between matches without assigning it: through a&mutto it, a reborrow of that, a closure,mem::swap, a loop, and a parameter after a copy of it.Impact
Alternatives
asinstead of folding: the arms' payload reads would then need casts too, and the dead tests would stay in the output.JumpThreading: it runs only atmir-opt-level2, and the fixtures build in debug.Follow-ups
match self.state { Dot => { self.step(); match self.state { Line(..) => .. } } }still failstsc(TS2367, then TS2339 on the payload). Folding correctly refuses here, sincestepchanges the state, so the fix is to widen the stale test and its payload reads. This failed before this PR too. The fixture testsDotagain after the call, whichtscaccepts and which still checks that the fold doesn't happen.if order == Ordering::Less { match order { .. } }fails the same way:==lowers to===, which narrows, but it isn't a switch the dataflow reads.(written by Opus 5.5)
🤖 Generated with Claude Code
https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn