You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Summary:
We allow monomorphic function types to be subtypes of polymorphic function types e.g.
```
function(mixed): void <: function<T>(T): void
```
However, since we do not disallow type variables in monomorphic function types this rule allows higher-rank type parameters to be present in the bounds of type variables in an outer scope.
This diff introduces ranks for type parameters and type variables to avoid this escape.
When we reach a subtype proposition involving a higher-rank type (currently this is only polymorphic function types) we increment the rank. This means that when we introduce fresh type parameters (for supertypes) or fresh type variables (for subtypes) and substitute them into a polymorphic function type, those type parameters & type variables will have higher rank than those occuring in the outer scope.
When we reach a subtype proposition involving a type variable (i.e. C-Var-L `facebook#1 <: T` or C-Var-R `T <: facebook#1`) we check to ensure that there is no type parameter contained in `T` with a greater rank than the rank of `facebook#1`.
This check involves traversing a type, potentially deeply, so we would like to avoid it whenever possible. To do so we add two optimizations:
1) We record when we are in a subtype proposition involving higher rank generics; if we are not then a type _cannot_ contain a higher rank generic and we can avoid the check
2) If we have to perform the check, we avoid fully traversing the type whenever possible by finding the first type parameter with a higher rank. The downside of this will be that when there are multiple higher-rank generics, on the first will be reported and the developer will only see the subsequent errors after the first is fixed.
Reviewed By: andrewjkennedy
Differential Revision: D72312308
fbshipit-source-id: 02ffef9eeb3c1c176a3ce9b8d47ab74da3abafc7
0 commit comments