Skip to content

Fix inlined char switches losing their result - #9492

Merged
cknitt merged 1 commit into
masterfrom
fix-constant-switch-fallthrough
Dec 2, 2026
Merged

cknitt merged 1 commit into
masterfrom
fix-constant-switch-fallthrough

Conversation

@cknitt

@cknitt cknitt commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Fixes #9489.

An inlined switch on a char constant could lose its result:

@inline
let classify = x =>
  switch x {
  | 'a' => "a"
  | 98 => "b"
  | 'c' .. 'e' => "c-e"
  | 102 | 'g' => "f-g"
  | _ => "other"
  }

@inline
let check = (actual, expected) => Console.log((actual, expected))

check(classify('d'), "c-e") // printed [undefined, 'c-e']

When classify('d') is inlined, the switch's argument is the constant 'd', so constant folding can replace the whole switch with the matching case. Lambda did this for int constants but not for char constants, so here JavaScript generation folded the switch instead. It took only the body of the matching case, 100. But case 100 shares its action with cases 99 and 101, so its own body is empty, and nothing assigned the result.

The fix. JavaScript generation now falls through empty cases to the shared body when it folds a switch on a constant. Lambda also folds a switch on a char constant, as it does for an int. This shortens char_int_alias_test's output too.

Tests. inline_constant_switch_test runs the example. Once Lambda folds char constants, no program I found still reaches the fold in JavaScript generation, so the new unit tests in ounit_constant_switch_tests call it directly.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gH9cXWp3xtBmfRV3Du

@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Fix an inlined switch on a char constant that lost its result when its case shared an action with the next case.

Semantic Diff

1 commit · Full page

✏️ Fix inlined char switches losing their result

3e46383 · cknitt + Claude · Dec 01, 2026, 19:42 UTC · edited

Why

When a function with a switch is inlined into a call such as classify('d'), the switch's argument is a constant, so constant folding can replace the switch with the matching case. Lambda did this for int constants but not for char constants, so JavaScript generation folded the switch instead. It took only the body of the matching case. But cases with the same action share one body: case 100 has an empty body and falls through to case 101. So nothing assigned the result.

JavaScript generation now follows the fall-through when it folds a switch: it takes the first body that isn't empty. Lambda also folds a switch on a char constant, as it does for an int.

JS modified (inlined switch on a char constant)

@inline
let classify = x =>
  switch x {
  | 'a' => "a"
  | 98 => "b"
  | 'c' .. 'e' => "c-e"
  | 102 | 'g' => "f-g"
  | _ => "other"
  }

@inline
let check = (actual, expected) => Console.log((actual, expected))

check(classify('d'), "c-e")

js

-let actual;
-console.log([actual, "c-e"]);
+console.log(["c-e", "c-e"]);

Fixes #9489

@codecov

codecov Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.74074% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.52%. Comparing base (69e11bf) to head (3e46383).

Files with missing lines Patch % Lines
tests/ounit_tests/ounit_constant_switch_tests.ml 90.62% 3 Missing ⚠️
compiler/backend/js_stmt_make.ml 83.33% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9492      +/-   ##
==========================================
+ Coverage   82.49%   82.52%   +0.02%     
==========================================
  Files         467      468       +1     
  Lines       66125    66161      +36     
==========================================
+ Hits        54553    54602      +49     
+ Misses      11572    11559      -13     
Files with missing lines Coverage Δ
compiler/typing/lambda.ml 69.74% <100.00%> (+0.05%) ⬆️
tests/ounit_tests/ounit_tests_main.ml 100.00% <ø> (ø)
compiler/backend/js_stmt_make.ml 87.71% <83.33%> (+14.74%) ⬆️
tests/ounit_tests/ounit_constant_switch_tests.ml 90.62% <90.62%> (ø)
🚀 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.

@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

✅ Compiler benchmarks

Allocated words

In total: 655,172,566 → 655,168,748 (±0.00%).

Generated projects: 627,299,514 → 627,299,374 (±0.00%).

None of the 265 benchmarks changed by 0.1% or more.

Instructions

In total: 9,128,430,307 → 9,127,893,614 (-0.01%).

Benchmark Base Head Change
packages/@rescript/runtime/Stdlib_Math.resi 21,240,136 21,217,103 -0.11%
packages/@rescript/runtime/Stdlib_Temporal_PlainDate.res 24,077,531 24,053,307 -0.10%

Output size

In total: 27,368,788 → 27,368,754 (±0.00%).

None of the 13 benchmarks changed by 0.1% or more.

Pull request 3e46383 compared with base 69e11bf in this run.

@cknitt
cknitt force-pushed the fix-constant-switch-fallthrough branch from f27e32b to 90cff12 Compare December 1, 2026 18:26
@pkg-pr-new

pkg-pr-new Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 3e46383

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown

@cknitt

cknitt commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 1, 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-12-01T19:10:35.910410Z 90cff12 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. 🚀

Reviewed commit: 90cff12d9b

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

@cknitt
cknitt force-pushed the fix-constant-switch-fallthrough branch from 90cff12 to 35b5f9a Compare December 1, 2026 19:27
@cknitt
cknitt marked this pull request as ready for review December 1, 2026 19:27
@cknitt cknitt changed the title Keep the result of an inlined switch on a char constant Fix inlined char switches losing their result Dec 1, 2026
@cknitt
cknitt force-pushed the fix-constant-switch-fallthrough branch from 35b5f9a to 0ac84c7 Compare December 1, 2026 19:41
Why:
    When a function with a switch is inlined into a call such as
    `classify('d')`, the switch's argument is a constant, so constant
    folding can replace the switch with the matching case. Lambda did
    this for int constants but not for char constants, so JavaScript
    generation folded the switch instead. It took only the body of the
    matching case. But cases with the same action share one body: case
    100 has an empty body and falls through to case 101. So nothing
    assigned the result.

JavaScript generation now follows the fall-through when it folds a
switch: it takes the first body that isn't empty. Lambda also folds a
switch on a char constant, as it does for an int.

JS modified (inlined switch on a char constant):
Example:
    @inline
    let classify = x =>
      switch x {
      | 'a' => "a"
      | 98 => "b"
      | 'c' .. 'e' => "c-e"
      | 102 | 'g' => "f-g"
      | _ => "other"
      }

    @inline
    let check = (actual, expected) => Console.log((actual, expected))

    check(classify('d'), "c-e")
Before (js):
    let actual;
    console.log([actual, "c-e"]);
After (js):
    console.log(["c-e", "c-e"]);

Fixes #9489

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gH9cXWp3xtBmfRV3Du
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt force-pushed the fix-constant-switch-fallthrough branch from 0ac84c7 to 3e46383 Compare December 1, 2026 19:42
@cknitt
cknitt requested a review from cristianoc December 1, 2026 20:48
@cknitt
cknitt merged commit 9684aa5 into master Dec 2, 2026
23 checks passed
@cknitt
cknitt deleted the fix-constant-switch-fallthrough branch December 2, 2026 06:10
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.

An inlined switch with a char range can lose its result

2 participants