Skip to content

Commit 588ac1b

Browse files
authored
Merge pull request #8611 from scalacenter/replace-implicit-mismatch-with-implicit-not-found
Turn mismatch given errors into given not found errors
2 parents 911093c + d525c59 commit 588ac1b

10 files changed

+60
-19
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ class Definitions {
391391
@tu lazy val Predef_undefined: Symbol = ScalaPredefModule.requiredMethod(nme.???)
392392

393393
def SubTypeClass(implicit ctx: Context): ClassSymbol = ctx.requiredClass("scala.<:<")
394+
@tu lazy val SubType_refl: Symbol = SubTypeClass.companionModule.requiredMethod(nme.refl)
394395

395396
def DummyImplicitClass(implicit ctx: Context): ClassSymbol = ctx.requiredClass("scala.DummyImplicit")
396397

compiler/src/dotty/tools/dotc/core/StdNames.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ object StdNames {
549549
val productIterator: N = "productIterator"
550550
val productPrefix: N = "productPrefix"
551551
val raw_ : N = "raw"
552+
val refl: N = "refl"
552553
val reflect: N = "reflect"
553554
val reflectiveSelectable: N = "reflectiveSelectable"
554555
val reify : N = "reify"

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -969,11 +969,15 @@ trait Implicits { self: Typer =>
969969
}
970970
if (ctx.reporter.hasErrors) {
971971
ctx.reporter.removeBufferedMessages
972-
SearchFailure {
973-
adapted.tpe match {
974-
case _: SearchFailureType => adapted
975-
case _ => adapted.withType(new MismatchedImplicit(ref, pt, argument))
976-
}
972+
adapted.tpe match {
973+
case _: SearchFailureType => SearchFailure(adapted)
974+
case _ =>
975+
// Special case for `$conforms` and `<:<.refl`. Showing them to the users brings
976+
// no value, so we instead report a `NoMatchingImplicitsFailure`
977+
if (adapted.symbol == defn.Predef_conforms || adapted.symbol == defn.SubType_refl)
978+
NoMatchingImplicitsFailure
979+
else
980+
SearchFailure(adapted.withType(new MismatchedImplicit(ref, pt, argument)))
977981
}
978982
}
979983
else {

tests/neg/implicitSearch.check

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-- Error: tests/neg/implicitSearch.scala:13:12 -------------------------------------------------------------------------
2+
13 | sort(xs) // error (with a partially constructed implicit argument shown)
3+
| ^
4+
| no implicit argument of type Test.Ord[List[List[T]]] was found for parameter o of method sort in object Test.
5+
| I found:
6+
|
7+
| Test.listOrd[T](Test.listOrd[T](/* missing */implicitly[Test.Ord[T]]))
8+
|
9+
| But no implicit values were found that match type Test.Ord[T].
10+
-- Error: tests/neg/implicitSearch.scala:15:38 -------------------------------------------------------------------------
11+
15 | listOrd(listOrd(implicitly[Ord[T]] /*not found*/)) // error
12+
| ^
13+
| no implicit argument of type Test.Ord[T] was found for parameter ev of method implicitly in object DottyPredef

tests/neg/implicitSearch.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
object Test {
22

3-
type T = String
4-
53
class Ord[T]
64
implicit def listOrd[T](implicit o: Ord[T]): Ord[List[T]] = ???
75
implicit def intOrd: Ord[Int] = ???

tests/neg/missing-implicit3.check

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- Error: tests/neg/missing-implicit3.scala:13:36 ----------------------------------------------------------------------
2+
13 |val sortedFoos = sort(List(new Foo)) // error
3+
| ^
4+
|no implicit argument of type ord.Ord[ord.Foo] was found for an implicit parameter of method sort in package ord.
5+
|I found:
6+
|
7+
| ord.Ord.ordered[A](/* missing */implicitly[ord.Foo => Comparable[? >: ord.Foo]])
8+
|
9+
|But no implicit values were found that match type ord.Foo => Comparable[? >: ord.Foo].
10+
|
11+
|The following import might make progress towards fixing the problem:
12+
|
13+
| import ord.Ord.ordered
14+
|

tests/neg/missing-implicit3.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package ord
2+
3+
trait Ord[A]
4+
5+
object Ord {
6+
given ordered[A](using A => java.lang.Comparable[? >: A]) as Ord[A] = ???
7+
}
8+
9+
def sort[A: Ord](as: List[A]): List[A] = ???
10+
11+
class Foo
12+
13+
val sortedFoos = sort(List(new Foo)) // error

tests/neg/subtyping.check

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
-- Error: tests/neg/subtyping.scala:8:27 -------------------------------------------------------------------------------
22
8 | implicitly[B#X <:< A#X] // error: no implicit argument
33
| ^
4-
| Cannot prove that B#X <:< A#X..
5-
| I found:
6-
|
7-
| <:<.refl[Nothing]
8-
|
9-
| But method refl in object <:< does not match type B#X <:< A#X.
4+
| Cannot prove that B#X <:< A#X.
105
-- Error: tests/neg/subtyping.scala:12:27 ------------------------------------------------------------------------------
116
12 | implicitly[a.T <:< a.U] // error: no implicit argument
127
| ^
13-
| Cannot prove that a.T <:< a.U..
14-
| I found:
15-
|
16-
| <:<.refl[Nothing]
17-
|
18-
| But method refl in object <:< does not match type a.T <:< a.U.
8+
| Cannot prove that a.T <:< a.U.

tests/neg/summon-function.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- Error: tests/neg/summon-function.scala:2:23 -------------------------------------------------------------------------
2+
2 | summon[Int => String] // error
3+
| ^
4+
| No implicit view available from Int => String.

tests/neg/summon-function.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object Test {
2+
summon[Int => String] // error
3+
}

0 commit comments

Comments
 (0)