Skip to content

Commit c90eb55

Browse files
committed
Extend untyped and types ast with async attribute.
1 parent 0745025 commit c90eb55

24 files changed

+119
-80
lines changed

compiler/frontend/ast_compatible.ml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,20 @@ let app3 ?(loc = default_loc) ?(attrs = []) fn arg1 arg2 arg3 : expression =
6464
Pexp_apply (fn, [(Nolabel, arg1); (Nolabel, arg2); (Nolabel, arg3)]);
6565
}
6666

67-
let fun_ ?(loc = default_loc) ?(attrs = []) ~arity pat exp =
67+
let fun_ ?(loc = default_loc) ?(attrs = []) ?(async = false) ~arity pat exp =
6868
{
6969
pexp_loc = loc;
7070
pexp_attributes = attrs;
7171
pexp_desc =
7272
Pexp_fun
73-
{arg_label = Nolabel; default = None; lhs = pat; rhs = exp; arity};
73+
{
74+
arg_label = Nolabel;
75+
default = None;
76+
lhs = pat;
77+
rhs = exp;
78+
arity;
79+
async;
80+
};
7481
}
7582

7683
let const_exp_string ?(loc = default_loc) ?(attrs = []) ?delimiter (s : string)

compiler/frontend/ast_compatible.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ val apply_labels :
7474
val fun_ :
7575
?loc:Location.t ->
7676
?attrs:attrs ->
77+
?async:bool ->
7778
arity:int option ->
7879
pattern ->
7980
expression ->

compiler/frontend/ast_uncurry_gen.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ let to_method_callback loc (self : Bs_ast_mapper.mapper) label
3636
match Ast_attributes.process_attributes_rev body.pexp_attributes with
3737
| Nothing, attrs -> (
3838
match body.pexp_desc with
39-
| Pexp_fun {arg_label; lhs = arg; rhs = body} ->
39+
| Pexp_fun {arg_label; lhs = arg; rhs = body; async} ->
4040
Bs_syntaxerr.optional_err loc arg_label;
41-
aux ((arg_label, self.pat self arg, attrs) :: acc) body
41+
aux ((arg_label, self.pat self arg, attrs, async) :: acc) body
4242
| _ -> (self.expr self body, acc))
4343
| _, _ -> (self.expr self body, acc)
4444
in
45-
let result, rev_extra_args = aux [(label, self_pat, [])] body in
45+
let result, rev_extra_args = aux [(label, self_pat, [], false)] body in
4646
let body =
47-
Ext_list.fold_left rev_extra_args result (fun e (label, p, attrs) ->
48-
Ast_helper.Exp.fun_ ~loc ~attrs ~arity:None label None p e)
47+
Ext_list.fold_left rev_extra_args result (fun e (label, p, attrs, async) ->
48+
Ast_helper.Exp.fun_ ~loc ~attrs ~arity:None ~async label None p e)
4949
in
5050
let arity = List.length rev_extra_args in
5151
let arity_s = string_of_int arity in

