Skip to content

Commit 712dec7

Browse files
authored
await AST node for expressions (#7368)
* wip: await AST * Restore ast ppx test. * cleanup * Clean up `cli.bsc.js` invocation and mentions. * final cleanup
1 parent 41e41b9 commit 712dec7

30 files changed

+222
-86
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
- Remove `Stdlib_Char` module for now. https://github.com/rescript-lang/rescript/pull/7367
2626
- Convert internal JavaScript codebase into ESM, ReScript package itself is now ESM (`"type": "module"`). https://github.com/rescript-lang/rescript/pull/6899
2727
- Add built-in support for the JavaScript `in` operator. https://github.com/rescript-lang/rescript/pull/7342
28+
- AST cleanup: add `Pexp_await` ast node instead of `res.await` attribute. (The attribute is still used for await on modules currently). https://github.com/rescript-lang/rescript/pull/7368
2829

2930
#### :nail_care: Polish
3031

CONTRIBUTING.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,19 @@ After adding a new file to the repository that should go into the npm package -
123123

124124
```sh
125125
make lib # Build compiler and standard library
126-
./cli/bsc myTestFile.res
126+
./cli/bsc.js myTestFile.res
127127
```
128128

129129
To view the untyped tree of the file run:
130130

131131
```sh
132-
./cli/bsc -dparsetree myTestFile.res
132+
./cli/bsc.js -dparsetree myTestFile.res
133133
```
134134

135135
To view the typed tree of the file run:
136136

137137
```sh
138-
./cli/bsc -dtypedtree myTestFile.res
138+
./cli/bsc.js -dtypedtree myTestFile.res
139139
```
140140

141141
### Project

analysis/src/CompletionFrontEnd.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -312,11 +312,12 @@ let rec exprToContextPathInner ~(inJsxContext : bool) (e : Parsetree.expression)
312312
if List.length exprs = List.length exprsAsContextPaths then
313313
Some (CTuple exprsAsContextPaths)
314314
else None
315+
| Pexp_await e -> exprToContextPathInner ~inJsxContext e
315316
| _ -> None
316317

317318
and exprToContextPath ~(inJsxContext : bool) (e : Parsetree.expression) =
318319
match
319-
( Res_parsetree_viewer.has_await_attribute e.pexp_attributes,
320+
( Res_parsetree_viewer.expr_is_await e,
320321
exprToContextPathInner ~inJsxContext e )
321322
with
322323
| true, Some ctxPath -> Some (CPAwait ctxPath)

analysis/src/Utils.ml

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ let identifyPexp pexp =
111111
| Pexp_pack _ -> "Pexp_pack"
112112
| Pexp_extension _ -> "Pexp_extension"
113113
| Pexp_open _ -> "Pexp_open"
114+
| Pexp_await _ -> "Pexp_await"
114115

115116
let identifyPpat pat =
116117
match pat with

compiler/frontend/bs_ast_mapper.ml

+1
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ module E = struct
366366
| Pexp_open (ovf, lid, e) ->
367367
open_ ~loc ~attrs ovf (map_loc sub lid) (sub.expr sub e)
368368
| Pexp_extension x -> extension ~loc ~attrs (sub.extension sub x)
369+
| Pexp_await e -> await ~loc ~attrs (sub.expr sub e)
369370
end
370371

371372
module P = struct

compiler/frontend/bs_builtin_ppx.ml

+6-4
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,12 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
242242
|| Ast_attributes.has_await_payload attrs2 ->
243243
check_await ();
244244
result
245-
| _ when Ast_attributes.has_await_payload e.pexp_attributes ->
246-
check_await ();
247-
Ast_await.create_await_expression result
248-
| _ -> result
245+
| _ -> (
246+
match result.pexp_desc with
247+
| Pexp_await e ->
248+
check_await ();
249+
Ast_await.create_await_expression e
250+
| _ -> result)
249251

250252
let typ_mapper (self : mapper) (typ : Parsetree.core_type) =
251253
Ast_core_type_class_type.typ_mapper self typ

compiler/ml/ast_helper.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ module Exp = struct
180180
let pack ?loc ?attrs a = mk ?loc ?attrs (Pexp_pack a)
181181
let open_ ?loc ?attrs a b c = mk ?loc ?attrs (Pexp_open (a, b, c))
182182
let extension ?loc ?attrs a = mk ?loc ?attrs (Pexp_extension a)
183-
183+
let await ?loc ?attrs a = mk ?loc ?attrs (Pexp_await a)
184184
let case lhs ?guard rhs = {pc_lhs = lhs; pc_guard = guard; pc_rhs = rhs}
185185
end
186186

compiler/ml/ast_helper.mli

+1
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ module Exp : sig
210210
val extension : ?loc:loc -> ?attrs:attrs -> extension -> expression
211211

212212
val case : pattern -> ?guard:expression -> expression -> case
213+
val await : ?loc:loc -> ?attrs:attrs -> expression -> expression
213214
end
214215

215216
(** Value declarations *)

compiler/ml/ast_iterator.ml

+1
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ module E = struct
344344
iter_loc sub lid;
345345
sub.expr sub e
346346
| Pexp_extension x -> sub.extension sub x
347+
| Pexp_await e -> sub.expr sub e
347348
end
348349

349350
module P = struct

compiler/ml/ast_mapper.ml

+1
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ module E = struct
329329
| Pexp_open (ovf, lid, e) ->
330330
open_ ~loc ~attrs ovf (map_loc sub lid) (sub.expr sub e)
331331
| Pexp_extension x -> extension ~loc ~attrs (sub.extension sub x)
332+
| Pexp_await e -> await ~loc ~attrs (sub.expr sub e)
332333
end
333334

334335
module P = struct

compiler/ml/ast_mapper_from0.ml

+20-1
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,30 @@ end
295295
module E = struct
296296
(* Value expressions for the core language *)
297297

298-
let map sub {pexp_loc = loc; pexp_desc = desc; pexp_attributes = attrs} =
298+
let has_await_attribute attrs =
299+
List.exists
300+
(function
301+
| {Location.txt = "res.await"}, _ -> true
302+
| _ -> false)
303+
attrs
304+
305+
let remove_await_attribute attrs =
306+
List.filter
307+
(function
308+
| {Location.txt = "res.await"}, _ -> false
309+
| _ -> true)
310+
attrs
311+
312+
let map sub ({pexp_loc = loc; pexp_desc = desc; pexp_attributes = attrs} as e)
313+
=
299314
let open Exp in
300315
let loc = sub.location sub loc in
301316
let attrs = sub.attributes sub attrs in
302317
match desc with
318+
| _ when has_await_attribute attrs ->
319+
let attrs = remove_await_attribute e.pexp_attributes in
320+
let e = sub.expr sub {e with pexp_attributes = attrs} in
321+
await ~loc e
303322
| Pexp_ident x -> ident ~loc ~attrs (map_loc sub x)
304323
| Pexp_constant x -> constant ~loc ~attrs (map_constant x)
305324
| Pexp_let (r, vbs, e) ->

compiler/ml/ast_mapper_to0.ml

+7
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,13 @@ module E = struct
407407
| Pexp_open (ovf, lid, e) ->
408408
open_ ~loc ~attrs ovf (map_loc sub lid) (sub.expr sub e)
409409
| Pexp_extension x -> extension ~loc ~attrs (sub.extension sub x)
410+
| Pexp_await e ->
411+
let e = sub.expr sub e in
412+
{
413+
e with
414+
pexp_attributes =
415+
(Location.mknoloc "res.await", Pt.PStr []) :: e.pexp_attributes;
416+
}
410417
end
411418

412419
module P = struct

compiler/ml/ast_uncurried.ml

-5
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,3 @@ let expr_is_uncurried_fun (expr : Parsetree.expression) =
1919
match expr.pexp_desc with
2020
| Pexp_fun {arity = Some _} -> true
2121
| _ -> false
22-
23-
let expr_extract_uncurried_fun (expr : Parsetree.expression) =
24-
match expr.pexp_desc with
25-
| Pexp_fun {arity = Some _} -> expr
26-
| _ -> assert false

compiler/ml/depend.ml

+1
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ let rec add_expr bv exp =
289289
| Pstr_eval ({pexp_desc = Pexp_construct (c, None)}, _) -> add bv c
290290
| _ -> handle_extension e)
291291
| Pexp_extension e -> handle_extension e
292+
| Pexp_await e -> add_expr bv e
292293

293294
and add_cases bv cases = List.iter (add_case bv) cases
294295

compiler/ml/parsetree.ml

+3-2
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,9 @@ and expression_desc =
313313
let open M in E
314314
let! open M in E *)
315315
| Pexp_extension of extension
316-
(* [%id] *)
317-
(* . *)
316+
(* [%id] *)
317+
(* . *)
318+
| Pexp_await of expression
318319

