dart-lang / dart-lang/language

Surprising flow-analysis of `switch` statements

Open
#4,048 7 comments 0 reactions 0 assignees View on GitHub
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

The rule about switch statements is [here](https://github.com/dart-lang/language/blob/master/resources/type-system/flow-analysis.md#statements). Consider the following example:

```dart
enum Enum { one, two }

void main() {
Enum e = Enum.one;
num n;
switch (e) {
case Enum.one: n = 1; break;
case Enum.two: n = 2; break;
}
print(n.isEven);
}
```

The switch statement rule might need a few adjustments (saying something about `default`, and handling the situation where there is no `default` and the last case has no `break;`), but I would in any case expect the rule to promote `n` to `int` after the switch statement. The actual flow analysis does not reach that conclusion, so the analyzer reports an error on `n.isEven`, showing that `n` has type `num`.

@stereotype441, what do you think?

Contributor guide

Open the contributing guide

Research direction

Start with the Statements section of resources/type-system/flow-analysis.md and trace its switch-statement rule against the Dart example in the issue. Check how default cases, a final case without break, and the post-switch type of n are specified; done means the rule accounts for this example and clearly defines the expected promotion to int.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.