Repository navigation
feat(ts): fold a match a bool decided - #251
Conversation
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>
|
Warning Review limit reachedYou'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. View limit details
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. Comment |
kixelated
left a comment
There was a problem hiding this comment.
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)
Problem
#175 folds a match that an enclosing match,
if, or guard already decided. Its dataflow ints/src/lower/mir/decided.rsknew nothing about abool. So a match that abooldecided kept its dead arms:if matches!(s, Shape::Dot) { match s { .. } }, where thebooljoins two edges before theif, andif s == Shape::Dot { match s { .. } }with a derivedPartialEq. Arms dropped this way could also widen a payload read. Closes the questquest/m3/ts-fold-bool.md.Approach
The dataflow now tracks, for each
boollocal a switch tests, the variants each of its values leaves a place:b = const true/falserecords 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, sox && matches!(s, Rect)still decides thetrueside.b = !candb = ccarryc's implications, swapped for!.b = <E as PartialEq>::eq(&x, &y)orne, whenEis an enum whosePartialEqis#[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::Dotpromotes one) or from an enum a local's single write builds.bnarrows each place on each edge. An edge it can't take (theboolknown 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.boolthat 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
unreachablenow 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 causedtscerrors likeProperty 'width' does not exist on type 'Dot | Rect'.Output size
just ts moqat the pinned revision): byte-identical before and after (610,242 bytes), and still type-checks.just ts benchmeasures are the same and there is nothing to time.shapes.ts75,296 to 74,710 bytes,dyns.ts30,762 to 30,731 bytes, +18/-64 lines. The new cases lose their dead arms.rematched'sif order == Ordering::Less { match order { .. } }folds throughOrdering's derivedPartialEqand loses its widening cast.dyns::anysfolds a drop-flagboolswitch, which leavestypedwith one reader, so the lowering holds it back aspart7.Tests
The new
shapes::decided_by_boolin 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::Dotand!= Shape::Dot(fieldless),==and!=against a builtLine(!=must keepLine's arm), and==of two places, one known by anif let,boolnegated on one way in, aboolcarried around a loop from the place's previous value, and aboolwritten through a raw pointer after its test.just cipasses locally.Impact
booldecided a match. Same behavior, verified by the differential test.Alternatives
knownfacts for every place: tried it. It also folded matches on a value built in sight (theknownfixture) into payload reads TypeScript hadn't narrowed, whichtscrejects. They now feed only the==operands.is_builtin_derived: it answers only for the local crate, so dependencies' derived impls (moq's enums from another crate, std'sOrdering) would never fold.is_automatically_derivedmatches whatcollections::derived_eqalready trusts.Choices made without the maintainer
#[automatically_derived]PartialEqon an enum, std's included, not only the crate's own.booledges, such as drop flags known at a point, beyond the quest'smatches!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 head05de1abcc489a6a2fb4f82f9ccace58761e34b4fagainstorigin/main(e51ecada). No findings.(written by Claude Opus 5.5)
🤖 Generated with Claude Code