-
Notifications
You must be signed in to change notification settings - Fork 26
add covariance checks for parameters #161
Comments
So, thinking about this again, now that I understand better how types work in Analyzer: What we want to do is instantiate (Incidentally: that's the same logic we'd use for covariant overrides, if we allow opt-in to that feature. The extra step here is we need to instantiate our base generics as their lower bounds. I'm assuming that strong mode will already disallow instantiation where it does not match the lower bound, e.g. given Rectangle< T extends num > it will not be allowed to instantiate that with dynamic.) |
I'm not sure I follow this. What lower bound do you mean, when you say "instantiate |
Haha, oops, I meant upper bound :) |
In other words: (We could certainly look at each parameter and see if the type parameter T occurs in certain positions in the base method's parameters. I thought subtype might be an easy way to get the same effect.) |
Another example: class C<T extends num> {
add(T t) {}
}
class D extends C<int> {
add(Object obj) {}
} Here we wouldn't need a check on D.add, because: {num/T}T <: Object. |
Ah, I see. Yes, I think that makes sense, and could be a nice way to do it. You may need to think about what exactly the base method you compare to should be. For example: class C<T> {
add(T t) {}
}
class D extends C<int> {
add(int i) {}
}
class E extends D {
add(num obj) {} Here both overrides need a check, but if you only compare the type of E.add against D.add I don't think you'll see it. It's possible it's always sufficient to check against the root definitions in the hierarchy (presumably there can be multiple roots). Might be worth thinking through the corner cases though. |
Chatted with Leaf, another thing we should consider: It would be nice if Analyzer knows about these implicit downcasts, so we can go through our normal warning logic. For example |
@rakudrama noticed another bug with the currently implemented logic -- it adds checks inside inner lambda, so for example: class C<T> {
Iterable<T> iter;
m() {
return iter.any((i) => i != null);
}
} there's a check added inside the lambda to assert that |
https://codereview.chromium.org/1988613006/ addresses the unnecessary checks inside of lambdas, and also eliminates checks when the type parameter is only used covariantly. |
This issue was moved to dart-lang/sdk#27259 |
we need to add covariance checks for parameters, e.g.
List<E>.add
... givencurrently I'm adding something simple to JS codegen, but it adds extra checks (Foo.forEach) and it misses checks (Bar.add). Also ideally we'd compute this earlier, in checker/coercion reifier.
The text was updated successfully, but these errors were encountered: