Skip to content

Commit 56e5aea

Browse files
committed
Simplify ast async: add async flag directly where it's needed.
1 parent 362426d commit 56e5aea

File tree

7 files changed

+18
-32
lines changed

7 files changed

+18
-32
lines changed

CHANGELOG.md

-5
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@
2424
- AST: always put type parameters first in function definitions. https://github.com/rescript-lang/rescript/pull/7233
2525
- AST cleanup: Remove `@res.async` attribute from the internal representation, and add a flag to untyped and typed ASTs instead. https://github.com/rescript-lang/rescript/pull/7234
2626

27-
#### :house: Internal
28-
29-
- 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
30-
31-
3227
# 12.0.0-alpha.7
3328

3429
#### :bug: Bug fix

compiler/ml/ast_async.ml

-12
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,6 @@ let rec dig_async_payload_from_function (expr : Parsetree.expression) =
44
| Pexp_newtype (_, body) -> dig_async_payload_from_function body
55
| _ -> false
66

7-
let add_async_attribute ~async (body : Parsetree.expression) =
8-
if async then
9-
let rec add_to_fun (exp : Parsetree.expression) =
10-
match exp.pexp_desc with
11-
| Pexp_newtype (txt, e) ->
12-
{exp with pexp_desc = Pexp_newtype (txt, add_to_fun e)}
13-
| Pexp_fun f -> {exp with pexp_desc = Pexp_fun {f with async}}
14-
| _ -> exp
15-
in
16-
add_to_fun body
17-
else body
18-
197
let add_promise_type ?(loc = Location.none) ~async
208
(result : Parsetree.expression) =
219
if async then

compiler/ml/ast_uncurried.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ let uncurried_type ~arity (t_arg : Parsetree.core_type) =
66
{t_arg with ptyp_desc = Ptyp_arrow (l, t1, t2, Some arity)}
77
| _ -> assert false
88

9-
let uncurried_fun ~arity fun_expr =
9+
let uncurried_fun ?(async = false) ~arity fun_expr =
1010
let fun_expr =
1111
match fun_expr.Parsetree.pexp_desc with
1212
| Pexp_fun f ->
13-
{fun_expr with pexp_desc = Pexp_fun {f with arity = Some arity}}
13+
{fun_expr with pexp_desc = Pexp_fun {f with arity = Some arity; async}}
1414
| _ -> assert false
1515
in
1616
fun_expr

compiler/ml/pprintast.ml

+5-3
Original file line numberDiff line numberDiff line change
@@ -993,17 +993,19 @@ and binding ctxt f {pvb_pat = p; pvb_expr = x; _} =
993993
if x.pexp_attributes <> [] then pp f "=@;%a" (expression ctxt) x
994994
else
995995
match x.pexp_desc with
996-
| Pexp_fun {arg_label = label; default = eo; lhs = p; rhs = e; arity} ->
996+
| Pexp_fun
997+
{arg_label = label; default = eo; lhs = p; rhs = e; arity; async} ->
997998
let arity_str =
998999
match arity with
9991000
| None -> ""
10001001
| Some arity -> "[arity:" ^ string_of_int arity ^ "]"
10011002
in
1003+
let async_str = if async then "async " else "" in
10021004
if label = Nolabel then
1003-
pp f "%s%a@ %a" arity_str (simple_pattern ctxt) p
1005+
pp f "%s%s%a@ %a" async_str arity_str (simple_pattern ctxt) p
10041006
pp_print_pexp_function e
10051007
else
1006-
pp f "%s%a@ %a" arity_str (label_exp ctxt) (label, eo, p)
1008+
pp f "%s%s%a@ %a" async_str arity_str (label_exp ctxt) (label, eo, p)
10071009
pp_print_pexp_function e
10081010
| Pexp_newtype (str, e) ->
10091011
pp f "(type@ %s)@ %a" str.txt pp_print_pexp_function e

compiler/syntax/src/jsx_v4.ml

