Skip to content

Commit 7d10374

Browse files
committed
Fix logic.
1 parent 56704f3 commit 7d10374

File tree

3 files changed

+26
-30
lines changed

3 files changed

+26
-30
lines changed

compiler/core/js_exp_make.ml

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,10 @@ let rec push_negation (e : t) : t option =
718718
- [(typeof x === "boolean" | "string" | "number") && (x !== boolean/null/undefined)] -> [typeof x === ...]
719719
- [(Array.isArray(x)) && (x !== boolean/null/undefined)] -> [Array.isArray(x)]
720720
721+
Equality optimizations:
722+
- [e && e] -> [e]
723+
- [(x === boolean/null/undefined) && (x === boolean/null/undefined)] -> [false] (when not equal)
724+
721725
Note: The function preserves the semantics of the original expression while
722726
attempting to reduce it to a simpler form. If no simplification is possible,
723727
returns [None].
@@ -726,7 +730,6 @@ let rec simplify_and (e1 : t) (e2 : t) : t option =
726730
if debug then
727731
Printf.eprintf "simplify_and %s %s\n" (!string_of_expression e1)
728732
(!string_of_expression e2);
729-
730733
match (e1.expression_desc, e2.expression_desc) with
731734
| Bool false, _ -> Some false_
732735
| _, Bool false -> Some false_
@@ -735,39 +738,21 @@ let rec simplify_and (e1 : t) (e2 : t) : t option =
735738
| Bin (And, a, b), _ -> (
736739
let ao = simplify_and a e2 in
737740
let bo = simplify_and b e2 in
738-
if ao = None && bo = None then None
739-
else
740-
let a_ =
741-
match ao with
742-
| None -> a
743-
| Some a_ -> a_
744-
in
745-
let b_ =
746-
match bo with
747-
| None -> b
748-
| Some b_ -> b_
749-
in
741+
match (ao, bo) with
742+
| None, _ | _, None -> None
743+
| Some a_, Some b_ -> (
750744
match simplify_and a_ b_ with
751745
| None -> Some {expression_desc = Bin (And, a_, b_); comment = None}
752-
| Some e -> Some e)
746+
| Some e -> Some e))
753747
| Bin (Or, a, b), _ -> (
754748
let ao = simplify_and a e2 in
755749
let bo = simplify_and b e2 in
756-
if ao = None && bo = None then None
757-
else
758-
let a_ =
759-
match ao with
760-
| None -> a
761-
| Some a_ -> a_
762-
in
763-
let b_ =
764-
match bo with
765-
| None -> b
766-
| Some b_ -> b_
767-
in
750+
match (ao, bo) with
751+
| None, _ | _, None -> None
752+
| Some a_, Some b_ -> (
768753
match simplify_or a_ b_ with
769754
| None -> Some {expression_desc = Bin (Or, a_, b_); comment = None}
770-
| Some e -> Some e)
755+
| Some e -> Some e))
771756
| ( Bin
772757
( ((EqEqEq | NotEqEq) as op1),
773758
{expression_desc = Var i1},
@@ -882,6 +867,17 @@ let rec simplify_and (e1 : t) (e2 : t) : t option =
882867
when Js_op_util.same_vident ia ib ->
883868
Some {expression_desc = is_array; comment = None}
884869
| x, y when x = y -> Some e1
870+
| ( Bin
871+
( EqEqEq,
872+
{expression_desc = Var ia},
873+
{expression_desc = Bool _ | Null | Undefined _} ),
874+
Bin
875+
( EqEqEq,
876+
{expression_desc = Var ib},
877+
{expression_desc = Bool _ | Null | Undefined _} ) )
878+
when Js_op_util.same_vident ia ib ->
879+
(* Note: case x = y is handled above *)
880+
Some false_
885881
| _ -> None
886882

887883
(**

tests/tests/src/UntaggedVariants.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,9 +386,9 @@ function check$1(s) {
386386
return;
387387
}
388388
let match = s[0];
389-
if (match === undefined || match === null || match === true) {
389+
if (match === true) {
390390
let match$1 = s[1];
391-
if (match$1 === undefined || match$1 === null || match$1 === false) {
391+
if (match$1 === false) {
392392
let match$2 = s[2];
393393
if (match$2 === undefined || match$2 === null || match$2 === false || match$2 === true) {
394394
console.log("Nope...");

tests/tests/src/core/Core_NullableTests.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function shouldHandleNullableValues() {
77
let tUndefined = undefined;
88
let tValue = "hello";
99
let tmp;
10-
tmp = tNull === null || tNull === undefined ? true : false;
10+
tmp = tNull === null ? true : false;
1111
Test.run([
1212
[
1313
"Core_NullableTests.res",

0 commit comments

Comments
 (0)