Skip to content

Commit e8ba9f9

Browse files
authored
Print null checks with the precedence of === (#9291)
Why: `Nullable.isNullable(e)`, and any other `%is_nullable`, compiles to the JS IR node `Is_null_or_undefined e`, which `Js_dump` prints as `e == null`. It printed `e` at level 1, below the precedence of `==`, so an operand that binds more loosely lost its parentheses and the check applied to part of it: `Nullable.isNullable(c ? a : b)` compiled to `c ? a : b == null`, which JavaScript reads as `c ? a : (b == null)`. The same happened to `||`, `&&` and bitwise operands, and an object in an arrow function's body gave a syntax error. It also put the whole check in parentheses wherever it was not at the top level, which the output of `Stdlib_Nullable` shows. Both now follow `Js_op_util.op_prec EqEqEq`, as `x === null` does: parentheses when the level is above that of `==`, and the operand at its left level. JS modified (operand of a null check): Example: let pick = (c, a: Nullable.t<int>, b: Nullable.t<int>) => Nullable.isNullable(c ? a : b) let sum = (a: int, b: int) => Nullable.isNullable(Nullable.make(a + b)) Before (js): function pick(c, a, b) { return c ? a : b == null; } function sum(a, b) { return a + b | 0 == null; } After (js): function pick(c, a, b) { return ( c ? a : b ) == null; } function sum(a, b) { return (a + b | 0) == null; } JS modified (null check as an operand): Example: let equal = (a, b, eq) => Option.equal(Nullable.toOption(a), Nullable.toOption(b), eq) Before (js): function equal(a, b, eq) { return Stdlib_Option.equal((a == null) ? undefined : Primitive_option.some(a), (b == null) ? undefined : Primitive_option.some(b), eq); } After (js): function equal(a, b, eq) { return Stdlib_Option.equal(a == null ? undefined : Primitive_option.some(a), b == null ? undefined : Primitive_option.some(b), eq); } Review: Is the precedence of `===` right for `==`? Both are equality operators with the same precedence and left associativity, so a context that takes `a === b` without parentheses takes `a == null` too: arguments, `let` initializers, `?:`, `||`, `&&`, bitwise operators and the left side of `===`. The right side of `===` and the operand of `!` still get parentheses, as in `y === (x == null)` and `!(x == null)`. Review: Which checked-in outputs change? Only `Stdlib_Nullable`, `Stdlib_TempTests` and `optional_external_arg_once_test`, where the test of `?:` loses its parentheses, and `Primitive_option`, where `x == null` as an operand of `||` does. No checked-in output had an operand that lost them. Review: Why does the conditional break over lines? `Cond` prints its own parentheses that way when it needs them, as elsewhere in the output. Review: What else prints an operand below its precedence? `In` prints both operands at level 0 (#9222), `Typeof` puts itself in no parentheses, and `Fun` ignores the level. They are separate fixes. Signed-off-by: mununki <woonki.moon@gmail.com>
1 parent 32116b5 commit e8ba9f9

10 files changed

Lines changed: 172 additions & 10 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Fix `Nullable.isNullable` of a conditional, `||` or bitwise expression testing only part of it, as in `Nullable.isNullable(c ? a : b)`.

‎compiler/core/js_dump.ml‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -754,8 +754,10 @@ and expression_desc cxt ~(level : int) f x : cxt =
754754
if need_paren then P.paren f action else action ();
755755
cxt
756756
| Is_null_or_undefined e ->
757-
P.cond_paren_group f (level > 0) (fun _ ->
758-
let cxt = expression ~level:1 cxt f e in
757+
(* Printed as [e == null], which binds like [===] *)
758+
let out, lft, _ = Js_op_util.op_prec EqEqEq in
759+
P.cond_paren_group f (level > out) (fun _ ->
760+
let cxt = expression ~level:lft cxt f e in
759761
P.space f;
760762
P.string f "==";
761763
P.space f;

‎packages/@rescript/runtime/lib/es6/Primitive_option.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function fromNull(x) {
4646
}
4747

4848
function valFromOption(x) {
49-
if ((x == null) || x.BS_PRIVATE_NESTED_SOME_NONE === undefined) {
49+
if (x == null || x.BS_PRIVATE_NESTED_SOME_NONE === undefined) {
5050
return x;
5151
}
5252
let depth = x.BS_PRIVATE_NESTED_SOME_NONE;

‎packages/@rescript/runtime/lib/es6/Stdlib_Nullable.mjs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ function fromOption(option) {
1010
}
1111

1212
function equal(a, b, eq) {
13-
return Stdlib_Option.equal((a == null) ? undefined : Primitive_option.some(a), (b == null) ? undefined : Primitive_option.some(b), eq);
13+
return Stdlib_Option.equal(a == null ? undefined : Primitive_option.some(a), b == null ? undefined : Primitive_option.some(b), eq);
1414
}
1515

1616
function compare(a, b, cmp) {
17-
return Stdlib_Option.compare((a == null) ? undefined : Primitive_option.some(a), (b == null) ? undefined : Primitive_option.some(b), cmp);
17+
return Stdlib_Option.compare(a == null ? undefined : Primitive_option.some(a), b == null ? undefined : Primitive_option.some(b), cmp);
1818
}
1919

2020
function getOr(value, $$default) {

‎packages/@rescript/runtime/lib/js/Primitive_option.cjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function fromNull(x) {
4646
}
4747

4848
function valFromOption(x) {
49-
if ((x == null) || x.BS_PRIVATE_NESTED_SOME_NONE === undefined) {
49+
if (x == null || x.BS_PRIVATE_NESTED_SOME_NONE === undefined) {
5050
return x;
5151
}
5252
let depth = x.BS_PRIVATE_NESTED_SOME_NONE;

‎packages/@rescript/runtime/lib/js/Stdlib_Nullable.cjs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ function fromOption(option) {
1010
}
1111

1212
function equal(a, b, eq) {
13-
return Stdlib_Option.equal((a == null) ? undefined : Primitive_option.some(a), (b == null) ? undefined : Primitive_option.some(b), eq);
13+
return Stdlib_Option.equal(a == null ? undefined : Primitive_option.some(a), b == null ? undefined : Primitive_option.some(b), eq);
1414
}
1515

1616
function compare(a, b, cmp) {
17-
return Stdlib_Option.compare((a == null) ? undefined : Primitive_option.some(a), (b == null) ? undefined : Primitive_option.some(b), cmp);
17+
return Stdlib_Option.compare(a == null ? undefined : Primitive_option.some(a), b == null ? undefined : Primitive_option.some(b), cmp);
1818
}
1919

2020
function getOr(value, $$default) {
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
import * as Mocha from "mocha";
4+
import * as Test_utils from "./test_utils.mjs";
5+
6+
let opaque = (x => x);
7+
8+
function ofCond(c, a, b) {
9+
return (
10+
c ? a : b
11+
) == null;
12+
}
13+
14+
function ofIf(c, a, b) {
15+
return (
16+
c ? a : b
17+
) == null;
18+
}
19+
20+
function ofOr(a, b) {
21+
return (a || b) == null;
22+
}
23+
24+
function ofSum(a, b) {
25+
return (a + b | 0) == null;
26+
}
27+
28+
function ofObject(a) {
29+
return () => ({
30+
a: a
31+
}) == null;
32+
}
33+
34+
function inOr(x, y) {
35+
if (x == null) {
36+
return true;
37+
} else {
38+
return y;
39+
}
40+
}
41+
42+
function inCond(x) {
43+
if (x == null) {
44+
return "none";
45+
} else {
46+
return "some";
47+
}
48+
}
49+
50+
function inEq(x, y) {
51+
return y === (x == null);
52+
}
53+
54+
Mocha.describe("Is_nullable_paren_test", () => {
55+
Mocha.test("isNullable of a conditional", () => {
56+
let t = opaque(true);
57+
let f = opaque(false);
58+
let $$null = opaque(null);
59+
let one = opaque(1);
60+
Test_utils.eq("File \"is_nullable_paren_test.res\", line 33, characters 7-14", ofCond(t, $$null, one), true);
61+
Test_utils.eq("File \"is_nullable_paren_test.res\", line 34, characters 7-14", ofCond(t, one, $$null), false);
62+
Test_utils.eq("File \"is_nullable_paren_test.res\", line 35, characters 7-14", ofCond(f, one, opaque(undefined)), true);
63+
Test_utils.eq("File \"is_nullable_paren_test.res\", line 36, characters 7-14", ofIf(t, $$null, one), true);
64+
Test_utils.eq("File \"is_nullable_paren_test.res\", line 37, characters 7-14", ofIf(f, $$null, one), false);
65+
});
66+
Mocha.test("isNullable of a logical or", () => {
67+
let a = opaque(true);
68+
let b = opaque(false);
69+
Test_utils.eq("File \"is_nullable_paren_test.res\", line 40, characters 46-53", (a || b) == null, false);
70+
});
71+
Mocha.test("isNullable of an int sum", () => {
72+
let a = opaque(1);
73+
Test_utils.eq("File \"is_nullable_paren_test.res\", line 42, characters 44-51", (a + 2 | 0) == null, false);
74+
});
75+
Mocha.test("isNullable of an object in an arrow body", () => {
76+
let a = opaque(1);
77+
Test_utils.eq("File \"is_nullable_paren_test.res\", line 44, characters 60-67", ({
78+
a: a
79+
}) == null, false);
80+
});
81+
Mocha.test("isNullable as an operand", () => {
82+
let $$null = opaque(null);
83+
let one = opaque(1);
84+
Test_utils.eq("File \"is_nullable_paren_test.res\", line 48, characters 7-14", $$null == null, true);
85+
Test_utils.eq("File \"is_nullable_paren_test.res\", line 49, characters 7-14", one == null, false);
86+
Test_utils.eq("File \"is_nullable_paren_test.res\", line 50, characters 7-14", inCond($$null), "none");
87+
Test_utils.eq("File \"is_nullable_paren_test.res\", line 51, characters 7-14", inCond(one), "some");
88+
Test_utils.eq("File \"is_nullable_paren_test.res\", line 52, characters 7-14", true === ($$null == null), true);
89+
Test_utils.eq("File \"is_nullable_paren_test.res\", line 53, characters 7-14", true === (one == null), false);
90+
});
91+
});
92+
93+
export {
94+
opaque,
95+
ofCond,
96+
ofIf,
97+
ofOr,
98+
ofSum,
99+
ofObject,
100+
inOr,
101+
inCond,
102+
inEq,
103+
}
104+
/* Not a pure module */
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
open Mocha
2+
open Test_utils
3+
4+
// `Nullable.isNullable(e)` is printed as `e == null`, which binds like `===`:
5+
// `e` keeps the parentheses it needs, and the check needs none as an operand
6+
// of `||` or as the test of `?:`.
7+
8+
// Hides values from the optimizer, so the calls below are not folded away
9+
let opaque: 'a => 'a = %raw(`x => x`)
10+
11+
let ofCond = (c, a: Nullable.t<int>, b: Nullable.t<int>) => Nullable.isNullable(c ? a : b)
12+
13+
let ofIf = (c, a: Nullable.t<int>, b: Nullable.t<int>) =>
14+
Nullable.isNullable(
15+
if c {
16+
a
17+
} else {
18+
b
19+
},
20+
)
21+
22+
let ofOr = (a: bool, b: bool) => Nullable.isNullable(Nullable.make(a || b))
23+
let ofSum = (a: int, b: int) => Nullable.isNullable(Nullable.make(a + b))
24+
let ofObject = (a: int) => () => Nullable.isNullable(Nullable.make({"a": a}))
25+
let inOr = (x: Nullable.t<int>, y: bool) => Nullable.isNullable(x) || y
26+
let inCond = (x: Nullable.t<int>) => Nullable.isNullable(x) ? "none" : "some"
27+
let inEq = (x: Nullable.t<int>, y: bool) => y == Nullable.isNullable(x)
28+
29+
describe(__MODULE__, () => {
30+
test("isNullable of a conditional", () => {
31+
let (t, f) = (opaque(true), opaque(false))
32+
let (null, one) = (opaque(Nullable.null), opaque(Nullable.make(1)))
33+
eq(__LOC__, ofCond(t, null, one), true)
34+
eq(__LOC__, ofCond(t, one, null), false)
35+
eq(__LOC__, ofCond(f, one, opaque(Nullable.undefined)), true)
36+
eq(__LOC__, ofIf(t, null, one), true)
37+
eq(__LOC__, ofIf(f, null, one), false)
38+
})
39+
40+
test("isNullable of a logical or", () => eq(__LOC__, ofOr(opaque(true), opaque(false)), false))
41+
42+
test("isNullable of an int sum", () => eq(__LOC__, ofSum(opaque(1), 2), false))
43+
44+
test("isNullable of an object in an arrow body", () => eq(__LOC__, ofObject(opaque(1))(), false))
45+
46+
test("isNullable as an operand", () => {
47+
let (null, one) = (opaque(Nullable.null), opaque(Nullable.make(1)))
48+
eq(__LOC__, inOr(null, false), true)
49+
eq(__LOC__, inOr(one, false), false)
50+
eq(__LOC__, inCond(null), "none")
51+
eq(__LOC__, inCond(one), "some")
52+
eq(__LOC__, inEq(null, true), true)
53+
eq(__LOC__, inEq(one, true), false)
54+
})
55+
})

‎tests/tests/src/optional_external_arg_once_test.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Mocha.describe("Optional_external_arg_once_test", () => {
8585
"first",
8686
1
8787
], [
88-
(key == null) ? undefined : Primitive_option.some(key),
88+
key == null ? undefined : Primitive_option.some(key),
8989
s$2.reads
9090
]);
9191
let s$3 = makeSource();

‎tests/tests/src/stdlib/Stdlib_TempTests.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ console.log(regex.test(string));
202202

203203
let result = regex.exec(string);
204204

205-
let result$1 = (result == null) ? undefined : Primitive_option.some(result);
205+
let result$1 = result == null ? undefined : Primitive_option.some(result);
206206

207207
console.log(Stdlib_Option.map(result$1, prim => prim.input));
208208

0 commit comments

Comments
 (0)