dart-lang / dart-lang/language

Should `Null` constrain inference?

Open
#436 6 comments 0 reactions 1 assignee Claimed by @leafpetersen View on GitHub
nnbd
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

Currently our implementations are inconsistent on inference from `Null`.

This code:

```dart
import 'dart:async';

class A {
A(FutureOr z);
A.named(T z);
}
void main() {
Null z;
var a = A(z);
var b = A.named(z);
print(a.runtimeType);
print(staticType(a));

print(b.runtimeType);
print(staticType(b));
}

Type staticType(T x) => T;
```

prints

```
A
A
A
A
```

on the VM, and

```
A
A
A
A
```

on DDC.

This should be made consistent. I think the best thing to do is to keep this as `A`, since that's what the analyzer is already reporting. This needs a little thinking through though, to make precise what behavior we expect here.

In general, when matching `A <: FutureOr` and solving for `X`, we will prefer matches against `Future` over `X`, since we want `Future <: FutureOr` to choose `int` for `X`, not `Future`. But if we prefer the match of `Null <: Future`, then we don't constrain `X`.

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.