Commit 3c79c1e
authored
Fix override checking for Java methods with covariant array (#24408)
Fixes #24074
When resolving overridden methods 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`.
---
It seems in Scala2, we use `Types#matches` (which does match in Scala3
as well), so Scala3 is more strict?
https://github.com/scala/scala/blob/ae6ae4dd59cb90af62093cde52a292e5bd8bb7a8/src/reflect/scala/reflect/internal/Symbols.scala#L2461
https://github.com/scala/scala/blob/ae6ae4dd59cb90af62093cde52a292e5bd8bb7a8/src/reflect/scala/reflect/internal/Symbols.scala#L2445-L2452
https://github.com/scala/scala/blob/ae6ae4dd59cb90af62093cde52a292e5bd8bb7a8/src/reflect/scala/reflect/internal/Types.scala#L862-L874
---
Previous try #24377File tree
4 files changed
+48
-3
lines changed- compiler/src/dotty/tools/dotc/core
- tests/pos/i24074
4 files changed
+48
-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 | |
|---|---|---|---|
| |||
994 | 994 | | |
995 | 995 | | |
996 | 996 | | |
997 | | - | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
| 1006 | + | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
| 1010 | + | |
998 | 1011 | | |
999 | 1012 | | |
1000 | 1013 | | |
| |||
3357 | 3370 | | |
3358 | 3371 | | |
3359 | 3372 | | |
| 3373 | + | |
| 3374 | + | |
| 3375 | + | |
| 3376 | + | |
| 3377 | + | |
| 3378 | + | |
| 3379 | + | |
3360 | 3380 | | |
3361 | 3381 | | |
3362 | 3382 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
0 commit comments