dart-lang / dart-lang/language

Should we restrict cycles of F-bounded types through union types?

Open
#433 12 comments 0 reactions 0 assignees View on GitHub
nnbd technical-debt
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

We don't allow type variable declarations of the form `T extends T`, or `T extends S, S extends T`. The analyzer currently accepts type variable declarations of the form `T extends FutureOr`, but the CFE crashes on such declarations. With NNBD, we will also have the ability to write `T extends T?` or `T extends S, S extends T?` unless we specify otherwise.

Should we consider making these an error?

My inclination is yes. In general, this feels to be pushing up against (or crossing) an important boundary in the equational theory that may bite us if we eventually add union types and attempt to re-interpret `?` and `FutureOr` as union types. Note that for example `T extends T | Never` is essentially another way of writing `T extends T`, which suggests the kinds of problems that may arise.

I also haven't found any uses for this kind of equation, and since the CFE doesn't accept the `FutureOr` example, there is almost certainly no existing code that uses this feature.

One way of specifying this is to say that given a set of type variable declarations of the form `X0 extends B0, ..., Xn extends Bn`, construct a graph with one node for every `Xi`, and edges from `Xi` to `Xj` iff `Xi` occurs in a top level union in `Bj`, and say that it is an error if that graph has any cycles. We say that `Xi` occurs in a top level union in a type `T` iff:
- `T` is `Xi
- `T` is `S?` and `Xi` occurs in a top level union in `S`
- `T` is `FutureOr` and `Xi` occurs in a top level union in `S`

@eernstg @lrhn @munificent thoughts?

cc @stereotype441

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.