Skip to content

Keep the default of a dense int switch ending in an unreachable case - #9418

Merged
cknitt merged 1 commit into
rescript-lang:masterfrom
illusionalsagacity:fix-dense-switch-default
Nov 27, 2026
Merged

cknitt merged 1 commit into
rescript-lang:masterfrom
illusionalsagacity:fix-dense-switch-default

Conversation

@illusionalsagacity

Copy link
Copy Markdown
Contributor

Part of #9398 (item 11).

Problem

When the highest cases of a dense int or char switch go 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 JS switch had no default and returned undefined.

@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:

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:

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;
  }
}

Before, f(7, false) returns undefined instead of 99. The compiler warns that this case is unused, but a | 5 if false arm triggers the bug without a warning.

Fix

The trailing failure interval in as_interval_canfail now runs to the top of the range. The failure action is also stored outside the assert that checked it, so -noassert builds would not drop it; that changes no output.

Tests

dense_switch_default_test.res covers 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 against maintenance/12.x once 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.

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>
@github-actions

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Fix a dense switch on ints or chars losing its default arm when its highest
    case is unreachable, so that it returned undefined for values above the
    cases.

Semantic Diff

1 commit · Full page

Keep the default of a dense int switch ending in an unreachable case

5052dbe · illusionalsagacity · Nov 23, 2026, 20:40 UTC

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)

@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
  }

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 :
+    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.

@codecov

codecov Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.24%. Comparing base (45dba2c) to head (5052dbe).
⚠️ Report is 12 commits behind head on master.

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     
Files with missing lines Coverage Δ
compiler/typing/matching.ml 79.30% <100.00%> (+0.80%) ⬆️

... and 4 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pkg-pr-new

pkg-pr-new Bot commented Nov 26, 2026

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@9418

@rescript/belt

npm i https://pkg.pr.new/@rescript/belt@9418

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@9418

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@9418

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@9418

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@9418

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@9418

commit: 5052dbe

@cknitt

cknitt commented Nov 26, 2026

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T20:23:34.502851Z 5052dbe Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 5052dbef12

ℹ️ 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".

@illusionalsagacity
illusionalsagacity marked this pull request as ready for review November 27, 2026 20:00

@cknitt cknitt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@cknitt
cknitt merged commit ea323ae into rescript-lang:master Nov 27, 2026
23 checks passed
@illusionalsagacity
illusionalsagacity deleted the fix-dense-switch-default branch November 27, 2026 21:50
illusionalsagacity added a commit to illusionalsagacity/rescript-compiler that referenced this pull request Nov 27, 2026
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants