Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for toplevel await. #6054

Merged
merged 1 commit into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
#### :bug: Bug Fix
- Fix implementation of directives https://github.com/rescript-lang/rescript-compiler/pull/6052

#### :rocket: New Feature
- Add support for toplevel `await` https://github.com/rescript-lang/rescript-compiler/pull/6054

# 10.1.3

#### :rocket: New Feature
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