@@ -76,7 +76,9 @@ let pat_mapper (self : mapper) (e : Parsetree.pattern) =
76
76
| Ppat_constant (Pconst_integer (s , Some 'l' )) ->
77
77
{e with ppat_desc = Ppat_constant (Pconst_integer (s,None ))}
78
78
| _ -> default_pat_mapper self e
79
- let expr_mapper ~async_context (self : mapper ) (e : Parsetree.expression ) =
79
+ let expr_mapper ~async_context ~in_function_def (self : mapper ) (e : Parsetree.expression ) =
80
+ let old_in_function_def = ! in_function_def in
81
+ in_function_def := false ;
80
82
match e.pexp_desc with
81
83
(* Its output should not be rewritten anymore *)
82
84
| Pexp_extension extension ->
@@ -122,13 +124,13 @@ let expr_mapper ~async_context (self : mapper) (e : Parsetree.expression) =
122
124
123
125
| Pexp_fun (label, _, pat , body)
124
126
->
125
- async_context := false ;
126
127
let async = Ast_attributes. has_async_payload e.pexp_attributes <> None in
127
128
begin match Ast_attributes. process_attributes_rev e.pexp_attributes with
128
129
| Nothing , _ ->
129
- if async then
130
- Location. raise_errorf ~loc: e.pexp_loc " Async can only be applied to uncurried function" ;
131
- default_expr_mapper self e
130
+ (* Handle @async x => y => ... is in async context *)
131
+ async_context := (old_in_function_def && ! async_context) || async;
132
+ in_function_def := true ;
133
+ Ast_async. make_function_async ~async (default_expr_mapper self e)
132
134
| Uncurry _, pexp_attributes
133
135
->
134
136
async_context := async;
@@ -140,6 +142,7 @@ let expr_mapper ~async_context (self : mapper) (e : Parsetree.expression) =
140
142
| Meth_callback _, pexp_attributes
141
143
->
142
144
(* FIXME: does it make sense to have a label for [this] ? *)
145
+ async_context := false ;
143
146
{e with pexp_desc = Ast_uncurry_gen. to_method_callback e.pexp_loc self label pat body ;
144
147
pexp_attributes }
145
148
end
@@ -200,9 +203,9 @@ let expr_mapper ~async_context (self : mapper) (e : Parsetree.expression) =
200
203
*)
201
204
| _ -> default_expr_mapper self e
202
205
203
- let expr_mapper ~async_context (self : mapper ) (e : Parsetree.expression ) =
206
+ let expr_mapper ~async_context ~ in_function_def (self : mapper ) (e : Parsetree.expression ) =
204
207
let async_saved = ! async_context in
205
- let result = expr_mapper ~async_context self e in
208
+ let result = expr_mapper ~async_context ~in_function_def self e in
206
209
async_context := async_saved;
207
210
match Ast_attributes. has_await_payload e.pexp_attributes with
208
211
| None -> result
@@ -472,7 +475,7 @@ let rec
472
475
473
476
let mapper : mapper =
474
477
{ default_mapper with
475
- expr = expr_mapper ~async_context: (ref false );
478
+ expr = expr_mapper ~async_context: (ref false ) ~in_function_def: ( ref false ) ;
476
479
pat = pat_mapper;
477
480
typ = typ_mapper ;
478
481
class_type = class_type_mapper;
0 commit comments