-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Inconsistent errors for covariant subtyping violations on generic function fields. #36800
Comments
DDC seems correct, even if it's not that clear from the error message. It expected You can get into this situation because Dart has covariant class generics, and that's not type-safe. You hit one of the cases where that type-safety can occur, and where we insert extra run-time checks to catch it. The rest are wrong. I'm marking this as a front-end issue because the front end should make the implicit check explicit, and then the VM and dart2js would not let the invalidly typed value proceed. |
Cf. dart-lang/language#296: The field The run-time type error from DDC arises because of a caller-side check on the value of My proposal in dart-lang/language#297 is to use a sound type in the first place, which would give That type is not very informative (it's "the type But right now the most consistent fix is probably to make sure that the backends get the same caller-side checks that DDC already has. |
I will close this issue now. Here is an updated version of the example: typedef G<X> = Function<Y extends X>(Y); // `X` is invariant.
class C<X> {
G<X> g;
C(this.g);
}
main() {
C<Object> c = C<num>(<Y extends num>(Y y) => y.isNegative); // Covariance is allowed, as always.
c.g<String>('hi'); // The type of `c.g` is checked at run time.
// The previous line throws because `c.g` has static type `G<Object>` and dynamic
// type `G<num>`, and the latter is _not_ a subtype of the former.
} The situation where an instance variable ( This means that there will not be any compile-time errors, but there will be a dynamic error at It is still true that it is highly recommended to avoid having an instance variable whose type is non-covariant in the enclosing class type parameters. |
Turns out that we did not have a linter request to avoid having declarations like |
For the following snippet:
DDC emits a runtime type error:
The VM emits a runtime method-not-found error:
Dart2JS emits a weirder runtime type error:
DDK silently fails.
What's the expected behavior here? I would expect a runtime type assertion to report something along the lines of "String does not extend num".
The text was updated successfully, but these errors were encountered: