Skip to content

Commit 8a5f506

Browse files
committed
fix a potential bug: missed block
1 parent 1c17a46 commit 8a5f506

File tree

4 files changed

+74
-45
lines changed

4 files changed

+74
-45
lines changed

jscomp/core/lam_compile.ml

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,17 +1231,22 @@ and
12311231
if Ext_list.length_ge sw_blocks sw_numblocks
12321232
then Complete
12331233
else Default x in
1234-
let compile_whole ({st; _} as cxt : Lam_compile_context.t ) =
1235-
match sw_numconsts, sw_numblocks,
1236-
compile_lambda {cxt with should_return = ReturnFalse; st = NeedValue}
1237-
lam with
1238-
| 0 , _ , {block; value = Some e} ->
1239-
compile_cases cxt (E.tag e ) sw_blocks sw_blocks_default
1240-
| _, 0, {block; value = Some e} ->
1241-
compile_cases cxt e sw_consts sw_num_default
1242-
| _, _, { block; value = Some e} -> (* [e] will be used twice *)
1243-
let dispatch e =
1244-
[
1234+
let compile_whole (cxt : Lam_compile_context.t ) =
1235+
match
1236+
compile_lambda
1237+
{cxt with should_return = ReturnFalse; st = NeedValue}
1238+
lam
1239+
with
1240+
| {value = None; _} -> assert false
1241+
| { block; value = Some e } ->
1242+
block @
1243+
(if sw_numconsts = 0 then
1244+
compile_cases cxt (E.tag e) sw_blocks sw_blocks_default
1245+
else if sw_numblocks = 0 then
1246+
compile_cases cxt e sw_consts sw_num_default
1247+
else
1248+
(* [e] will be used twice *)
1249+
let dispatch e =
12451250
S.if_
12461251
(E.is_type_number e )
12471252
(compile_cases cxt e sw_consts sw_num_default
@@ -1250,16 +1255,15 @@ and
12501255
~else_:
12511256
(compile_cases cxt (E.tag e ) sw_blocks
12521257
sw_blocks_default)
1253-
] in
1254-
begin
1255-
match e.expression_desc with
1256-
| J.Var _ -> dispatch e
1257-
| _ ->
1258-
let v = Ext_ident.create_tmp () in
1259-
(* Necessary avoid duplicated computation*)
1260-
(S.define_variable ~kind:Variable v e ) :: dispatch (E.var v)
1261-
end
1262-
| _, _, {value = None; _} -> assert false
1258+
in
1259+
begin
1260+
match e.expression_desc with
1261+
| J.Var _ -> [ dispatch e]
1262+
| _ ->
1263+
let v = Ext_ident.create_tmp () in
1264+
(* Necessary avoid duplicated computation*)
1265+
[ S.define_variable ~kind:Variable v e ; dispatch (E.var v)]
1266+
end )
12631267
in
12641268
begin
12651269
match st with (* Needs declare first *)

jscomp/test/gpr_2413_test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,16 @@ function f(param) {
2828

2929
}
3030

31+
function ff(c) {
32+
c[0] = c[0] + 1 | 0;
33+
var match = (1 + c[0] | 0) + 1 | 0;
34+
if (match > 3 || match < 0) {
35+
return 0;
36+
} else {
37+
return match + 1 | 0;
38+
}
39+
}
40+
3141
exports.f = f;
42+
exports.ff = ff;
3243
/* No side effect */

jscomp/test/gpr_2413_test.ml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
type inner =
22
| P of int
33
| S of int
4-
4+
55
type outer =
66
| A of inner
77
| B of inner
88
| C of inner
9-
9+
1010
let f = function
1111
(* These cause unreachable code *)
1212
| A P a -> a + a
1313
| A S a -> a - a
1414
(* These don't, because there's commonality between them *)
15-
| B P a | B S a | C P a | C S a -> a * a
15+
| B P a | B S a | C P a | C S a -> a * a
16+
17+
18+
let ff c =
19+
match let a = 1 in let b = 1 in
20+
incr c; a + !c + b with
21+
| 0 -> 1
22+
| 1 -> 2
23+
| 2 -> 3
24+
| 3 -> 4
25+
| _ -> 0

lib/whole_compiler.ml

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -98601,17 +98601,22 @@ and
9860198601
if Ext_list.length_ge sw_blocks sw_numblocks
9860298602
then Complete
9860398603
else Default x in
98604-
let compile_whole ({st; _} as cxt : Lam_compile_context.t ) =
98605-
match sw_numconsts, sw_numblocks,
98606-
compile_lambda {cxt with should_return = ReturnFalse; st = NeedValue}
98607-
lam with
98608-
| 0 , _ , {block; value = Some e} ->
98609-
compile_cases cxt (E.tag e ) sw_blocks sw_blocks_default
98610-
| _, 0, {block; value = Some e} ->
98611-
compile_cases cxt e sw_consts sw_num_default
98612-
| _, _, { block; value = Some e} -> (* [e] will be used twice *)
98613-
let dispatch e =
98614-
[
98604+
let compile_whole (cxt : Lam_compile_context.t ) =
98605+
match
98606+
compile_lambda
98607+
{cxt with should_return = ReturnFalse; st = NeedValue}
98608+
lam
98609+
with
98610+
| {value = None; _} -> assert false
98611+
| { block; value = Some e } ->
98612+
block @
98613+
(if sw_numconsts = 0 then
98614+
compile_cases cxt (E.tag e) sw_blocks sw_blocks_default
98615+
else if sw_numblocks = 0 then
98616+
compile_cases cxt e sw_consts sw_num_default
98617+
else
98618+
(* [e] will be used twice *)
98619+
let dispatch e =
9861598620
S.if_
9861698621
(E.is_type_number e )
9861798622
(compile_cases cxt e sw_consts sw_num_default
@@ -98620,16 +98625,15 @@ and
9862098625
~else_:
9862198626
(compile_cases cxt (E.tag e ) sw_blocks
9862298627
sw_blocks_default)
98623-
] in
98624-
begin
98625-
match e.expression_desc with
98626-
| J.Var _ -> dispatch e
98627-
| _ ->
98628-
let v = Ext_ident.create_tmp () in
98629-
(* Necessary avoid duplicated computation*)
98630-
(S.define_variable ~kind:Variable v e ) :: dispatch (E.var v)
98631-
end
98632-
| _, _, {value = None; _} -> assert false
98628+
in
98629+
begin
98630+
match e.expression_desc with
98631+
| J.Var _ -> [ dispatch e]
98632+
| _ ->
98633+
let v = Ext_ident.create_tmp () in
98634+
(* Necessary avoid duplicated computation*)
98635+
[ S.define_variable ~kind:Variable v e ; dispatch (E.var v)]
98636+
end )
9863398637
in
9863498638
begin
9863598639
match st with (* Needs declare first *)

0 commit comments

Comments
 (0)