Skip to content

Hint about forbidden combination of implicit values and conversions #16735

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 19 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from 10 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
10 changes: 9 additions & 1 deletion compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2554,7 +2554,8 @@ class MissingImplicitArgument(
pt: Type,
where: String,
paramSymWithMethodCallTree: Option[(Symbol, tpd.Tree)] = None,
ignoredInstanceNormalImport: => Option[SearchSuccess]
ignoredInstanceNormalImport: => Option[SearchSuccess],
ignoredConversions: => Iterable[TermRef]
)(using Context) extends TypeMsg(MissingImplicitArgumentID), ShowMatchTrace(pt):

arg.tpe match
Expand Down Expand Up @@ -2743,8 +2744,15 @@ class MissingImplicitArgument(
// show all available additional info
def hiddenImplicitNote(s: SearchSuccess) =
i"\n\nNote: ${s.ref.symbol.showLocated} was not considered because it was not imported with `import given`."
def noChainConversionsNote(ignoredConversions: Iterable[TermRef]): Option[String] =
Option.when(ignoredConversions.nonEmpty)(
i"\n\nNote: implicit conversions are not automatically applied to arguments of using clauses. " +
i"You will have to pass the argument explicitly.\n" +
i"The following conversions in scope result in ${pt.show}: ${ignoredConversions.map(g => s"\n - ${g.symbol.showDcl}").mkString}"
)
super.msgPostscript
++ ignoredInstanceNormalImport.map(hiddenImplicitNote)
.orElse(noChainConversionsNote(ignoredConversions))
.getOrElse(ctx.typer.importSuggestionAddendum(pt))

def explain(using Context) = ""
Expand Down
10 changes: 9 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,15 @@ trait Implicits:
// example where searching for a nested type causes an infinite loop.
None

MissingImplicitArgument(arg, pt, where, paramSymWithMethodCallTree, ignoredInstanceNormalImport)
def ignoredConversions = arg.tpe match
case fail: SearchFailureType =>
if (fail.expectedType eq pt) || isFullyDefined(fail.expectedType, ForceDegree.none) then
ctx.implicits.eligible(ViewProto(WildcardType, wildApprox(fail.expectedType)))
.collect { case c if c.isConversion => c.ref }
else
Nil

MissingImplicitArgument(arg, pt, where, paramSymWithMethodCallTree, ignoredInstanceNormalImport, ignoredConversions)
}

/** A string indicating the formal parameter corresponding to a missing argument */
Expand Down
18 changes: 18 additions & 0 deletions tests/neg/i16453.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- [E172] Type Error: tests/neg/i16453.scala:14:21 ---------------------------------------------------------------------
14 | summon[Option[Int]] // error
| ^
|No given instance of type Option[Int] was found for parameter x of method summon in object Predef
|
|Note: implicit conversions are not automatically applied to arguments of using clauses. You will have to pass the argument explicitly.
|The following conversions in scope result in Option[Int]:
| - final given def given_Conversion_T_Option[T]: Conversion[T, Option[T]]
| - implicit def toOption[T](t: T): Option[T]
-- [E172] Type Error: tests/neg/i16453.scala:15:25 ---------------------------------------------------------------------
15 | implicitly[Option[Int]] // error
| ^
|No given instance of type Option[Int] was found for parameter e of method implicitly in object Predef
|
|Note: implicit conversions are not automatically applied to arguments of using clauses. You will have to pass the argument explicitly.
|The following conversions in scope result in Option[Int]:
| - final given def given_Conversion_T_Option[T]: Conversion[T, Option[T]]
| - implicit def toOption[T](t: T): Option[T]
16 changes: 16 additions & 0 deletions tests/neg/i16453.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import scala.language.implicitConversions

// Scala 3 style conversion
given [T]: Conversion[T, Option[T]] = ???
// Scala 2 style conversion
implicit def toOption[T](t: T): Option[T] = Option(t)

// This one is irrelevant, shouldn't be included in error message
given irrelevant: Conversion[Int, Option[Long]] = ???

def test() = {
given foo: Int = 0

summon[Option[Int]] // error
implicitly[Option[Int]] // error
}