-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Type inferrence of generic to Never #53668
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Working as intended. The context type for the second operand of The static type of You'll see the same behavior in any place where a generic constant expression is assigned to a type with a type variable, including Using Choosing People would write things like So if you don't want that, you can write either: final A value = subject ?? const A(); // because `A` means `A<dynamic>` as a raw type or final value = subject ?? const A<dynamic>(); The throwing is bad, but it's a consequence of covariant generics being unsafe. You'll get the same error with: List<int> list = <Never>[]; // Generic up-cast, Never <: int => List<Never> <: List<int>
list.add(1); // Calling method which uses type variable contravariantly. Any time you up-cast a generic type to a supertype of the type variable, you risk errors if calling a method which has a contravariant occurrence of the type variable. The compiler needs to throw in that case, because continuing would be unsound. |
Thanks for the insightful explanation.
Totally makes sense. This is probably a feature request for the analyzer team but I somehow expected either |
It's related to #34058, but mainly in the contravariance issue. That other issue is about under-constrained type arguments being solved to extreme types (top types like I don't know if there is a lint for this. The |
Yes, it doesn't. None of these language features cover Never. |
If you want a lint, you should ask for one. Defintely won't happen otherwise. What would it warn about? Whenever a And considering that a That suggests that there could be a somewhat large number of false positives for such a lint. That's not inherently an argument against a lint, they are allowed to have false positives. That's why lints are optional. But it can mean that there will not be a lot of users of the lint, if it gets in the way of their code. If the real issue is the unsafe upcast, it may be better to put resources into variance annotations. If the type parameter of |
The following code infers
const A()
toA<Never>
when assigned using the nullcheck operator.This is unexpected as I'd either think
A<dynamic>
or betterA<T>
is inferred.The code throws at runtime which is bad.
General info
Project info
Process info
The text was updated successfully, but these errors were encountered: