Skip to content

Commit aef7987

Browse files
Merge pull request #12614 from dotty-staging/fix-12951
Improve display of references for ambiguous implicit errors
2 parents 0cbaccb + 0c9d8c0 commit aef7987

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

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

+6-1
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/neg/i12591.check

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- Error: tests/neg/i12591/Inner.scala:12:31 ---------------------------------------------------------------------------
2+
12 |val badSummon = summon[TC[Bar]] // error here
3+
| ^
4+
|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

tests/neg/i12591/Inner.scala

+13
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/neg/i12591/Outer.scala

+3
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)