Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions compiler/src/dotty/tools/dotc/core/Denotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -480,9 +480,14 @@ object Denotations {

val matchLoosely = sym1.matchNullaryLoosely || sym2.matchNullaryLoosely

if symScore <= 0 && info2.overrides(info1, matchLoosely, checkClassInfo = false) then
val compareCtx =
if sym1.is(JavaDefined) && sym2.is(JavaDefined) then
ctx.withProperty(TypeComparer.ComparingJavaMethods, Some(()))
Copy link
Member Author

@tanishiking tanishiking Nov 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let TypeComparer know we're comparing Java defined methods, since TypeComparer alone can't tell the symbols are defined in Java.
Not sure this is legid usage of Context#property?

else ctx

if symScore <= 0 && info2.overrides(info1, matchLoosely, checkClassInfo = false)(using compareCtx) then
denot2
else if symScore >= 0 && info1.overrides(info2, matchLoosely, checkClassInfo = false) then
else if symScore >= 0 && info1.overrides(info2, matchLoosely, checkClassInfo = false)(using compareCtx) then
denot1
else
val jointInfo = infoMeet(info1, info2, safeIntersection)
Expand Down
22 changes: 21 additions & 1 deletion compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,20 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
(sym1 eq NullClass) && isNullable(tp2)
}
case tp1 @ AppliedType(tycon1, args1) =>
compareAppliedType1(tp1, tycon1, args1)
// Special case: Java arrays are covariant.
// When checking overrides (frozenConstraint) of Java methods, allow B[] <: A[] if B <: A.
def checkJavaArrayCovariance: Boolean = tp2 match {
case AppliedType(tycon2, arg2 :: Nil)
if frozenConstraint
&& tycon1.typeSymbol == defn.ArrayClass
&& tycon2.typeSymbol == defn.ArrayClass
&& args1.length == 1 =>
// Arrays are covariant in Java: B[] <: A[] if B <: A
isSubType(args1.head, arg2)
case _ => false
}
(ctx.property(ComparingJavaMethods).isDefined && checkJavaArrayCovariance)
|| compareAppliedType1(tp1, tycon1, args1)
case tp1: SingletonType =>
def comparePaths = tp2 match
case tp2: (TermRef | ThisType) =>
Expand Down Expand Up @@ -3356,6 +3369,13 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling

object TypeComparer {

import util.Property

/** A property key to indicate we're comparing Java-defined methods.
* When it is set, arrays are treated as covariant for override checking.
*/
val ComparingJavaMethods = new Property.Key[Unit]

/** A richer compare result, returned by `testSubType` and `test`. */
enum CompareResult:
case OK, OKwithGADTUsed, OKwithOpaquesUsed
Expand Down
17 changes: 17 additions & 0 deletions tests/pos/i24074/JavaPart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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 { }
}
3 changes: 3 additions & 0 deletions tests/pos/i24074/Test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object Test:
def test(lvl3: JavaPart.Lvl3): Unit =
lvl3.getData.head.onlyInB()
Loading