319320
and case = {
320321
(* (P -> E) or (P when E0 -> E) *)

compiler/ml/pprintast.ml

+1
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,7 @@ and expression ctxt f x =
722722
(expression ctxt) e
723723
| Pexp_variant (l, Some eo) -> pp f "@[<2>`%s@;%a@]" l (simple_expr ctxt) eo
724724
| Pexp_extension e -> extension ctxt f e
725+
| Pexp_await e -> pp f "@[<hov2>await@ %a@]" (simple_expr ctxt) e
725726
| _ -> expression1 ctxt f x
726727

727728
and expression1 ctxt f x =

compiler/ml/printast.ml

+3
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,9 @@ and expression i ppf x =
345345
| Pexp_extension (s, arg) ->
346346
line i ppf "Pexp_extension \"%s\"\n" s.txt;
347347
payload i ppf arg
348+
| Pexp_await e ->
349+
line i ppf "Pexp_await\n";
350+
expression i ppf e
348351

349352
and value_description i ppf x =
350353
line i ppf "value_description %a %a\n" fmt_string_loc x.pval_name fmt_location

compiler/ml/typecore.ml

+2
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ let iter_expression f e =
178178
expr e;
179179
module_expr me
180180
| Pexp_pack me -> module_expr me
181+
| Pexp_await _ -> assert false (* should be handled earlier *)
181182
and case {pc_lhs = _; pc_guard; pc_rhs} =
182183
may expr pc_guard;
183184
expr pc_rhs
@@ -3195,6 +3196,7 @@ and type_expect_ ?type_clash_context ?in_function ?(recarg = Rejected) env sexp
31953196
| _ -> raise (Error (loc, env, Invalid_extension_constructor_payload)))
31963197
| Pexp_extension ext ->
31973198
raise (Error_forward (Builtin_attributes.error_of_extension ext))
3199+
| Pexp_await _ -> (* should be handled earlier *) assert false
31983200
31993201
and type_function ?in_function ~arity ~async loc attrs env ty_expected_ l
32003202
caselist =

