Repository navigation
Conversation
Changelog🐛 Bug fix
Semantic Diff1 commit · Full page Merge null and undefined checks only of operands without side effectsd6de460 · mununki · Nov 22, 2026, 04:48 UTC
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)
function check(next) {
- console.log(next() == null);
+ console.log(next() === null || next() === undefined);
}
function field(r) {
console.log(r.v == null);
}Review
|
✅ Compiler benchmarksAllocated wordsIn 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. InstructionsIn total: 9,221,447,040 → 9,221,397,724 (±0.00%). None of the 33 benchmarks changed by 0.1% or more. Output sizeIn 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 Report✅ All modified and coverable lines are covered by tests. 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
🚀 New features to boost your workflow:
|
rescript
@rescript/belt
@rescript/darwin-arm64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
|
Developer playground preview: https://rescript-lang.github.io/rescript/dev-playground/?version=pr-9376 |
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>
01b155a to
d6de460
Compare
Fixes: #9355
Js_dumpprintede === null || e === undefined, and the same with the checks swapped, ase == nullwhenever the two operands were the same expression.e == nullevaluateseonce where the checks may evaluate it twice, so the result changed whenehas side effects: withnext()returning1and thenundefined,next() === Nullable.null || next() === Nullable.undefinedwasfalseafter one call instead oftrueafter two.The checks are now merged only when
Js_analyzer.no_side_effect_expressionacceptse.r.v == nullstays.Tests:
null_check_merge_test.rescheckse === null || e === undefinedwith a call that returns1and thenundefined, and with a field read. Compiled with master, the check of the call isfalse.Checklist
changes/README.md), or no release notes are needed and a maintainer should applychangelog:skip.CONTRIBUTING.md).git commit -s).