Skip to content

Commit 1e0e325

Browse files
committed
Fix issue with nested async functions
Nested curried functions are combined into one, and the async inner function is not generated. This PR Avoids combining nested functions when they are async. Fixes #5980
1 parent 61e4db6 commit 1e0e325

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ These are only breaking changes for unformatted code.
5555
- Fix type inference issue with uncurried functions applied to a single unit argument. The issue was introduced in https://github.com/rescript-lang/rescript-compiler/pull/5907 when adding support to `foo()` when `foo` has only optional arguments. https://github.com/rescript-lang/rescript-compiler/pull/5970
5656
- Fix issue where uncurried functions were incorrectly converting the type of a prop given as a default value to curried https://github.com/rescript-lang/rescript-compiler/pull/5971
5757
- Fix issue with error messages for uncurried functions where expected and given type were swapped https://github.com/rescript-lang/rescript-compiler/pull/5973
58+
- Fix issue with nested async functions, where the inner function would be emitted without `async` https://github.com/rescript-lang/rescript-compiler/pull/5983
5859

5960
#### :nail_care: Polish
6061

jscomp/ml/translcore.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,8 @@ let rec cut n l =
681681

682682
let try_ids = Hashtbl.create 8
683683

684+
let has_async_attribute exp = exp.exp_attributes |> List.exists (fun ({txt}, _payload) -> txt = "res.async")
685+
684686
let rec transl_exp e =
685687
List.iter (Translattribute.check_attribute e) e.exp_attributes;
686688
transl_exp0 e
@@ -695,7 +697,7 @@ and transl_exp0 (e : Typedtree.expression) : Lambda.lambda =
695697
| Texp_let (rec_flag, pat_expr_list, body) ->
696698
transl_let rec_flag pat_expr_list (transl_exp body)
697699
| Texp_function { arg_label = _; param; cases; partial } ->
698-
let async = e.exp_attributes |> List.exists (fun ({txt}, _payload) -> txt = "res.async") in
700+
let async = has_async_attribute e in
699701
let params, body, return_unit =
700702
let pl = push_defaults e.exp_loc [] cases partial in
701703
transl_function e.exp_loc partial param pl
@@ -1036,7 +1038,7 @@ and transl_function loc partial param cases =
10361038
} as exp;
10371039
};
10381040
]
1039-
when Parmatch.inactive ~partial pat ->
1041+
when Parmatch.inactive ~partial pat && not (exp |> has_async_attribute) ->
10401042
let params, body, return_unit =
10411043
transl_function exp.exp_loc partial' param' cases
10421044
in

jscomp/test/async_inline.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ var tui = 3;
6060

6161
var tuia = uncurriedIdAsync(3);
6262

63+
function nested1(param) {
64+
return async function (y) {
65+
return await y;
66+
};
67+
}
68+
69+
async function nested2(param) {
70+
return async function (y) {
71+
return await y;
72+
};
73+
}
74+
6375
var tci = 3;
6476

6577
exports.willBeInlined = willBeInlined;
@@ -76,4 +88,6 @@ exports.tci = tci;
7688
exports.tcia = tcia;
7789
exports.tui = tui;
7890
exports.tuia = tuia;
91+
exports.nested1 = nested1;
92+
exports.nested2 = nested2;
7993
/* inlined Not a pure module */

jscomp/test/async_inline.res

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,8 @@ let uncurriedIdAsync = async (.x ) => x
4545
let tci = curriedId(3)
4646
let tcia = curriedIdAsync(3)
4747
let tui = uncurriedId(. 3)
48-
let tuia = uncurriedIdAsync(. 3)
48+
let tuia = uncurriedIdAsync(. 3)
49+
50+
let nested1 = () => async (y) => await y
51+
52+
let nested2 = async () => async (y) => await y

0 commit comments

Comments
 (0)