Skip to content

Commit 4eb8647

Browse files
committed
[code gen] improve the pattern match
1 parent ce3b925 commit 4eb8647

11 files changed

+106
-78
lines changed

jscomp/bin/whole_compiler.ml

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70661,13 +70661,13 @@ module Js_analyzer : sig
7066170661
(** Analyzing utilities for [J] module *)
7066270662

7066370663
(** for example, whether it has side effect or not.
70664-
*)
70664+
*)
7066570665

7066670666
val free_variables_of_statement :
70667-
Ident_set.t -> Ident_set.t -> J.statement -> Ident_set.t
70667+
Ident_set.t -> Ident_set.t -> J.statement -> Ident_set.t
7066870668

7066970669
val free_variables_of_expression :
70670-
Ident_set.t -> Ident_set.t -> J.finish_ident_expression -> Ident_set.t
70670+
Ident_set.t -> Ident_set.t -> J.finish_ident_expression -> Ident_set.t
7067170671

7067270672
val no_side_effect_expression_desc :
7067370673
J.expression_desc -> bool
@@ -70680,21 +70680,23 @@ val no_side_effect_expression :
7068070680
when you want to do a deep copy, the expression passed to you is pure
7068170681
but you still have to call the function to make a copy,
7068270682
since it maybe changed later
70683-
*)
70683+
*)
7068470684

7068570685
val no_side_effect_statement :
70686-
J.statement -> bool
70686+
J.statement -> bool
7068770687
(**
7068870688
here we say
70689-
{[ var x = no_side_effect_expression ]}
70689+
{[ var x = no_side_effect_expression ]}
7069070690
is [no side effect], but it is actually side effect,
7069170691
since we are defining a variable, however, if it is not exported or used,
7069270692
then it's fine, so we delay this check later
70693-
*)
70693+
*)
7069470694

70695-
val eq_expression : J.expression -> J.expression -> bool
70695+
val eq_expression :
70696+
J.expression -> J.expression -> bool
7069670697

70697-
val eq_statement : J.statement -> J.statement -> bool
70698+
(* val eq_statement :
70699+
J.statement -> J.statement -> bool *)
7069870700

7069970701
val rev_flatten_seq : J.expression -> J.block
7070070702

@@ -70709,7 +70711,7 @@ val is_constant : J.expression -> bool
7070970711
*)
7071070712

7071170713
val is_simple_no_side_effect_expression
70712-
: J.expression -> bool
70714+
: J.expression -> bool
7071370715
end = struct
7071470716
#1 "js_analyzer.ml"
7071570717
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
@@ -70910,6 +70912,7 @@ let rec eq_expression (x : J.expression) (y : J.expression) =
7091070912
Ident.same i j
7091170913
| Bin (op0, a0,b0) , Bin(op1,a1,b1) ->
7091270914
op0 = op1 && eq_expression a0 a1 && eq_expression b0 b1
70915+
| Str(a0,b0), Str(a1,b1) -> a0 = a1 && b0 = b1
7091370916
| _, _ -> false
7091470917

7091570918
and eq_expression_list xs ys =

jscomp/core/js_analyzer.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ let rec eq_expression (x : J.expression) (y : J.expression) =
196196
Ident.same i j
197197
| Bin (op0, a0,b0) , Bin(op1,a1,b1) ->
198198
op0 = op1 && eq_expression a0 a1 && eq_expression b0 b1
199+
| Str(a0,b0), Str(a1,b1) -> a0 = a1 && b0 = b1
199200
| _, _ -> false
200201

201202
and eq_expression_list xs ys =

jscomp/core/js_analyzer.mli

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@
3232
(** Analyzing utilities for [J] module *)
3333

3434
(** for example, whether it has side effect or not.
35-
*)
35+
*)
3636

3737
val free_variables_of_statement :
38-
Ident_set.t -> Ident_set.t -> J.statement -> Ident_set.t
38+
Ident_set.t -> Ident_set.t -> J.statement -> Ident_set.t
3939

4040
val free_variables_of_expression :
41-
Ident_set.t -> Ident_set.t -> J.finish_ident_expression -> Ident_set.t
41+
Ident_set.t -> Ident_set.t -> J.finish_ident_expression -> Ident_set.t
4242

4343
val no_side_effect_expression_desc :
4444
J.expression_desc -> bool
@@ -51,21 +51,21 @@ val no_side_effect_expression :
5151
when you want to do a deep copy, the expression passed to you is pure
5252
but you still have to call the function to make a copy,
5353
since it maybe changed later
54-
*)
54+
*)
5555

5656
val no_side_effect_statement :
57-
J.statement -> bool
57+
J.statement -> bool
5858
(**
5959
here we say
60-
{[ var x = no_side_effect_expression ]}
60+
{[ var x = no_side_effect_expression ]}
6161
is [no side effect], but it is actually side effect,
6262
since we are defining a variable, however, if it is not exported or used,
6363
then it's fine, so we delay this check later
64-
*)
64+
*)
6565

66-
val eq_expression : J.expression -> J.expression -> bool
66+
val eq_expression :
67+
J.expression -> J.expression -> bool
6768

68-
val eq_statement : J.statement -> J.statement -> bool
6969

7070
val rev_flatten_seq : J.expression -> J.block
7171

@@ -80,4 +80,4 @@ val is_constant : J.expression -> bool
8080
*)
8181

8282
val is_simple_no_side_effect_expression
83-
: J.expression -> bool
83+
: J.expression -> bool

