Skip to content

Commit 27bd546

Browse files
committed
Find implicitNotFoundMessage on types
Fixes #11797
1 parent 422ffbc commit 27bd546

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

compiler/src/dotty/tools/dotc/typer/ErrorReporting.scala

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -375,17 +375,30 @@ class ImplicitSearchError(
375375
* def foo(implicit foo: Foo): Any = ???
376376
*/
377377
private def userDefinedImplicitNotFoundTypeMessage: Option[String] =
378-
pt.baseClasses.iterator
379-
// Don't inherit "No implicit view available..." message if subtypes of Function1 are not treated as implicit conversions anymore
380-
.filter(sym => Feature.migrateTo3 || sym != defn.Function1)
381-
.map(userDefinedImplicitNotFoundTypeMessage(_))
382-
.find(_.isDefined).flatten
383-
384-
private def userDefinedImplicitNotFoundTypeMessage(classSym: ClassSymbol): Option[String] =
385-
userDefinedMsg(classSym, defn.ImplicitNotFoundAnnot).map { rawMsg =>
386-
val substituteType = (_: Type).asSeenFrom(pt, classSym)
387-
formatAnnotationMessage(rawMsg, classSym, substituteType)
388-
}
378+
def recur(tp: Type): Option[String] = tp match
379+
case tp: TypeRef =>
380+
val sym = tp.symbol
381+
userDefinedImplicitNotFoundTypeMessage(sym).orElse(recur(tp.info))
382+
case tp: ClassInfo =>
383+
tp.baseClasses.iterator
384+
.map(userDefinedImplicitNotFoundTypeMessage)
385+
.find(_.isDefined).flatten
386+
case tp: TypeProxy =>
387+
recur(tp.underlying)
388+
case tp: AndType =>
389+
recur(tp.tp1).orElse(recur(tp.tp2))
390+
case _ =>
391+
None
392+
recur(pt)
393+
394+
private def userDefinedImplicitNotFoundTypeMessage(sym: Symbol): Option[String] =
395+
for
396+
rawMsg <- userDefinedMsg(sym, defn.ImplicitNotFoundAnnot)
397+
if Feature.migrateTo3 || sym != defn.Function1
398+
// Don't inherit "No implicit view available..." message if subtypes of Function1 are not treated as implicit conversions anymore
399+
yield
400+
val substituteType = (_: Type).asSeenFrom(pt, sym)
401+
formatAnnotationMessage(rawMsg, sym, substituteType)
389402

390403
private def hiddenImplicitsAddendum: String =
391404
def hiddenImplicitNote(s: SearchSuccess) =

tests/neg/i11797.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- Error: tests/neg/i11797.scala:6:17 ----------------------------------------------------------------------------------
2+
6 | summon[Foo.Bar] // error
3+
| ^
4+
| Oops

tests/neg/i11797.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
object Foo:
2+
@annotation.implicitNotFound("Oops")
3+
type Bar
4+
5+
@main def run(): Unit =
6+
summon[Foo.Bar] // error

0 commit comments

Comments
 (0)