compiler/frontend/bs_ast_mapper.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,9 @@ module E = struct
315315
sub vbs)
316316
(sub.expr sub e)
317317
(* #end *)
318-
| Pexp_fun {arg_label = lab; default = def; lhs = p; rhs = e; arity} ->
319-
fun_ ~loc ~attrs ~arity lab
318+
| Pexp_fun {arg_label = lab; default = def; lhs = p; rhs = e; arity; async}
319+
->
320+
fun_ ~loc ~attrs ~arity ~async lab
320321
(map_opt (sub.expr sub) def)
321322
(sub.pat sub p) (sub.expr sub e)
322323
| Pexp_apply (e, l) ->

compiler/frontend/bs_builtin_ppx.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,7 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
113113
| Pexp_newtype (s, body) ->
114114
let res = self.expr self body in
115115
{e with pexp_desc = Pexp_newtype (s, res)}
116-
| Pexp_fun {arg_label = label; lhs = pat; rhs = body} -> (
117-
let async = Ast_async.has_async_payload e.pexp_attributes in
116+
| Pexp_fun {arg_label = label; lhs = pat; rhs = body; async} -> (
118117
match Ast_attributes.process_attributes_rev e.pexp_attributes with
119118
| Nothing, _ ->
120119
(* Handle @async x => y => ... is in async context *)

compiler/ml/ast_async.ml

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,16 @@
1-
let has_async_payload attrs =
2-
Ext_list.exists attrs (fun ({Location.txt}, _) -> txt = "res.async")
3-
41
let rec dig_async_payload_from_function (expr : Parsetree.expression) =
52
match expr.pexp_desc with
6-
| Pexp_fun _ -> has_async_payload expr.pexp_attributes
3+
| Pexp_fun {async} -> async
74
| Pexp_newtype (_, body) -> dig_async_payload_from_function body
85
| _ -> false
96

107
let add_async_attribute ~async (body : Parsetree.expression) =
11-
let add (exp : Parsetree.expression) =
12-
if has_async_payload exp.pexp_attributes then exp
13-
else
14-
{
15-
exp with
16-
pexp_attributes =
17-
({txt = "res.async"; loc = Location.none}, PStr [])
18-
:: exp.pexp_attributes;
19-
}
20-
in
218
if async then
229
let rec add_to_fun (exp : Parsetree.expression) =
2310
match exp.pexp_desc with
2411
| Pexp_newtype (txt, e) ->
2512
{exp with pexp_desc = Pexp_newtype (txt, add_to_fun e)}
26-
| Pexp_fun _ -> add exp
13+
| Pexp_fun f -> {exp with pexp_desc = Pexp_fun {f with async}}
2714
| _ -> exp
2815
in
2916
add_to_fun body

compiler/ml/ast_helper.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ module Exp = struct
151151
let ident ?loc ?attrs a = mk ?loc ?attrs (Pexp_ident a)
152152
let constant ?loc ?attrs a = mk ?loc ?attrs (Pexp_constant a)
153153
let let_ ?loc ?attrs a b c = mk ?loc ?attrs (Pexp_let (a, b, c))
154-
let fun_ ?loc ?attrs ~arity a b c d =
154+
let fun_ ?loc ?attrs ?(async = false) ~arity a b c d =
155155
mk ?loc ?attrs
156-
(Pexp_fun {arg_label = a; default = b; lhs = c; rhs = d; arity})
156+
(Pexp_fun {arg_label = a; default = b; lhs = c; rhs = d; arity; async})
157157
let apply ?loc ?attrs a b = mk ?loc ?attrs (Pexp_apply (a, b))
158158
let match_ ?loc ?attrs a b = mk ?loc ?attrs (Pexp_match (a, b))
159159
let try_ ?loc ?attrs a b = mk ?loc ?attrs (Pexp_try (a, b))

compiler/ml/ast_helper.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ module Exp : sig
138138
val fun_ :
139139
?loc:loc ->
140140
?attrs:attrs ->
141+
?async:bool ->
141142
arity:int option ->
142143
arg_label ->
143144
expression option ->

compiler/ml/ast_mapper.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,9 @@ module E = struct
278278
| Pexp_constant x -> constant ~loc ~attrs x
279279
| Pexp_let (r, vbs, e) ->
280280
let_ ~loc ~attrs r (List.map (sub.value_binding sub) vbs) (sub.expr sub e)
281-
| Pexp_fun {arg_label = lab; default = def; lhs = p; rhs = e; arity} ->
282-
fun_ ~loc ~attrs ~arity lab
281+
| Pexp_fun {arg_label = lab; default = def; lhs = p; rhs = e; arity; async}
282+
->
283+
fun_ ~loc ~attrs ~arity ~async lab
283284
(map_opt (sub.expr sub) def)
284285
(sub.pat sub p) (sub.expr sub e)
285286
| Pexp_apply (e, l) ->

compiler/ml/ast_mapper_from0.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,8 @@ module E = struct
304304
| Pexp_let (r, vbs, e) ->
305305
let_ ~loc ~attrs r (List.map (sub.value_binding sub) vbs) (sub.expr sub e)
306306
| Pexp_fun (lab, def, p, e) ->
307-
fun_ ~loc ~attrs ~arity:None lab
307+
let async = Ext_list.exists attrs (fun ({txt}, _) -> txt = "res.async") in
308+
fun_ ~loc ~attrs ~async ~arity:None lab
308309
(map_opt (sub.expr sub) def)
309310
(sub.pat sub p) (sub.expr sub e)
310311
| Pexp_function _ -> assert false

0 commit comments

Comments
 (0)