From 66a931b8a1e9102a90a68cc8aa2da8b12a7df20a Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Wed, 26 May 2021 19:05:59 +0200 Subject: [PATCH 1/3] Improve display of references for ambiguous implicit errors If the first attempt yields equal strings, print the full path to the implicits instead. Fixes #12591 --- compiler/src/dotty/tools/dotc/typer/Implicits.scala | 7 ++++++- tests/pos/i12591/Inner.scala | 13 +++++++++++++ tests/pos/i12591/Outer.scala | 3 +++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 tests/pos/i12591/Inner.scala create mode 100644 tests/pos/i12591/Outer.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Implicits.scala b/compiler/src/dotty/tools/dotc/typer/Implicits.scala index 7b743aaab49f..9306a6ac50fe 100644 --- a/compiler/src/dotty/tools/dotc/typer/Implicits.scala +++ b/compiler/src/dotty/tools/dotc/typer/Implicits.scala @@ -492,7 +492,12 @@ object Implicits: /** An ambiguous implicits failure */ class AmbiguousImplicits(val alt1: SearchSuccess, val alt2: SearchSuccess, val expectedType: Type, val argument: Tree) extends SearchFailureType { def explanation(using Context): String = - em"both ${err.refStr(alt1.ref)} and ${err.refStr(alt2.ref)} $qualify" + var str1 = err.refStr(alt1.ref) + var str2 = err.refStr(alt2.ref) + if str1 == str2 then + str1 = ctx.printer.toTextRef(alt1.ref).show + str2 = ctx.printer.toTextRef(alt2.ref).show + em"both $str1 and $str2 $qualify" override def whyNoConversion(using Context): String = if !argument.isEmpty && argument.tpe.widen.isRef(defn.NothingClass) then "" diff --git a/tests/pos/i12591/Inner.scala b/tests/pos/i12591/Inner.scala new file mode 100644 index 000000000000..aae9bd5b9234 --- /dev/null +++ b/tests/pos/i12591/Inner.scala @@ -0,0 +1,13 @@ +package outer +package inner + +sealed trait Foo +object Foo: + trait TC[T] + given ofFoo[T <: Foo]: TC[T] = ??? + trait Bar extends Foo + +import Foo.TC +//Adding import Foo.Bar resolves the issue +val badSummon = summon[TC[Bar]] // error here + diff --git a/tests/pos/i12591/Outer.scala b/tests/pos/i12591/Outer.scala new file mode 100644 index 000000000000..5c810ec785fe --- /dev/null +++ b/tests/pos/i12591/Outer.scala @@ -0,0 +1,3 @@ +package outer +export inner.Foo +export Foo.Bar From 6b36e32a3e6bf35d6fcf547d0937b64bc7ea39a9 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Wed, 26 May 2021 20:39:45 +0200 Subject: [PATCH 2/3] Reclassify test --- tests/{pos => neg}/i12591/Inner.scala | 0 tests/{pos => neg}/i12591/Outer.scala | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename tests/{pos => neg}/i12591/Inner.scala (100%) rename tests/{pos => neg}/i12591/Outer.scala (100%) diff --git a/tests/pos/i12591/Inner.scala b/tests/neg/i12591/Inner.scala similarity index 100% rename from tests/pos/i12591/Inner.scala rename to tests/neg/i12591/Inner.scala diff --git a/tests/pos/i12591/Outer.scala b/tests/neg/i12591/Outer.scala similarity index 100% rename from tests/pos/i12591/Outer.scala rename to tests/neg/i12591/Outer.scala From 0c9d8c0c70863b7031288b90a845217df553325b Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Fri, 28 May 2021 11:22:01 +0200 Subject: [PATCH 3/3] Add check file --- tests/neg/i12591.check | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 tests/neg/i12591.check diff --git a/tests/neg/i12591.check b/tests/neg/i12591.check new file mode 100644 index 000000000000..694eb68e3299 --- /dev/null +++ b/tests/neg/i12591.check @@ -0,0 +1,4 @@ +-- Error: tests/neg/i12591/Inner.scala:12:31 --------------------------------------------------------------------------- +12 |val badSummon = summon[TC[Bar]] // error here + | ^ + |ambiguous implicit arguments: both outer.inner.Foo.ofFoo and outer.Foo.ofFoo match type outer.inner.Foo.TC[outer.Bar] of parameter x of method summon in object Predef