Commit f67a1a0
authored
Fix varargs overload resolution with wildcard types (#24669)
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.
```scala
def blub[T](a: Class[? <: T]): Unit // m1
def blub[T](a: Class[T], ints: Int*): Unit // m2
blub(classOf[Object]) // m1 should be picked, but fails to resolve
````
The problem is `compare(m1, m2)` returned 0 because:
- (1). `m2` (varargs) is correctly considered "not as good" as `m1`.
- (2). `m1` (non-varargs) was also considered "not as good" as `m2`.
(but `m1` should be as good as `m2`! because `Class[Concrete]` can be
applied to both m1 and m2).
The (2) occurred because `Class[? <: T]` is not a subtype of `Class[T]`
(due to invariance). Consequently, `isApplicableMethodRef(m2, Class[? <:
T])` returned `false` because `isCompatible(Class[? <: T], Class[T])`
returned `false` during the applicability check against the method.
This commit adds special handling in `TestApplication.argOK` to check if
wildcard upper bounds are compatible with their formal types during
overload resolution, in addition to `isCompatible`.File tree
4 files changed
+77
-2
lines changed- compiler/src/dotty/tools/dotc/typer
- tests
- neg
- run/overload_repeated
4 files changed
+77
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
883 | 883 | | |
884 | 884 | | |
885 | 885 | | |
886 | | - | |
| 886 | + | |
| 887 | + | |
| 888 | + | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
887 | 909 | | |
888 | 910 | | |
889 | 911 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
5 | 10 | | |
| 11 | + | |
6 | 12 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
14 | 30 | | |
15 | 31 | | |
16 | 32 | | |
17 | 33 | | |
18 | 34 | | |
19 | 35 | | |
20 | 36 | | |
21 | | - | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
22 | 43 | | |
23 | 44 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
16 | 32 | | |
17 | 33 | | |
18 | 34 | | |
| |||
22 | 38 | | |
23 | 39 | | |
24 | 40 | | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
25 | 46 | | |
26 | 47 | | |
27 | 48 | | |
28 | 49 | | |
29 | 50 | | |
30 | 51 | | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
31 | 57 | | |
32 | 58 | | |
0 commit comments