Skip to content

Commit 27da8bf

Browse files
authored
Merge pull request #2641 from BuckleScript/clean_up_raise_catch
fix #2413
2 parents 4a5190a + bf0a070 commit 27da8bf

39 files changed

+2099
-2157
lines changed

jscomp/all.depend

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -761,9 +761,11 @@ super_errors/super_main.cmx : super_errors/super_typetexp.cmx \
761761
super_errors/super_env.cmx
762762
super_errors/super_reason_react.cmi :
763763
super_errors/super_misc.cmi :
764+
outcome_printer/reason_syntax_util.cmx :
764765
outcome_printer/outcome_printer_ns.cmx : ext/ext_namespace.cmx \
765766
outcome_printer/outcome_printer_ns.cmi
766-
outcome_printer/tweaked_reason_oprint.cmx :
767+
outcome_printer/tweaked_reason_oprint.cmx : \
768+
outcome_printer/reason_syntax_util.cmx
767769
outcome_printer/reason_outcome_printer_main.cmx : \
768770
outcome_printer/tweaked_reason_oprint.cmx
769771
outcome_printer/outcome_printer_ns.cmi :

jscomp/core/bs_conditional_initial.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
let setup_env () =
2727
#if BS_DEBUG then
28-
Js_config.set_debug_file "gpr_2352_test.ml";
28+
Js_config.set_debug_file "gpr_2413_test.ml";
2929
#end
3030
Lexer.replace_directive_bool "BS" true;
3131
Lexer.replace_directive_string "BS_VERSION" Bs_version.version

jscomp/core/j.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,8 @@ and variable_declaration = {
379379
}
380380

381381
and 'a case_clause = {
382-
case : 'a ;
383-
body : block * bool ; (* true means break *)
382+
switch_case : 'a ;
383+
switch_body : block * bool ; (* true means break *)
384384
}
385385

