Skip to content

Commit 912b428

Browse files
committed
Simplify conditionals.
Fixes #7142 - x ? y : false --> x && y - x ? false : y --> !x && y - push negation inside when it leads to simplification
1 parent 971f935 commit 912b428

31 files changed

+96
-121
lines changed

compiler/core/js_exp_make.ml

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,7 @@ let rec simplify_and ~n (e1 : t) (e2 : t) : t option =
997997
_ ) as is_array) )
998998
when Js_op_util.same_vident ia ib ->
999999
Some {expression_desc = is_array; comment = None}
1000-
| x, y when x = y -> Some e1
1000+
| _ when Js_analyzer.eq_expression e1 e2 -> Some e1
10011001
| ( Bin
10021002
( EqEqEq,
10031003
{expression_desc = Var ia},
@@ -1022,7 +1022,9 @@ let rec simplify_and ~n (e1 : t) (e2 : t) : t option =
10221022
({expression_desc = Bool _ | Null | Undefined _ | Number _ | Str _}
10231023
as v2) ) )
10241024
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
1025+
if Js_analyzer.eq_expression v1 v2 then Some false_
1026+
else if op1 = EqEqEq then Some e1
1027+
else Some e2
10261028
| _ -> None
10271029
in
10281030
(if debug then
@@ -1138,14 +1140,22 @@ let not (e : t) : t =
11381140
| Bin (Ge, a, b) -> {e with expression_desc = Bin (Lt, a, b)}
11391141
| Bin (Le, a, b) -> {e with expression_desc = Bin (Gt, a, b)}
11401142
| Bin (Gt, a, b) -> {e with expression_desc = Bin (Le, a, b)}
1141-
| _ -> {expression_desc = Js_not e; comment = None}
1143+
| _ -> (
1144+
match push_negation e with
1145+
| Some e_ -> e_
1146+
| None -> {expression_desc = Js_not e; comment = None})
11421147

11431148
let not_empty_branch (x : t) =
11441149
match x.expression_desc with
11451150
| Number (Int {i = 0l}) | Undefined _ -> false
11461151
| _ -> true
11471152

11481153
let rec econd ?comment (pred : t) (ifso : t) (ifnot : t) : t =
1154+
let default () =
1155+
if Js_analyzer.eq_expression ifso ifnot then
1156+
if no_side_effect pred then ifso else seq ?comment pred ifso
1157+
else {expression_desc = Cond (pred, ifso, ifnot); comment}
1158+
in
11491159
match (pred.expression_desc, ifso.expression_desc, ifnot.expression_desc) with
11501160
| Bool false, _, _ -> ifnot
11511161
| Number (Int {i = 0l; _}), _, _ -> ifnot
@@ -1179,10 +1189,15 @@ let rec econd ?comment (pred : t) (ifso : t) (ifnot : t) : t =
11791189
Seq (a, {expression_desc = Undefined _}),
11801190
Seq (b, {expression_desc = Undefined _}) ) ->
11811191
seq (econd ?comment pred a b) undefined
1182-
| _ ->
1183-
if Js_analyzer.eq_expression ifso ifnot then
1184-
if no_side_effect pred then ifso else seq ?comment pred ifso
1185-
else {expression_desc = Cond (pred, ifso, ifnot); comment}
1192+
| _, _, Bool false -> (
1193+
match simplify_and ~n:0 pred ifso with
1194+
| Some e -> e
1195+
| None -> default ())
1196+
| _, Bool false, _ -> (
1197+
match simplify_and ~n:0 (not pred) ifnot with
1198+
| Some e -> e
1199+
| None -> default ())
1200+
| _ -> default ()
11861201

11871202
let rec float_equal ?comment (e0 : t) (e1 : t) : t =
11881203
match (e0.expression_desc, e1.expression_desc) with

