-
Notifications
You must be signed in to change notification settings - Fork 362
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
idea: check and fix use of declaration-site type variance #2621
Comments
I'm a big fan of this for a set of hard-coded well-known classes. |
Are there also a series of types for which we wouldn't add variance? For example: |
I think so. However, I suggest discussing meta-issues in jspecify issue (the first link) to avoid splitting the questions/findings into several issues ;) |
Automatically added variance should skip type parameters that are final classes as well. |
Note that when a method overrides another method, it cannot use declaration-site type variance without changing the overridden method. Nor can on overridding method remove declaration-site type variance when overriding one that has it. interface Test {
void test(Function<List, List> f);
}
class TestImpl implements Test {
@Override
public void test(Function<? super List, ? extends List> f) {
}
} |
A class can become non-final, so having variance for final classes might be helpful. |
You can tinker with the current form of this recipe at https://public.moderne.io/recipes/org.openrewrite.java.cleanup.CommonDeclarationSiteTypeVariances. |
I think |
In the latest commit I added an option |
What I mean is ? super variance should be added for google/error-prone#3711 (comment) google/error-prone#3711 (comment) all the classes no matter if they are final or not. ? super Object is special: google/error-prone#3711 (comment) |
By setting |
What is the point of skipping ? super variance if somebody sets excludeFinalClasses=true? |
I'm just coming around on this -- my intuition was originally that |
Not really. void act(Consumer<Integer> intConsumer) { }
...
Consumer<Object> tst=...;
act(tst); <-- this will fail to compile.
// act(Consumer<? super Integer> ...) will heal the case |
I see, so you were literally meaning just the contravariant case? Makes sense. Should we avoid |
I am not sure regarding I guess it might be great to have an option like suppressOutVarianceForClasses:..., and put String, Integer, there by default. |
An unfortunate case is |
And similar for every collection type likely... |
Honestly these results are looking pretty good https://public.moderne.io/results/aX1Tl. Once b47d9b3 finishes building we'll have the ability to suppress out variance for final classes (as well as in and out variance for a specified set of excluded glob patterns of fully qualified types). |
One more thing: this recipe shouldn't modify any parameter that is assigned to a field in the method body. |
Why? On contrary, it should modify the type of the field as well. |
For completeness yes. Trying to limit complexity somewhat. The assignment may be to a base class coming from a binary dependency, but easy enough to check for. |
|
Can the recipie be suppressed for a particular placeholder? E.g. |
-MicrometerCollector collector, HistogramSupport histogramSupport, Supplier<Exemplar[]> exemplarsSupplier,
+MicrometerCollector collector, HistogramSupport histogramSupport, Supplier<? extends Exemplar[]> exemplarsSupplier, It looks like a false-positive. One can't inherit arrays, so However, Just in case, The same goes for |
One more idea: I guess the recepie should work both ways: if it detects |
I found there is already a recipe there: |
See:
Currently, Java requires use-site type variance, so if someone has
Function<IN, OUT>
method parameter, it should rather beFunction<? super IN, ? extends OUT>
.Unfortunately, it is not easy to notice that
? super
and? extends
is missing, so it would be nice if there was a tool that could detect missing variance and suggest adding it.The list of well-known classes could be hard-coded within OpenRewrite:
Function
,Predicate
,BiFunction
,Consumer
,Supplier
, and so on.Here is a recent case:
WDYT?
Corner cases:
See also:
The text was updated successfully, but these errors were encountered: