Repository navigation
Keep the default of a dense int switch ending in an unreachable case - #9418
Conversation
Why:
When the highest cases of a dense int or char switch map to the
failure action (for example an unreachable case whose action becomes
the exit), the trailing failure interval stopped at the last case
instead of the top of the range. Values above it were left out of the
interval table, so the generated JS switch had no default and returned
undefined for them. The trailing failure interval now runs to the top
of the range. The store that reserves action index 0 for the failure
action moves out of an assert, so it no longer depends on assertions
being compiled in.
JS modified (dense switch with an unreachable last case):
Example:
@warning("-11")
let f = (n: int, flag: bool) =>
switch (n, flag) {
| (_, true) => 0
| (0, _) => 10
| (1, _) => 11
| (2, _) => 12
| (3, _) => 13
| (4, _) => 14
| (5, true) => 15
| _ => 99
}
Before (js):
function f(n, flag) {
if (flag) {
return 0;
}
if (n < 0) {
return 99;
}
switch (n) {
case 0 :
return 10;
case 1 :
return 11;
case 2 :
return 12;
case 3 :
return 13;
case 4 :
return 14;
case 5 :
return 99;
}
}
After (js):
function f(n, flag) {
if (flag) {
return 0;
}
switch (n) {
case 0 :
return 10;
case 1 :
return 11;
case 2 :
return 12;
case 3 :
return 13;
case 4 :
return 14;
default:
return 99;
}
}
Review: Does binding the failure store outside the assert change any output? No; asserts are compiled in today, and it only matters for a build with -noassert.
Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
Changelog🐛 Bug fix
Semantic Diff1 commit · Full page Keep the default of a dense int switch ending in an unreachable case5052dbe · illusionalsagacity · Nov 23, 2026, 20:40 UTC
JS modified (dense switch with an unreachable last case) @warning("-11")
let f = (n: int, flag: bool) =>
switch (n, flag) {
| (_, true) => 0
| (0, _) => 10
| (1, _) => 11
| (2, _) => 12
| (3, _) => 13
| (4, _) => 14
| (5, true) => 15
| _ => 99
}
function f(n, flag) {
if (flag) {
return 0;
}
- if (n < 0) {
- return 99;
- }
switch (n) {
case 0 :
return 10;
case 1 :
return 11;
case 2 :
return 12;
case 3 :
return 13;
case 4 :
return 14;
- case 5 :
+ default:
return 99;
}
}Review
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #9418 +/- ##
==========================================
+ Coverage 82.22% 82.24% +0.01%
==========================================
Files 461 462 +1
Lines 65573 65577 +4
==========================================
+ Hits 53916 53932 +16
+ Misses 11657 11645 -12
🚀 New features to boost your workflow:
|
rescript
@rescript/belt
@rescript/darwin-arm64
@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. Already looking forward to the next diff. 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.
Looks good to me. I built it, checked the fix against master with extra cases, and found no problems.
This review was done by Claude.
Bring the backport in line with the version of rescript-lang#9418 merged on master (ea323ae). Its change to Matching.as_interval_canfail is the same here, with the (low, action) pairs 12.x has in place of master's (low, high, action) triples. The tests differ from master's. 12.x has no int range patterns, and a case guarded by `if false` doesn't reach the failure action there, so master's tests pass on 12.x without the fix. Each case here is guarded by a second tuple component instead, which fails without the fix, and a char range case is added, as 12.x expands char ranges into single cases. Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
Part of #9398 (item 11).
Problem
When the highest cases of a dense int or char
switchgo to the failure action (for example because the last case is unreachable), the last failure interval stopped at the highest case instead of the top of the range. Values above it had no arm, so the JSswitchhad no default and returnedundefined.Before:
After:
Before,
f(7, false)returnsundefinedinstead of99. The compiler warns that this case is unused, but a| 5 if falsearm triggers the bug without a warning.Fix
The trailing failure interval in
as_interval_canfailnow runs to the top of the range. The failure action is also stored outside theassertthat checked it, so-noassertbuilds would not drop it; that changes no output.Tests
dense_switch_default_test.rescovers int and char cases and ranges; all three paths that end in the failure interval are covered. One function has its unused-case warning disabled. No other checked-in output changes.12.x
12.x has the same bug, and I have a backport ready on
fix-dense-switch-default-12.x. I'll open it againstmaintenance/12.xonce this one is reviewed.The fix was written by Claude. A second Claude agent reviewed the first version; this version addresses that review and has not been reviewed again.