dart-lang / dart-lang/language

Inconsistent inference in analyzer and CFE

Open
#2,130 3 comments 0 reactions 1 assignee Claimed by @chloestefantsova View on GitHub
type-inference
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

The following code is inferred inconsistently between the CFE and the analyzer:

```dart
void main(){
String? variable;
var f = Future.value(variable);
Future x = f;
}
```

The analyzer emits no error, suggesting that inference is inferring `String` for the type argument to `Future.value`, whereas the CFE emits the following error:

```
leafp-macbookpro2:sdk leafp$ ~/src/dart-repo/sdk/xcodebuild/ReleaseARM64/dart ~/tmp/example.dart
../../../tmp/example.dart:5:22: Error: A value of type 'Future' can't be assigned to a variable of type 'Future' because 'String?' is nullable and 'String' isn't.
- 'Future' is from 'dart:async'.
Future x = f;
```

I believe the analyzer behavior here is in general more useful, I'm not sure off hand which is consistent with the spec. cc @stereotype441 @eernstg @johnniwinther @scheglov

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.