dart-lang / dart-lang/language
[analyzer] Switch cases for destructured records do not promote types
- Dominant language
- TeX
- Stars
- 2.9k
- Forks
- 239
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 14
Description
When trying to have switch cases where the record fields each match a certain subtype of a common supertype, it appears that when switching on the destructured variables, the type promotion does not happen. However, as indicated in `demoB`, it could work without destructuring. Is this the intended behavior?
Example:
```dart
class Foo {}
class FooA extends Foo {}
class FooB extends Foo {}
void funA(FooA first, FooA second) {}
void funB(FooB first, FooB second) {}
void demoA(Foo first, Foo second) {
switch ((first, second)) {
case ((FooA(), FooA())):
// The argument type 'Foo' can't be assigned to the parameter type 'FooA'
funA(first, second);
case ((FooB(), FooB())):
// The argument type 'Foo' can't be assigned to the parameter type 'FooB'
funB(first, second);
}
}
void demoB(Foo first, Foo second) {
final record = (first, second);
switch (record) {
case ((FooA(), FooA())):
funA(record.$1, record.$2); // OK
case ((FooB(), FooB())):
funB(record.$1, record.$2); // OK
}
}
```
Dart info:
```
- Dart 3.5.0-319.0.dev (dev) (Sun Jun 30 17:06:39 2024 -0700) on "linux_x64"
- on linux / Linux 6.1.0-22-amd64 dart-lang/sdk#1 SMP PREEMPT_DYNAMIC Debian 6.1.94-1 (2024-06-21)
```
Related:
- dart-lang/sdk#45357
- https://github.com/zulip/zulip-flutter/pull/808
Contributor guide
Assessment
This issue has not been assessed yet.