Fix varargs overload resolution with wildcard types #24669
+75
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #24072
When comparing overloaded methods where one is non-varargs with wildcard types (e.g.,
Class[? <: T]) and another is varargs, the non-varargs method should be preferred.Previously, the compiler failed to distinguish these methods , and results in an ambiguity error.
The problem is
compare(m1, m2)returned 0 because:m2(varargs) is correctly considered "not as good" asm1.m1(non-varargs) was also considered "not as good" asm2. (butm1should be as good asm2! becauseClass[Concrete]can be applied to both m1 and m2).The (2) occurred because
Class[? <: T]is not a subtype ofClass[T](due to invariance). Consequently,isApplicableMethodRef(m2, Class[? <: T])returnedfalsebecauseisCompatible(Class[? <: T], Class[T])returnedfalseduring the applicability check against the method.This commit adds special handling in
TestApplication.argOKto check if wildcard upper bounds are compatible with their formal types during overload resolution, in addition toisCompatible.