dart-lang / dart-lang/language
Nested intersections OK?
- Dominant language
- TeX
- Stars
- 2.9k
- Forks
- 239
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 14
Description
Thanks to @chloestefantsova for noticing this. We have a rule about [**NonNull**](https://github.com/dart-lang/language/blob/main/accepted/2.12/nnbd/feature-specification.md#null-promotion) that is capable of producing a nested intersection type:
> NonNull(X & T) = X & NonNull(T)
Here is an example where it matters:
```dart
foo(X x) {
if (x is Y) {
x!;
// `x` now has type `X & (Y & int)`.
X v1 = x;
Y v2 = x;
int v3 = x;
}
}
```
We don't otherwise allow an intersection type to be a subterm of any other type, but it might actually be benign to allow intersection types inside intersection types.
We could change the definition of **NonNull** in order to prevent this from happening. We could also double-check all usages of intersection types in spec documents and ensure that it does not cause any dangers to allow these "multi-operand" intersection types.
Note that the current implementation of the CFE as well as the analyzer allows the example program, which means that it is (at least in principle) a breaking change to eliminate this kind of type.
@dart-lang/language-team, WDYT?
Contributor guide
Assessment
This issue has not been assessed yet.