Skip to content
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
1 change: 1 addition & 0 deletions changes/async-guard-lookahead.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Parse a switch guard that calls a function named `async`.
11 changes: 7 additions & 4 deletions compiler/syntax/src/res_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2321,11 +2321,14 @@ and parse_operand_expr ~context p =
(* we need to be careful when we're in a ternary true branch:
`condition ? ternary-true-branch : false-branch`
Arrow expressions could be of the form: `async (): int => stuff()`
But if we're in a ternary, the `:` of the ternary takes precedence
But if we're in a ternary, the `:` of the ternary takes precedence.
Like other arrows, none starts in a guard or a condition, where
`async(x) =>` is a call followed by the case's `=>`.
*)
when is_es6_arrow_expression
~in_ternary:(context = TernaryTrueBranchExpr)
p ->
when context != WhenExpr
&& is_es6_arrow_expression
~in_ternary:(context = TernaryTrueBranchExpr)
Comment thread
cknitt marked this conversation as resolved.
p ->
let arrow_attrs = !attrs in
let () = attrs := [] in
parse_async_arrow_expression ~arrow_attrs p
Expand Down
7 changes: 7 additions & 0 deletions tests/syntax_tests/data/parsing/grammar/expressions/async.res
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,10 @@ let attr2 = @a async (type a) => (type b c, x) => 3
let attr3 = @a (type a) => async (type b c, x) => 3
let attr4 = @a (type a) => @b async (type b c, x) => 3
let attr5 : int = @a @b async (type a, type b c) => (x:a) => x

// A guard calling a function named async is a call, not an async arrow
let guarded = v =>
switch v {
| Some(x) if async(x) => async () => x
| _ => async () => 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ let attr4 =
let (attr5 : int) =
((async fun (type a) (type b) (type c) [arity:1]() ->
fun [arity:1](x : a) -> x)
[@a ][@b ])
[@a ][@b ])
let guarded [arity:1]v =
match v with
| Some x when async x -> (async fun [arity:1]() -> x)
| _ -> (async fun [arity:1]() -> 0)
Loading