dart-lang / dart-lang/language
FutureOr conditional expression typing issue
- Dominant language
- TeX
- Stars
- 2.9k
- Forks
- 239
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 14
Description
I'm running into a typing issue similar to dart-lang/sdk#28783, but in the context of a conditional expression (`?:`)
```dart
FutureOr routerCanDeactivate(next, prev) =>
someExpression
? true
: methodReturningFutureBool();
```
Under 1.22.1, this results in an analyzer warning:
> WARNING: Unsafe implicit cast from 'Object' to 'FutureOr'. This usually indicates that type information was lost and resulted in 'dynamic' and/or a place that will have a failure at runtime.
But the analyzer is happy if I put an explicit cast (which IMHO should be unnecessary):
```dart
FutureOr routerCanDeactivate(next, prev) =>
someExpression
? true as FutureOr // <================== cast
: methodReturningFutureBool();
```
@leafpetersen @bwilkerson @nex - I wasn't sure if you'd want a separate issue for this, or whether you'd prefer clumping it up with dart-lang/sdk#28783. It felt like `?:` might have its typing rules handled separately from if-then-else statements. Anyhow, I'll let you mark this as a dup if you want to merge the issues.
cc @kwalrath @matanlurey
Contributor guide
Research direction
Start with the conditional-expression example in the issue and reproduce the analyzer warning under the reported Dart version. Compare its typing behavior with the related dart-lang/sdk#28783 issue and the explicit-cast variant; done means the intended behavior and any required language or analyzer change are established.
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