dart-lang / dart-lang/language
Pattern matching non-exhaustive error on sealed class and multiple type parameters
- Dominant language
- TeX
- Stars
- 2.9k
- Forks
- 239
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 14
Description
```dart
sealed class BaseType {}
class ChildA extends BaseType {}
class ChildB extends BaseType {}
test(BaseType a) => switch (a) {
ChildA() => "ChildA",
ChildB() => "ChildB",
};
```
This fails with a compilation error:
```
The type 'BaseType' is not exhaustively matched by the switch cases since it doesn't match 'ChildB()'.
Try adding a wildcard pattern or cases that match 'ChildB()'. dart(non_exhaustive_switch_expression)
```
I'm not sure why it asks for `ChildB()`.
I don't want to use that as I'll loose type information.
I expect the matched type `ChildB` to be enough.
Tested using `Dart version 3.2.6`
Contributor guide
Research direction
Start by reproducing the sealed-class example with Dart 3.2.6 and confirm the non-exhaustive switch diagnostic. Then trace the pattern-matching exhaustiveness analysis for generic sealed classes; done means determining whether ChildB should satisfy the match and documenting or correcting the behavior.
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
- 38/100