dart-lang / dart-lang/language

Can't `rethrow` in switch expression

Open
#3,097 6 comments 7 reactions 0 assignees View on GitHub
feature request small-feature
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

Shouldn't be possible to `rethrow` in a switch expression since we can `throw e`?
Random example:
```dart
void main() {
try {
// some code that might throw
} catch (e) {
return switch (e) {
Exception() => print('Return what this function asks for (void).'),
Error() => throw e, //Works, but warning "Use 'rethrow' to rethrow a caught exception."
_ => rethrow, // Error "Undefined name 'rethrow'"
};
}
}
```

Inside a `catch` clause I think we could allow a `rethrow` statement.

Contributor guide

Open the contributing guide

Research direction

Start with the Dart language specification rules for catch clauses, rethrow, and switch expressions, using the example in the issue as the motivating case. Trace how the expression is analyzed inside a catch clause and define the intended language and diagnostic behavior; done means the specification change and its edge cases are agreed.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.