Description
This compiles with Scala 2, but not with Dotty:
class A {
def foo[T <: Cloneable with Serializable](x: T): Unit = {}
}
class B extends A {
override def foo[T <: Serializable](x: T): Unit = {}
}
error overriding method foo in class A of type [T <: Cloneable & Serializable](x: T): Unit;
method foo of type [T <: Serializable](x: T): Unit has incompatible type
This happens because TypeComparer requires matching signatures for subtypes:
https://github.com/lampepfl/dotty/blob/b0147ea5e44ce34a986b2858532fc390e4625f61/compiler/src/dotty/tools/dotc/core/TypeComparer.scala#L656
https://github.com/lampepfl/dotty/blob/b0147ea5e44ce34a986b2858532fc390e4625f61/compiler/src/dotty/tools/dotc/core/TypeComparer.scala#L666
This particular example is perhaps not very important, but the same issue can happen when overriding a Java method which is much more problematic: because Java erases some types differently, in some situation it might not be possible to write a method with a matching signature that is also a valid override.
To fix this, I think that we can just remove the consistentParams
checks from TypeComparer. As far as I can tell, the subsequent matchingPolyParams/matchingMethodParams should be enough to guarantee soundness, so it's just a (hopefully insignificant) performance hit. /cc @odersky