Skip to content

Commit

Permalink
Add support for toplevel await.
Browse files Browse the repository at this point in the history
Fixes #5925
  • Loading branch information
cristianoc committed Mar 9, 2023
1 parent b77be05 commit 2dfbd00
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#### :bug: Bug Fix
- Fix implementation of directives https://github.com/rescript-lang/rescript-compiler/pull/6052
- Add support for toplevel `await` https://github.com/rescript-lang/rescript-compiler/pull/6054

# 10.1.3

Expand Down
11 changes: 11 additions & 0 deletions jscomp/build_tests/super_errors/expected/await.res.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

We've found a bug for you!
/.../fixtures/await.res:4:9-17

2 │ let foo = async () => {
3 │ let _ = ()
4 │ () => await a()
5 │ }
6 │

Await on expression not in an async context
5 changes: 5 additions & 0 deletions jscomp/build_tests/super_errors/fixtures/await.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let a = async () => 3
let foo = async () => {
let _ = ()
() => await a()
}
2 changes: 1 addition & 1 deletion jscomp/frontend/bs_builtin_ppx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ let rec structure_mapper (self : mapper) (stru : Ast_structure.t) =
let mapper : mapper =
{
default_mapper with
expr = expr_mapper ~async_context:(ref false) ~in_function_def:(ref false);
expr = expr_mapper ~async_context:(ref true) ~in_function_def:(ref false);
pat = pat_mapper;
typ = typ_mapper;
class_type = class_type_mapper;
Expand Down
44 changes: 44 additions & 0 deletions jscomp/test/async_await.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';

var Curry = require("../../lib/js/curry.js");
var Caml_array = require("../../lib/js/caml_array.js");

function next(n) {
return n + 1 | 0;
}

async function useNext(param) {
return 4;
}

function Make(I) {
var get = async function (key) {
return await Curry._1(I.get, key);
};
return {
get: get
};
}

async function topFoo(param) {
return 1;
}

var arr = [
1,
2,
3
];

var toplevelAwait = await topFoo(undefined);

var toplevelAwait2 = Caml_array.get(arr, await topFoo(undefined));

exports.next = next;
exports.useNext = useNext;
exports.Make = Make;
exports.topFoo = topFoo;
exports.arr = arr;
exports.toplevelAwait = toplevelAwait;
exports.toplevelAwait2 = toplevelAwait2;
/* toplevelAwait Not a pure module */
18 changes: 18 additions & 0 deletions jscomp/test/async_await.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@@uncurried

let next = n => n + 1
let useNext = async () => next(3)

module type Impl = {
let get: string => Js.Promise.t<string>
}

module Make = (I: Impl) => {
let get = async (key) => await I.get(key)
}

let topFoo = async () => 1
let arr = [1, 2, 3]

let toplevelAwait = await topFoo()
let toplevelAwait2 = arr[await topFoo()]
3 changes: 2 additions & 1 deletion jscomp/test/build.ninja

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/4.06.1/unstable/js_compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -273452,7 +273452,7 @@ let rec structure_mapper (self : mapper) (stru : Ast_structure.t) =
let mapper : mapper =
{
default_mapper with
expr = expr_mapper ~async_context:(ref false) ~in_function_def:(ref false);
expr = expr_mapper ~async_context:(ref true) ~in_function_def:(ref false);
pat = pat_mapper;
typ = typ_mapper;
class_type = class_type_mapper;
Expand Down
2 changes: 1 addition & 1 deletion lib/4.06.1/unstable/js_playground_compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -273452,7 +273452,7 @@ let rec structure_mapper (self : mapper) (stru : Ast_structure.t) =
let mapper : mapper =
{
default_mapper with
expr = expr_mapper ~async_context:(ref false) ~in_function_def:(ref false);
expr = expr_mapper ~async_context:(ref true) ~in_function_def:(ref false);
pat = pat_mapper;
typ = typ_mapper;
class_type = class_type_mapper;
Expand Down
2 changes: 1 addition & 1 deletion lib/4.06.1/whole_compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -283849,7 +283849,7 @@ let rec structure_mapper (self : mapper) (stru : Ast_structure.t) =
let mapper : mapper =
{
default_mapper with
expr = expr_mapper ~async_context:(ref false) ~in_function_def:(ref false);
expr = expr_mapper ~async_context:(ref true) ~in_function_def:(ref false);
pat = pat_mapper;
typ = typ_mapper;
class_type = class_type_mapper;
Expand Down

0 comments on commit 2dfbd00

Please sign in to comment.