+1-2
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,7 @@ let map_binding ~config ~empty_loc ~pstr_loc ~file_name ~rec_flag binding =
11251125
| _ -> Pat.record (List.rev patterns_with_label) Open
11261126
in
11271127
let expression =
1128-
Exp.fun_ ~arity:(Some 1) Nolabel None
1128+
Exp.fun_ ~arity:(Some 1) ~async:is_async Nolabel None
11291129
(Pat.constraint_ record_pattern
11301130
(Typ.constr ~loc:empty_loc
11311131
{txt = Lident "props"; loc = empty_loc}
@@ -1140,7 +1140,6 @@ let map_binding ~config ~empty_loc ~pstr_loc ~file_name ~rec_flag binding =
11401140
| _ -> [Typ.any ()]))))
11411141
expression
11421142
in
1143-
let expression = Ast_async.add_async_attribute ~async:is_async expression in
11441143
let expression =
11451144
(* Add new tupes (type a,b,c) to make's definition *)
11461145
newtypes

compiler/syntax/src/res_core.ml

+8-6
Original file line numberDiff line numberDiff line change
@@ -1537,7 +1537,7 @@ and parse_ternary_expr left_operand p =
15371537
| _ -> left_operand
15381538

15391539
and parse_es6_arrow_expression ?(arrow_attrs = []) ?(arrow_start_pos = None)
1540-
?context ?term_parameters p =
1540+
?context ?term_parameters ~async p =
15411541
let start_pos = p.Parser.start_pos in
15421542
Parser.leave_breadcrumb p Grammar.Es6ArrowExpr;
15431543
(* Parsing function parameters and attributes:
@@ -1609,7 +1609,9 @@ and parse_es6_arrow_expression ?(arrow_attrs = []) ?(arrow_start_pos = None)
16091609
term_parameters body
16101610
in
16111611
let arrow_expr =
1612-
Ast_uncurried.uncurried_fun ~arity:(List.length term_parameters) arrow_expr
1612+
Ast_uncurried.uncurried_fun
1613+
~arity:(List.length term_parameters)
1614+
~async arrow_expr
16131615
in
16141616
let arrow_expr =
16151617
match type_param_opt with
@@ -2159,7 +2161,7 @@ and parse_operand_expr ~context p =
21592161
then
21602162
let arrow_attrs = !attrs in
21612163
let () = attrs := [] in
2162-
parse_es6_arrow_expression ~arrow_attrs ~context p
2164+
parse_es6_arrow_expression ~async:false ~arrow_attrs ~context p
21632165
else parse_unary_expr p
21642166
in
21652167
(* let endPos = p.Parser.prevEndPos in *)
@@ -3000,7 +3002,7 @@ and parse_braced_or_record_expr p =
30003002
let loc = mk_loc start_pos ident_end_pos in
30013003
let ident = Location.mkloc (Longident.last path_ident.txt) loc in
30023004
let a =
3003-
parse_es6_arrow_expression
3005+
parse_es6_arrow_expression ~async:false
30043006
~term_parameters:
30053007
[
30063008
{
@@ -3303,8 +3305,8 @@ and parse_expr_block ?first p =
33033305
and parse_async_arrow_expression ?(arrow_attrs = []) p =
33043306
let start_pos = p.Parser.start_pos in
33053307
Parser.expect (Lident "async") p;
3306-
Ast_async.add_async_attribute ~async:true
3307-
(parse_es6_arrow_expression ~arrow_attrs ~arrow_start_pos:(Some start_pos) p)
3308+
parse_es6_arrow_expression ~async:true ~arrow_attrs
3309+
~arrow_start_pos:(Some start_pos) p
33083310

33093311
and parse_await_expression p =
33103312
let await_loc = mk_loc p.Parser.start_pos p.end_pos in

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
let greetUser [arity:1]userId =
1+
let greetUser async [arity:1]userId =
22
((let name = ((getUserName userId)[@res.await ]) in
33
({js|Hello |js} ^ name) ^ {js|!|js})
44
[@res.braces ])
@@ -22,7 +22,7 @@ let f =
2222
else (async fun [arity:2]c -> fun d -> (c - d : int)))
2323
[@res.ternary ])
2424
let foo = async ~a:((34)[@res.namedArgLoc ])
25-
let bar [arity:1]~a:((a)[@res.namedArgLoc ]) = a + 1
25+
let bar async [arity:1]~a:((a)[@res.namedArgLoc ]) = a + 1
2626
let ex1 = ((3)[@res.await ]) + ((4)[@res.await ])
2727
let ex2 = ((3)[@res.await ]) ** ((4)[@res.await ])
2828
let ex3 = ((foo |.u (bar ~arg:((arg)[@res.namedArgLoc ])))[@res.await ])

0 commit comments

Comments
 (0)