Skip to content

Suggest imports for the expected type of the underlying implicit not found error #17976

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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("")
Expand Down
14 changes: 14 additions & 0 deletions tests/neg/i8827a.check
Original file line number Diff line number Diff line change
@@ -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
|
16 changes: 16 additions & 0 deletions tests/neg/i8827a.scala
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions tests/neg/i8827b.check
Original file line number Diff line number Diff line change
@@ -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
|
16 changes: 16 additions & 0 deletions tests/neg/i8827b.scala
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions tests/neg/missing-implicit3.check
Original file line number Diff line number Diff line change
Expand Up @@ -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
|