Skip to content

Commit 4e78a3d

Browse files
committed
Add support for toplevel await.
Fixes #5925
1 parent b77be05 commit 4e78a3d

File tree

10 files changed

+87
-5
lines changed

10 files changed

+87
-5
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
#### :bug: Bug Fix
1616
- Fix implementation of directives https://github.com/rescript-lang/rescript-compiler/pull/6052
1717

18+
#### :rocket: New Feature
19+
- Add support for toplevel `await` https://github.com/rescript-lang/rescript-compiler/pull/6054
20+
1821
# 10.1.3
1922

2023
#### :rocket: New Feature
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/await.res:4:9-17
4+
5+
2 │ let foo = async () => {
6+
3 │ let _ = ()
7+
4 │ () => await a()
8+
5 │ }
9+
6 │
10+
11+
Await on expression not in an async context
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
let a = async () => 3
2+
let foo = async () => {
3+
let _ = ()
4+
() => await a()
5+
}

jscomp/frontend/bs_builtin_ppx.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ let rec structure_mapper (self : mapper) (stru : Ast_structure.t) =
551551
let mapper : mapper =
552552
{
553553
default_mapper with
554-
expr = expr_mapper ~async_context:(ref false) ~in_function_def:(ref false);
554+
expr = expr_mapper ~async_context:(ref true) ~in_function_def:(ref false);
555555
pat = pat_mapper;
556556
typ = typ_mapper;
557557
class_type = class_type_mapper;

jscomp/test/async_await.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
3+
var Curry = require("../../lib/js/curry.js");
4+
var Caml_array = require("../../lib/js/caml_array.js");
5+
6+
function next(n) {
7+
return n + 1 | 0;
8+
}
9+
10+
async function useNext(param) {
11+
return 4;
12+
}
13+
14+
function Make(I) {
15+
var get = async function (key) {
16+
return await Curry._1(I.get, key);
17+
};
18+
return {
19+
get: get
20+
};
21+
}
22+
23+
async function topFoo(param) {
24+
return 1;
25+
}
26+
27+
var arr = [
28+
1,
29+
2,
30+
3
31+
];
32+
33+
var toplevelAwait = await topFoo(undefined);
34+
35+
var toplevelAwait2 = Caml_array.get(arr, await topFoo(undefined));
36+
37+
exports.next = next;
38+
exports.useNext = useNext;
39+
exports.Make = Make;
40+
exports.topFoo = topFoo;
41+
exports.arr = arr;
42+
exports.toplevelAwait = toplevelAwait;
43+
exports.toplevelAwait2 = toplevelAwait2;
44+
/* toplevelAwait Not a pure module */

jscomp/test/async_await.res

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
@@uncurried
2+
3+
let next = n => n + 1
4+
let useNext = async () => next(3)
5+
6+
module type Impl = {
7+
let get: string => Js.Promise.t<string>
8+
}
9+
10+
module Make = (I: Impl) => {
11+
let get = async (key) => await I.get(key)
12+
}
13+
14+
let topFoo = async () => 1
15+
let arr = [1, 2, 3]
16+
17+
let toplevelAwait = await topFoo()
18+
let toplevelAwait2 = arr[await topFoo()]

jscomp/test/build.ninja

+2-1
Large diffs are not rendered by default.

lib/4.06.1/unstable/js_compiler.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -273452,7 +273452,7 @@ let rec structure_mapper (self : mapper) (stru : Ast_structure.t) =
273452273452
let mapper : mapper =
273453273453
{
273454273454
default_mapper with
273455-
expr = expr_mapper ~async_context:(ref false) ~in_function_def:(ref false);
273455+
expr = expr_mapper ~async_context:(ref true) ~in_function_def:(ref false);
273456273456
pat = pat_mapper;
273457273457
typ = typ_mapper;
273458273458
class_type = class_type_mapper;

lib/4.06.1/unstable/js_playground_compiler.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -273452,7 +273452,7 @@ let rec structure_mapper (self : mapper) (stru : Ast_structure.t) =
273452273452
let mapper : mapper =
273453273453
{
273454273454
default_mapper with
273455-
expr = expr_mapper ~async_context:(ref false) ~in_function_def:(ref false);
273455+
expr = expr_mapper ~async_context:(ref true) ~in_function_def:(ref false);
273456273456
pat = pat_mapper;
273457273457
typ = typ_mapper;
273458273458
class_type = class_type_mapper;

lib/4.06.1/whole_compiler.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -283849,7 +283849,7 @@ let rec structure_mapper (self : mapper) (stru : Ast_structure.t) =
283849283849
let mapper : mapper =
283850283850
{
283851283851
default_mapper with
283852-
expr = expr_mapper ~async_context:(ref false) ~in_function_def:(ref false);
283852+
expr = expr_mapper ~async_context:(ref true) ~in_function_def:(ref false);
283853283853
pat = pat_mapper;
283854283854
typ = typ_mapper;
283855283855
class_type = class_type_mapper;

0 commit comments

Comments
 (0)