Skip to content

Commit afd0efb

Browse files
committedJan 9, 2025
More cleanup of async handling.
1 parent 1319ddd commit afd0efb

8 files changed

+21
-43
lines changed
 

‎CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#### :house: Internal
2222

2323
- AST cleanup: Prepare for ast async cleanup: Refactor code for "@res.async" payload handling and clean up handling of type and term parameters, so that now each `=>` in a function definition corresponds to a function. https://github.com/rescript-lang/rescript/pull/7223
24-
24+
- AST: always put type parameters first in function definitions. https://github.com/rescript-lang/rescript/pull/7233
2525

2626
# 12.0.0-alpha.7
2727

‎compiler/ml/ast_async.ml

+2-14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
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 [])
1+
let has_async_payload attrs =
2+
Ext_list.exists attrs (fun ({Location.txt}, _) -> txt = "res.async")
63

74
let add_async_attribute ~async (body : Parsetree.expression) =
85
if async then
@@ -14,15 +11,6 @@ let add_async_attribute ~async (body : Parsetree.expression) =
1411
}
1512
else body
1613

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
25-
2614
let add_promise_type ?(loc = Location.none) ~async
2715
(result : Parsetree.expression) =
2816
if async then

‎compiler/syntax/src/jsx_v4.ml

+2-6
Original file line numberDiff line numberDiff line change
@@ -955,10 +955,8 @@ let map_binding ~config ~empty_loc ~pstr_loc ~file_name ~rec_flag binding =
955955
modified_binding ~binding_loc ~binding_pat_loc ~fn_name binding
956956
in
957957
let is_async =
958-
Ext_list.find_first binding.pvb_expr.pexp_attributes Ast_async.is_async
959-
|> Option.is_some
958+
Ast_async.has_async_payload binding.pvb_expr.pexp_attributes
960959
in
961-
(* do stuff here! *)
962960
let named_arg_list, newtypes, _typeConstraints =
963961
recursively_transform_named_args_for_make
964962
(modified_binding_old binding)
@@ -1192,9 +1190,7 @@ let map_binding ~config ~empty_loc ~pstr_loc ~file_name ~rec_flag binding =
11921190
in
11931191

11941192
let is_async =
1195-
Ext_list.find_first modified_binding.pvb_expr.pexp_attributes
1196-
Ast_async.is_async
1197-
|> Option.is_some
1193+
Ast_async.has_async_payload modified_binding.pvb_expr.pexp_attributes
11981194
in
11991195

12001196
let make_new_binding ~loc ~full_module_name binding =

‎compiler/syntax/src/res_core.ml

+2-6
Original file line numberDiff line numberDiff line change
@@ -3303,12 +3303,8 @@ and parse_expr_block ?first p =
33033303
and parse_async_arrow_expression ?(arrow_attrs = []) p =
33043304
let start_pos = p.Parser.start_pos in
33053305
Parser.expect (Lident "async") p;
3306-
let async_attr =
3307-
Ast_async.make_async_attr (mk_loc start_pos p.prev_end_pos)
3308-
in
3309-
parse_es6_arrow_expression
3310-
~arrow_attrs:(async_attr :: arrow_attrs)
3311-
~arrow_start_pos:(Some start_pos) p
3306+
Ast_async.add_async_attribute ~async:true
3307+
(parse_es6_arrow_expression ~arrow_attrs ~arrow_start_pos:(Some start_pos) p)
33123308

33133309
and parse_await_expression p =
33143310
let await_loc = mk_loc p.Parser.start_pos p.end_pos in

‎compiler/syntax/src/res_parens.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ let ternary_operand expr =
301301
Nothing
302302
| {pexp_desc = Pexp_constraint _} -> Parenthesized
303303
| _ when Res_parsetree_viewer.is_fun_newtype expr -> (
304-
let _parameters, return_expr = ParsetreeViewer.fun_expr expr in
304+
let _, _parameters, return_expr = ParsetreeViewer.fun_expr expr in
305305
match return_expr.pexp_desc with
306306
| Pexp_constraint _ -> Parenthesized
307307
| _ -> Nothing)

‎compiler/syntax/src/res_parsetree_viewer.ml

+7-7
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ type fun_param_kind =
165165
}
166166
| NewTypes of {attrs: Parsetree.attributes; locs: string Asttypes.loc list}
167167

