Skip to content

Commit 4a014eb

Browse files
authored
Merge branch 'master' into string_concat
2 parents 7f0576c + 40fc40b commit 4a014eb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+10036
-8810
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ These are only breaking changes for unformatted code.
7777
- New internal representation for uncurried functions using built-in type `function$<fun_type, arity>` this avoids having to declare all the possible arities ahead of time https://github.com/rescript-lang/rescript-compiler/pull/5870
7878
- PPX V3: allow uncurried `make` function and treat it like a curried one https://github.com/rescript-lang/rescript-compiler/pull/6081
7979
- Add support for `|>` in uncurried mode by desugaring it https://github.com/rescript-lang/rescript-compiler/pull/6083
80-
- Improve code generate for string templates https://github.com/rescript-lang/rescript-compiler/pull/6090
80+
- Change the compilation of pattern matching for variants so it does not depends on variats being integers https://github.com/rescript-lang/rescript-compiler/pull/6085
81+
- Improve code generated for string templates https://github.com/rescript-lang/rescript-compiler/pull/6090
8182

8283
# 10.1.4
8384

jscomp/core/js_exp_make.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,9 @@ let string_equal ?comment (e0 : t) (e1 : t) : t =
762762
let is_type_number ?comment (e : t) : t =
763763
string_equal ?comment (typeof e) (str "number")
764764

765+
let is_tag (e : t) : t =
766+
string_equal ~comment:"tag" (typeof e) (str "number")
767+
765768
let is_type_string ?comment (e : t) : t =
766769
string_equal ?comment (typeof e) (str "string")
767770

jscomp/core/js_exp_make.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ val eq_null_undefined_boolean : ?comment:string -> t -> t -> t
199199
val neq_null_undefined_boolean : ?comment:string -> t -> t -> t
200200

201201
val is_type_number : ?comment:string -> t -> t
202+
val is_tag : t -> t
202203

203204
val is_type_string : ?comment:string -> t -> t
204205

jscomp/core/lam_compile.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ and compile_switch (switch_arg : Lam.t) (sw : Lam.lambda_switch)
616616
else
617617
(* [e] will be used twice *)
618618
let dispatch e =
619-
S.if_ (E.is_type_number e)
619+
S.if_ (E.is_tag e)
620620
(compile_cases cxt e sw_consts sw_num_default get_const_name)
621621
(* default still needed, could simplified*)
622622
~else_:

