Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch expression syntax should be recognized inside switch statements #52404

Open
stereotype441 opened this issue May 15, 2023 · 1 comment
Open
Labels
area-fe-analyzer-shared Assigned by engineers; when triaging, prefer either area-front-end or area-analyzer. fe-analyzer-shared-parser-recovery Issues with the shared parser's handling of incorrect code

Comments

@stereotype441
Copy link
Member

stereotype441 commented May 15, 2023

To the extent possible, it would be nice if the parser could recognize if the syntax of a switch expression is erroneously used where a switch statement is expected, e.g.:

f(x) {
  switch (x) {
    0 => 'zero',
    1 => 'one',
    _ => 'other'
  }
}

Currently this produces the error Expected to find 'case' at the location of the 0, and then no further messages (because the parser gives up and skips to the end of the switch).

It would be a lot nicer if the parser:

  1. gave a specific error message indicating that the user appears to be using switch expression syntax where a switch statement is expected, and
  2. continued parsing the rest of the switch expression.

(1) would allow us to associate some documentation with the error message explaining how switch statements and switch expressions are disambiguated, and the analysis server could in principle even include some suggested fixups (e.g. convert to a switch expression by enclosing in parentheses). (2) would allow code completions and other analysis server features to continue working in spite of the error.

@stereotype441 stereotype441 added area-fe-analyzer-shared Assigned by engineers; when triaging, prefer either area-front-end or area-analyzer. fe-analyzer-shared-parser-recovery Issues with the shared parser's handling of incorrect code labels May 15, 2023
@parlough
Copy link
Member

parlough commented May 15, 2023

This would be amazing! We're hoping to introduce some extra documentation/info on dart.dev to make this more clear as well since it has been coming up a lot and I imagine these improvements won't land for a while. \cc @MaryaBelanger

In dart-lang/language#3061 there was another motivating example:

enum Color { red, blue, green }

void main() {
  final color = Color.red;
  switch (color) {
    Color.red => "red",
    Color.blue => "blue",
    Color.green => "green",
  };
}

Which ends up with 4 different diagnostics confusing the user on what the actual problem is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-fe-analyzer-shared Assigned by engineers; when triaging, prefer either area-front-end or area-analyzer. fe-analyzer-shared-parser-recovery Issues with the shared parser's handling of incorrect code
Projects
None yet
Development

No branches or pull requests

2 participants