|
| 1 | +# A Non-Covariant Type Variable in a Superinterface is an Error. |
| 2 | + |
| 3 | +Author: [email protected] ( @eernstg) |
| 4 | + |
| 5 | +Version: 0.1. |
| 6 | + |
| 7 | +## Motivation and Scope |
| 8 | + |
| 9 | +The ability to use a type variable contravariantly in a superinterface of a |
| 10 | +generic class creates an "anti-parallel" subtype relationship for a given |
| 11 | +class and a direct superinterface thereof, and the same thing can happen |
| 12 | +indirectly in many ways. This creates various complications for the static |
| 13 | +analysis and the enforcement of the heap invariant (aka soundness), as |
| 14 | +illustrated below. Similar complications arise in the invariant |
| 15 | +case. Hence, this feature makes all non-covariant usages of a type variable |
| 16 | +in a superinterface a compile-time error. |
| 17 | + |
| 18 | +Here is an example: |
| 19 | + |
| 20 | +```dart |
| 21 | +class A<X> { |
| 22 | + X x; |
| 23 | + A(this.x); |
| 24 | +} |
| 25 | +
|
| 26 | +class B<X> extends A<void Function(X)> { |
| 27 | + B(void Function(X) f): super(f); |
| 28 | +} |
| 29 | +
|
| 30 | +main() { |
| 31 | + // Upcast: `B<int> <: B<num>` by class covariance. |
| 32 | + B<num> b = B<int>((int i) => print(i.runtimeType)); |
| 33 | + // Upcast: `B<num> <: A<void Function(num)>` by `extends` clause. |
| 34 | + A<void Function(num)> a = b; |
| 35 | + // Upcast: `A<void Function(num)> <: A<void Function(double)>` |
| 36 | + // by class covariance, plus `double <: num` and `void <: void`. |
| 37 | + a.x(3.14); |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +Every assignment in `main` involves an upcast, so there are no downcasts at |
| 42 | +all and the program should be safe. However, execution fails at `a.x(3.14)` |
| 43 | +because we are passing an actual argument of type `double` to a function |
| 44 | +whose corresponding parameter type is `int`. |
| 45 | + |
| 46 | +Note that the heap invariant is violated during execution at the point |
| 47 | +where `a` is initialized, even though the program has no error according |
| 48 | +the the existing rules (before the inclusion of this feature). |
| 49 | + |
| 50 | +The underlying issue is that the contravariant usage of a type variable in |
| 51 | +a superinterface creates a twisted subtype lattice where `B` "goes in one |
| 52 | +direction" (`B<int> <: B<num>`) and the superinterface `A` "goes in the |
| 53 | +opposite direction" (`A<void Function(int)>` is a direct superinterface of |
| 54 | +`B<int>` and `A<void Function(num)>` is a direct superinterface of |
| 55 | +`B<num>`, but we have `A<void Function(num)> <: A<void Function(int)>` |
| 56 | +rather than the opposite): |
| 57 | + |
| 58 | +```dart |
| 59 | + A<void Function(int)> :> A<void Function(num)> |
| 60 | + ^ ^ |
| 61 | + | | |
| 62 | + | | |
| 63 | + B<int> <: B<num> |
| 64 | +``` |
| 65 | + |
| 66 | +We typically have a "parallel" subtype relationship: |
| 67 | + |
| 68 | +```dart |
| 69 | + Iterable<int> <: Iterable<num> |
| 70 | + ^ ^ |
| 71 | + | | |
| 72 | + | | |
| 73 | + List<int> <: List<num> |
| 74 | +``` |
| 75 | + |
| 76 | +But with the example above we have an "anti-parallel" relationship, and |
| 77 | +that creates the opportunity to have a series of upcasts that takes us from |
| 78 | +`int` to `double` in part of the type without ever seeing a discrepancy |
| 79 | +(because we can just as well go up to `A<void Function(double)>` in the |
| 80 | +last step rather than `A<void Function(int)>`). |
| 81 | + |
| 82 | +With such scenarios in mind, this feature amounts to adding a new |
| 83 | +compile-time error, as specified below. |
| 84 | + |
| 85 | + |
| 86 | +## Static Analysis |
| 87 | + |
| 88 | +Let `C` be a generic class that declares a formal type parameter `X`, and |
| 89 | +assume that `T` is a direct superinterface of `C`. It is a compile-time |
| 90 | +error if `X` occurs contravariantly or invariantly in `T`. |
| 91 | + |
| 92 | + |
| 93 | +## Dynamic Semantics |
| 94 | + |
| 95 | +There is no dynamic semantics associated with this feature. |
| 96 | + |
| 97 | + |
| 98 | +## Revisions |
| 99 | + |
| 100 | +* Version 0.1, Nov 29 2018: Initial version. |
0 commit comments