Skip to content

Drop leftover conditions and empty branches from if statements - #9024

Merged
cknitt merged 3 commits into
masterfrom
drop-pure-empty-if
Oct 24, 2026
Merged

cknitt merged 3 commits into
masterfrom
drop-pure-empty-if

Conversation

@cknitt

@cknitt cknitt commented Oct 24, 2026 •

Copy link
Copy Markdown
Member

Two places in JS generation left an if behind with nothing to run in a branch. Removing those branches negates more conditions, so a negation bug that was already on master is fixed first.

1. Negated float comparisons. Js_exp_make.not negated a comparison by swapping its operator. That's wrong for floats, because a comparison with NaN is always false:

let k = (x: float) => !(x < 0.0)

Before:

function k(x) {
  return x >= 0.0;
}

After:

function k(x) {
  return !(x < 0.0);
}

k(NaN) returned false instead of true. Float comparisons (Pfloatcomp) and comparisons of operands of unknown type (Pjscomp, from %unsafe_lt etc.) now use their own operators, Float_lt, Float_le, Float_gt and Float_ge. They are printed as <, <=, >, >=, and not leaves them as !(…). Int, bigint and string comparisons are unchanged, since their operands can't be NaN. The only existing output this changes is caml_compare_bigint_test.mjs, whose values come from @val external bigint: string => float and so are typed as floats. New test: negated_float_comparison_test.res.

Fixes #9026.

2. A bare condition. Js_stmt_make.if_ turned an if with two empty branches into its condition as a statement. This happened, for example, when the remaining cases of a switch all fall through. The condition is now dropped when no_side_effect_expression accepts it. Empty blocks in the branches are ignored, so an enclosing if can still merge conditions. The both-branches-pure case in Lam_compile now goes through the same path.

let f3 = ({rank: lhs, _}, {rank: rhs}) =>
  switch (lhs, rhs) {
  | (Ranked(x), Ranked(y)) => Pervasives.compare(x, y)
  | _ => assert(false)
  }

Before:

function f3(param, param$1) {
  let lhs = param.rank;
  let rhs = param$1.rank;
  if (typeof lhs !== "object") {
    lhs === "Uninitialized";
  } else {
    if (typeof rhs === "object") {
      return Primitive_int.compare(lhs._0, rhs._0);
    }
    rhs === "Uninitialized";
  }
  throw { ... };
}

After:

function f3(param, param$1) {
  let lhs = param.rank;
  let rhs = param$1.rank;
  if (typeof lhs === "object" && typeof rhs === "object") {
    return Primitive_int.compare(lhs._0, rhs._0);
  }
  throw { ... };
}

3. An empty branch. Js_pass_flatten turns a statement c ? b : e into if (c) { b; } else { e; }. When b or e was pure, a later pass removed it and left {}. A pure branch is now left out: with an empty then-branch the condition is negated, and with both empty the condition is dropped when it is pure.

let f = (x: int) => {
  switch x {
  | 1 => r := 2
  | _ => ()
  }
  Console.log(r)
}

Before:

function f(x) {
  if (x !== 1) {
    
  } else {
    r.contents = 2;
  }
  console.log(r);
}

After:

function f(x) {
  if (x === 1) {
    r.contents = 2;
  }
  console.log(r);
}

In Stdlib_IteratorTests.mjs, a dropped condition was the only use of a let match = next(it), so the call is now a plain statement and the later match$N names are renumbered.

🤖 Generated with Claude Code

https://claude.ai/code/session_015yE4gCmTmJKVecrVt1PU8C

@github-actions

github-actions Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Fix negated float comparisons with NaN: !(x < 0.0) compiled to x >= 0.0, which is false for NaN.

💅 Polish

  • Stop emitting bare conditions like x === null; and empty if/else branches.

Semantic Diff

3 commits · Full page

✨ Keep the operator of a negated float comparison

137367c · cknitt + Claude · Oct 24, 2026, 18:32 UTC · new

Js_exp_make.not negated a comparison by swapping its operator, turning !(x < 0.0) into x >= 0.0, which is false for NaN. Float comparisons, and comparisons of operands of unknown type, now use their own operators, printed as <, <=, >, >=, which not leaves as !(…). Fixes #9026.

