Skip to content

Find implicitNotFoundMessage on types #11823

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
Mar 22, 2021
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
35 changes: 24 additions & 11 deletions compiler/src/dotty/tools/dotc/typer/ErrorReporting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -375,17 +375,30 @@ class ImplicitSearchError(
* def foo(implicit foo: Foo): Any = ???
*/
private def userDefinedImplicitNotFoundTypeMessage: Option[String] =
pt.baseClasses.iterator
// Don't inherit "No implicit view available..." message if subtypes of Function1 are not treated as implicit conversions anymore
.filter(sym => Feature.migrateTo3 || sym != defn.Function1)
.map(userDefinedImplicitNotFoundTypeMessage(_))
.find(_.isDefined).flatten

private def userDefinedImplicitNotFoundTypeMessage(classSym: ClassSymbol): Option[String] =
userDefinedMsg(classSym, defn.ImplicitNotFoundAnnot).map { rawMsg =>
val substituteType = (_: Type).asSeenFrom(pt, classSym)
formatAnnotationMessage(rawMsg, classSym, substituteType)
}
def recur(tp: Type): Option[String] = tp match
case tp: TypeRef =>
val sym = tp.symbol
userDefinedImplicitNotFoundTypeMessage(sym).orElse(recur(tp.info))
case tp: ClassInfo =>
tp.baseClasses.iterator
.map(userDefinedImplicitNotFoundTypeMessage)
.find(_.isDefined).flatten
case tp: TypeProxy =>
recur(tp.underlying)
case tp: AndType =>
recur(tp.tp1).orElse(recur(tp.tp2))
case _ =>
None
recur(pt)

private def userDefinedImplicitNotFoundTypeMessage(sym: Symbol): Option[String] =
for
rawMsg <- userDefinedMsg(sym, defn.ImplicitNotFoundAnnot)
if Feature.migrateTo3 || sym != defn.Function1
// Don't inherit "No implicit view available..." message if subtypes of Function1 are not treated as implicit conversions anymore
yield
val substituteType = (_: Type).asSeenFrom(pt, sym)
formatAnnotationMessage(rawMsg, sym, substituteType)

private def hiddenImplicitsAddendum: String =
def hiddenImplicitNote(s: SearchSuccess) =
Expand Down
4 changes: 4 additions & 0 deletions tests/neg/i11797.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Error: tests/neg/i11797.scala:6:17 ----------------------------------------------------------------------------------
6 | summon[Foo.Bar] // error
| ^
| Oops
6 changes: 6 additions & 0 deletions tests/neg/i11797.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
object Foo:
@annotation.implicitNotFound("Oops")
type Bar
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to also add a test with an opaque type? Are opaque types just represented as a TypeRef like transparent types?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes that's the same


@main def run(): Unit =
summon[Foo.Bar] // error