Skip to content

Keep both checks in x !== true && x !== false - #9113

Merged
cknitt merged 1 commit into
masterfrom
fix-9098-bool-neq-simplify
Oct 28, 2026
Merged

cknitt merged 1 commit into
masterfrom
fix-9098-bool-neq-simplify

Conversation

@cknitt

@cknitt cknitt commented Oct 28, 2026

Copy link
Copy Markdown
Member

Untagged variants with boolean literal cases could be miscompiled: a match with both @as(true) and @as(false) cases that doesn't name them dropped its other checks (switch v { | D(_) => 2 | _ => 4 } compiled to return 4), and a bool payload beside one boolean literal checked the wrong thing (Bo(false) matched S("hello")).

The cause is a rule in Js_exp_make.simplify_and_ that folded x === b1 && x === b2 and x !== b1 && x !== b2 on boolean constants as if x were a boolean, so x !== true && x !== false became false (and, through negation in simplify_or_, x === true || x === false became true). In an untagged variant x can be any payload. The rule is removed. The general constant rules that follow it already fold the combinations that hold for every x (equal constants, x === a && x === b with different constants, mixed ===/!==), so the only change in output is that the unsound case is kept.

The new test tests/tests/src/untagged_bool_literals_test.res runs both programs from the issue and a match with Lt | Lf in one case, on every constructor; all three tests fail with the previous compiler. No other generated test output changes.

Fixes #9098

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gGkPma9LAZimuQjnrP

Why:
    The JS simplifier rewrote `x !== true && x !== false` to `false` (and
    `x === true || x === false` to `true`) as if `x` were a boolean. In an
    untagged variant `x` can be any payload, so matches with both boolean
    literal cases, or a `bool` payload beside one, lost their other checks
    (#9098). The boolean-only rule is removed; the general constant rules
    below it still fold every case that holds for any `x`.

JS modified (untagged match with both boolean literals):
Example:
    @unboxed type t = D(Date.t) | @as(true) Lt | @as(false) Lf | R(RegExp.t)
    let g = (v: t) => switch v { | D(_) => 2 | _ => 4 }
    let h = (v: t) => switch v { | Lt | Lf => 1 | _ => 3 }
Before (js):
    function g(v) {
      return 4;
    }

    function h(v) {
      return 1;
    }
After (js):
    function g(v) {
      if (v === true || v === false || !(v instanceof Date)) {
        return 4;
      } else {
        return 2;
      }
    }

    function h(v) {
      if (v === true || v === false) {
        return 1;
      } else {
        return 3;
      }
    }

JS modified (bool payload beside a boolean literal):
Example:
    @unboxed type t = Bo(bool) | S(string) | @as(true) Lt
    let f = (v: t) => switch v { | Bo(false) => 2 | _ => 6 }
Before (js):
    if (v === true) {
After (js):
    if (v !== false) {

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Kbf3gGkPma9LAZimuQjnrP
Signed-off-by: Christoph Knittel <[email protected]>
@github-actions

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Fix untagged variant matches involving boolean literals that skipped the checks for the other cases.

Semantic Diff

1 commit · Full page

Keep both boolean checks in x !== true && x !== false

f61e918 · cknitt + Claude · Oct 28, 2026, 15:27 UTC

Why

The JS simplifier rewrote x !== true && x !== false to false (and x === true || x === false to true) as if x were a boolean. In an untagged variant x can be any payload, so matches with both boolean literal cases, or a bool payload beside one, lost their other checks (#9098). The boolean-only rule is removed; the general constant rules below it still fold every case that holds for any x.

JS modified (untagged match with both boolean literals)

@unboxed type t = D(Date.t) | @as(true) Lt | @as(false) Lf | R(RegExp.t)
let g = (v: t) => switch v { | D(_) => 2 | _ => 4 }
let h = (v: t) => switch v { | Lt | Lf => 1 | _ => 3 }

js

 function g(v) {
-  return 4;
+  if (v === true || v === false || !(v instanceof Date)) {
+    return 4;
+  } else {
+    return 2;
+  }
 }
 
 function h(v) {
-  return 1;
+  if (v === true || v === false) {
+    return 1;
+  } else {
+    return 3;
+  }
 }

JS modified (bool payload beside a boolean literal)

@unboxed type t = Bo(bool) | S(string) | @as(true) Lt
let f = (v: t) => switch v { | Bo(false) => 2 | _ => 6 }

js

-if (v === true) {
+if (v !== false) {

@codecov

codecov Bot commented Oct 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.47%. Comparing base (85f31fb) to head (f61e918).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9113      +/-   ##
==========================================
+ Coverage   81.46%   81.47%   +0.01%     
==========================================
  Files         460      460              
  Lines       64675    64668       -7     
==========================================
+ Hits        52689    52691       +2     
+ Misses      11986    11977       -9     
Files with missing lines Coverage Δ
compiler/core/js_exp_make.ml 66.42% <ø> (+0.94%) ⬆️
🚀 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 28, 2026

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: f61e918

@github-actions

Copy link
Copy Markdown

@cknitt

cknitt commented Oct 28, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 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-28T16:42:03.406625Z f61e918 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: f61e918d42

ℹ️ 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 marked this pull request as ready for review October 28, 2026 17:53
@cknitt
cknitt requested a review from cristianoc October 28, 2026 17:53
@cknitt
cknitt enabled auto-merge (squash) October 28, 2026 17:56

@cristianoc cristianoc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good. Removing the boolean-only rule preserves the non-boolean payload checks, and the regression tests cover both examples from #9098 plus the combined literal arm. CI is green. The committed JS tests also pass in a minimal runtime harness; I did not rebuild the compiler locally.

@cknitt
cknitt merged commit d9fc473 into master Oct 28, 2026
26 checks passed
@cknitt
cknitt deleted the fix-9098-bool-neq-simplify branch October 28, 2026 18:43
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.

x !== true && x !== false is simplified to false when x is not a boolean

2 participants