Skip to content

Fold untagged matches by evaluating the dispatch plan - #9046

Merged
cknitt merged 1 commit into
dispatch-planfrom
dispatch-plan-fold
Oct 26, 2026
Merged

cknitt merged 1 commit into
dispatch-planfrom
dispatch-plan-fold

Conversation

@cknitt

@cknitt cknitt commented Oct 25, 2026

Copy link
Copy Markdown
Member

Stacked on #9045.

Constant folding decided which arm an untagged constant takes with its own copy of the dispatch rules (untagged_action, value_has_block_type). So it could disagree with the generated code, and it left values outside every case unfolded.

Untagged_dispatch.select now evaluates a plan on the JavaScript value of a constant, with JavaScript's semantics for each test, including the short circuits of && and ||. Lambda.switch folds with it, and Lambda.untagged_plan builds the one plan that both folding and Lam_compile use. Folding therefore agrees with the generated code by construction.

Folding now also covers constants outside every case. For example, a true cast to I(int) | S(string) folds to the unchecked last arm, as rule 10 says.

New tests:

  • A property test in ounit_untagged_dispatch_tests.ml builds 3000 random variants and matches (literals, primitive kinds, classes with a subclass, arrays, objects, unknown payloads, random arm subsets with shared bodies). It checks that select gives, for one value of every case, the arm rules 7 to 9 give.
  • untagged_variants_spec_test.res checks rule 10 both folded and at run time.

🤖 Generated with Claude Code

https://claude.ai/code/session_01CMkrYaktctdKc4M41WF36t

@github-actions

github-actions Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Changelog

💅 Polish

  • Fold a match on an untagged constant by evaluating the dispatch the code is printed from, so folding and the generated code agree, also for values outside every case.

Semantic Diff

1 commit · Full page

Fold untagged matches by evaluating the dispatch plan

3cf4066 · cknitt + Claude · Oct 25, 2026, 20:30 UTC

Why

Constant folding decided which arm an untagged constant takes with its own copy of the dispatch rules, so it could disagree with the generated code, and it left values outside every case unfolded.

Untagged_dispatch.select evaluates a plan on the JavaScript value of a constant, with JavaScript's semantics for each test. Lambda.switch folds with it, and Lambda.untagged_plan builds the plan for both folding and Lam_compile. A property test compares the plans of random variants and matches with rules 7 to 9.

Generated code changed (a constant outside every case):

@unboxed type t = I(int) | S(string)
let name = (x: t) => switch x { | I(_) => "int" | S(_) => "string" }
let r = name(raw(true))

Before (JS):

let r = name(true);

After (JS):

let r = "string";

@codecov

codecov Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.27778% with 14 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.38%. Comparing base (d764e3d) to head (3cf4066).

Files with missing lines Patch % Lines
compiler/ml/lambda.ml 77.77% 8 Missing ⚠️
tests/ounit_tests/ounit_untagged_dispatch_tests.ml 94.11% 4 Missing ⚠️
compiler/ml/untagged_dispatch.ml 94.73% 2 Missing ⚠️
Additional details and impacted files
@@                Coverage Diff                @@
##           dispatch-plan    #9046      +/-   ##
=================================================
+ Coverage          81.36%   81.38%   +0.02%     
=================================================
  Files                458      458              
  Lines              64366    64452      +86     
=================================================
+ Hits               52369    52457      +88     
+ Misses             11997    11995       -2     
Files with missing lines Coverage Δ
compiler/core/lam_compile.ml 91.51% <100.00%> (-0.13%) ⬇️
compiler/ml/untagged_dispatch.ml 94.73% <94.73%> (+3.00%) ⬆️
tests/ounit_tests/ounit_untagged_dispatch_tests.ml 96.32% <94.11%> (-2.21%) ⬇️
compiler/ml/lambda.ml 70.04% <77.77%> (-0.15%) ⬇️

... and 1 file 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 Oct 25, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 3cf4066

@cknitt
cknitt marked this pull request as ready for review October 25, 2026 21:27
@cknitt

cknitt commented Oct 25, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-26T06:34:58.758925Z 3cf4066 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. What shall we delve into next?

Reviewed commit: 7fe417c953

ℹ️ 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 requested a review from cristianoc October 26, 2026 06:02
Why:
    Constant folding decided which arm an untagged constant takes with its
    own copy of the dispatch rules, so it could disagree with the generated
    code, and it left values outside every case unfolded.

`Untagged_dispatch.select` evaluates a plan on the JavaScript value of a
constant, with JavaScript's semantics for each test. `Lambda.switch` folds
with it, and `Lambda.untagged_plan` builds the plan for both folding and
`Lam_compile`. A property test compares the plans of random variants and
matches with rules 7 to 9.

Generated code changed (a constant outside every case):
Example:
    @unboxed type t = I(int) | S(string)
    let name = (x: t) => switch x { | I(_) => "int" | S(_) => "string" }
    let r = name(raw(true))
Before (JS):
    let r = name(true);
After (JS):
    let r = "string";

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CMkrYaktctdKc4M41WF36t
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt force-pushed the dispatch-plan-fold branch from 7fe417c to 3cf4066 Compare October 26, 2026 06:16
@cknitt

cknitt commented Oct 26, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: 3cf406653f

ℹ️ 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 merged commit 2f7b4d2 into master Oct 26, 2026
27 of 42 checks passed
@cknitt
cknitt deleted the dispatch-plan-fold branch October 26, 2026 08:57
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