Skip to content

Commit 457909f

Browse files
committed
Fix issue where optimizations for booleans would apply to user defined variants.
1 parent f88d094 commit 457909f

File tree

3 files changed

+15
-19
lines changed

3 files changed

+15
-19
lines changed

jscomp/ml/matching.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2359,11 +2359,11 @@ let combine_constructor sw_names loc arg ex_pat cstr partial ctx def
23592359
else arg
23602360
in
23612361
Lifthenelse(arg, act2, act1)
2362-
| (2,0, [(i1,act1); (_,act2)],[]) ->
2363-
let arg = match act1 with
2364-
| Lconst (Const_pointer (_, Pt_constructor _ )) ->
2365-
Lprim (Pintcomp(Ceq), [arg; act1], loc)
2366-
| _ -> arg in
2362+
| (2,0, [(i1,act1); (_,act2)],[]) when
2363+
(match act1, act2 with
2364+
| Lconst (Const_pointer (_, Pt_constructor _ )), _ -> false
2365+
| _, Lconst (Const_pointer (_, Pt_constructor _ )) -> false
2366+
| _ -> true) ->
23672367
if i1 = 0 then Lifthenelse(arg, act2, act1)
23682368
else Lifthenelse (arg, act1, act2)
23692369
| (n,0,_,[]) when false (* relies on tag being an int *) -> (* The type defines constant constructors only *)

jscomp/test/mario_game.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,10 +1114,10 @@ function collide_block(check_xOpt, dir, obj) {
11141114
}
11151115

11161116
function opposite_dir(dir) {
1117-
if (dir === /* Right */1) {
1118-
return /* Left */0;
1119-
} else {
1117+
if (dir === /* Left */0) {
11201118
return /* Right */1;
1119+
} else {
1120+
return /* Left */0;
11211121
}
11221122
}
11231123

jscomp/test/variantsMatching.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,22 @@ function bar(x) {
2828
}
2929

3030
function and_(x, y) {
31-
if (x || y === /* True */0) {
32-
return /* False */1;
31+
if (x === /* True */0) {
32+
return y;
3333
} else {
34-
return /* True */0;
34+
return /* False */1;
3535
}
3636
}
3737

3838
function id(x) {
39-
if (x === /* True */0) {
40-
return /* False */1;
41-
} else {
42-
return /* True */0;
43-
}
39+
return x;
4440
}
4541

4642
function not_(x) {
47-
if (x === /* False */1) {
48-
return /* True */0;
49-
} else {
43+
if (x === /* True */0) {
5044
return /* False */1;
45+
} else {
46+
return /* True */0;
5147
}
5248
}
5349

0 commit comments

Comments
 (0)