Commit 5e43e34
committed
Fix override checking for Java methods with covariant array
Fixes #24074
When resolving overloads from Java interfaces, treat arrays as
covariant.
This fixes incorrect method selection when multiple Java interfaces override methods with array return types.
Previously, in the example below, we get the compilation error:
```
value foo is not a member of org.test.Test2.A
lvl3.foo.head.foo()
```
```java
public class JavaPart {
public interface A { }
public interface B extends A {
int onlyInB();
}
public interface Lvl1 {
A[] getData();
}
public interface Lvl2 extends Lvl1 {
@OverRide
B[] getData();
}
public interface Lvl3 extends Lvl2, Lvl1 { }
}
```
```scala
def test(lvl3: JavaPart.Lvl3): Unit =
lvl3.getData.head.onlyInB()
```
because `Denotations#mergeSingleDenot` creates a `JointRefDenotation`
for `Lvl1.getData: A[]` and `Lvl2.getData: B[]`,
with a return type of `JArray[A] & JArray[B]`
(since the compiler doesn't recognize that `Lvl2.getData` overrides `Lvl1.getData`).
And because `JArray` isn't recognized as covariant,
`JArray[A & B] <: JArray[A] & JArray[B]` cannot be derived.
Consequently, `lvl3.getData.head` returns a value typed as
`A` instead of neither `B` nor `A & B`,
which fails to resolve the method `onlyInB`.1 parent e29c66d commit 5e43e34
File tree
2 files changed
+28
-3
lines changed- compiler/src/dotty/tools/dotc/core
2 files changed
+28
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
480 | 480 | | |
481 | 481 | | |
482 | 482 | | |
483 | | - | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
484 | 489 | | |
485 | | - | |
| 490 | + | |
486 | 491 | | |
487 | 492 | | |
488 | 493 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
993 | 993 | | |
994 | 994 | | |
995 | 995 | | |
996 | | - | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
| 1006 | + | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
997 | 1010 | | |
998 | 1011 | | |
999 | 1012 | | |
| |||
3356 | 3369 | | |
3357 | 3370 | | |
3358 | 3371 | | |
| 3372 | + | |
| 3373 | + | |
| 3374 | + | |
| 3375 | + | |
| 3376 | + | |
| 3377 | + | |
| 3378 | + | |
3359 | 3379 | | |
3360 | 3380 | | |
3361 | 3381 | | |
| |||
0 commit comments