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 == nullstays. - Which checked-in outputs change? None.