Skip to content

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

@leafpetersen

Description

@leafpetersen

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<T>, 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<S> and Xi occurs in a top level union in S

@eernstg @lrhn @munificent thoughts?

cc @stereotype441

Metadata

Metadata

Assignees

No one assigned

    Labels

    nnbdNNBD related issuestechnical-debtDealing with a part of the language which needs clarification or adjustments

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions