Skip to content

Commit 7a24ba9

Browse files
committed
Fix async context checking for module await.
The check was not firing for `module M: ModTyp` as it was looking for attributes in the wrong place.
1 parent aea2fed commit 7a24ba9

File tree

7 files changed

+41
-29
lines changed

7 files changed

+41
-29
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
- Allow single newline in JSX. https://github.com/rescript-lang/rescript/pull/7269
1818

19+
#### :bug: Bug fix
20+
21+
- Fix async context checking for module await. https://github.com/rescript-lang/rescript/pull/7271
22+
1923
# 12.0.0-alpha.8
2024

2125
#### :bug: Bug fix

compiler/frontend/ast_attributes.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ let is_inline : attr -> bool = fun ({txt}, _) -> txt = "inline"
142142

143143
let has_inline_payload (attrs : t) = Ext_list.find_first attrs is_inline
144144

145-
let has_await_payload (attrs : t) = Ext_list.find_first attrs Ast_await.is_await
145+
let has_await_payload (attrs : t) = Ext_list.exists attrs Ast_await.is_await
146146

147147
type derive_attr = {bs_deriving: Ast_payload.action list option} [@@unboxed]
148148

compiler/frontend/ast_attributes.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ val process_attributes_rev : t -> attr_kind * t
3535

3636
val has_inline_payload : t -> attr option
3737

38-
val has_await_payload : t -> attr option
38+
val has_await_payload : t -> bool
3939

4040
type derive_attr = {bs_deriving: Ast_payload.action list option} [@@unboxed]
4141

compiler/frontend/bs_builtin_ppx.ml

+19-21
Original file line numberDiff line numberDiff line change
@@ -217,29 +217,27 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
217217
let async_saved = !async_context in
218218
let result = expr_mapper ~async_context ~in_function_def self e in
219219
async_context := async_saved;
220-
let is_module, has_await =
221-
match e.pexp_desc with
222-
| Pexp_letmodule (_, {pmod_desc = Pmod_ident _; pmod_attributes}, _)
223-
| Pexp_letmodule
224-
( _,
225-
{
226-
pmod_desc =
227-
Pmod_constraint
228-
({pmod_desc = Pmod_ident _}, {pmty_desc = Pmty_ident _});
229-
pmod_attributes;
230-
},
231-
_ ) ->
232-
(true, Ast_attributes.has_await_payload pmod_attributes)
233-
| _ -> (false, Ast_attributes.has_await_payload e.pexp_attributes)
234-
in
235-
match has_await with
236-
| None -> result
237-
| Some _ ->
220+
let check_await () =
238221
if !async_context = false then
239222
Location.raise_errorf ~loc:e.pexp_loc
240-
"Await on expression not in an async context";
241-
if is_module = false then Ast_await.create_await_expression result
242-
else result
223+
"Await on expression not in an async context"
224+
in
225+
match e.pexp_desc with
226+
| Pexp_letmodule (_, {pmod_desc = Pmod_ident _; pmod_attributes}, _)
227+
| Pexp_letmodule
228+
( _,
229+
{
230+
pmod_desc =
231+
Pmod_constraint ({pmod_desc = Pmod_ident _; pmod_attributes}, _);
232+
},
233+
_ )
234+
when Ast_attributes.has_await_payload pmod_attributes ->
235+
check_await ();
236+
result
237+
| _ when Ast_attributes.has_await_payload e.pexp_attributes ->
238+
check_await ();
239+
Ast_await.create_await_expression result
240+
| _ -> result
243241

244242
let typ_mapper (self : mapper) (typ : Parsetree.core_type) =
245243
Ast_core_type_class_type.typ_mapper self typ

compiler/syntax/src/res_core.ml

+1-6
Original file line numberDiff line numberDiff line change
@@ -5873,12 +5873,7 @@ and parse_module_expr p =
58735873
| _ -> (false, mk_loc start_pos start_pos)
58745874
in
58755875
let attrs = parse_attributes p in
5876-
let attrs =
5877-
if has_await then
5878-
(({txt = "res.await"; loc = loc_await}, PStr []) : Parsetree.attribute)
5879-
:: attrs
5880-
else attrs
5881-
in
5876+
let attrs = if has_await then make_await_attr loc_await :: attrs else attrs in
58825877
let mod_expr =
58835878
if is_es6_arrow_functor p then parse_functor_module_expr p
58845879
else parse_primary_mod_expr p
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/ModuleAwait.res:2:3-3:11
4+
5+
1 │ let f0 = () => {
6+
2 │ module O: module type of Belt.Option = await Belt.Option
7+
3 │  O.forEach
8+
4 │ }
9+
5 │
10+
11+
Await on expression not in an async context
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
let f0 = () => {
2+
module O: module type of Belt.Option = await Belt.Option
3+
O.forEach
4+
}

0 commit comments

Comments
 (0)