Skip to content

Commit 870db32

Browse files
committed
Fix issue with async and locally abstract types
To fix the printer, the async internal representation via an attribute needs to be preserved on functions starting with a locally abstract type. For this reason, annotations cannot be placed individually on the first locally abstract type, if that's how the function starts. Fixes #5974
1 parent 1e0e325 commit 870db32

File tree

6 files changed

+21
-13
lines changed

6 files changed

+21
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ These are only breaking changes for unformatted code.
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
5858
- Fix issue with nested async functions, where the inner function would be emitted without `async` https://github.com/rescript-lang/rescript-compiler/pull/5983
59+
- Fix issue with async context check, and printer, for async functions with locally abstract type https://github.com/rescript-lang/rescript-compiler/pull/5982
5960

6061
#### :nail_care: Polish
6162

jscomp/frontend/bs_builtin_ppx.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
127127
(* Treat @this (. x, y, z) => ... just like @this (x, y, z) => ... *)
128128
let fun_expr = Ast_uncurried.exprExtractUncurriedFun e in
129129
self.expr self fun_expr
130+
| Pexp_newtype (s, body) ->
131+
let async = Ast_attributes.has_async_payload e.pexp_attributes <> None in
132+
let body = Ast_async.add_async_attribute ~async body in
133+
let res = self.expr self body in
134+
{e with pexp_desc = Pexp_newtype(s, res)}
130135
| Pexp_fun (label, _, pat, body) -> (
131136
let async = Ast_attributes.has_async_payload e.pexp_attributes <> None in
132137
match Ast_attributes.process_attributes_rev e.pexp_attributes with

res_syntax/src/res_parsetree_viewer.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ let funExpr expr =
182182
| expr -> (uncurried, attrsBefore, List.rev acc, expr)
183183
in
184184
match expr with
185-
| {pexp_desc = Pexp_fun _} ->
185+
| {pexp_desc = Pexp_fun _ | Pexp_newtype _} ->
186186
collect ~uncurried:false ~nFun:0 expr.pexp_attributes []
187187
{expr with pexp_attributes = []}
188188
| _ when Ast_uncurried.exprIsUncurriedFun expr ->

res_syntax/tests/printer/expr/asyncAwait.res

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,7 @@ let c3 = (. x) => @foo y => x+y
120120

121121
type t1 = (. int) => string => bool
122122
type t2 = (. int, string) => bool
123+
124+
let f = async (type a, ()) => {
125+
await Js.Promise.resolve(())
126+
}

res_syntax/tests/printer/expr/expected/asyncAwait.res.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,7 @@ let c3 = (. x) => {@foo y => x + y}
142142

143143
type t1 = (. int) => string => bool
144144
type t2 = (. int, string) => bool
145+
146+
let f = async (type a, ()) => {
147+
await Js.Promise.resolve()
148+
}

res_syntax/tests/printer/expr/expected/newtype.res.txt

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
let f = (type t, xs: list<t>) => ()
2-
let f = (@attr type t, xs: list<t>) => ()
2+
let f = @attr (type t, xs: list<t>) => ()
33
let f = (type t, xs: list<t>, type s, ys: list<s>) => ()
4-
let f = (@attr type t, xs: list<t>, @attr2 type s, ys: list<s>) => ()
4+
let f = @attr (type t, xs: list<t>, @attr2 type s, ys: list<s>) => ()
55
let f = (type t u v, xs: list<(t, u, v)>) => ()
6-
let f = (@attr type t u v, xs: list<(t, u, v)>) => ()
6+
let f = @attr (type t u v, xs: list<(t, u, v)>) => ()
77
let f = (type t u v, xs: list<(t, u, v)>, type s w z, ys: list<(s, w, z)>) => ()
8-
let f = (@attr type t u v, xs: list<(t, u, v)>, @attr2 type s w z, ys: list<(s, w, z)>) => ()
9-
let f = (
10-
@attr type t,
11-
@attr type s,
12-
xs: list<(t, s)>,
13-
@attr type u,
14-
@attr type v w,
15-
ys: list<(u, v, w)>,
16-
) => ()
8+
let f = @attr (type t u v, xs: list<(t, u, v)>, @attr2 type s w z, ys: list<(s, w, z)>) => ()
9+
let f = @attr
10+
(type t, @attr type s, xs: list<(t, s)>, @attr type u, @attr type v w, ys: list<(u, v, w)>) => ()
1711

1812
let mk_formatting_gen:
1913
type a b c d e f. formatting_gen<a, b, c, d, e, f> => Parsetree.expression =

0 commit comments

Comments
 (0)