168-
let fun_expr expr =
168+
let fun_expr expr_ =
169+
let async = Ast_async.has_async_payload expr_.pexp_attributes in
169170
let rec collect_params ~n_fun ~params expr =
170171
match expr with
171172
| {
@@ -182,10 +183,9 @@ let fun_expr expr =
182183
}
183184
when arity = None || n_fun = 0 ->
184185
let parameter = Parameter {attrs; lbl; default_expr; pat = pattern} in
185-
collect_params ~n_fun:(n_fun + 1) ~params:(parameter :: params) return_expr
186-
(* If a fun has an attribute, then it stops here and makes currying.
187-
i.e attributes outside of (...), uncurried `(.)` and `async` make currying *)
188-
| _ -> (List.rev params, expr)
186+
collect_params ~n_fun:(n_fun + 1) ~params:(parameter :: params)
187+
return_expr
188+
| _ -> (async, List.rev params, expr)
189189
in
190190
(* Turns (type t, type u, type z) into "type t u z" *)
191191
let rec collect_new_types acc return_expr =
@@ -194,12 +194,12 @@ let fun_expr expr =
194194
collect_new_types (string_loc :: acc) return_expr
195195
| return_expr -> (List.rev acc, return_expr)
196196
in
197-
match expr with
197+
match expr_ with
198198
| {pexp_desc = Pexp_newtype (string_loc, rest)} ->
199199
let string_locs, return_expr = collect_new_types [string_loc] rest in
200200
let param = NewTypes {attrs = []; locs = string_locs} in
201201
collect_params ~n_fun:0 ~params:[param] return_expr
202-
| _ -> collect_params ~n_fun:0 ~params:[] {expr with pexp_attributes = []}
202+
| _ -> collect_params ~n_fun:0 ~params:[] {expr_ with pexp_attributes = []}
203203

204204
let process_braces_attr expr =
205205
match expr.pexp_attributes with

‎compiler/syntax/src/res_parsetree_viewer.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ type fun_param_kind =
5353
| NewTypes of {attrs: Parsetree.attributes; locs: string Asttypes.loc list}
5454

5555
val fun_expr :
56-
Parsetree.expression -> fun_param_kind list * Parsetree.expression
56+
Parsetree.expression -> bool * fun_param_kind list * Parsetree.expression
5757

5858
(* example:
5959
* `makeCoordinate({

‎compiler/syntax/src/res_printer.ml

+5-7
Original file line numberDiff line numberDiff line change
@@ -1980,7 +1980,7 @@ and print_value_binding ~state ~rec_flag (vb : Parsetree.value_binding) cmt_tbl
19801980
};
19811981
pvb_expr = {pexp_desc = Pexp_newtype _} as expr;
19821982
} -> (
1983-
let parameters, return_expr = ParsetreeViewer.fun_expr expr in
1983+
let _, parameters, return_expr = ParsetreeViewer.fun_expr expr in
19841984
let abstract_type =
19851985
match parameters with
19861986
| [NewTypes {locs = vars}] ->
@@ -2695,9 +2695,8 @@ and print_if_chain ~state pexp_attributes ifs else_expr cmt_tbl =
26952695

26962696
and print_expression ~state (e : Parsetree.expression) cmt_tbl =
26972697
let print_arrow e =
2698-
let parameters, return_expr = ParsetreeViewer.fun_expr e in
2698+
let async, parameters, return_expr = ParsetreeViewer.fun_expr e in
26992699
let attrs_on_arrow = e.pexp_attributes in
2700-
let async, attrs = Ast_async.extract_async_attribute attrs_on_arrow in
27012700
let return_expr, typ_constraint =
27022701
match return_expr.pexp_desc with
27032702
| Pexp_constraint (expr, typ) ->
@@ -2760,7 +2759,7 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
27602759
Doc.concat [Doc.text ": "; typ_doc]
27612760
| _ -> Doc.nil
27622761
in
2763-
let attrs = print_attributes ~state attrs cmt_tbl in
2762+
let attrs = print_attributes ~state attrs_on_arrow cmt_tbl in
27642763
Doc.group
27652764
(Doc.concat
27662765
[
@@ -3437,9 +3436,8 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
34373436
| _ -> expr_with_await
34383437

34393438
and print_pexp_fun ~state ~in_callback e cmt_tbl =
3440-
let parameters, return_expr = ParsetreeViewer.fun_expr e in
3439+
let async, parameters, return_expr = ParsetreeViewer.fun_expr e in
34413440
let attrs_on_arrow = e.pexp_attributes in
3442-
let async, attrs = Ast_async.extract_async_attribute attrs_on_arrow in
34433441
let return_expr, typ_constraint =
34443442
match return_expr.pexp_desc with
34453443
| Pexp_constraint (expr, typ) ->
@@ -3505,7 +3503,7 @@ and print_pexp_fun ~state ~in_callback e cmt_tbl =
35053503
in
35063504
Doc.concat
35073505
[
3508-
print_attributes ~state attrs cmt_tbl;
3506+
print_attributes ~state attrs_on_arrow cmt_tbl;
35093507
parameters_doc;
35103508
typ_constraint_doc;
35113509
Doc.text " =>";

0 commit comments

Comments
 (0)
Please sign in to comment.