dart-lang / dart-lang/language
Cast pattern executes even when operand subpattern is refuted
Nobody has claimed this yet.
- Dominant language
- TeX
- Stars
- 2.9k
- Forks
- 239
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 14
Description
Example taken from tests/language/patterns/exhaustiveness/nullable_cast_test.dart in the SDK:
import 'dart:async';
exhaustiveNonNullableFutureOrTypeVariable2<T extends Object>(FutureOr<T?> o) =>
switch (o) {
FutureOr<T>() as FutureOr<T> => 0,
};
main() {
exhaustiveNonNullableFutureOrTypeVariable2<int>(
Future<int?>.value(null));
}
This code is currently expected to throw. I find it unintuitive that the cast in a cast pattern will execute even when the operand is refuted. Can we change the semantics so that the cast only occurs if the pattern match on the operand succeeds?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the example in tests/language/patterns/exhaustiveness/nullable_cast_test.dart, especially the referenced cast-pattern case. Run the test to confirm the current throw, then read the surrounding test and the linked language issue context to identify the specified cast-pattern semantics. Done means the behavior and corresponding language specification tests reflect whether the operand subpattern succeeds before casting.
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
- Clearly specified
- Newbie friendliness
- 35/100