dart-lang / dart-lang/language
Surprising flow-analysis of `switch` statements
- 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
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