Skip to content

Commit ef383a8

Browse files
committed
Better error message for Java/Scala method discrepancies
1 parent b897386 commit ef383a8

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

compiler/src/dotty/tools/dotc/typer/ErrorReporting.scala

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,23 @@ object ErrorReporting {
111111
errorTree(tree, typeMismatchMsg(normalize(tree.tpe, pt), pt, implicitFailure.postscript))
112112

113113
/** A subtype log explaining why `found` does not conform to `expected` */
114-
def whyNoMatchStr(found: Type, expected: Type) =
115-
if (ctx.settings.explaintypes.value)
114+
def whyNoMatchStr(found: Type, expected: Type) = {
115+
def dropJavaMethod(tp: Type): Type = tp match {
116+
case tp: PolyType =>
117+
tp.derivedPolyType(resType = dropJavaMethod(tp.resultType))
118+
case tp: JavaMethodType =>
119+
MethodType(tp.paramNames, tp.paramTypes, dropJavaMethod(tp.resultType))
120+
case tp => tp
121+
}
122+
val found1 = dropJavaMethod(found)
123+
val expected1 = dropJavaMethod(expected)
124+
if ((found1 eq found) != (expected eq expected1) && (found1 <:< expected1))
125+
"\n (Note that Scala's and Java's representation of this type differs)"
126+
else if (ctx.settings.explaintypes.value)
116127
"\n" + ctx.typerState.show + "\n" + TypeComparer.explained((found <:< expected)(_))
117128
else
118129
""
130+
}
119131

120132
def typeMismatchMsg(found: Type, expected: Type, postScript: String = "") = {
121133
// replace constrained polyparams and their typevars by their bounds where possible

0 commit comments

Comments
 (0)