dart-lang / dart-lang/language
Switch expressions don't exaustevely check for local functions that return exceptions.
- Dominant language
- TeX
- Stars
- 2.9k
- Forks
- 239
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 14
Description
I tried to make the following code with switch expression.
```dart
static FirebaseOptions forPlatform({FirebaseOptions? android, FirebaseOptions? ios}) {
void throwUnsupportedPlatform(TargetPlatform platform) => throw UnsupportedError('DefaultFirebaseOptions are not supported for $platform.');
return switch (defaultTargetPlatform) {
TargetPlatform.android => android ?? throwUnsupportedPlatform(defaultTargetPlatform),
TargetPlatform.iOS => ios ?? throwUnsupportedPlatform(defaultTargetPlatform),
_ => throwUnsupportedPlatform(defaultTargetPlatform),
};
}
```
The analyzer complains that `A value of type 'void' can't be returned from the method 'forPlatform' because it has a return type of 'FirebaseOptions'.`. I imagined the analyzer would be able to know that `throwUnsupportedPlatform` always throws, so this is valid code.
This code works:
```dart
return switch (defaultTargetPlatform) {
TargetPlatform.android => android ?? (throw UnsupportedError('DefaultFirebaseOptions are not supported for $defaultTargetPlatform.')),
TargetPlatform.iOS => ios ?? (throw UnsupportedError('DefaultFirebaseOptions are not supported for $defaultTargetPlatform.')),
_ => throw UnsupportedError('DefaultFirebaseOptions are not supported for $defaultTargetPlatform.'),
};
```
Contributor guide
Research direction
Start by reproducing the reported switch-expression example and compare it with the version that throws inline. Trace how the analyzer types a local function declared to return void, then identify the language or analyzer behavior that should recognize an always-throwing function. Done means the example is accepted without weakening return-type checking, with coverage for both switch-expression branches and the existing inline-throw 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
- 35/100