compiler/syntax/src/res_ast_debugger.ml

+1
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,7 @@ module SexpAst = struct
707707
]
708708
| Pexp_extension ext ->
709709
Sexp.list [Sexp.atom "Pexp_extension"; extension ext]
710+
| Pexp_await e -> Sexp.list [Sexp.atom "Pexp_await"; expression e]
710711
in
711712
Sexp.list [Sexp.atom "expression"; desc]
712713

compiler/syntax/src/res_core.ml

+3-6
Original file line numberDiff line numberDiff line change
@@ -3307,15 +3307,12 @@ and parse_async_arrow_expression ?(arrow_attrs = []) p =
33073307

33083308
and parse_await_expression p =
33093309
let await_loc = mk_loc p.Parser.start_pos p.end_pos in
3310-
let await_attr = make_await_attr await_loc in
33113310
Parser.expect Await p;
33123311
let token_prec = Token.precedence MinusGreater in
33133312
let expr = parse_binary_expr ~context:OrdinaryExpr p token_prec in
3314-
{
3315-
expr with
3316-
pexp_attributes = await_attr :: expr.pexp_attributes;
3317-
pexp_loc = {expr.pexp_loc with loc_start = await_loc.loc_start};
3318-
}
3313+
Ast_helper.Exp.await
3314+
~loc:{expr.pexp_loc with loc_start = await_loc.loc_start}
3315+
~attrs:[] expr
33193316

33203317
and parse_try_expression p =
33213318
let start_pos = p.Parser.start_pos in

compiler/syntax/src/res_parens.ml

+7-15
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ let call_expr expr =
5656
} ->
5757
Parenthesized
5858
| _ when Ast_uncurried.expr_is_uncurried_fun expr -> Parenthesized
59-
| _ when ParsetreeViewer.has_await_attribute expr.pexp_attributes ->
60-
Parenthesized
59+
| _ when ParsetreeViewer.expr_is_await expr -> Parenthesized
6160
| _ -> Nothing)
6261

6362
let structure_expr expr =
@@ -109,8 +108,7 @@ let unary_expr_operand expr =
109108
| Pexp_try _ | Pexp_while _ | Pexp_for _ | Pexp_ifthenelse _ );
110109
} ->
111110
Parenthesized
112-
| _ when ParsetreeViewer.has_await_attribute expr.pexp_attributes ->
113-
Parenthesized
111+
| _ when ParsetreeViewer.expr_is_await expr -> Parenthesized
114112
| _ -> Nothing)
115113