jscomp/core/lam_compile_group.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ let lambda_as_module
376376
begin
377377
Js_config.set_current_file filename ;
378378
#if BS_DEBUG then
379-
Js_config.set_debug_file "inner_define.ml";
379+
Js_config.set_debug_file "gpr_1891_test.ml";
380380
#end
381381
let lambda_output = compile ~filename output_prefix env sigs lam in
382382
let (//) = Filename.concat in

jscomp/test/.depend

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ gpr_1760_test.cmj : mt.cmj ../stdlib/int64.cmj ../stdlib/int32.cmj
244244
gpr_1762_test.cmj : mt.cmj
245245
gpr_1817_test.cmj : mt.cmj ../runtime/js.cmj
246246
gpr_1822_test.cmj : mt.cmj
247+
gpr_1891_test.cmj :
247248
gpr_405_test.cmj : ../stdlib/hashtbl.cmj gpr_405_test.cmi
248249
gpr_441.cmj :
249250
gpr_459_test.cmj : mt.cmj

jscomp/test/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ OTHERS := literals a test_ari test_export2 test_internalOO test_obj_simple_ffi t
180180
gpr_1822_test\
181181
bs_unwrap_test\
182182
dollar_escape_test\
183-
js_nullable_test
183+
js_nullable_test\
184+
gpr_1891_test
184185

185186
# bs_uncurry_test
186187
# needs Lam to get rid of Uncurry arity first

jscomp/test/gpr_1891_test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
3+
4+
function foo(x) {
5+
if (typeof x === "number") {
6+
console.log("2");
7+
return /* () */0;
8+
} else if (x[0] !== 3505894) {
9+
console.log("2");
10+
return /* () */0;
11+
} else if (x[1] !== 3) {
12+
console.log("2");
13+
return /* () */0;
14+
} else {
15+
console.log("1");
16+
return /* () */0;
17+
}
18+
}
19+
20+
function foo2(x) {
21+
if (typeof x === "number" || x[0] !== 3505894 || x[1] !== 3) {
22+
return "xxx";
23+
} else {
24+
return "xxxx";
25+
}
26+
}
27+
28+
function foo3(x) {
29+
if (typeof x === "number" || x[0] !== 3505894 || x[1] !== 3) {
30+
return 2;
31+
} else {
32+
return 1;
33+
}
34+
}
35+
36+
exports.foo = foo;
37+
exports.foo2 = foo2;
38+
exports.foo3 = foo3;
39+
/* No side effect */

jscomp/test/gpr_1891_test.ml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
let foo x =
3+
match x with
4+
| `Foo 3 -> print_endline "1"
5+
| _ -> print_endline "2"
6+
7+
8+
let foo2 x =
9+
match x with
10+
| `Foo 3 ->
11+
"xxxx"
12+
| _ -> "xxx"
13+
14+
15+
let foo3 x =
16+
match x with
17+
| `Foo 3 ->
18+
1
19+
| _ -> 2
20+
21+
22+
23+

jscomp/test/ocaml_parsetree_test.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12486,20 +12486,8 @@ if (match) {
1248612486
eq("File \"ocaml_parsetree_main.ml\", line 216, characters 12-19", /* true */1, /* false */0);
1248712487
} else {
1248812488
var match$99 = match$97[/* loc_end */1];
12489-
if (match$99[/* pos_fname */0] === "") {
12490-
if (match$99[/* pos_lnum */1] !== 4) {
12491-
eq("File \"ocaml_parsetree_main.ml\", line 216, characters 12-19", /* true */1, /* false */0);
12492-
} else if (match$99[/* pos_bol */2] !== 46) {
12493-
eq("File \"ocaml_parsetree_main.ml\", line 216, characters 12-19", /* true */1, /* false */0);
12494-
} else if (match$99[/* pos_cnum */3] !== 71) {
12495-
eq("File \"ocaml_parsetree_main.ml\", line 216, characters 12-19", /* true */1, /* false */0);
12496-
} else if (match$97[/* loc_ghost */2] !== 0) {
12497-
eq("File \"ocaml_parsetree_main.ml\", line 216, characters 12-19", /* true */1, /* false */0);
12498-
} else if (match$2[1]) {
12499-
eq("File \"ocaml_parsetree_main.ml\", line 216, characters 12-19", /* true */1, /* false */0);
12500-
} else {
12501-
eq("File \"ocaml_parsetree_main.ml\", line 215, characters 10-17", /* true */1, /* true */1);
12502-
}
12489+
if (match$99[/* pos_fname */0] === "" && !(match$99[/* pos_lnum */1] !== 4 || match$99[/* pos_bol */2] !== 46 || match$99[/* pos_cnum */3] !== 71 || match$97[/* loc_ghost */2] !== 0 || match$2[1])) {
12490+
eq("File \"ocaml_parsetree_main.ml\", line 215, characters 10-17", /* true */1, /* true */1);
1250312491
} else {
1250412492
eq("File \"ocaml_parsetree_main.ml\", line 216, characters 12-19", /* true */1, /* false */0);
1250512493
}

jscomp/test/ocaml_proto_test.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4808,11 +4808,7 @@ function gen_type_record(mutable_, and_, param, sc) {
48084808
}
48094809
};
48104810
var field_prefix = function (field_type, field_mutable) {
4811-
if (field_mutable) {
4812-
return "mutable ";
4813-
} else if (is_imperative_type(field_type)) {
4814-
return "";
4815-
} else if (mutable_$1) {
4811+
if (field_mutable || !(is_imperative_type(field_type) || !mutable_$1)) {
48164812
return "mutable ";
48174813
} else {
48184814
return "";

0 commit comments

Comments
 (0)