jscomp/core/lam_convert.ml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,25 +119,24 @@ let lam_is_var (x : Lam.t) (y : Ident.t) =
119119
(** Make sure no int range overflow happens
120120
also we only check [int]
121121
*)
122-
let happens_to_be_diff (sw_consts : (int * Lambda.lambda) list) : int option =
122+
let happens_to_be_diff (sw_consts : (int * Lambda.lambda) list) sw_names : int option =
123123
match sw_consts with
124124
| ( a,
125-
Lconst (Const_pointer (a0, Pt_constructor _) | Const_base (Const_int a0))
125+
Lconst (Const_base (Const_int a0))
126126
)
127127
:: ( b,
128128
Lconst
129-
(Const_pointer (b0, Pt_constructor _) | Const_base (Const_int b0)) )
129+
(Const_base (Const_int b0)) )
130130
:: rest
131-
when no_over_flow a && no_over_flow a0 && no_over_flow b && no_over_flow b0
131+
when sw_names = None && no_over_flow a && no_over_flow a0 && no_over_flow b && no_over_flow b0
132132
->
133133
let diff = a0 - a in
134134
if b0 - b = diff then
135135
if
136136
Ext_list.for_all rest (fun (x, lam) ->
137137
match lam with
138138
| Lconst
139-
( Const_pointer (x0, Pt_constructor _)
140-
| Const_base (Const_int x0) )
139+
( Const_base (Const_int x0) )
141140
when no_over_flow x0 && no_over_flow x ->
142141
x0 - x = diff
143142
| _ -> false)
@@ -701,8 +700,9 @@ let convert (exports : Set_ident.t) (lam : Lambda.lambda) :
701700
sw_numblocks = 0;
702701
sw_consts;
703702
sw_numconsts;
703+
sw_names;
704704
} -> (
705-
match happens_to_be_diff sw_consts with
705+
match happens_to_be_diff sw_consts sw_names with
706706
| Some 0 -> e
707707
| Some i ->
708708
prim ~primitive:Paddint
@@ -712,7 +712,7 @@ let convert (exports : Set_ident.t) (lam : Lambda.lambda) :
712712
Lam.const (Const_int { i = Int32.of_int i; comment = None });
713713
]
714714
Location.none
715-
| None ->
715+
| _ ->
716716
Lam.switch e
717717
{
718718
sw_failaction = None;

jscomp/ml/matching.ml

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2348,19 +2348,25 @@ let combine_constructor sw_names loc arg ex_pat cstr partial ctx def
23482348
match
23492349
(cstr.cstr_consts, cstr.cstr_nonconsts, consts, nonconsts)
23502350
with
2351-
| (1, 1, [0, act1], [0, act2]) ->
2352-
(* Typically, match on lists, will avoid isint primitive in that
2353-
case *)
2351+
| (1, 1, [0, act1], [0, act2])
2352+
when cstr.cstr_name = "::" || cstr.cstr_name = "[]" || Datarepr.constructor_has_optional_shape cstr
2353+
->
2354+
(* Typically, match on lists, will avoid isint primitive in that
2355+
case *)
23542356
let arg =
23552357
if !Config.bs_only && Datarepr.constructor_has_optional_shape cstr then
23562358
Lprim(is_not_none_bs_primitve , [arg], loc)
23572359
else arg
23582360
in
23592361
Lifthenelse(arg, act2, act1)
2360-
| (2,0, [(i1,act1); (_,act2)],[]) ->
2361-
if i1 = 0 then Lifthenelse(arg, act2, act1)
2362-
else Lifthenelse (arg,act1,act2)
2363-
| (n,0,_,[]) -> (* The type defines constant constructors only *)
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) ->
2367+
if i1 = 0 then Lifthenelse(arg, act2, act1)
2368+
else Lifthenelse (arg, act1, act2)
2369+
| (n,0,_,[]) when false (* relies on tag being an int *) -> (* The type defines constant constructors only *)
23642370
call_switcher loc fail_opt arg 0 (n-1) consts sw_names
23652371
| (n, _, _, _) ->
23662372
let act0 =
@@ -2373,15 +2379,15 @@ let combine_constructor sw_names loc arg ex_pat cstr partial ctx def
23732379
else None
23742380
| None,_ -> same_actions nonconsts in
23752381
match act0 with
2376-
| Some act ->
2382+
| Some act when false (* relies on tag being an int *) ->
23772383
Lifthenelse
23782384
(Lprim (Pisint, [arg], loc),
23792385
call_switcher loc
23802386
fail_opt arg
23812387
0 (n-1) consts sw_names,
23822388
act)
23832389
(* Emit a switch, as bytecode implements this sophisticated instruction *)
2384-
| None ->
2390+
| _ ->
23852391
let sw =
23862392
{sw_numconsts = cstr.cstr_consts; sw_consts = consts;
23872393
sw_numblocks = cstr.cstr_nonconsts; sw_blocks = nonconsts;

jscomp/test/adt_optimize_test.js

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,66 @@
22

33

44
function f(x) {
5-
return x + 1 | 0;
5+
switch (x) {
6+
case /* A */0 :
7+
return 1;
8+
case /* B */1 :
9+
return 2;
10+
case /* C */2 :
11+
return 3;
12+
13+
}
614
}
715

816
function f_0(x) {
9-
return x - 1 | 0;
17+
switch (x) {
18+
case /* A */0 :
19+
return -1;
20+
case /* B */1 :
21+
return 0;
22+
case /* C */2 :
23+
return 1;
24+
25+
}
1026
}
1127

1228
function f2(param) {
1329
if (param >= 3) {
1430
return /* T003 */3;
15-
} else {
16-
return param;
31+
}
32+
switch (param) {
33+
case 0 :
34+
return /* T000 */0;
35+
case 1 :
36+
return /* T001 */1;
37+
case 2 :
38+
return /* T002 */2;
39+
1740
}
1841
}
1942

2043
function f3(param) {
21-
return param;
44+
switch (param) {
45+
case /* X0 */0 :
46+
return /* Y0 */0;
47+
case /* X1 */1 :
48+
return /* Y1 */1;
49+
case /* X2 */2 :
50+
return /* Y2 */2;
51+
case /* X3 */3 :
52+
return /* Y3 */3;
53+
case /* X4 */4 :
54+
return /* Y4 */4;
55+
56+
}
2257
}
2358

2459
function f4(param) {
2560
return 3;
2661
}
2762

2863
function f5(param) {
29-
if (typeof param === "number") {
64+
if (/* tag */typeof param === "number") {
3065
switch (param) {
3166
case /* A */0 :
3267
return 1;
@@ -49,19 +84,21 @@ function f5(param) {
4984
}
5085

5186
function f6(param) {
52-
if (typeof param === "number") {
53-
if (param >= 2) {
54-
return 2;
55-
} else {
56-
return 0;
57-
}
58-
} else {
87+
if (/* tag */typeof param !== "number") {
5988
return 1;
6089
}
90+
switch (param) {
91+
case /* A */0 :
92+
case /* B */1 :
93+
return 0;
94+
case /* F */2 :
95+
return 2;
96+
97+
}
6198
}
6299

63100
function f7(param) {
64-
if (typeof param === "number") {
101+
if (/* tag */typeof param === "number") {
65102
switch (param) {
66103
case /* A */0 :
67104
return 1;
@@ -85,7 +122,7 @@ function f7(param) {
85122
}
86123

87124
function f8(param) {
88-
if (typeof param === "number") {
125+
if (/* tag */typeof param === "number") {
89126
switch (param) {
90127
case /* T60 */0 :
91128
case /* T61 */1 :
@@ -105,7 +142,7 @@ function f8(param) {
105142
}
106143

107144
function f9(param) {
108-
if (typeof param === "number") {
145+
if (/* tag */typeof param === "number") {
109146
if (param === /* T63 */3) {
110147
return 3;
111148
} else {
@@ -124,7 +161,7 @@ function f9(param) {
124161
}
125162

126163
function f10(param) {
127-
if (typeof param === "number") {
164+
if (/* tag */typeof param === "number") {
128165
switch (param) {
129166
case /* T60 */0 :
130167
return 0;
@@ -150,7 +187,7 @@ function f10(param) {
150187
}
151188

152189
function f11(x) {
153-
if (typeof x === "number") {
190+
if (/* tag */typeof x === "number") {
154191
return 2;
155192
}
156193
if (x.TAG === /* D */0) {

0 commit comments

Comments
 (0)