Skip to content

Commit d329c13

Browse files
authored
Merge pull request #10190 from prolativ/implicitNotFound-inheritance
Fix #10098: Handle inheritance for implicitNotFound annotation
2 parents cbc5210 + e5a1da5 commit d329c13

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ class ImplicitSearchError(
250250
*/
251251
private def userDefinedMsg(sym: Symbol, cls: Symbol) = for {
252252
ann <- sym.getAnnotation(cls)
253-
Trees.Literal(Constant(msg: String)) <- ann.argument(0)
253+
msg <- ann.argumentConstantString(0)
254254
} yield msg
255255

256256
private def location(preposition: String) = if (where.isEmpty) "" else s" $preposition $where"
@@ -313,7 +313,7 @@ class ImplicitSearchError(
313313
*
314314
* def foo(implicit @annotation.implicitNotFound("Foo is missing") foo: Foo): Any = ???
315315
*/
316-
private def userDefinedImplicitNotFoundParamMessage = paramSymWithMethodCallTree.flatMap { (sym, applTree) =>
316+
private def userDefinedImplicitNotFoundParamMessage: Option[String] = paramSymWithMethodCallTree.flatMap { (sym, applTree) =>
317317
userDefinedMsg(sym, defn.ImplicitNotFoundAnnot).map { rawMsg =>
318318
val (fn, targs, _) = tpd.decomposeCall(applTree)
319319
val methodOwner = fn.symbol.owner
@@ -332,8 +332,12 @@ class ImplicitSearchError(
332332
*
333333
* def foo(implicit foo: Foo): Any = ???
334334
*/
335-
private def userDefinedImplicitNotFoundTypeMessage =
336-
val classSym = pt.classSymbol
335+
private def userDefinedImplicitNotFoundTypeMessage: Option[String] =
336+
pt.baseClasses.iterator
337+
.map(userDefinedImplicitNotFoundTypeMessage(_))
338+
.find(_.isDefined).flatten
339+
340+
private def userDefinedImplicitNotFoundTypeMessage(classSym: ClassSymbol): Option[String] =
337341
userDefinedMsg(classSym, defn.ImplicitNotFoundAnnot).map { rawMsg =>
338342
val substituteType = (_: Type).asSeenFrom(pt, classSym)
339343
formatAnnotationMessage(rawMsg, classSym, substituteType)

tests/neg/i10098.check

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
-- Error: tests/neg/i10098.scala:20:32 ---------------------------------------------------------------------------------
2+
20 | implicitly[Bar12[Int, String]] // error
3+
| ^
4+
| There's no Foo2[String, Int]
5+
-- Error: tests/neg/i10098.scala:21:32 ---------------------------------------------------------------------------------
6+
21 | implicitly[Bar21[Int, String]] // error
7+
| ^
8+
| There's no Foo1[String, Int]
9+
-- Error: tests/neg/i10098.scala:22:32 ---------------------------------------------------------------------------------
10+
22 | implicitly[Baz12[Int, String]] // error
11+
| ^
12+
| There's no Baz12[Int, String]
13+
-- Error: tests/neg/i10098.scala:23:32 ---------------------------------------------------------------------------------
14+
23 | implicitly[Baz21[Int, String]] // error
15+
| ^
16+
| There's no Baz21[Int, String]

tests/neg/i10098.scala

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import annotation.implicitNotFound
2+
3+
@implicitNotFound("There's no Foo1[${A}, ${B}]")
4+
trait Foo1[A, B]
5+
6+
@implicitNotFound("There's no Foo2[${A}, ${B}]")
7+
trait Foo2[A, B]
8+
9+
trait Bar12[C, D] extends Foo1[D, C] with Foo2[D, C]
10+
11+
trait Bar21[C, D] extends Foo2[D, C] with Foo1[D, C]
12+
13+
@implicitNotFound("There's no Baz12[${C}, ${D}]")
14+
trait Baz12[C, D] extends Foo1[D, C] with Foo2[D, C]
15+
16+
@implicitNotFound("There's no Baz21[${C}, ${D}]")
17+
trait Baz21[C, D] extends Foo2[D, C] with Foo1[D, C]
18+
19+
object Test {
20+
implicitly[Bar12[Int, String]] // error
21+
implicitly[Bar21[Int, String]] // error
22+
implicitly[Baz12[Int, String]] // error
23+
implicitly[Baz21[Int, String]] // error
24+
}

tests/neg/i4986c.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ class Outer[A] {
1616

1717
trait X$Y
1818

19-
@implicitNotFound("There's no U[${X}, ${Y}, ${Z}]")
19+
@implicitNotFound(msg = "There's no U[${X}, ${Y}, ${Z}]")
2020
trait U[X, Y[_], Z[_, ZZ]] {
2121
class I[R] {
2222
def m[S](implicit @implicitNotFound("${X}; ${Y}; ${ Z }; ${R}; ${S}; ${XX}") i: Int) = ???
2323
}
2424
}
2525

2626
class Test[A] {
27-
def f(implicit @implicitNotFound("Missing X$Y for Test[${A}]") xy: X$Y) = ???
27+
def f(implicit @implicitNotFound(msg = "Missing X$Y for Test[${A}]") xy: X$Y) = ???
2828
def g[B: Outer] = ???
2929
def h[B](implicit outer: Outer[B]) = ???
3030
def i[B](implicit @implicitNotFound("Missing implicit outer param of type Outer[${B}] for Test[${A}]") outer: Outer[B]) = ???

0 commit comments

Comments
 (0)