Skip to content

Commit f693afc

Browse files
committedJan 9, 2025
Put async attributes on function at construction time, not with the builtin ppx.
1 parent afd0efb commit f693afc

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed
 

‎compiler/frontend/bs_builtin_ppx.ml

-2
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,6 @@ 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_async.has_async_payload e.pexp_attributes in
115-
let body = Ast_async.add_async_attribute ~async body in
116114
let res = self.expr self body in
117115
{e with pexp_desc = Pexp_newtype (s, res)}
118116
| Pexp_fun {arg_label = label; lhs = pat; rhs = body} -> (

‎compiler/ml/ast_async.ml

+18-6
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,25 @@ let has_async_payload attrs =
22
Ext_list.exists attrs (fun ({Location.txt}, _) -> txt = "res.async")
33

44
let add_async_attribute ~async (body : Parsetree.expression) =
5+
let add (exp : Parsetree.expression) =
6+
if has_async_payload exp.pexp_attributes then exp
7+
else
8+
{
9+
exp with
10+
pexp_attributes =
11+
({txt = "res.async"; loc = Location.none}, PStr [])
12+
:: exp.pexp_attributes;
13+
}
14+
in
515
if async then
6-
{
7-
body with
8-
pexp_attributes =
9-
({txt = "res.async"; loc = Location.none}, PStr [])
10-
:: body.pexp_attributes;
11-
}
16+
let rec add_to_fun (exp : Parsetree.expression) =
17+
match exp.pexp_desc with
18+
| Pexp_newtype (txt, e) ->
19+
{exp with pexp_desc = Pexp_newtype (txt, add_to_fun e)}
20+
| Pexp_fun _ -> add exp
21+
| _ -> exp
22+
in
23+
add (add_to_fun body)
1224
else body
1325

1426
let add_promise_type ?(loc = Location.none) ~async

‎tests/syntax_tests/data/parsing/grammar/expressions/expected/async.res.txt

+8-7
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,16 @@ let ex3 = ((foo |.u (bar ~arg:((arg)[@res.namedArgLoc ])))[@res.await ])
3434
let ex4 = (((foo.bar).baz)[@res.await ])
3535
let attr1 = ((fun [arity:1]x -> x + 1)[@res.async ][@a ])
3636
let attr2 = ((fun (type a) ->
37-
fun [arity:1]() -> fun (type b) -> fun (type c) -> fun [arity:1]x -> 3)
38-
[@res.async ][@a ])
37+
((fun [arity:1]() -> fun (type b) -> fun (type c) -> fun [arity:1]x -> 3)
38+
[@res.async ]))[@res.async ][@a ])
3939
let attr3 = ((fun (type a) ->
40-
fun [arity:1]() -> ((fun (type b) -> fun (type c) -> fun [arity:1]x -> 3)
41-
[@res.async ]))
40+
fun [arity:1]() -> ((fun (type b) -> fun (type c) -> ((fun [arity:1]x -> 3)
41+
[@res.async ]))[@res.async ]))
4242
[@a ])
4343
let attr4 = ((fun (type a) ->
44-
fun [arity:1]() -> ((fun (type b) -> fun (type c) -> fun [arity:1]x -> 3)
45-
[@res.async ][@b ]))
44+
fun [arity:1]() -> ((fun (type b) -> fun (type c) -> ((fun [arity:1]x -> 3)
45+
[@res.async ]))[@res.async ][@b ]))
4646
[@a ])
4747
let (attr5 : int) = ((fun (type a) -> fun (type b) -> fun (type c) ->
48-
fun [arity:1]() -> fun [arity:1](x : a) -> x)[@res.async ][@a ][@b ])
48+
((fun [arity:1]() -> fun [arity:1](x : a) -> x)[@res.async ]))
49+
[@res.async ][@a ][@b ])

0 commit comments

Comments
 (0)