Skip to content

Merge null and undefined checks only of operands without side effects - #9376

Open
mununki wants to merge 1 commit into
masterfrom
merge-null-checks-of-pure-expressions
Open

mununki wants to merge 1 commit into
masterfrom
merge-null-checks-of-pure-expressions

Conversation

@mununki

@mununki mununki commented Nov 22, 2026

Copy link
Copy Markdown
Member

Fixes: #9355

Js_dump printed e === null || e === undefined, and the same with the checks swapped, as e == null whenever the two operands were the same expression. e == null evaluates e once where the checks may evaluate it twice, so the result changed when e has side effects: with next() returning 1 and then undefined, next() === Nullable.null || next() === Nullable.undefined was false after one call instead of true after two.

The checks are now merged only when Js_analyzer.no_side_effect_expression accepts e.

  • Operands without side effects are still merged, such as variables and the fields read from them: r.v == null stays.
  • No checked-in output changes.

Tests: null_check_merge_test.res checks e === null || e === undefined with a call that returns 1 and then undefined, and with a field read. Compiled with master, the check of the call is false.

Checklist

  • Added a changelog fragment (see changes/README.md), or no release notes are needed and a maintainer should apply changelog:skip.
  • Added tests for the fix or feature.
  • Gave every commit a description below its subject, saying why it is made (see "Commit messages" in CONTRIBUTING.md).
  • Signed off every commit for the DCO (git commit -s).

@github-actions

github-actions Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Fix e === null || e === undefined compiling to e == null, which evaluates e once, when e has side effects.

Semantic Diff

1 commit · Full page

Merge null and undefined checks only of operands without side effects

d6de460 · mununki · Nov 22, 2026, 04:48 UTC

Why

Js_dump printed e === null || e === undefined, and the same with the checks swapped, as e == null whenever the two operands were the same expression. e == null evaluates e once where the checks may evaluate it twice, so the result changed when e has side effects: with next() returning 1 and then undefined, the check was false after one call instead of true after two. The checks are now merged only when Js_analyzer.no_side_effect_expression accepts e.

JS modified (null and undefined checks)

let check = (next: unit => Nullable.t<int>) =>
  Console.log(next() === Nullable.null || next() === Nullable.undefined)
type r = {v: Nullable.t<int>}
let field = (r: r) => Console.log(r.v === Nullable.null || r.v === Nullable.undefined)

js

 function check(next) {
-  console.log(next() == null);
+  console.log(next() === null || next() === undefined);
 }
 
 function field(r) {
   console.log(r.v == null);
 }

Review

  • Which operands are still merged? Those without side effects, such as variables and the fields read from them: r.v == null stays.
  • Which checked-in outputs change? None.

@github-actions

github-actions Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

✅ Compiler benchmarks

Allocated words

In total: 636,515,521 → 636,516,236 (±0.00%).

Generated projects: 629,040,730 → 629,041,236 (±0.00%).

None of the 265 benchmarks changed by 0.1% or more.

Instructions

In total: 9,221,447,040 → 9,221,397,724 (±0.00%).

None of the 33 benchmarks changed by 0.1% or more.

Output size

In total: 25,962,507 → 25,962,485 (±0.00%).

None of the 13 benchmarks changed by 0.1% or more.

Pull request d6de460 compared with base 643e9c0 in this run.

@codecov

codecov Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.21%. Comparing base (643e9c0) to head (d6de460).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #9376   +/-   ##
=======================================
  Coverage   82.21%   82.21%           
=======================================
  Files         460      460           
  Lines       65551    65552    +1     
=======================================
+ Hits        53891    53892    +1     
  Misses      11660    11660           
Files with missing lines Coverage Δ
compiler/backend/js_dump.ml 94.07% <100.00%> (+<0.01%) ⬆️
🚀 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 Nov 22, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: d6de460

@github-actions

Copy link
Copy Markdown

@mununki
mununki requested review from cknitt and cristianoc November 22, 2026 14:18
Why:
    `Js_dump` printed `e === null || e === undefined`, and the same with
    the checks swapped, as `e == null` whenever the two operands were the
    same expression. `e == null` evaluates `e` once where the checks may
    evaluate it twice, so the result changed when `e` has side effects:
    with `next()` returning `1` and then `undefined`, the check was `false`
    after one call instead of `true` after two. The checks are now merged
    only when `Js_analyzer.no_side_effect_expression` accepts `e`.

JS modified (null and undefined checks):
Example:
    let check = (next: unit => Nullable.t<int>) =>
      Console.log(next() === Nullable.null || next() === Nullable.undefined)
    type r = {v: Nullable.t<int>}
    let field = (r: r) => Console.log(r.v === Nullable.null || r.v === Nullable.undefined)
Before (js):
    function check(next) {
      console.log(next() == null);
    }

    function field(r) {
      console.log(r.v == null);
    }
After (js):
    function check(next) {
      console.log(next() === null || next() === undefined);
    }

    function field(r) {
      console.log(r.v == null);
    }

Review: Which operands are still merged? Those without side effects, such
    as variables and the fields read from them: `r.v == null` stays.
Review: Which checked-in outputs change? None.

Signed-off-by: mununki <woonki.moon@gmail.com>
@mununki
mununki force-pushed the merge-null-checks-of-pure-expressions branch from 01b155a to d6de460 Compare November 23, 2026 08:31

This branch has not been deployed

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

e === null || e === undefined compiles to e == null, which evaluates e once

1 participant