Skip to content

Commit 0a92245

Browse files
cknittaspeddro
authored andcommitted
Parse a switch guard that calls a function named async (rescript-lang#8935)
Signed-off-by: Pedro Castro <aspeddro@gmail.com>
1 parent d39e6c8 commit 0a92245

4 files changed

Lines changed: 20 additions & 5 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Parse a switch guard that calls a function named `async`.

‎compiler/syntax/src/res_core.ml‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2321,11 +2321,14 @@ and parse_operand_expr ~context p =
23212321
(* we need to be careful when we're in a ternary true branch:
23222322
`condition ? ternary-true-branch : false-branch`
23232323
Arrow expressions could be of the form: `async (): int => stuff()`
2324-
But if we're in a ternary, the `:` of the ternary takes precedence
2324+
But if we're in a ternary, the `:` of the ternary takes precedence.
2325+
Like other arrows, none starts in a guard or a condition, where
2326+
`async(x) =>` is a call followed by the case's `=>`.
23252327
*)
2326-
when is_es6_arrow_expression
2327-
~in_ternary:(context = TernaryTrueBranchExpr)
2328-
p ->
2328+
when context != WhenExpr
2329+
&& is_es6_arrow_expression
2330+
~in_ternary:(context = TernaryTrueBranchExpr)
2331+
p ->
23292332
let arrow_attrs = !attrs in
23302333
let () = attrs := [] in
23312334
parse_async_arrow_expression ~arrow_attrs p

‎tests/syntax_tests/data/parsing/grammar/expressions/async.res‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,10 @@ let attr2 = @a async (type a) => (type b c, x) => 3
4242
let attr3 = @a (type a) => async (type b c, x) => 3
4343
let attr4 = @a (type a) => @b async (type b c, x) => 3
4444
let attr5 : int = @a @b async (type a, type b c) => (x:a) => x
45+
46+
// A guard calling a function named async is a call, not an async arrow
47+
let guarded = v =>
48+
switch v {
49+
| Some(x) if async(x) => async () => x
50+
| _ => async () => 0
51+
}

‎tests/syntax_tests/data/parsing/grammar/expressions/expected/async.res.txt‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,8 @@ let attr4 =
3636
let (attr5 : int) =
3737
((async fun (type a) (type b) (type c) [arity:1]() ->
3838
fun [arity:1](x : a) -> x)
39-
[@a ][@b ])
39+
[@a ][@b ])
40+
let guarded [arity:1]v =
41+
match v with
42+
| Some x when async x -> (async fun [arity:1]() -> x)
43+
| _ -> (async fun [arity:1]() -> 0)

0 commit comments

Comments
 (0)