Skip to content

Keep cases handled by a default arm out of the arms a switch names - #9010

Merged
cknitt merged 1 commit into
masterfrom
untagged-default-arm
Oct 24, 2026
Merged

cknitt merged 1 commit into
masterfrom
untagged-default-arm

Conversation

@cknitt

@cknitt cknitt commented Oct 24, 2026

Copy link
Copy Markdown
Member

Fixes #8995.

A switch on an untagged variant with a _ arm let the cases it does not name reach the arms it names. It shows when the default survives into the untagged dispatch, which takes a variant with several cases:

Declaration switch arms Value Before After
D(Date.t) | R(RegExp.t) | O({x: int}) | S(string) | I(int) | F(int => int) O, S, _ D(Date.make()) O _
@as("lit") Lit | S(string) | I(int) | B(bool) | O({x: int}) | F(int => int) S, O, _ Lit S _

Two fixes in lam_compile.ml:

  • Instances: the switch (typeof x) form is preceded by a guard sending every instance case without an arm to the default, generalizing the existing guard for arrays: if (v instanceof RegExp || v instanceof Date) { return "other"; }.
  • Literals: compile_switch skipped the literal test whenever the switch names no literal case and literals and payloads share the default. It now does so only when no literal would pass the check of a payload arm of its kind, so "lit" is tested first.

UntaggedVariants.md: rules 7 and 9 state both.

New test: tests/tests/src/untagged_default_arm_test.res. No other generated output changes.

This fix is also part of #8967 in the @instanceOf stack, adapted there to declared classes. That PR will be rebased onto this one.

🤖 Generated with Claude Code

https://claude.ai/code/session_01CMkrYaktctdKc4M41WF36t

