Repository navigation
Description
Thank you for filing! Check list:
- Is it a bug? Usage questions should often be asked in the forum instead.
- Concise, focused, friendly issue title & description.
- A minimal, reproducible example.
- OS and browser versions, if relevant.
- Is it already fixed in master? Instructions
Summary
Every 12.x release silently miscompiles a call to a function that switches on a variant with @as(int) constructors when the argument is a constant constructor. The folded result is the case whose position in the type (counting from 0) equals the constructor's runtime value, not the case for that constructor. The emitted function itself is correct; only the folded call site is wrong.
This is a regression from ReScript 11. It is already fixed on master (13.0.0-alpha.6 and later), but no 12.x release has the fix.
Reproduction
type t = | @as(1) A | @as(0) B
let name = x =>
switch x {
| A => "A"
| B => "B"
}
let a = name(A)Expected:
let a = "A";Actual (12.0.0-alpha.4 through 12.3.1):
function name(x) {
if (x === 1) {
return "A";
} else {
return "B";
}
}
let a = "B";A direct switch A { | A => "A" | B => "B" }, with no function call in between, also evaluates to "B".
A longer example
type frequency =
| @as(3) Weekly
| @as(4) Monthly
| @as(5) BiWeekly
| @as(6) Quarterly
| @as(7) Annual
let toValue = x =>
switch x {
| Weekly => 3
| Monthly => 4
| BiWeekly => 5
| Quarterly => 6
| Annual => 7
}| Expression | 12.3.1 output | Expected |
|---|---|---|
toValue(Weekly) |
6 |
3 |
toValue(Monthly) |
7 |
4 |
toValue(BiWeekly) |
toValue(5) (not folded, correct at runtime) |
5 |
toValue(Quarterly) |
toValue(6) (not folded, correct at runtime) |
6 |
toValue(Annual) |
toValue(7) (not folded, correct at runtime) |
7 |
Constructors whose value is a valid position get the wrong case. Those whose value is past the last position aren't folded, so they happen to work. The same happens when @as(int) constructors are mixed with constructors that have no @as. For example, with | @as(1) A | B, name(A) folds to B's case.
Affected versions
Published npm versions, checked against the minimal repro:
| Version | Result |
|---|---|
| 11.1.4 | correct |
| 12.0.0-alpha.1 – alpha.3 | correct |
| 12.0.0-alpha.4 | wrong (first bad release) |
| 12.0.0-alpha.4 – 12.3.1 | wrong |
| 13.0.0-alpha.5 | wrong |
| 13.0.0-alpha.6, alpha.7 | correct |
Cause
git bisect between v12.0.0-alpha.3 and v12.0.0-alpha.4 finds the first bad commit: 1437815, "Fix variant coercion with as attribute" (#7058).
That commit changed compiler/core/lam_constant_convert.ml so that a constant constructor with @as(int) becomes Const_int {i = <runtime value>}, where i used to be the constructor's position. That change was needed to fold (A :> int) correctly. Lam.switch (compiler/core/lam.ml) wasn't updated, though: it still folds a constant scrutinee by looking up i among the switch cases, which are keyed by constructor position.
Status on master
git bisect between v13.0.0-alpha.5 and v13.0.0-alpha.6 finds the fix: 108aa8f, "Replace constructor ordinals with semantic identity and representation", part of #8579 ("Make variant runtime representation canonical across compiler IRs"). That PR removes constructor positions from every compiler IR and touches 73 files, so it isn't practical to backport. Its description says output is unchanged, so it doesn't look like anyone noticed it fixed this bug.
Proposed 12.x fix
A targeted change to Lam.switch: when the scrutinee is a constant constructor, look up its position by tag in the switch's sw_names.consts instead of using i, and leave the switch unfolded if it isn't found. That's about 20 lines in one function, with no change to emitted code anywhere else in the test suite. I have a branch with this fix and regression tests and can open a PR against maintenance/12.x.
Related folding bugs on 12.x (fixed on master)
Found while investigating; noted here for completeness rather than as part of this report:
- Merged switch arms (also caused by Fix variant coercion with as attribute #7058). Lambda constant equality compares only
i, so an@as(1)constructor and the constructor at position 1 count as equal and their arms get merged. For example, withtype t = | @as(1) A | B,switch x { | X => B | Y => A | Z => B }returns"B"forY. - Constant equality (also caused by Fix variant coercion with as attribute #7058). With
type t = | @as(1) A | B | C,A == Bfolds totrue. With| @as(1) U | @as("x") V,U == Vfolds totrue. - Coercion that reorders constructors (older than Fix variant coercion with as attribute #7058). For example,
type r = | Y(int) | X(int)coerced totype o = | X(int) | Y(int): the fold goes by position in the source type, so(X(1): r :> o)takes theYcase. This affects constructors without payloads too. The proposed fix covers those, but not constructors with payloads. - Unboxed variants (Constant folding a pattern match on an unboxed variant produces an incorrect result #6950).
colorName(Color("primary"))still folds to"primary"instead of"not Color". This is fixed on master by Fix constant folding of overlapping unboxed variant matches #8631.
Activity
Compare constructor constants by runtime value
[12.x] Fix constant folding of a switch on an @as(int) constructor (#…
illusionalsagacity commentedon Nov 21, 2026
Status of the cases in this issue on
maintenance/12.x:switchon a constant@as(int)constructor folds to the wrong case@as(int)constructorsA == B,U == V)#9319 also keeps comparisons of
@asstrings spelled differently, such as an escaped"A"and a plain"A", as runtime comparisons, and covers@as(null),@as(true)and@as(1.5)constructors and ordering comparisons.While working on these I found one more 12.x-only folding bug, filed as #9385 and fixed by #9386: switch arms returning coerced variant or record constants are merged when the blocks share a tag, so the wrong value is returned. It isn't caused by
@as(int)and is fixed on master.[12.x] Compare constructor constants by runtime value (#9319)