116114
let binary_expr_operand ~is_lhs expr =
@@ -133,8 +131,7 @@ let binary_expr_operand ~is_lhs expr =
133131
| expr when ParsetreeViewer.is_binary_expression expr -> Parenthesized
134132
| expr when ParsetreeViewer.is_ternary_expr expr -> Parenthesized
135133
| {pexp_desc = Pexp_lazy _ | Pexp_assert _} when is_lhs -> Parenthesized
136-
| _ when ParsetreeViewer.has_await_attribute expr.pexp_attributes ->
137-
Parenthesized
134+
| _ when ParsetreeViewer.expr_is_await expr -> Parenthesized
138135
| {Parsetree.pexp_attributes = attrs} ->
139136
if ParsetreeViewer.has_printable_attributes attrs then Parenthesized
140137
else Nothing)
@@ -229,9 +226,7 @@ let lazy_or_assert_or_await_expr_rhs ?(in_await = false) expr =
229226
| Pexp_while _ | Pexp_for _ | Pexp_ifthenelse _ );
230227
} ->
231228
Parenthesized
232-
| _
233-
when (not in_await)
234-
&& ParsetreeViewer.has_await_attribute expr.pexp_attributes ->
229+
| _ when (not in_await) && ParsetreeViewer.expr_is_await expr ->
235230
Parenthesized
236231
| _ -> Nothing)
237232

@@ -277,8 +272,7 @@ let field_expr expr =
277272
| Pexp_try _ | Pexp_while _ | Pexp_for _ | Pexp_ifthenelse _ );
278273
} ->
279274
Parenthesized
280-
| _ when ParsetreeViewer.has_await_attribute expr.pexp_attributes ->
281-
Parenthesized
275+
| _ when ParsetreeViewer.expr_is_await expr -> Parenthesized
282276
| _ -> Nothing)
283277

284278
let set_field_expr_rhs expr =
@@ -339,8 +333,7 @@ let jsx_prop_expr expr =
339333
}
340334
when starts_with_minus x ->
341335
Parenthesized
342-
| _ when ParsetreeViewer.has_await_attribute expr.pexp_attributes ->
343-
Parenthesized
336+
| _ when ParsetreeViewer.expr_is_await expr -> Parenthesized
344337
| {
345338
Parsetree.pexp_desc =
346339
( Pexp_ident _ | Pexp_constant _ | Pexp_field _ | Pexp_construct _
@@ -377,8 +370,7 @@ let jsx_child_expr expr =
377370
}
378371
when starts_with_minus x ->
379372
Parenthesized
380-
| _ when ParsetreeViewer.has_await_attribute expr.pexp_attributes ->
381-
Parenthesized
373+
| _ when ParsetreeViewer.expr_is_await expr -> Parenthesized
382374
| {
383375
Parsetree.pexp_desc =
384376
( Pexp_ident _ | Pexp_constant _ | Pexp_field _ | Pexp_construct _

compiler/syntax/src/res_parsetree_viewer.ml

+6-6
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ let has_await_attribute attrs =
7272
| _ -> false)
7373
attrs
7474

75+
let expr_is_await e =
76+
match e.pexp_desc with
77+
| Pexp_await _ -> true
78+
| _ -> false
79+
7580
let has_inline_record_definition_attribute attrs =
7681
List.exists
7782
(function
@@ -111,12 +116,7 @@ let collect_list_expressions expr =
111116

112117
(* (__x) => f(a, __x, c) -----> f(a, _, c) *)
113118
let rewrite_underscore_apply expr =
114-
let expr_fun =
115-
if Ast_uncurried.expr_is_uncurried_fun expr then
116-
Ast_uncurried.expr_extract_uncurried_fun expr
117-
else expr
118-
in
119-
match expr_fun.pexp_desc with
119+
match expr.pexp_desc with
120120
| Pexp_fun
121121
{
122122
arg_label = Nolabel;

compiler/syntax/src/res_parsetree_viewer.mli

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ val functor_type :
1414
list
1515
* Parsetree.module_type
1616

17+
val expr_is_await : Parsetree.expression -> bool
1718
val has_await_attribute : Parsetree.attributes -> bool
1819
val has_inline_record_definition_attribute : Parsetree.attributes -> bool
1920
val has_res_pat_variant_spread_attribute : Parsetree.attributes -> bool

0 commit comments

Comments
 (0)