dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.25k stars 1.58k forks source link

Incorrect computation of subtype relationship #32275

Closed eernstg closed 5 years ago

eernstg commented 6 years ago

Consider the following program:

typedef F<X> = void Function(X);
class A<X> {}
class B<X> extends A<F<X>> {}
A<F<String>> a = new B<Object>();

main() => a;

Note that B<Object> <: A<F<Object>> because of the extends clause, and A<F<Object>> <: A<F<String>> because F<Object> <: F<String> because the type argument of the type alias F occurs contravariantly (and only contravariantly), so the initialization of a is actually an upcast. Still, it is rejected:

> dartanalyzer --preview-dart-2 n028.dart
Analyzing n028.dart...
  warning • A value of type 'B<Object>' can't be assigned to a variable of type 'A<(String) → void>' at n028.dart:6:18 • invalid_assignment
1 warning found.
eernstg commented 5 years ago

The declaration class B<X> extends A<F<X>> {} is an error according to this feature specification.

Said feature has been accepted, but is not yet implemented. But it makes no sense to change an implementation to handle a subtype relationship differently, when one of the types involved will be a compile-time error in itself. So I'll close this issue.