Semantic Diff

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

1 commit

643e9c03dbf69629e7d42ce5652a50b828982210..pr-9376

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

Merge null and undefined checks only of operands without side effects

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.

modifiedJS 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
Before
After
function check(next) {function check(next) { console.log(next() == null); console.log(next() === null || next() === undefined);}} function field(r) {function field(r) { console.log(r.v == null); 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.