-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Avoid contravariant function fields #57799
Comments
We're considering invariance support in the language. In this situation, I believe declaration-site invariance (dart-lang/language#214) would fit quite well. |
I wonder, @srawlins - do you think this is still valuable? |
I think we can keep this open, if only as an approximate, partial, lightweight, supplemental linter rule to dart-lang/language#214 |
Yes, please! With Dart of today, this lint can inform developers that they're creating a dangerous situation if they ever declare an instance variable whose type has a non-covariant occurrence of a class type variable (or a method with such an occurrence in its return type, etc). But if and when we get support for statically checked variance (declaration-site, a more recent proposal: dart-lang/language#524, or use-site: dart-lang/language#753, or preferably both), developers would then have the tool they need in order to eliminate the potential for run-time errors. class C<X> { // Make it `in X` or `inout X`, and the danger goes away.
void Function(X) myDangerousFunction;
C(this.myDangerousFunction);
} I think the dynamic error that we have today is considerably more dangerous than having a method parameter with the same property (like class C<X> {
void Function(X) f;
C(this.f);
}
void main() {
C<num> c = C<int>((int i) => print(i));
(c as dynamic).f(1); // No problem: pass `1` to a `void Function(int)`.
c.f(1); // Throw, even though `1` _is_ an OK argument to `c.f`!
} So when we're calling So I believe that we need this lint without statically checked variance, and we need it with statically checked variance, but in the latter case we would need to include advice about using statically checked variance to eliminate the problem. |
The upcoming experimental lint |
Background: dart-lang/site-www#1017
I got positive support for this rule from other internal TLs on
dart-lints@
:For example:
Here is my proposed lint in short:
I imagine we could allow private function fields accessed through a safe method:
If we ever get variance control, invariant
"T"
could be allowed safely.The one real issue with be landing this lint, there already many (mis)-uses of this pattern. I could see (a) adding yet-another annotation
@suppressUnsafeGeneric
, (b) using// ignore: avoid_contravariant_function_fields
, or (c) making this a non-error in google3 (i.e. a hint on new code only).The text was updated successfully, but these errors were encountered: