Skip to content

Fix #1747: Tweak for <:< between method types #1926

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,12 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
case tp2 @ MethodType(_, formals2) =>
def compareMethod = tp1 match {
case tp1 @ MethodType(_, formals1) =>
(tp1.signature consistentParams tp2.signature) &&
val potentialMatch =
tp1.signature.consistentParams(tp2.signature) ||
tp1.isJava != tp2.isJava
// Overriding Java and non-Java methods might differ in their signature:
// Java maps Array[T] to Array[Object], Scala maps it to Object. See #1747.
potentialMatch &&
matchingParams(formals1, formals2, tp1.isJava, tp2.isJava) &&
(!tp1.isImplicit || tp2.isImplicit) && // non-implicit functions shadow implicit ones
isSubType(tp1.resultType, tp2.resultType.subst(tp2, tp1))
Expand Down Expand Up @@ -1489,7 +1494,7 @@ class ExplainingTypeComparer(initctx: Context) extends TypeComparer(initctx) {
}

override def isSubType(tp1: Type, tp2: Type) =
traceIndented(s"${show(tp1)} <:< ${show(tp2)}${if (Config.verboseExplainSubtype) s" ${tp1.getClass} ${tp2.getClass}" else ""}${if (frozenConstraint) " frozen" else ""}") {
traceIndented(s"${show(tp1)} <:< ${show(tp2)}${if (Config.verboseExplainSubtype) s" ${tp1.getClass} ${tp2.getClass} ${tp1.signature} ${tp2.signature}" else ""}${if (frozenConstraint) " frozen" else ""}") {
super.isSubType(tp1, tp2)
}

Expand Down
1 change: 1 addition & 0 deletions tests/run/i1747-a.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
523
4 changes: 4 additions & 0 deletions tests/run/i1747-a/Foo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

public interface Foo<E> {
<T> T[] foo(T[] a, T e);
}
13 changes: 13 additions & 0 deletions tests/run/i1747-a/i1747.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import java.util
object Test {
def main(args: Array[String]): Unit = {
println(new Bar[Int].foo(Array(1, 2, 3), 5).mkString)
}
}

class Bar[E] extends Foo[E] {
def foo[T](a: Array[T], e: T): Array[T] = {
a(0) = e
a
}
}
1 change: 1 addition & 0 deletions tests/run/i1747-b.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
null23
27 changes: 27 additions & 0 deletions tests/run/i1747-b/i1747.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import java.util
object Test {
def main(args: Array[String]): Unit = {
println(new Coll[Int].toArray(Array(1, 2, 3)).mkString)
}
}

class Coll[E] extends java.util.Collection[E] {

def toArray[T](a: Array[T]): Array[T] = {
a(0) = null.asInstanceOf[T]
a
}

def removeAll(c: util.Collection[_]): Boolean = ???
def retainAll(c: util.Collection[_]): Boolean = ???
def clear(): Unit = ???
def toArray: Array[AnyRef] = ???
def size(): Int = ???
def remove(o: scala.Any): Boolean = ???
def contains(o: scala.Any): Boolean = ???
def addAll(c: util.Collection[_ <: E]): Boolean = ???
def iterator(): util.Iterator[E] = ???
def isEmpty: Boolean = ???
def containsAll(c: util.Collection[_]): Boolean = ???
def add(e: E): Boolean = ???
}