From 0af703747ba0dab11a069429a8474a9477d5ebf8 Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Tue, 1 Dec 2026 21:15:36 +0000 Subject: [PATCH] Merge null and undefined checks with source maps on too MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Why: With source maps on, the two reads of `e` in `e === null || e === undefined` carry different source locations. `Js_dump` compared them with `=`, which includes the locations, so it printed both checks instead of `e == null`, and the JavaScript depended on whether source maps were on. It now compares them with `Js_analyzer.eq_expression`, which ignores locations. Fixes #9502. JS modified (null and undefined checks with source maps): Example: let r = opaque({v: Nullable.null}) eq(__LOC__, r.v === Nullable.null || r.v === Nullable.undefined, true) Before (js): tests/compiler_tests/src/null_check_merge_test.res, with source maps Test_utils.eq("…", r.v === null || r.v === undefined, true); After (js): with source maps, the same as without them Test_utils.eq("…", r.v == null, true); Review: Can `eq_expression` stop a merge that `=` allowed? It answers false for some shapes, such as floats or `Length`. Building tests/compiler_tests without source maps gives the committed output, so no merge there is lost. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kbf3gKGdW5oiHFukU4zkSu Signed-off-by: Christoph Knittel --- changes/null-check-merge-source-maps.fix.md | 1 + compiler/backend/js_dump.ml | 7 +++++-- tests/project_tests/source_map/input.js | 17 +++++++++++++++-- tests/project_tests/source_map/src/Helper.res | 7 +++++++ 4 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 changes/null-check-merge-source-maps.fix.md diff --git a/changes/null-check-merge-source-maps.fix.md b/changes/null-check-merge-source-maps.fix.md new file mode 100644 index 0000000000..0b4a6b704a --- /dev/null +++ b/changes/null-check-merge-source-maps.fix.md @@ -0,0 +1 @@ +- Print `e == null` for a null and undefined check with source maps on too, so source maps no longer change the generated JavaScript. diff --git a/compiler/backend/js_dump.ml b/compiler/backend/js_dump.ml index 2c6590ff0d..770451a16b 100644 --- a/compiler/backend/js_dump.ml +++ b/compiler/backend/js_dump.ml @@ -865,9 +865,12 @@ and expression_desc cxt ~(level : int) f x : cxt = ( Or, {expression_desc = Bin (EqEqEq, e1, {expression_desc = Undefined _})}, {expression_desc = Bin (EqEqEq, e2, {expression_desc = Null})} ) - when e1 = e2 && Js_analyzer.no_side_effect_expression e1 -> + when Js_analyzer.eq_expression e1 e2 + && Js_analyzer.no_side_effect_expression e1 -> (* [e == null] evaluates [e] once, where the two checks may evaluate it - twice, so they are merged only for an [e] without side effects *) + twice, so they are merged only for an [e] without side effects. + [eq_expression] ignores source locations, which differ between the two + operands when source maps are on. *) expression_desc cxt ~(level : int) f (Is_null_or_undefined e1) | Bin (op, e1, e2) -> let out, lft, rght = Js_op_util.op_prec op in diff --git a/tests/project_tests/source_map/input.js b/tests/project_tests/source_map/input.js index f1906ca45c..16847f71ce 100644 --- a/tests/project_tests/source_map/input.js +++ b/tests/project_tests/source_map/input.js @@ -461,7 +461,8 @@ async function assertNodeStackTraceUsesSourceMap() { } } -async function assertHiddenOutput() { +/** @param {Map} defaultOutput */ +async function assertHiddenOutput(defaultOutput) { const sourceRoot = "rescript://source-map-test/"; await removeGeneratedMapFiles(); await fs.writeFile( @@ -480,6 +481,11 @@ async function assertHiddenOutput() { /\/\/# sourceMappingURL=/, `${filename} should not include a sourceMappingURL comment in hidden mode`, ); + assert.equal( + js, + defaultOutput.get(filename), + `${filename} should be the same JS as without source maps`, + ); assert.ok(await fileExists(mapPath), `${filename}.map should exist`); assertSourceMap( filename, @@ -510,6 +516,13 @@ async function assertDefaultOutput() { false, `${filename}.map should not exist by default`, ); + if (filename.startsWith("Helper.")) { + assert.match( + js, + /value\.label == null/, + `${filename} should merge the null and undefined checks`, + ); + } output.set(filename, js); } @@ -577,7 +590,7 @@ try { const defaultOutput = await assertDefaultOutput(); await assertLinkedOutput(); await assertDisabledOutput(defaultOutput); - await assertHiddenOutput(); + await assertHiddenOutput(defaultOutput); await assertInlineOutput(); } finally { await fs.writeFile(configPath, originalConfig); diff --git a/tests/project_tests/source_map/src/Helper.res b/tests/project_tests/source_map/src/Helper.res index 8ef1ab805e..069e72de0e 100644 --- a/tests/project_tests/source_map/src/Helper.res +++ b/tests/project_tests/source_map/src/Helper.res @@ -18,3 +18,10 @@ let unicodeLabel = "helper ν•œκΈ€ 🌏" let fail = () => JsError.throwWithMessage("helper source map") let value = pipeThrough(21, multiply) + +type maybeLabel = {label: Nullable.t} + +// The two reads of `value.label` have different source locations, which +// must not stop `value.label == null` from being printed with source maps +let isMissing = (value: maybeLabel) => + String.make(value.label === Nullable.null || value.label === Nullable.undefined)