Skip to content

Commit 2c322b6

Browse files
committed
AST: clean up async.
1 parent 57b7390 commit 2c322b6

File tree

9 files changed

+34
-63
lines changed

9 files changed

+34
-63
lines changed

compiler/frontend/ast_attributes.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ let is_inline : attr -> bool = fun ({txt}, _) -> txt = "inline"
143143
let has_inline_payload (attrs : t) = Ext_list.find_first attrs is_inline
144144

145145
let has_await_payload (attrs : t) = Ext_list.find_first attrs Ast_await.is_await
146-
let has_async_payload (attrs : t) = Ext_list.find_first attrs Ast_async.is_async
147146

148147
type derive_attr = {bs_deriving: Ast_payload.action list option} [@@unboxed]
149148

compiler/frontend/ast_attributes.mli

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ val process_attributes_rev : t -> attr_kind * t
3636
val has_inline_payload : t -> attr option
3737

3838
val has_await_payload : t -> attr option
39-
val has_async_payload : t -> attr option
4039

4140
type derive_attr = {bs_deriving: Ast_payload.action list option} [@@unboxed]
4241

compiler/frontend/bs_builtin_ppx.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,12 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
111111
{e with pexp_desc = Pexp_constant (Pconst_integer (s, None))}
112112
(* End rewriting *)
113113
| Pexp_newtype (s, body) ->
114-
let async = Ast_attributes.has_async_payload e.pexp_attributes <> None in
114+
let async = Ast_async.has_async_payload e.pexp_attributes in
115115
let body = Ast_async.add_async_attribute ~async body in
116116
let res = self.expr self body in
117117
{e with pexp_desc = Pexp_newtype (s, res)}
118118
| Pexp_fun {arg_label = label; lhs = pat; rhs = body} -> (
119-
let async = Ast_attributes.has_async_payload e.pexp_attributes <> None in
119+
let async = Ast_async.has_async_payload e.pexp_attributes in
120120
match Ast_attributes.process_attributes_rev e.pexp_attributes with
121121
| Nothing, _ ->
122122
(* Handle @async x => y => ... is in async context *)

compiler/ml/ast_async.ml

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
1-
let is_async : Parsetree.attribute -> bool =
2-
fun ({txt}, _) -> txt = "async" || txt = "res.async"
1+
let is_async : Parsetree.attribute -> bool = fun ({txt}, _) -> txt = "res.async"
2+
3+
let has_async_payload attrs = Ext_list.exists attrs is_async
4+
5+
let make_async_attr loc = (Location.mkloc "res.async" loc, Parsetree.PStr [])
6+
7+
let add_async_attribute ~async (body : Parsetree.expression) =
8+
if async then
9+
{
10+
body with
11+
pexp_attributes =
12+
({txt = "res.async"; loc = Location.none}, PStr [])
13+
:: body.pexp_attributes;
14+
}
15+
else body
16+
17+
let extract_async_attribute attrs =
18+
let rec process async acc attrs =
19+
match attrs with
20+
| [] -> (async, List.rev acc)
21+
| ({Location.txt = "res.async"}, _) :: rest -> process true acc rest
22+
| attr :: rest -> process async (attr :: acc) rest
23+
in
24+
process false [] attrs
325

426
let add_promise_type ?(loc = Location.none) ~async
527
(result : Parsetree.expression) =
@@ -11,33 +33,6 @@ let add_promise_type ?(loc = Location.none) ~async
1133
Ast_helper.Exp.apply ~loc unsafe_async [(Nolabel, result)]
1234
else result
1335

14-
let add_async_attribute ~async (body : Parsetree.expression) =
15-
if async then
16-
match body.pexp_desc with
17-
| Pexp_construct (x, Some e) when Ast_uncurried.expr_is_uncurried_fun body
18-
->
19-
{
20-
body with
21-
pexp_desc =
22-
Pexp_construct
23-
( x,
24-
Some
25-
{
26-
e with
27-
pexp_attributes =
28-
({txt = "res.async"; loc = Location.none}, PStr [])
29-
:: e.pexp_attributes;
30-
} );
31-
}
32-
| _ ->
33-
{
34-
body with
35-
pexp_attributes =
36-
({txt = "res.async"; loc = Location.none}, PStr [])
37-
:: body.pexp_attributes;
38-
}
39-
else body
40-
4136
let rec add_promise_to_result ~loc (e : Parsetree.expression) =
4237
match e.pexp_desc with
4338
| Pexp_fun f ->

compiler/ml/translcore.ml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -652,9 +652,6 @@ let rec cut n l =
652652

653653
let try_ids = Hashtbl.create 8
654654

655-
let has_async_attribute exp =
656-
exp.exp_attributes |> List.exists (fun ({txt}, _payload) -> txt = "res.async")
657-
658655
let extract_directive_for_fn exp =
659656
exp.exp_attributes
660657
|> List.find_map (fun ({txt}, payload) ->
@@ -675,7 +672,7 @@ and transl_exp0 (e : Typedtree.expression) : Lambda.lambda =
675672
| Texp_let (rec_flag, pat_expr_list, body) ->
676673
transl_let rec_flag pat_expr_list (transl_exp body)
677674
| Texp_function {arg_label = _; arity; param; case; partial} -> (
678-
let async = has_async_attribute e in
675+
let async = Ast_async.has_async_payload e.exp_attributes in
679676
let directive =
680677
match extract_directive_for_fn e with
681678
| None -> None
@@ -1056,7 +1053,8 @@ and transl_function loc partial param case =
10561053
};
10571054
} as exp;
10581055
}
1059-
when Parmatch.inactive ~partial pat && not (exp |> has_async_attribute) ->
1056+
when Parmatch.inactive ~partial pat
1057+
&& not (Ast_async.has_async_payload exp.exp_attributes) ->
10601058
let params, body, return_unit =
10611059
transl_function exp.exp_loc partial' param' case
10621060
in

