From a0dd67d075e149f4d1d6b520c662c24656e531f5 Mon Sep 17 00:00:00 2001 From: Julien Richard-Foy Date: Wed, 14 Jun 2023 15:58:34 +0200 Subject: [PATCH] Suggest imports for the expected type of the underlying implicit not found error We used to suggest imports for the outermost type of a chain of implicits. Now, we suggest imports for the `expectedType` of the underlying `NoMatchingImplicits`. Fixes #8827 --- .../dotty/tools/dotc/reporting/messages.scala | 9 ++++++++- tests/neg/i8827a.check | 14 ++++++++++++++ tests/neg/i8827a.scala | 16 ++++++++++++++++ tests/neg/i8827b.check | 14 ++++++++++++++ tests/neg/i8827b.scala | 16 ++++++++++++++++ tests/neg/missing-implicit3.check | 5 +++++ 6 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 tests/neg/i8827a.check create mode 100644 tests/neg/i8827a.scala create mode 100644 tests/neg/i8827b.check create mode 100644 tests/neg/i8827b.scala diff --git a/compiler/src/dotty/tools/dotc/reporting/messages.scala b/compiler/src/dotty/tools/dotc/reporting/messages.scala index 0218d8a9c915..104304c3409c 100644 --- a/compiler/src/dotty/tools/dotc/reporting/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/messages.scala @@ -2848,10 +2848,17 @@ class MissingImplicitArgument( i"The following implicits in scope can be implicitly converted to ${pt.show}:" + ignoredConvertibleImplicits.map { imp => s"\n- ${imp.symbol.showDcl}"}.mkString ) + def importSuggestionAddendum: String = + arg.tpe match + // If the failure was caused by an underlying NoMatchingImplicits, compute the addendum for its expected type + case noMatching: NoMatchingImplicits => // FIXME also handle SynthesisFailure + ctx.typer.importSuggestionAddendum(noMatching.expectedType) + case _ => + ctx.typer.importSuggestionAddendum(pt) super.msgPostscript ++ ignoredInstanceNormalImport.map(hiddenImplicitNote) .orElse(noChainConversionsNote(ignoredConvertibleImplicits)) - .getOrElse(ctx.typer.importSuggestionAddendum(pt)) + .getOrElse(importSuggestionAddendum) def explain(using Context) = userDefinedImplicitNotFoundMessage(explain = true) .getOrElse("") diff --git a/tests/neg/i8827a.check b/tests/neg/i8827a.check new file mode 100644 index 000000000000..3d6c2bfa500b --- /dev/null +++ b/tests/neg/i8827a.check @@ -0,0 +1,14 @@ +-- [E172] Type Error: tests/neg/i8827a.scala:16:26 --------------------------------------------------------------------- +16 | summon[Order[List[Foo]]] // error + | ^ + | No given instance of type pkg.Order[List[pkg.Foo]] was found for parameter x of method summon in object Predef. + | I found: + | + | pkg.Order.orderList[pkg.Foo](/* missing */summon[pkg.Order[pkg.Foo]]) + | + | But no implicit values were found that match type pkg.Order[pkg.Foo]. + | + | The following import might fix the problem: + | + | import pkg.Implicits.orderFoo + | diff --git a/tests/neg/i8827a.scala b/tests/neg/i8827a.scala new file mode 100644 index 000000000000..428028aa9448 --- /dev/null +++ b/tests/neg/i8827a.scala @@ -0,0 +1,16 @@ +package pkg + +trait Order[A] + +object Order { + implicit def orderList[A](implicit orderA: Order[A]): Order[List[A]] = ??? +} + +class Foo + +object Implicits { + implicit def orderFoo: Order[Foo] = ??? +} + +@main def main: Unit = + summon[Order[List[Foo]]] // error diff --git a/tests/neg/i8827b.check b/tests/neg/i8827b.check new file mode 100644 index 000000000000..6848c53aee28 --- /dev/null +++ b/tests/neg/i8827b.check @@ -0,0 +1,14 @@ +-- [E172] Type Error: tests/neg/i8827b.scala:16:28 --------------------------------------------------------------------- +16 | summon[Order[Option[Foo]]] // error + | ^ + |No given instance of type pkg.Order[Option[pkg.Foo]] was found for parameter x of method summon in object Predef. + |I found: + | + | pkg.Order.given_Order_Option[pkg.Foo](/* missing */summon[pkg.Order[pkg.Foo]]) + | + |But no implicit values were found that match type pkg.Order[pkg.Foo]. + | + |The following import might fix the problem: + | + | import pkg.Givens.orderFoo + | diff --git a/tests/neg/i8827b.scala b/tests/neg/i8827b.scala new file mode 100644 index 000000000000..283dd6b6d481 --- /dev/null +++ b/tests/neg/i8827b.scala @@ -0,0 +1,16 @@ +package pkg + +trait Order[A] + +object Order { + given [A](using orderA: Order[A]): Order[Option[A]] = ??? +} + +class Foo + +object Givens { + given orderFoo: Order[Foo] = ??? +} + +@main def main: Unit = + summon[Order[Option[Foo]]] // error diff --git a/tests/neg/missing-implicit3.check b/tests/neg/missing-implicit3.check index ab87bf99a32a..85201b3a772f 100644 --- a/tests/neg/missing-implicit3.check +++ b/tests/neg/missing-implicit3.check @@ -7,3 +7,8 @@ | ord.Ord.ordered[ord.Foo](/* missing */summon[ord.Foo => Comparable[? >: ord.Foo]]) | | But no implicit values were found that match type ord.Foo => Comparable[? >: ord.Foo]. + | + | The following import might make progress towards fixing the problem: + | + | import scala.math.Ordered.orderingToOrdered + |