Skip to content

Commit ebf58bb

Browse files
committed
Output "no best given instance" for ambiguous implicit error messages
1 parent 0e4d51a commit ebf58bb

File tree

6 files changed

+15
-6
lines changed

6 files changed

+15
-6
lines changed

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2938,11 +2938,20 @@ class MissingImplicitArgument(
29382938

29392939
def location(preposition: String) = if (where.isEmpty) "" else s" $preposition $where"
29402940

2941+
/** Default error message for non-nested ambiguous implicits. */
29412942
def defaultAmbiguousImplicitMsg(ambi: AmbiguousImplicits) =
29422943
s"Ambiguous given instances: ${ambi.explanation}${location("of")}"
29432944

2945+
/** Default error messages for non-ambiguous implicits, or nested ambiguous
2946+
* implicits.
2947+
*
2948+
* The default message is shown for ambiguous implicits only if they have
2949+
* the `nested` flag set. In this case, we output "no best given instance"
2950+
* instead of "no given instance".
2951+
*/
29442952
def defaultImplicitNotFoundMessage =
2945-
i"No given instance of type $pt was found${location("for")}"
2953+
val bestStr = if arg.tpe.isInstanceOf[AmbiguousImplicits] then " best" else ""
2954+
i"No$bestStr given instance of type $pt was found${location("for")}"
29462955

29472956
/** Construct a custom error message given an ambiguous implicit
29482957
* candidate `alt` and a user defined message `raw`.

tests/neg/19414-desugared.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- [E172] Type Error: tests/neg/19414-desugared.scala:22:34 ------------------------------------------------------------
22
22 | summon[BodySerializer[JsObject]] // error: Ambiguous given instances
33
| ^
4-
|No given instance of type BodySerializer[JsObject] was found for parameter x of method summon in object Predef.
4+
|No best given instance of type BodySerializer[JsObject] was found for parameter x of method summon in object Predef.
55
|I found:
66
|
77
| given_BodySerializer_B[B](

tests/neg/19414.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- [E172] Type Error: tests/neg/19414.scala:15:34 ----------------------------------------------------------------------
22
15 | summon[BodySerializer[JsObject]] // error: Ambiguous given instances
33
| ^
4-
|No given instance of type BodySerializer[JsObject] was found for parameter x of method summon in object Predef.
4+
|No best given instance of type BodySerializer[JsObject] was found for parameter x of method summon in object Predef.
55
|I found:
66
|
77
| given_BodySerializer_B[B](

tests/neg/given-ambiguous-1.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- [E172] Type Error: tests/neg/given-ambiguous-1.scala:12:23 ----------------------------------------------------------
22
12 |def f: Unit = summon[B] // error: Ambiguous given instances
33
| ^
4-
| No given instance of type B was found for parameter x of method summon in object Predef.
4+
| No best given instance of type B was found for parameter x of method summon in object Predef.
55
| I found:
66
|
77
| given_B(/* ambiguous: both given instance a1 and given instance a2 match type A */summon[A])

tests/neg/given-ambiguous-default-1.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- [E172] Type Error: tests/neg/given-ambiguous-default-1.scala:18:23 --------------------------------------------------
22
18 |def f: Unit = summon[B] // error: Ambiguous given instances
33
| ^
4-
| No given instance of type B was found for parameter x of method summon in object Predef.
4+
| No best given instance of type B was found for parameter x of method summon in object Predef.
55
| I found:
66
|
77
| given_B(a = /* ambiguous: both given instance a1 and given instance a2 match type A */summon[A])

tests/neg/given-ambiguous-default-2.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- [E172] Type Error: tests/neg/given-ambiguous-default-2.scala:18:23 --------------------------------------------------
22
18 |def f: Unit = summon[C] // error: Ambiguous given instances
33
| ^
4-
|No given instance of type C was found for parameter x of method summon in object Predef.
4+
|No best given instance of type C was found for parameter x of method summon in object Predef.
55
|I found:
66
|
77
| given_C(a = /* ambiguous: both given instance a1 and given instance a2 match type A */summon[A], this.given_C$default$2)

0 commit comments

Comments
 (0)