Skip to content

Commit c83442a

Browse files
authored
Backport "Suggest imports for the expected type of the underlying implicit not found error" to LTS (#19028)
Backports #17976 to the LTS branch. PR submitted by the release tooling. [skip ci]
2 parents 34386f4 + 140d5db commit c83442a

File tree

6 files changed

+73
-1
lines changed

6 files changed

+73
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2848,10 +2848,17 @@ class MissingImplicitArgument(
28482848
i"The following implicits in scope can be implicitly converted to ${pt.show}:" +
28492849
ignoredConvertibleImplicits.map { imp => s"\n- ${imp.symbol.showDcl}"}.mkString
28502850
)
2851+
def importSuggestionAddendum: String =
2852+
arg.tpe match
2853+
// If the failure was caused by an underlying NoMatchingImplicits, compute the addendum for its expected type
2854+
case noMatching: NoMatchingImplicits => // FIXME also handle SynthesisFailure
2855+
ctx.typer.importSuggestionAddendum(noMatching.expectedType)
2856+
case _ =>
2857+
ctx.typer.importSuggestionAddendum(pt)
28512858
super.msgPostscript
28522859
++ ignoredInstanceNormalImport.map(hiddenImplicitNote)
28532860
.orElse(noChainConversionsNote(ignoredConvertibleImplicits))
2854-
.getOrElse(ctx.typer.importSuggestionAddendum(pt))
2861+
.getOrElse(importSuggestionAddendum)
28552862

28562863
def explain(using Context) = userDefinedImplicitNotFoundMessage(explain = true)
28572864
.getOrElse("")

tests/neg/i8827a.check

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- [E172] Type Error: tests/neg/i8827a.scala:16:26 ---------------------------------------------------------------------
2+
16 | summon[Order[List[Foo]]] // error
3+
| ^
4+
| No given instance of type pkg.Order[List[pkg.Foo]] was found for parameter x of method summon in object Predef.
5+
| I found:
6+
|
7+
| pkg.Order.orderList[pkg.Foo](/* missing */summon[pkg.Order[pkg.Foo]])
8+
|
9+
| But no implicit values were found that match type pkg.Order[pkg.Foo].
10+
|
11+
| The following import might fix the problem:
12+
|
13+
| import pkg.Implicits.orderFoo
14+
|

tests/neg/i8827a.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package pkg
2+
3+
trait Order[A]
4+
5+
object Order {
6+
implicit def orderList[A](implicit orderA: Order[A]): Order[List[A]] = ???
7+
}
8+
9+
class Foo
10+
11+
object Implicits {
12+
implicit def orderFoo: Order[Foo] = ???
13+
}
14+
15+
@main def main: Unit =
16+
summon[Order[List[Foo]]] // error

tests/neg/i8827b.check

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- [E172] Type Error: tests/neg/i8827b.scala:16:28 ---------------------------------------------------------------------
2+
16 | summon[Order[Option[Foo]]] // error
3+
| ^
4+
|No given instance of type pkg.Order[Option[pkg.Foo]] was found for parameter x of method summon in object Predef.
5+
|I found:
6+
|
7+
| pkg.Order.given_Order_Option[pkg.Foo](/* missing */summon[pkg.Order[pkg.Foo]])
8+
|
9+
|But no implicit values were found that match type pkg.Order[pkg.Foo].
10+
|
11+
|The following import might fix the problem:
12+
|
13+
| import pkg.Givens.orderFoo
14+
|

tests/neg/i8827b.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package pkg
2+
3+
trait Order[A]
4+
5+
object Order {
6+
given [A](using orderA: Order[A]): Order[Option[A]] = ???
7+
}
8+
9+
class Foo
10+
11+
object Givens {
12+
given orderFoo: Order[Foo] = ???
13+
}
14+
15+
@main def main: Unit =
16+
summon[Order[Option[Foo]]] // error

tests/neg/missing-implicit3.check

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,8 @@
77
| ord.Ord.ordered[ord.Foo](/* missing */summon[ord.Foo => Comparable[? >: ord.Foo]])
88
|
99
| 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 scala.math.Ordered.orderingToOrdered
14+
|

0 commit comments

Comments
 (0)