Skip to content

Switch guard calling a function named async fails to parse #8933

Closed

Description

@cknitt

A switch guard that calls a function named async fails to parse:

let async = x => x > 0

let f = v =>
  switch v {
  | Some(x) if async(x) => 1
  | _ => 0
  }
  Syntax error!
  4 │   | Some(x) if async(x) => 1
  5 │   | _ => 0

  Did you forget a `=>` here?

Cause: to tell an async arrow (async (x) => …) apart from a call (async(x)), the parser looks ahead for a => after the parentheses (is_es6_arrow_expression in compiler/syntax/src/res_core.ml). In a guard, the case's own => always follows the guard, so async(x) => is taken as the start of an async arrow.

Expected: a function is never a valid guard (guards are bool), so the arrow lookahead should not apply when parsing a guard (the WhenExpr context).

Nobody names a function async, so this rarely matters today. It will matter if gen becomes a contextual prefix for generator functions in the same way, because gen is a common function name.

Reproduced on master (8556f0a).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions