Repository navigation
[12.x] Fix constant folding of a switch on an @as(int) constructor - #9318
illusionalsagacity wants to merge 3 commits into
Conversation
Lam.switch folded a constant scrutinee by looking its integer up in sw_consts, which is keyed by constructor position. Since rescript-lang#7058, Lam_constant_convert stores the runtime value of an @as(int) constructor in that integer, so the fold took the case whose position equals the constructor's runtime value: with `type t = | @as(1) A | @as(0) B`, `name(A)` folded to B's case. Look a constant constructor up by its tag in sw_names.consts instead, and a constant block by its constructor name in sw_names.blocks, leaving the switch unfolded when it is not found. This also fixes folding after a coercion to a variant with another constructor order, which picked the case at the constructor's position in the source type. Fixes rescript-lang#9317. Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
rescript
@rescript/darwin-arm64
@rescript/darwin-x64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
|
@codex review |
Codex Review Summary: ✅ CompletedThis 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. |
|
Codex Review: Didn't find any major issues. 🎉 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
cknitt
left a comment
There was a problem hiding this comment.
This review was done by Claude (Claude Code) on my behalf. I've read it and agree.
Thanks for the careful investigation and the thorough tests!
I built bsc from this PR and from its base (5ee4c56) on linux-arm64 and ran 27 extra cases through both. Each case calls the function with a constant, which the compiler folds, and again through an @inline(never) identity, which runs the real code. The cases covered:
@as(int)with a default arm, and mixed with payload constructors- coercion from a smaller to a bigger variant, and between
@as(int)types @as(string)and@as(bool)@unboxedwithnull,undefinedor int literals@tagwith inline records- option, list and polymorphic variants
- an int switch on
(A :> int) - a call across modules
Results:
- Wrong folded results: 9 on base, none with this PR.
- Folding: the PR folds every call base folded, and a few more.
- Emitted functions: unchanged.
Matching constants by their full tag (name plus @as value) and blocks by name gives the same result as the fix on master. When the constructor isn't found, the switch stays unfolded, which is always safe.
One request: please drop the issue link from the CHANGELOG entry. All other 12.3.x entries link only the PR. The entry could also be shorter, for example:
- Fix constant folding of a
switchpicking the wrong case for@as(int)constructors and for coerced variants. #9318
The failing Semantic Diff check isn't caused by this PR. For fork PRs, that workflow runs from master but takes its script from the base branch, and maintenance/12.x doesn't have that script. #9320 skips the job for fork PRs into maintenance branches.
Approving once the changelog entry is updated.
🤖 Generated with Claude Code
Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
A fork PR runs the workflow from master but checks out its base commit for the renderer. Maintenance branches don't have the renderer script, so the job failed there (seen on #9318 into maintenance/12.x). Claude-Session: https://claude.ai/code/session_01Kbf3gGHHcoxuVopoQtEcJ7 Signed-off-by: Christoph Knittel <ck@cca.io> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks @cknitt, I made that change to the CHANGELOG |
Fixes #9317.
Problem
Since 12.0.0-alpha.4, the optimizer picks the wrong case when it folds a
switchover a constant constructor that has@as(int):Before (every 12.x release, 12.3.1 included):
After:
The emitted
namedoesn't change. Only the folded call does.Root cause
#7058 ("Fix variant coercion with as attribute", 1437815) changed
Lam_constant_convertso that a constant constructor with@as(int)becomesConst_int {i = <runtime value>}instead ofConst_int {i = <constructor position>}. That fixed folding of(A :> int).Lam.switchwas not updated. It still folds a constant scrutinee by looking upiinsw_consts, andsw_constsis keyed by constructor position. So the fold picks the case whose position equals the constructor's runtime value. If no position matches, the call is not folded at all. This explains the five-constructor example:@as(3)..@as(7)foldsWeeklytoQuarterly's case andMonthlytoAnnual's, and leavesBiWeekly,QuarterlyandAnnualas calls. A directswitch A { ... }goes through the same function and had the same bug.git bisectbetween v12.0.0-alpha.3 and v12.0.0-alpha.4 names 1437815 as the first bad commit.Fix
Lam.switchnow finds the case for a constant constructor by its tag (name and@asvalue) insw_names.consts, which lists the constant constructors in position order, instead of usingi(switch_const_key). Constant blocks are looked up the same way, by constructor name insw_names.blocks(switch_block_key). If the constructor isn't in that list, the switch is left unfolded, which is always correct. A switch withoutsw_names, such as an int switch on(A :> int), folds onias before.Looking up by tag also fixes an older bug, present before #7058, where coercing between variants with different constructor order folded to the wrong case. The same lookup by name is applied to constant blocks (
Const_block), so constructors with a payload, including inline records, fold to the right case after such a coercion too:On master this was fixed as part of #8579 ("Make variant runtime representation canonical across compiler IRs", commit 108aa8f "Replace constructor ordinals with semantic identity and representation"). That change removes constructor ordinals from every compiler IR and touches 73 files, so it isn't a candidate for a backport. This PR is a targeted fix for 12.x.
Tests
The new file
tests/tests/src/variant_as_int_fold_test.resfollows the pattern ofunboxed_variant_fold_test(#8631). Each function is called with a constant argument, which folds, and through an@inline(never) id, which runs the real dispatch. Both results are checked against the expected value. The committed.mjsshows that the constant calls fold to the right literals and that the emitted functions are unchanged. It covers:@as(3)..@as(7), every constructor (both the ones that used to fold wrongly and the ones past the last position);@as(int)mixed with a constructor that has no@as, plus a negative value and@as(0);switchdirectly on a constant, with no call in between;switch (A :> int), which must still fold;Without the compiler change, the 7 folding tests fail. With it all 8 pass, and none of the other 663 modules in
tests/testschange their output.Tested against a production codebase
With the
binaries-darwin-arm64andlib-ocamlartifacts from this PR's CI run (head 220ef98) installed overrescript@12.3.1:A standalone project with the repro above, the five-constructor case,
@as(int)mixed with a constructor that has no@as, a default arm, and a coercion between variant types: 8 of its 15 folded values are wrong on 12.3.1. All 15 are correct with this PR, and all are folded to constants.A production codebase with about 2,900 ReScript modules that use
@as(int)variants heavily. Compiled with stock 12.3.1 and with this PR, only one of the 2,935 emitted.res.mjsfiles differs, and the change is this bug being fixed (frequencyToValue(Weekly), whereWeeklyis@as(3)):Its test suite (10,008 tests) passes on this build, including a test that fails on 12.3.1 because of this bug.