386386
(* TODO: For efficency: block should not be a list, it should be able to

jscomp/core/js_dump.ml

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -359,32 +359,31 @@ and pp_function method_
359359
and output_one : 'a .
360360
_ -> P.t -> (P.t -> 'a -> unit) -> 'a J.case_clause -> _
361361
= fun cxt f pp_cond
362-
({case = e; body = (sl,break)} : _ J.case_clause) ->
362+
({switch_case = e; switch_body = (sl,should_break)} : _ J.case_clause) ->
363363
let cxt =
364-
P.group f 1 @@ fun _ ->
365-
P.group f 1 @@ (fun _ ->
366-
P.string f L.case;
367-
P.space f ;
368-
pp_cond f e; (* could be integer or string*)
369-
P.space f ;
370-
P.string f L.colon );
371-
372-
P.space f;
373-
P.group f 1 @@ fun _ ->
374-
let cxt =
375-
match sl with
376-
| [] -> cxt
377-
| _ ->
378-
P.newline f ;
379-
statement_list false cxt f sl
380-
in
381-
(if break then
382-
begin
383-
P.newline f ;
384-
P.string f L.break;
385-
semi f;
386-
end) ;
387-
cxt
364+
P.group f 1 (fun _ ->
365+
P.group f 1 (fun _ ->
366+
P.string f L.case;
367+
P.space f ;
368+
pp_cond f e; (* could be integer or string *)
369+
P.space f ;
370+
P.string f L.colon );
371+
P.space f;
372+
P.group f 1 (fun _ ->
373+
let cxt =
374+
match sl with
375+
| [] -> cxt
376+
| _ ->
377+
P.newline f ;
378+
statement_list false cxt f sl
379+
in
380+
(if should_break then
381+
begin
382+
P.newline f ;
383+
P.string f L.break;
384+
semi f;
385+
end) ;
386+
cxt))
388387
in
389388
P.newline f;
390389
cxt

jscomp/core/js_fold.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ class virtual fold =
461461
{[ goto : label option ; ]}
462462
*)
463463
'a. ('self_type -> 'a -> 'self_type) -> 'a case_clause -> 'self_type =
464-
fun _f_a { case = _x; body = _x_i1 } ->
464+
fun _f_a { switch_case = _x; switch_body = _x_i1 } ->
465465
let o = _f_a o _x in
466466
let o =
467467
(fun (_x, _x_i1) -> let o = o#block _x in let o = o#bool _x_i1 in o)

jscomp/core/js_map.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,13 +512,13 @@ class virtual map =
512512
*)
513513
'a 'a_out.
514514
('self_type -> 'a -> 'a_out) -> 'a case_clause -> 'a_out case_clause =
515-
fun _f_a { case = _x; body = _x_i1 } ->
515+
fun _f_a { switch_case = _x; switch_body = _x_i1 } ->
516516
let _x = _f_a o _x in
517517
let _x_i1 =
518518
(fun (_x, _x_i1) ->
519519
let _x = o#block _x in let _x_i1 = o#bool _x_i1 in (_x, _x_i1))
520520
_x_i1
521-
in { case = _x; body = _x_i1; }
521+
in { switch_case = _x; switch_body = _x_i1; }
522522
method block : block -> block = (* true means break *)
523523
(* TODO: For efficency: block should not be a list, it should be able to
524524
be concatenated in both ways

jscomp/core/js_of_lam_variant.ml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ let eval (arg : J.expression) (dispatches : (int * string) list ) : E.t =
4040
E.of_block
4141
[(S.int_switch arg
4242
(Ext_list.map (fun (i,r) ->
43-
{J.case = i ;
44-
body = [S.return_stmt (E.str r)],
43+
{J.switch_case = i ;
44+
switch_body = [S.return_stmt (E.str r)],
4545
false (* FIXME: if true, still print break*)
4646
}) dispatches))]
4747

@@ -58,8 +58,8 @@ let eval_as_event (arg : J.expression) (dispatches : (int * string) list ) =
5858
(E.of_block
5959
[(S.int_switch (E.index arg 0l)
6060
(Ext_list.map (fun (i,r) ->
61-
{J.case = i ;
62-
body = [S.return_stmt (E.str r)],
61+
{J.switch_case = i ;
62+
switch_body = [S.return_stmt (E.str r)],
6363
false (* FIXME: if true, still print break*)
6464
}) dispatches))]
6565
, (* TODO: improve, one dispatch later,
@@ -86,8 +86,8 @@ let eval_as_int (arg : J.expression) (dispatches : (int * int) list ) : E.t =
8686
E.of_block
8787
[(S.int_switch arg
8888
(Ext_list.map (fun (i,r) ->
89-
{J.case = i ;
90-
body = [S.return_stmt (E.int (Int32.of_int r))],
89+
{J.switch_case = i ;
90+
switch_body = [S.return_stmt (E.int (Int32.of_int r))],
9191
false (* FIXME: if true, still print break*)
9292
}) dispatches))]
9393

jscomp/core/js_output.ml

Lines changed: 73 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
2-
*
2+
*
33
* This program is free software: you can redistribute it and/or modify
44
* it under the terms of the GNU Lesser General Public License as published by
55
* the Free Software Foundation, either version 3 of the License, or
@@ -17,159 +17,160 @@
1717
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1818
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1919
* GNU Lesser General Public License for more details.
20-
*
20+
*
2121
* You should have received a copy of the GNU Lesser General Public License
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

2525

2626

27-
module E = Js_exp_make
28-
module S = Js_stmt_make
27+
module E = Js_exp_make
28+
module S = Js_stmt_make
2929

30-
type finished =
31-
| True
32-
| False
30+
type finished =
31+
| True
32+
| False
3333
| Dummy (* Have no idea, so that when [++] is applied, always use the other *)
3434

35-
type t = {
35+
type t = {
3636
block : J.block ;
3737
value : J.expression option;
38-
finished : finished ;
38+
output_finished : finished ;
3939

4040
}
4141

4242
type continuation = Lam_compile_context.continuation
4343

44-
let make ?value ?(finished=False) block =
45-
{ block ; value ; finished }
44+
let make ?value ?(output_finished=False) block =
45+
{ block ; value ; output_finished }
4646

4747

48-
let dummy =
49-
{value = None; block = []; finished = Dummy }
48+
let dummy =
49+
{value = None; block = []; output_finished = Dummy }
5050

51-
let output_of_expression
51+
let output_of_expression
5252
(continuation : continuation)
5353
(should_return : Lam_compile_context.return_type)
5454
(lam : Lam.t) (exp : J.expression) : t =
55-
begin match continuation, should_return with
56-
| EffectCall, ReturnFalse ->
57-
if Lam_analysis.no_side_effects lam
55+
begin match continuation, should_return with
56+
| EffectCall, ReturnFalse ->
57+
if Lam_analysis.no_side_effects lam
5858
then dummy
59-
else {block = []; value = Some exp ; finished = False}
60-
| Declare (kind, n), ReturnFalse ->
59+
else {block = []; value = Some exp ; output_finished = False}
60+
| Declare (kind, n), ReturnFalse ->
6161
make [ S.define_variable ~kind n exp]
62-
| Assign n ,ReturnFalse ->
62+
| Assign n ,ReturnFalse ->
6363
make [S.assign n exp ]
6464
| EffectCall, ReturnTrue _ ->
65-
make [S.return_stmt exp] ~finished:True
66-
| (Declare _ | Assign _ ), ReturnTrue _ ->
67-
make [S.unknown_lambda lam] ~finished:True
68-
| NeedValue, _ ->
69-
{block = []; value = Some exp; finished = False }
65+
make [S.return_stmt exp] ~output_finished:True
66+
| (Declare _ | Assign _ ), ReturnTrue _ ->
67+
make [S.unknown_lambda lam] ~output_finished:True
68+
| NeedValue, _ ->
69+
{block = []; value = Some exp; output_finished = False }
7070
end
7171

72-
let output_of_block_and_expression
73-
(continuation : continuation)
72+
let output_of_block_and_expression
73+
(continuation : continuation)
7474
(should_return : Lam_compile_context.return_type)
75-
(lam : Lam.t) (block : J.block) exp : t =
76-
match continuation, should_return with
75+
(lam : Lam.t) (block : J.block) exp : t =
76+
match continuation, should_return with
7777
| EffectCall, ReturnFalse -> make block ~value:exp
78-
| Declare (kind,n), ReturnFalse ->
78+
| Declare (kind,n), ReturnFalse ->
7979
make (block @ [ S.define_variable ~kind n exp])
80-
| Assign n, ReturnFalse -> make (block @ [S.assign n exp])
81-
| EffectCall, ReturnTrue _ -> make (block @ [S.return_stmt exp]) ~finished:True
80+
| Assign n, ReturnFalse -> make (block @ [S.assign n exp])
81+
| EffectCall, ReturnTrue _ -> make (block @ [S.return_stmt exp]) ~output_finished:True
8282
| (Declare _ | Assign _), ReturnTrue _ ->
83-
make [S.unknown_lambda lam] ~finished:True
84-
| NeedValue, (ReturnTrue _ | ReturnFalse) ->
83+
make [S.unknown_lambda lam] ~output_finished:True
84+
| NeedValue, (ReturnTrue _ | ReturnFalse) ->
8585
make block ~value:exp
8686

8787

8888

89-
let block_with_opt_expr block (x : J.expression option) : J.block =
90-
match x with
89+
let block_with_opt_expr block (x : J.expression option) : J.block =
90+
match x with
9191
| None -> block
9292
| Some x when Js_analyzer.no_side_effect_expression x -> block
9393
| Some x -> block @ [S.exp x ]
9494

95-
let opt_expr_with_block (x : J.expression option) block : J.block =
96-
match x with
95+
let opt_expr_with_block (x : J.expression option) block : J.block =
96+
match x with
9797
| None -> block
9898
| Some x when Js_analyzer.no_side_effect_expression x -> block
9999
| Some x -> (S.exp x) :: block
100-
101100

102-
let rec unnest_block (block : J.block) : J.block =
103-
match block with
104-
| [{statement_desc = Block block}] -> unnest_block block
105-
| _ -> block
106101

107-
let output_as_block ( x : t) : J.block =
108-
match x with
109-
| {block; value = opt; finished} ->
102+
let rec unnest_block (block : J.block) : J.block =
103+
match block with
104+
| [{statement_desc = Block block}] -> unnest_block block
105+
| _ -> block
106+
107+
let output_as_block ( x : t) : J.block =
108+
match x with
109+
| {block; value = opt; output_finished} ->
110110
let block = unnest_block block in
111-
if finished = True then block
112-
else
111+
if output_finished = True then block
112+
else
113113
block_with_opt_expr block opt
114-
115114

116-
let to_break_block (x : t) : J.block * bool =
117-
let block = unnest_block x.block in
118-
match x with
119-
| {finished = True; _ } ->
115+
116+
let to_break_block (x : t) : J.block * bool =
117+
let block = unnest_block x.block in
118+
match x with
119+
| {output_finished = True; _ } ->
120120
block, false
121121
(* value does not matter when [finished] is true
122122
TODO: check if it has side efects
123123
*)
124-
| { value = None; finished } ->
125-
block, (match finished with | True -> false | (False | Dummy) -> true )
124+
| { value = None; output_finished } ->
125+
block,
126+
(match output_finished with | True -> false | (False | Dummy) -> true )
126127

127-
| {value = Some _ as opt; _} ->
128+
| {value = Some _ as opt; _} ->
128129
block_with_opt_expr block opt, true
129130

130131

131132
(** TODO: make everything expression make inlining hard, and code not readable?
132-
1. readability dpends on how we print the expression
133+
1. readability dpends on how we print the expression
133134
2. inlining needs generate symbols, which are statements, type mismatch
134135
we need capture [Exp e]
135136
136-
can we call them all [statement]? statement has no value
137+
can we call them all [statement]? statement has no value
137138
*)
138139
(* | {block = [{statement_desc = Exp e }]; value = None ; _}, _ *)
139140
(* -> *)
140141
(* append { x with block = []; value = Some e} y *)
141142
(* | _ , {block = [{statement_desc = Exp e }]; value = None ; _} *)
142143
(* -> *)
143144
(* append x { y with block = []; value = Some e} *)
144-
145-
let rec append_output (x : t ) (y : t ) : t =
145+
146+
let rec append_output (x : t ) (y : t ) : t =
146147
match x , y with (* ATTTENTION: should not optimize [opt_e2], it has to conform to [NeedValue]*)
147-
| {finished = True; _ }, _ -> x
148-
| _, {block = []; value= None; finished = Dummy } -> x
148+
| { output_finished = True; _ }, _ -> x
149+
| _, {block = []; value= None; output_finished = Dummy } -> x
149150
(* finished = true --> value = E.undefined otherwise would throw*)
150-
| {block = []; value= None; _ }, y -> y
151-
| {block = []; value= Some _; _}, {block = []; value= None; _ } -> x
152-
| {block = []; value = Some e1; _}, ({block = []; value = Some e2; finished } as z) ->
153-
if Js_analyzer.no_side_effect_expression e1
151+
| {block = []; value= None; _ }, y -> y
152+
| {block = []; value= Some _; _}, {block = []; value= None; _ } -> x
153+
| {block = []; value = Some e1; _}, ({block = []; value = Some e2; output_finished } as z) ->
154+
if Js_analyzer.no_side_effect_expression e1
154155
then z
155156
(* It would optimize cases like [module aliases]
156-
Bigarray, List
157+
Bigarray, List
157158
*)
158159
else
159-
{block = []; value = Some (E.seq e1 e2); finished}
160+
{block = []; value = Some (E.seq e1 e2); output_finished}
160161
(* {block = [S.exp e1]; value = Some e2(\* (E.seq e1 e2) *\); finished} *)
161162

162-
| {block = block1; value = opt_e1; _}, {block = block2; value = opt_e2; finished} ->
163+
| {block = block1; value = opt_e1; _}, {block = block2; value = opt_e2; output_finished} ->
163164
let block1 = unnest_block block1 in
164165
make (block1 @ (opt_expr_with_block opt_e1 @@ unnest_block block2))
165-
?value:opt_e2 ~finished
166+
?value:opt_e2 ~output_finished:output_finished
166167

167168

168169

169170

170171
(* Fold right is more efficient *)
171-
let concat (xs : t list) : t =
172+
let concat (xs : t list) : t =
172173
Ext_list.fold_right (fun x acc -> append_output x acc) xs dummy
173174

174-
let to_string x =
175+
let to_string x =
175176
Js_dump.string_of_block (output_as_block x)

0 commit comments

Comments
 (0)