Skip to content

Commit cbe5d9c

Browse files
committed
Reorganise code
1 parent d656ff2 commit cbe5d9c

File tree

3 files changed

+144
-127
lines changed

3 files changed

+144
-127
lines changed

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

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ trait Implicits { self: Typer =>
546546

547547
/** If `formal` is of the form ClassTag[T], where `T` is a class type,
548548
* synthesize a class tag for `T`.
549-
*/
549+
*/
550550
def synthesizedClassTag(formal: Type): Tree = formal.argInfos match {
551551
case arg :: Nil =>
552552
fullyDefinedType(arg, "ClassTag argument", pos) match {
@@ -591,7 +591,7 @@ trait Implicits { self: Typer =>
591591

592592
/** If `formal` is of the form Eq[T, U], where no `Eq` instance exists for
593593
* either `T` or `U`, synthesize `Eq.eqAny[T, U]` as solution.
594-
*/
594+
*/
595595
def synthesizedEq(formal: Type)(implicit ctx: Context): Tree = {
596596
//println(i"synth eq $formal / ${formal.argTypes}%, %")
597597
formal.argTypes match {
@@ -686,23 +686,21 @@ trait Implicits { self: Typer =>
686686
}
687687
}
688688
def location(preposition: String) = if (where.isEmpty) "" else s" $preposition $where"
689-
def userDefinedMessage(annot: Annotation, params: List[String], args: List[Type]): Option[String] =
690-
for (Trees.Literal(Constant(raw: String)) <- annot.argument(0)) yield {
691-
err.userDefinedErrorString(
692-
raw,
693-
params,
694-
args)
695-
}
689+
690+
def userDefinedMsg(sym: Symbol, cls: Symbol) = for {
691+
ann <- sym.getAnnotation(cls)
692+
Trees.Literal(Constant(msg: String)) <- ann.argument(0)
693+
} yield msg
694+
695+
696696
arg.tpe match {
697697
case ambi: AmbiguousImplicits =>
698-
val maybeAnnot = ambi.alt1.ref.symbol.getAnnotation(defn.ImplicitAmbiguousAnnot).map(
699-
(_, ambi.alt1)
700-
).orElse(
701-
ambi.alt2.ref.symbol.getAnnotation(defn.ImplicitAmbiguousAnnot).map(
702-
(_, ambi.alt2)
703-
)
704-
)
705-
val userDefined = maybeAnnot.flatMap { case (annot, alt) =>
698+
object AmbiguousImplicitMsg {
699+
def unapply(search: SearchSuccess) =
700+
userDefinedMsg(search.ref.symbol, defn.ImplicitAmbiguousAnnot)
701+
}
702+
703+
def userDefinedAmbiguousImplicitMsg(alt: SearchSuccess, raw: String) = {
706704
val params = alt.ref.underlying match {
707705
case p: PolyType => p.paramNames.map(_.toString)
708706
case _ => Nil
@@ -717,16 +715,25 @@ trait Implicits { self: Typer =>
717715
case _ =>
718716
Nil
719717
}
720-
userDefinedMessage(annot, params, args)
718+
err.userDefinedErrorString(raw, params, args)
719+
}
720+
721+
(ambi.alt1, ambi.alt2) match {
722+
case (AmbiguousImplicitMsg(msg), _) =>
723+
userDefinedAmbiguousImplicitMsg(ambi.alt1, msg)
724+
case (_, AmbiguousImplicitMsg(msg)) =>
725+
userDefinedAmbiguousImplicitMsg(ambi.alt2, msg)
726+
case _ =>
727+
msg(s"ambiguous implicit arguments: ${ambi.explanation}${location("of")}")(
728+
s"ambiguous implicit arguments of type ${pt.show} found${location("for")}")
721729
}
722-
userDefined.map(msg(_)()).getOrElse(
723-
msg(s"ambiguous implicit arguments: ${ambi.explanation}${location("of")}")(
724-
s"ambiguous implicit arguments of type ${pt.show} found${location("for")}")
725-
)
730+
726731
case _ =>
727-
val userDefined = pt.typeSymbol.getAnnotation(defn.ImplicitNotFoundAnnot).flatMap(
728-
userDefinedMessage(_, pt.typeSymbol.typeParams.map(_.name.unexpandedName.toString), pt.argInfos)
729-
)
732+
val userDefined = userDefinedMsg(pt.typeSymbol, defn.ImplicitNotFoundAnnot).map(raw =>
733+
err.userDefinedErrorString(
734+
raw,
735+
pt.typeSymbol.typeParams.map(_.name.unexpandedName.toString),
736+
pt.argInfos))
730737
msg(userDefined.getOrElse(em"no implicit argument of type $pt was found${location("for")}"))()
731738
}
732739
}

compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala

Lines changed: 0 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,106 +1306,4 @@ class ErrorMessagesTests extends ErrorMessagesTest {
13061306
val DoubleDeclaration(symbol, previousSymbol) :: Nil = messages
13071307
assertEquals(symbol.name.mangledString, "a")
13081308
}
1309-
1310-
@Test def userDefinedImplicitAmbiguous1 =
1311-
checkMessagesAfter("frontend") {
1312-
"""
1313-
|object Test {
1314-
| trait =!=[C, D]
1315-
|
1316-
| implicit def neq[E, F] : E =!= F = null
1317-
|
1318-
| @annotation.implicitAmbiguous("Could not prove ${J} =!= ${J}")
1319-
| implicit def neqAmbig1[G, H, J] : J =!= J = null
1320-
| implicit def neqAmbig2[I] : I =!= I = null
1321-
|
1322-
| implicitly[Int =!= Int]
1323-
|}
1324-
1325-
""".stripMargin
1326-
}.expect { (itcx, messages) =>
1327-
import diagnostic.NoExplanation
1328-
implicit val ctx: Context = itcx
1329-
1330-
assertMessageCount(1, messages)
1331-
val (m: NoExplanation) :: Nil = messages
1332-
1333-
assertEquals(m.msg, "Could not prove Int =!= Int")
1334-
}
1335-
1336-
@Test def userDefinedImplicitAmbiguous2 =
1337-
checkMessagesAfter("frontend") {
1338-
"""
1339-
|object Test {
1340-
| trait =!=[C, D]
1341-
|
1342-
| implicit def neq[E, F] : E =!= F = null
1343-
|
1344-
| implicit def neqAmbig1[G, H, J] : J =!= J = null
1345-
| @annotation.implicitAmbiguous("Could not prove ${I} =!= ${I}")
1346-
| implicit def neqAmbig2[I] : I =!= I = null
1347-
|
1348-
| implicitly[Int =!= Int]
1349-
|}
1350-
1351-
""".stripMargin
1352-
}.expect { (itcx, messages) =>
1353-
import diagnostic.NoExplanation
1354-
implicit val ctx: Context = itcx
1355-
1356-
assertMessageCount(1, messages)
1357-
val (m: NoExplanation) :: Nil = messages
1358-
1359-
assertEquals(m.msg, "Could not prove Int =!= Int")
1360-
}
1361-
1362-
@Test def userDefinedImplicitAmbiguous3 =
1363-
checkMessagesAfter("frontend") {
1364-
"""
1365-
|object Test {
1366-
| trait =!=[C, D]
1367-
|
1368-
| implicit def neq[E, F] : E =!= F = null
1369-
|
1370-
| @annotation.implicitAmbiguous("Could not prove ${J} =!= ${J}")
1371-
| implicit def neqAmbig1[G, H, J] : J =!= J = null
1372-
| @annotation.implicitAmbiguous("Could not prove ${I} =!= ${I}")
1373-
| implicit def neqAmbig2[I] : I =!= I = null
1374-
|
1375-
| implicitly[Int =!= Int]
1376-
|}
1377-
1378-
""".stripMargin
1379-
}.expect { (itcx, messages) =>
1380-
import diagnostic.NoExplanation
1381-
implicit val ctx: Context = itcx
1382-
1383-
assertMessageCount(1, messages)
1384-
val (m: NoExplanation) :: Nil = messages
1385-
1386-
assertEquals(m.msg, "Could not prove Int =!= Int")
1387-
}
1388-
1389-
@Test def userDefinedImplicitAmbiguous4 =
1390-
checkMessagesAfter("frontend") {
1391-
"""
1392-
|class C {
1393-
| @annotation.implicitAmbiguous("msg A=${A}")
1394-
| implicit def f[A](x: Int): String = "f was here"
1395-
| implicit def g(x: Int): String = "f was here"
1396-
| def test: Unit = {
1397-
| implicitly[Int => String]
1398-
| }
1399-
|}
1400-
1401-
""".stripMargin
1402-
}.expect { (itcx, messages) =>
1403-
import diagnostic.NoExplanation
1404-
implicit val ctx: Context = itcx
1405-
1406-
assertMessageCount(1, messages)
1407-
val (m: NoExplanation) :: Nil = messages
1408-
1409-
assertEquals(m.msg, "msg A=Any")
1410-
}
14111309
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package dotty.tools
2+
package dotc
3+
package reporting
4+
5+
import dotty.tools.dotc.core.Contexts.Context
6+
import dotty.tools.dotc.reporting.diagnostic.messages._
7+
import org.junit.Assert._
8+
import org.junit.Test
9+
10+
class UserDefinedErrorMessages extends ErrorMessagesTest {
11+
@Test def userDefinedImplicitAmbiguous1 =
12+
checkMessagesAfter("frontend") {
13+
"""
14+
|object Test {
15+
| trait =!=[C, D]
16+
|
17+
| implicit def neq[E, F] : E =!= F = null
18+
|
19+
| @annotation.implicitAmbiguous("Could not prove ${J} =!= ${J}")
20+
| implicit def neqAmbig1[G, H, J] : J =!= J = null
21+
| implicit def neqAmbig2[I] : I =!= I = null
22+
|
23+
| implicitly[Int =!= Int]
24+
|}
25+
26+
""".stripMargin
27+
}.expect { (itcx, messages) =>
28+
import diagnostic.NoExplanation
29+
implicit val ctx: Context = itcx
30+
31+
assertMessageCount(1, messages)
32+
val (m: NoExplanation) :: Nil = messages
33+
34+
assertEquals(m.msg, "Could not prove Int =!= Int")
35+
}
36+
37+
@Test def userDefinedImplicitAmbiguous2 =
38+
checkMessagesAfter("frontend") {
39+
"""
40+
|object Test {
41+
| trait =!=[C, D]
42+
|
43+
| implicit def neq[E, F] : E =!= F = null
44+
|
45+
| implicit def neqAmbig1[G, H, J] : J =!= J = null
46+
| @annotation.implicitAmbiguous("Could not prove ${I} =!= ${I}")
47+
| implicit def neqAmbig2[I] : I =!= I = null
48+
|
49+
| implicitly[Int =!= Int]
50+
|}
51+
52+
""".stripMargin
53+
}.expect { (itcx, messages) =>
54+
import diagnostic.NoExplanation
55+
implicit val ctx: Context = itcx
56+
57+
assertMessageCount(1, messages)
58+
val (m: NoExplanation) :: Nil = messages
59+
60+
assertEquals(m.msg, "Could not prove Int =!= Int")
61+
}
62+
63+
@Test def userDefinedImplicitAmbiguous3 =
64+
checkMessagesAfter("frontend") {
65+
"""
66+
|object Test {
67+
| trait =!=[C, D]
68+
|
69+
| implicit def neq[E, F] : E =!= F = null
70+
|
71+
| @annotation.implicitAmbiguous("Could not prove ${J} =!= ${J}")
72+
| implicit def neqAmbig1[G, H, J] : J =!= J = null
73+
| @annotation.implicitAmbiguous("Could not prove ${I} =!= ${I}")
74+
| implicit def neqAmbig2[I] : I =!= I = null
75+
|
76+
| implicitly[Int =!= Int]
77+
|}
78+
79+
""".stripMargin
80+
}.expect { (itcx, messages) =>
81+
import diagnostic.NoExplanation
82+
implicit val ctx: Context = itcx
83+
84+
assertMessageCount(1, messages)
85+
val (m: NoExplanation) :: Nil = messages
86+
87+
assertEquals(m.msg, "Could not prove Int =!= Int")
88+
}
89+
90+
@Test def userDefinedImplicitAmbiguous4 =
91+
checkMessagesAfter("frontend") {
92+
"""
93+
|class C {
94+
| @annotation.implicitAmbiguous("msg A=${A}")
95+
| implicit def f[A](x: Int): String = "f was here"
96+
| implicit def g(x: Int): String = "f was here"
97+
| def test: Unit = {
98+
| implicitly[Int => String]
99+
| }
100+
|}
101+
102+
""".stripMargin
103+
}.expect { (itcx, messages) =>
104+
import diagnostic.NoExplanation
105+
implicit val ctx: Context = itcx
106+
107+
assertMessageCount(1, messages)
108+
val (m: NoExplanation) :: Nil = messages
109+
110+
assertEquals(m.msg, "msg A=Any")
111+
}
112+
}

0 commit comments

Comments
 (0)