Skip to content

Commit a04403c

Browse files
mbovelWojciechMazur
authored andcommitted
Output "no best given instance" for ambiguous implicit error messages
[Cherry-picked ebf58bb]
1 parent 4b84055 commit a04403c

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
@@ -2913,11 +2913,20 @@ class MissingImplicitArgument(
29132913

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

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

2920+
/** Default error messages for non-ambiguous implicits, or nested ambiguous
2921+
* implicits.
2922+
*
2923+
* The default message is shown for ambiguous implicits only if they have
2924+
* the `nested` flag set. In this case, we output "no best given instance"
2925+
* instead of "no given instance".
2926+
*/
29192927
def defaultImplicitNotFoundMessage =
2920-
i"No given instance of type $pt was found${location("for")}"
2928+
val bestStr = if arg.tpe.isInstanceOf[AmbiguousImplicits] then " best" else ""
2929+
i"No$bestStr given instance of type $pt was found${location("for")}"
29212930

29222931
/** Construct a custom error message given an ambiguous implicit
29232932
* 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)