JS modified (negated float comparison)

let k = (x: float) => !(x < 0.0)

js

 function k(x) {
-  return x >= 0.0;
+  return !(x < 0.0);
 }

Review

  • Do int, bigint and string comparisons change? No, they keep the old operators, so negating them still swaps the operator. The only changed output is in caml_compare_bigint_test, whose bigint values are typed as float.
Drop a pure condition when both branches of an if are empty

adde988 · cknitt + Claude · Oct 24, 2026, 16:35 UTC

A switch whose remaining cases all fall through to the same code left their test behind as a bare statement, such as x === null;, and the empty block it left in an enclosing if blocked merging the conditions.

JS modified (switch with cases that fall through)

type rank = Uninitialized | Visited | Ranked(int)
let f3 = (lhs: rank, rhs: rank) =>
  switch (lhs, rhs) {
  | (Ranked(x), Ranked(y)) => Pervasives.compare(x, y)
  | _ => assert(false)
  }

js · tests/tests/src/fun_pattern_match.res

-if (typeof lhs !== "object") {
-  lhs === "Uninitialized";
-} else {
-  if (typeof rhs === "object") {
-    return Primitive_int.compare(lhs._0, rhs._0);
-  }
-  rhs === "Uninitialized";
+if (typeof lhs === "object" && typeof rhs === "object") {
+  return Primitive_int.compare(lhs._0, rhs._0);
 }
 throw {

Review

  • Is a condition with side effects still evaluated? Yes, only conditions that no_side_effect_expression accepts are dropped.
Leave no empty branch when flattening a conditional statement

6e90587 · cknitt + Claude · Oct 24, 2026, 17:09 UTC

The flatten pass turned a statement c ? b : e into an if with b; and e; as branches; a pure branch value such as undefined was dropped later and left an empty branch behind.

JS modified (switch with an empty case at statement level)

let r = ref(0)
let f = (x: int) => {
  switch x {
  | 1 => r := 2
  | _ => ()
  }
  Console.log(r)
}

js

-if (x !== 1) {
-
-} else {
+if (x === 1) {
   r.contents = 2;
 }

Review

  • Why not build the if with S.if_? It folds an if/else of two expression statements back into a conditional expression, which is what this pass undoes.

@cknitt

cknitt commented Oct 24, 2026

Copy link
Copy Markdown
Member Author

@codex review

@cknitt
cknitt marked this pull request as ready for review October 24, 2026 16:36
@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-24T19:45:03.934796Z 6e90587 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. Bravo.

Reviewed commit: 2f29cd28e5

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

@codecov

codecov Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.88889% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.32%. Comparing base (188e24e) to head (6e90587).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
compiler/core/js_pass_flatten.ml 62.50% 3 Missing ⚠️
compiler/core/lam_compile_util.ml 85.71% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master    #9024   +/-   ##
=======================================
  Coverage   81.32%   81.32%           
=======================================
  Files         456      456           
  Lines       64017    64042   +25     
=======================================
+ Hits        52059    52082   +23     
- Misses      11958    11960    +2     
Files with missing lines Coverage Δ
compiler/core/js_exp_make.ml 68.46% <100.00%> (+0.18%) ⬆️
compiler/core/js_op_util.ml 91.07% <100.00%> (ø)
compiler/core/js_stmt_make.ml 71.92% <100.00%> (+0.63%) ⬆️
compiler/core/lam_compile.ml 90.25% <100.00%> (ø)
compiler/core/lam_compile_primitive.ml 81.25% <100.00%> (+0.11%) ⬆️
compiler/core/lam_compile_util.ml 95.23% <85.71%> (-4.77%) ⬇️
compiler/core/js_pass_flatten.ml 76.74% <62.50%> (-4.34%) ⬇️

... and 3 files 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.

@cknitt
cknitt requested a review from cristianoc October 24, 2026 16:55
@cknitt
cknitt enabled auto-merge (squash) October 24, 2026 16:55
@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@9024

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 6e90587

@cknitt cknitt changed the title Drop a pure condition when both branches of an if are empty Drop leftover conditions and empty branches from if statements Oct 24, 2026
@cknitt

cknitt commented Oct 24, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f074ff9361

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

Comment thread compiler/core/js_pass_flatten.ml
@github-actions

Copy link
Copy Markdown

cknitt and others added 3 commits October 24, 2026 18:32
Js_exp_make.not negated a comparison by swapping its operator, turning
!(x < 0.0) into x >= 0.0, which is false for NaN. Float comparisons, and
comparisons of operands of unknown type, now use their own operators,
printed as <, <=, >, >=, which not leaves as !(…). Fixes #9026.

JS modified (negated float comparison):
Example:
    let k = (x: float) => !(x < 0.0)
Before (js):
    function k(x) {
      return x >= 0.0;
    }
After (js):
    function k(x) {
      return !(x < 0.0);
    }

Review: Do int, bigint and string comparisons change? No, they keep the
old operators, so negating them still swaps the operator. The only
changed output is in caml_compare_bigint_test, whose bigint values are
typed as float.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015yE4gCmTmJKVecrVt1PU8C
Signed-off-by: Christoph Knittel <ck@cca.io>
A switch whose remaining cases all fall through to the same code left
their test behind as a bare statement, such as `x === null;`, and the
empty block it left in an enclosing if blocked merging the conditions.

JS modified (switch with cases that fall through):
Example:
    type rank = Uninitialized | Visited | Ranked(int)
    let f3 = (lhs: rank, rhs: rank) =>
      switch (lhs, rhs) {
      | (Ranked(x), Ranked(y)) => Pervasives.compare(x, y)
      | _ => assert(false)
      }
Before (js): tests/tests/src/fun_pattern_match.res
    if (typeof lhs !== "object") {
      lhs === "Uninitialized";
    } else {
      if (typeof rhs === "object") {
        return Primitive_int.compare(lhs._0, rhs._0);
      }
      rhs === "Uninitialized";
    }
    throw {
After (js): tests/tests/src/fun_pattern_match.res
    if (typeof lhs === "object" && typeof rhs === "object") {
      return Primitive_int.compare(lhs._0, rhs._0);
    }
    throw {

Review: Is a condition with side effects still evaluated? Yes, only
conditions that no_side_effect_expression accepts are dropped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015yE4gCmTmJKVecrVt1PU8C
Signed-off-by: Christoph Knittel <ck@cca.io>
The flatten pass turned a statement `c ? b : e` into an if with `b;` and
`e;` as branches; a pure branch value such as `undefined` was dropped
later and left an empty branch behind.

JS modified (switch with an empty case at statement level):
Example:
    let r = ref(0)
    let f = (x: int) => {
      switch x {
      | 1 => r := 2
      | _ => ()
      }
      Console.log(r)
    }
Before (js):
    if (x !== 1) {

    } else {
      r.contents = 2;
    }
After (js):
    if (x === 1) {
      r.contents = 2;
    }

Review: Why not build the if with S.if_? It folds an if/else of two
expression statements back into a conditional expression, which is
what this pass undoes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015yE4gCmTmJKVecrVt1PU8C
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt force-pushed the drop-pure-empty-if branch from f074ff9 to 6e90587 Compare October 24, 2026 19:43
@cknitt

cknitt commented Oct 24, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 6e90587623

ℹ️ 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 d48d577 into master Oct 24, 2026
26 checks passed
@cknitt
cknitt deleted the drop-pure-empty-if branch October 24, 2026 19:58
e-cline added a commit to e-cline/rescript that referenced this pull request Oct 24, 2026
Brings in the 13 commits on master since the branch started, through
rescript-lang#9024. Only compiler/ml/depend.ml and yarn.lock changed on both sides,
in separate places: master adds the `finally` of `try` to the
dependency walk, and the branch records every prefix of a free path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Ethan Cline <7142767+e-cline@users.noreply.github.com>
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.

Negated float comparison is wrong for NaN: !(x < 0.0) compiles to x >= 0.0

2 participants