compiler/syntax/src/res_core.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ let jsx_attr = (Location.mknoloc "JSX", Parsetree.PStr [])
156156
let ternary_attr = (Location.mknoloc "res.ternary", Parsetree.PStr [])
157157
let if_let_attr = (Location.mknoloc "res.iflet", Parsetree.PStr [])
158158
let make_await_attr loc = (Location.mkloc "res.await" loc, Parsetree.PStr [])
159-
let make_async_attr loc = (Location.mkloc "res.async" loc, Parsetree.PStr [])
160159
let suppress_fragile_match_warning_attr =
161160
( Location.mknoloc "warning",
162161
Parsetree.PStr
@@ -3321,7 +3320,9 @@ and parse_expr_block ?first p =
33213320
and parse_async_arrow_expression ?(arrow_attrs = []) p =
33223321
let start_pos = p.Parser.start_pos in
33233322
Parser.expect (Lident "async") p;
3324-
let async_attr = make_async_attr (mk_loc start_pos p.prev_end_pos) in
3323+
let async_attr =
3324+
Ast_async.make_async_attr (mk_loc start_pos p.prev_end_pos)
3325+
in
33253326
parse_es6_arrow_expression
33263327
~arrow_attrs:(async_attr :: arrow_attrs)
33273328
~arrow_start_pos:(Some start_pos) p

compiler/syntax/src/res_parsetree_viewer.ml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,6 @@ let has_partial_attribute attrs =
8686
| _ -> false)
8787
attrs
8888

89-
type function_attributes_info = {async: bool; attributes: Parsetree.attributes}
90-
91-
let process_function_attributes attrs =
92-
let rec process async bs acc attrs =
93-
match attrs with
94-
| [] -> {async; attributes = List.rev acc}
95-
| ({Location.txt = "res.async"}, _) :: rest -> process true bs acc rest
96-
| attr :: rest -> process async bs (attr :: acc) rest
97-
in
98-
process false false [] attrs
99-
10089
let has_await_attribute attrs =
10190
List.exists
10291
(function

compiler/syntax/src/res_parsetree_viewer.mli

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ val process_partial_app_attribute :
1919

2020
val has_partial_attribute : Parsetree.attributes -> bool
2121

22-
type function_attributes_info = {async: bool; attributes: Parsetree.attributes}
23-
24-
(* determines whether a function is async and/or uncurried based on the given attributes *)
25-
val process_function_attributes :
26-
Parsetree.attributes -> function_attributes_info
27-
2822
val has_await_attribute : Parsetree.attributes -> bool
2923
val has_res_pat_variant_spread_attribute : Parsetree.attributes -> bool
3024
val has_dict_pattern_attribute : Parsetree.attributes -> bool

compiler/syntax/src/res_printer.ml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2696,9 +2696,7 @@ and print_if_chain ~state pexp_attributes ifs else_expr cmt_tbl =
26962696
and print_expression ~state (e : Parsetree.expression) cmt_tbl =
26972697
let print_arrow e =
26982698
let attrs_on_arrow, parameters, return_expr = ParsetreeViewer.fun_expr e in
2699-
let ParsetreeViewer.{async; attributes = attrs} =
2700-
ParsetreeViewer.process_function_attributes attrs_on_arrow
2701-
in
2699+
let async, attrs = Ast_async.extract_async_attribute attrs_on_arrow in
27022700
let return_expr, typ_constraint =
27032701
match return_expr.pexp_desc with
27042702
| Pexp_constraint (expr, typ) ->
@@ -3439,9 +3437,7 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
34393437

34403438
and print_pexp_fun ~state ~in_callback e cmt_tbl =
34413439
let attrs_on_arrow, parameters, return_expr = ParsetreeViewer.fun_expr e in
3442-
let ParsetreeViewer.{async; attributes = attrs} =
3443-
ParsetreeViewer.process_function_attributes attrs_on_arrow
3444-
in
3440+
let async, attrs = Ast_async.extract_async_attribute attrs_on_arrow in
34453441
let return_expr, typ_constraint =
34463442
match return_expr.pexp_desc with
34473443
| Pexp_constraint (expr, typ) ->

0 commit comments

Comments
 (0)