Skip to content

Compare literals by value when simplifying equality conjunctions - #9114

Merged
cknitt merged 1 commit into
masterfrom
fix-9099-float-const-eq
Oct 28, 2026
Merged

cknitt merged 1 commit into
masterfrom
fix-9099-float-const-eq

Conversation

@cknitt

@cknitt cknitt commented Oct 28, 2026

Copy link
Copy Markdown
Member

x == 2.5 && x == 2.5 on a float compiled to false, and x != 2.5 && x == 2.5 to x === 2.5. The same mistake sent untagged-variant matches to the wrong arm: with @unboxed type t = @as(1) L1 | N(float) | S(string), switch v { | N(1.0) => 4 | _ => 6 } returned 4 for L1, although the compiler warns that 1 always matches as L1.

The and-simplifier's rules for x === c1 && x === c2 and x !== c1 && x === c2 treated "not Js_analyzer.eq_expression" as "different constants". eq_expression is a conservative structural check: it never equates two float literals, nor an int literal with a float literal. The rules now use a new Js_analyzer.eq_literal, which compares literals the way === does (numbers by value, so 1 equals 1.0; NaN differs from itself; bigints never equal numbers; undefined equals undefined whatever its unit flag). It answers None for a float literal it cannot parse, and the rules then leave the expression alone.

New test tests/tests/src/number_literal_simplify_test.res checks at run time equal and different float literals under ==/!=, and the two untagged cases from the issue (an int tag and a float tag equal to the payload literal). The arguments go through an opaque identity so the calls are not folded to constants. The test fails without the fix.

Fixes #9099

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gGkPma9LAZimuQjnrP

…== c2

Why:
    The simplifier read "not eq_expression" as "different constants", but
    eq_expression never equates float literals, nor an int with a float. So
    `x === 2.5 && x === 2.5` became `false`, and an untagged case whose
    payload literal equals a literal tag (`@as(1) L1 | N(float)`, `N(1.0)`)
    went to the wrong arm. Js_analyzer.eq_literal now decides literal
    equality as `===` does, and the rules apply only when it knows.

JS modified (and-simplification of literal comparisons):
Example:
    let a = (x: float) => x == 2.5 && x == 2.5
    let b = (x: float) => x != 2.5 && x == 2.5
    @unboxed type t = @as(1) L1 | N(float) | S(string)
    let f = (v: t) => switch v { | N(1.0) => 4 | _ => 6 }
Before (js):
    function a(x) {
      return false;
    }
    function b(x) {
      return x === 2.5;
    }
    function f(v) {
      if (v !== 1.0) {
        return 6;
      } else {
        return 4;
      }
    }
After (js):
    function a(x) {
      return x === 2.5;
    }
    function b(x) {
      return false;
    }
    function f(v) {
      return 6;
    }

Fixes: #9099
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 the JavaScript simplifier treating equal float literals, or an int and a float literal of the same value, as different constants, which turned x == 2.5 && x == 2.5 into false.

Semantic Diff

1 commit · Full page

Compare literals by JavaScript value when simplifying x === c1 && x === c2

518b26e · cknitt + Claude · Oct 28, 2026, 15:31 UTC

Why

The simplifier read "not eq_expression" as "different constants", but eq_expression never equates float literals, nor an int with a float. So x === 2.5 && x === 2.5 became false, and an untagged case whose payload literal equals a literal tag (@as(1) L1 | N(float), N(1.0)) went to the wrong arm. Js_analyzer.eq_literal now decides literal equality as === does, and the rules apply only when it knows.

JS modified (and-simplification of literal comparisons)

let a = (x: float) => x == 2.5 && x == 2.5
let b = (x: float) => x != 2.5 && x == 2.5
@unboxed type t = @as(1) L1 | N(float) | S(string)
let f = (v: t) => switch v { | N(1.0) => 4 | _ => 6 }

js

 function a(x) {
-  return false;
+  return x === 2.5;
 }
 function b(x) {
-  return x === 2.5;
+  return false;
 }
 function f(v) {
-  if (v !== 1.0) {
-    return 6;
-  } else {
-    return 4;
-  }
+  return 6;
 }

@codecov

codecov Bot commented Oct 28, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 43.33333% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.45%. Comparing base (85f31fb) to head (518b26e).

Files with missing lines Patch % Lines
compiler/core/js_analyzer.ml 44.44% 10 Missing ⚠️
compiler/core/js_exp_make.ml 41.66% 7 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9114      +/-   ##
==========================================
- Coverage   81.46%   81.45%   -0.01%     
==========================================
  Files         460      460              
  Lines       64675    64695      +20     
==========================================
+ Hits        52689    52699      +10     
- Misses      11986    11996      +10     
Files with missing lines Coverage Δ
compiler/core/js_exp_make.ml 65.43% <41.66%> (-0.05%) ⬇️
compiler/core/js_analyzer.ml 80.11% <44.44%> (-4.07%) ⬇️

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

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 518b26e

@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:43.267114Z 518b26e 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. Delightful!

Reviewed commit: 518b26ee8c

ℹ️ 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:55
@cknitt
cknitt requested a review from cristianoc October 28, 2026 17:55
@cknitt
cknitt enabled auto-merge (squash) October 28, 2026 17:57

@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. Comparing literal values separately from structural expression equality fixes the float and int/float cases in #9099, with a conservative fallback when parsing fails. CI is green. The committed JS regression tests also pass in a minimal runtime harness; I did not rebuild the compiler locally.

@cknitt
cknitt merged commit 6cf78f6 into master Oct 28, 2026
26 checks passed
@cknitt
cknitt deleted the fix-9099-float-const-eq branch October 28, 2026 18:44
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.

Float constants never compare equal in the JS simplifier: x == 2.5 && x == 2.5 compiles to false

2 participants