Skip to content

Commit 4b70394

Browse files
committed
x === v1 && x !== v2
1 parent 9272b25 commit 4b70394

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

compiler/core/js_exp_make.ml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,8 @@ let rec push_negation (e : t) : t option =
728728
Equality optimizations:
729729
- [e && e] -> [e]
730730
- [(x === boolean/null/undefined/123/"hello") && (x === boolean/null/undefined/123/"hello")] -> [false] (when not equal)
731+
- [(x === boolean/null/undefined/123/"hello") && (x !== boolean/null/undefined/123/"hello")] -> [false] (when equal)
732+
- [(x === boolean/null/undefined/123/"hello") && (x !== boolean/null/undefined/123/"hello")] -> [(x === ...)] (when not equal)
731733
732734
Note: The function preserves the semantics of the original expression while
733735
attempting to reduce it to a simpler form. If no simplification is possible,
@@ -763,6 +765,10 @@ let rec simplify_and ~n (e1 : t) (e2 : t) : t option =
763765
let ao = simplify_and ~n:(n + 1) a e2 in
764766
let bo = simplify_and ~n:(n + 1) b e2 in
765767
match (ao, bo) with
768+
| Some {expression_desc = Bool false}, None ->
769+
Some {expression_desc = Bin (And, b, e2); comment = None}
770+
| None, Some {expression_desc = Bool false} ->
771+
Some {expression_desc = Bin (And, a, e2); comment = None}
766772
| None, _ | _, None -> (
767773
match simplify_or ~n:(n + 1) a b with
768774
| None -> None
@@ -1005,6 +1011,18 @@ let rec simplify_and ~n (e1 : t) (e2 : t) : t option =
10051011
when Js_op_util.same_vident ia ib ->
10061012
(* Note: case x = y is handled above *)
10071013
Some false_
1014+
| ( Bin
1015+
( ((EqEqEq | NotEqEq) as op1),
1016+
{expression_desc = Var ia},
1017+
({expression_desc = Bool _ | Null | Undefined _ | Number _ | Str _}
1018+
as v1) ),
1019+
Bin
1020+
( ((EqEqEq | NotEqEq) as op2),
1021+
{expression_desc = Var ib},
1022+
({expression_desc = Bool _ | Null | Undefined _ | Number _ | Str _}
1023+
as v2) ) )
1024+
when Js_op_util.same_vident ia ib && op1 != op2 ->
1025+
if v1 = v2 then Some false_ else if op1 = EqEqEq then Some e1 else Some e2
10081026
| _ -> None
10091027
in
10101028
(if debug then

tests/tests/src/core/Core_NullableTests.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function shouldHandleNullableValues() {
1818
"Should handle null"
1919
], tmp, (prim0, prim1) => prim0 === prim1, true);
2020
let tmp$1;
21-
tmp$1 = (tUndefined == null) && tUndefined !== null;
21+
tmp$1 = tUndefined === undefined;
2222
Test.run([
2323
[
2424
"Core_NullableTests.res",

0 commit comments

Comments
 (0)