lib/es6/Belt_List.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -988,11 +988,7 @@ function eq(_l1, _l2, p) {
988988
let l2 = _l2;
989989
let l1 = _l1;
990990
if (!l1) {
991-
if (l2) {
992-
return false;
993-
} else {
994-
return true;
995-
}
991+
return !l2;
996992
}
997993
if (!l2) {
998994
return false;

lib/es6/Belt_Result.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,7 @@ function isOk(x) {
5757
}
5858

5959
function isError(x) {
60-
if (x.TAG === "Ok") {
61-
return false;
62-
} else {
63-
return true;
64-
}
60+
return x.TAG !== "Ok";
6561
}
6662

6763
function eq(a, b, f) {
@@ -71,10 +67,8 @@ function eq(a, b, f) {
7167
} else {
7268
return false;
7369
}
74-
} else if (b.TAG === "Ok") {
75-
return false;
7670
} else {
77-
return true;
71+
return b.TAG !== "Ok";
7872
}
7973
}
8074

lib/es6/List.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,11 +1000,7 @@ function equal(_l1, _l2, p) {
10001000
let l2 = _l2;
10011001
let l1 = _l1;
10021002
if (!l1) {
1003-
if (l2) {
1004-
return false;
1005-
} else {
1006-
return true;
1007-
}
1003+
return !l2;
10081004
}
10091005
if (!l2) {
10101006
return false;

lib/es6/Primitive_option.js

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

4646
function valFromOption(x) {
47-
if (!(x !== null && x.BS_PRIVATE_NESTED_SOME_NONE !== undefined)) {
47+
if (x === null || x.BS_PRIVATE_NESTED_SOME_NONE === undefined) {
4848
return x;
4949
}
5050
let depth = x.BS_PRIVATE_NESTED_SOME_NONE;

lib/es6/Result.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,7 @@ function isOk(x) {
5151
}
5252

5353
function isError(x) {
54-
if (x.TAG === "Ok") {
55-
return false;
56-
} else {
57-
return true;
58-
}
54+
return x.TAG !== "Ok";
5955
}
6056

6157
function equal(a, b, f) {
@@ -65,10 +61,8 @@ function equal(a, b, f) {
6561
} else {
6662
return false;
6763
}
68-
} else if (b.TAG === "Ok") {
69-
return false;
7064
} else {
71-
return true;
65+
return b.TAG !== "Ok";
7266
}
7367
}
7468

lib/js/Belt_List.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -988,11 +988,7 @@ function eq(_l1, _l2, p) {
988988
let l2 = _l2;
989989
let l1 = _l1;
990990
if (!l1) {
991-
if (l2) {
992-
return false;
993-
} else {
994-
return true;
995-
}
991+
return !l2;
996992
}
997993
if (!l2) {
998994
return false;

lib/js/Belt_Result.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,7 @@ function isOk(x) {
5757
}
5858

5959
function isError(x) {
60-
if (x.TAG === "Ok") {
61-
return false;
62-
} else {
63-
return true;
64-
}
60+
return x.TAG !== "Ok";
6561
}
6662

6763
function eq(a, b, f) {
@@ -71,10 +67,8 @@ function eq(a, b, f) {
7167
} else {
7268
return false;
7369
}
74-
} else if (b.TAG === "Ok") {
75-
return false;
7670
} else {
77-
return true;
71+
return b.TAG !== "Ok";
7872
}
7973
}
8074

lib/js/List.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,11 +1000,7 @@ function equal(_l1, _l2, p) {
10001000
let l2 = _l2;
10011001
let l1 = _l1;
10021002
if (!l1) {
1003-
if (l2) {
1004-
return false;
1005-
} else {
1006-
return true;
1007-
}
1003+
return !l2;
10081004
}
10091005
if (!l2) {
10101006
return false;

lib/js/Primitive_option.js

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

4646
function valFromOption(x) {
47-
if (!(x !== null && x.BS_PRIVATE_NESTED_SOME_NONE !== undefined)) {
47+
if (x === null || x.BS_PRIVATE_NESTED_SOME_NONE === undefined) {
4848
return x;
4949
}
5050
let depth = x.BS_PRIVATE_NESTED_SOME_NONE;

0 commit comments

Comments
 (0)