Why:
    A `switch` on an untagged variant with a `_` arm let the cases it does
    not name reach the arms it names: a `Date` matched the object arm, since
    only arrays were guarded, and an `@as("lit")` literal matched the string
    arm, since the literal test was skipped when the switch names no
    literal (#8995).

The `switch (typeof x)` is preceded by a guard sending every instance case
without an arm to the default, as it already did for arrays. The literal test
is skipped only when no literal of the variant would pass the check of a
payload arm. Rules 7 and 9 of the spec state both.

JS modified (an instance handled by the default):
Example:
    @unboxed type t = D(Date.t) | R(RegExp.t) | O({x: int}) | S(string) | I(int) | F(int => int)
    let f = v => switch v { | O(_) => "O" | S(_) => "S" | _ => "other" }
Before (js):
    switch (typeof v) {
      case "object" :
        return "O";
After (js):
    if (v instanceof RegExp || v instanceof Date) {
      return "other";
    }
    switch (typeof v) {
      case "object" :
        return "O";

JS modified (a literal handled by the default):
Example:
    @unboxed type t = | @as("lit") Lit | S(string) | I(int) | B(bool) | O({x: int}) | F(int => int)
    let f = v => switch v { | S(_) => "S" | O(_) => "O" | _ => "other" }
Before (js):
    switch (typeof v) {
      case "string" :
        return "S";
After (js):
    if (v === "lit") {
      return "other";
    }
    switch (typeof v) {
      case "string" :
        return "S";

Fixes: #8995
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>
@github-actions

Copy link
Copy Markdown

Changelog

  • 🐛 Fix untagged variant switches with a default arm matching class instances in the object case and literals in a payload case of their kind.

Semantic Diff

1 commit · Full page

Keep cases handled by a default arm out of the arms a switch names

b35f485 · cknitt + Claude · Oct 24, 2026, 06:17 UTC

Why

A switch on an untagged variant with a _ arm let the cases it does not name reach the arms it names: a Date matched the object arm, since only arrays were guarded, and an @as("lit") literal matched the string arm, since the literal test was skipped when the switch names no literal (#8995).

The switch (typeof x) is preceded by a guard sending every instance case without an arm to the default, as it already did for arrays. The literal test is skipped only when no literal of the variant would pass the check of a payload arm. Rules 7 and 9 of the spec state both.

JS modified (an instance handled by the default)

@unboxed type t = D(Date.t) | R(RegExp.t) | O({x: int}) | S(string) | I(int) | F(int => int)
let f = v => switch v { | O(_) => "O" | S(_) => "S" | _ => "other" }

js

+if (v instanceof RegExp || v instanceof Date) {
+  return "other";
+}
 switch (typeof v) {
   case "object" :
     return "O";

JS modified (a literal handled by the default)

@unboxed type t = | @as("lit") Lit | S(string) | I(int) | B(bool) | O({x: int}) | F(int => int)
let f = v => switch v { | S(_) => "S" | O(_) => "O" | _ => "other" }

js

+if (v === "lit") {
+  return "other";
+}
 switch (typeof v) {
   case "string" :
     return "S";

@codecov

codecov Bot commented Oct 24, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 74.07407% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.29%. Comparing base (d8db3c6) to head (b35f485).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
compiler/core/lam_compile.ml 74.07% 7 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master    #9010   +/-   ##
=======================================
  Coverage   81.29%   81.29%           
=======================================
  Files         456      456           
  Lines       63836    63852   +16     
=======================================
+ Hits        51893    51908   +15     
- Misses      11943    11944    +1     
Files with missing lines Coverage Δ
compiler/core/lam_compile.ml 90.14% <74.07%> (-0.06%) ⬇️

... 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 24, 2026

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: b35f485

@cknitt
cknitt marked this pull request as ready for review October 24, 2026 06:41
@cknitt

cknitt commented Oct 24, 2026

Copy link
Copy Markdown
Member Author

@codex review

@github-actions

Copy link
Copy Markdown

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 24, 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-24T06:43:03.844777Z b35f485 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: b35f4859eb

ℹ️ 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 24, 2026 06:45
@cknitt
cknitt merged commit 0822b4d into master Oct 24, 2026
26 checks passed
@cknitt
cknitt deleted the untagged-default-arm branch October 24, 2026 08:46
cknitt added a commit that referenced this pull request Oct 24, 2026
Why:
    Two forms still ran class checks on primitives: the two-arm `if`, with
    the class arm declared first, and the literal test, in its form of
    negated payload checks. And the guard that sends instance cases without
    an arm to the default (#9010) knew only object instances, while a
    declared class can have callable instances, and its class checks ran
    before the primitive arms.

Of a class arm and an arm of another kind, the two-arm form checks the
other one. A variant with class cases tests its literals by comparison. The
guard for instance cases without an arm also protects a function arm from
class instances, and when the guard has class checks, the primitive arms are
tested first. The spec's rule 10 example of an unchecked branch changes
accordingly: with `PPromise(promise<int>) | PFloat(float)`, the float case
is checked, so a thenable that is not a `Promise` lands in `PPromise`.

JS modified (an instance handled by the default):
Example:
    @unboxed type t = D(Date.t) | R(RegExp.t) | O({x: int}) | S(string) | I(int) | F(int => int)
    let f = v => switch v { | O(_) => "O" | S(_) => "S" | _ => "other" }
Before (js):
    if (v instanceof RegExp || v instanceof Date) {
      return "other";
    }
    switch (typeof v) {
      case "object" :
        return "O";
      case "string" :
        return "S";
After (js):
    switch (typeof v) {
      case "string" :
        return "S";
      default:
        if (v instanceof RegExp || v instanceof Date) {
          return "other";
        }
        switch (typeof v) {
          case "object" :
            return "O";

JS modified (two arms):
Example:
    @unboxed type t = W(weird) | S(string)
Before (js): if (v instanceof Weird) { return "W"; } else { return "S"; }
After (js): if (typeof v === "string") { return "S"; } else { return "W"; }

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 added a commit that referenced this pull request Oct 24, 2026
Why:
    Two forms still ran class checks on primitives: the two-arm `if`, with
    the class arm declared first, and the literal test, in its form of
    negated payload checks. And the guard that sends instance cases without
    an arm to the default (#9010) knew only object instances, while a
    declared class can have callable instances, and its class checks ran
    before the primitive arms.

Of a class arm and an arm of another kind, the two-arm form checks the
other one. A variant with class cases tests its literals by comparison. The
guard for instance cases without an arm also protects a function arm from
class instances, and when the guard has class checks, the primitive arms are
tested first. The spec's rule 10 example of an unchecked branch changes
accordingly: with `PPromise(promise<int>) | PFloat(float)`, the float case
is checked, so a thenable that is not a `Promise` lands in `PPromise`.

JS modified (an instance handled by the default):
Example:
    @unboxed type t = D(Date.t) | R(RegExp.t) | O({x: int}) | S(string) | I(int) | F(int => int)
    let f = v => switch v { | O(_) => "O" | S(_) => "S" | _ => "other" }
Before (js):
    if (v instanceof RegExp || v instanceof Date) {
      return "other";
    }
    switch (typeof v) {
      case "object" :
        return "O";
      case "string" :
        return "S";
After (js):
    switch (typeof v) {
      case "string" :
        return "S";
      default:
        if (v instanceof RegExp || v instanceof Date) {
          return "other";
        }
        switch (typeof v) {
          case "object" :
            return "O";

JS modified (two arms):
Example:
    @unboxed type t = W(weird) | S(string)
Before (js): if (v instanceof Weird) { return "W"; } else { return "S"; }
After (js): if (typeof v === "string") { return "S"; } else { return "W"; }

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

Untagged variant switch with a default arm matches instances and literals in the wrong case

2 participants