From ef383a8a05547432c01d779976a4ea4ca2dffab9 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Tue, 14 Feb 2017 18:03:25 +0100 Subject: Better error message for Java/Scala method discrepancies --- compiler/src/dotty/tools/dotc/typer/ErrorReporting.scala | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/typer/ErrorReporting.scala b/compiler/src/dotty/tools/dotc/typer/ErrorReporting.scala index 1238ad568..9d6a01ab7 100644 --- a/compiler/src/dotty/tools/dotc/typer/ErrorReporting.scala +++ b/compiler/src/dotty/tools/dotc/typer/ErrorReporting.scala @@ -111,11 +111,23 @@ object ErrorReporting { errorTree(tree, typeMismatchMsg(normalize(tree.tpe, pt), pt, implicitFailure.postscript)) /** A subtype log explaining why `found` does not conform to `expected` */ - def whyNoMatchStr(found: Type, expected: Type) = - if (ctx.settings.explaintypes.value) + def whyNoMatchStr(found: Type, expected: Type) = { + def dropJavaMethod(tp: Type): Type = tp match { + case tp: PolyType => + tp.derivedPolyType(resType = dropJavaMethod(tp.resultType)) + case tp: JavaMethodType => + MethodType(tp.paramNames, tp.paramTypes, dropJavaMethod(tp.resultType)) + case tp => tp + } + val found1 = dropJavaMethod(found) + val expected1 = dropJavaMethod(expected) + if ((found1 eq found) != (expected eq expected1) && (found1 <:< expected1)) + "\n (Note that Scala's and Java's representation of this type differs)" + else if (ctx.settings.explaintypes.value) "\n" + ctx.typerState.show + "\n" + TypeComparer.explained((found <:< expected)(_)) else "" + } def typeMismatchMsg(found: Type, expected: Type, postScript: String = "") = { // replace constrained polyparams and their typevars by their bounds where possible -- cgit v1.2.3