Skip to content

Commit 66a931b

Browse files
committed
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
1 parent 89e57aa commit 66a931b

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,12 @@ object Implicits:
492492
/** An ambiguous implicits failure */
493493
class AmbiguousImplicits(val alt1: SearchSuccess, val alt2: SearchSuccess, val expectedType: Type, val argument: Tree) extends SearchFailureType {
494494
def explanation(using Context): String =
495-
em"both ${err.refStr(alt1.ref)} and ${err.refStr(alt2.ref)} $qualify"
495+
var str1 = err.refStr(alt1.ref)
496+
var str2 = err.refStr(alt2.ref)
497+
if str1 == str2 then
498+
str1 = ctx.printer.toTextRef(alt1.ref).show
499+
str2 = ctx.printer.toTextRef(alt2.ref).show
500+
em"both $str1 and $str2 $qualify"
496501
override def whyNoConversion(using Context): String =
497502
if !argument.isEmpty && argument.tpe.widen.isRef(defn.NothingClass) then
498503
""

tests/pos/i12591/Inner.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package outer
2+
package inner
3+
4+
sealed trait Foo
5+
object Foo:
6+
trait TC[T]
7+
given ofFoo[T <: Foo]: TC[T] = ???
8+
trait Bar extends Foo
9+
10+
import Foo.TC
11+
//Adding import Foo.Bar resolves the issue
12+
val badSummon = summon[TC[Bar]] // error here
13+

tests/pos/i12591/Outer.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package outer
2+
export inner.Foo
3+
export Foo.Bar

0 commit comments

Comments
 (0)