Skip to content

Commit 62a95bb

Browse files
committed
Moved Ambiguous Overload error in Typer to case class
1 parent df22149 commit 62a95bb

File tree

4 files changed

+54
-6
lines changed

4 files changed

+54
-6
lines changed

compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public enum ErrorMessageID {
5656
CyclicReferenceInvolvingID,
5757
CyclicReferenceInvolvingImplicitID,
5858
SuperQualMustBeParentID,
59+
AmbiguousOverloadID,
5960
;
6061

6162
public int errorNumber() {

compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import dotc.parsing.Tokens
1818
import printing.Highlighting._
1919
import printing.Formatting
2020
import ErrorMessageID._
21+
import Denotations.SingleDenotation
2122
import dotty.tools.dotc.core.SymDenotations.SymDenotation
2223

2324
object messages {
@@ -1223,4 +1224,26 @@ object messages {
12231224
|Attempting to define a field in a method signature after a varargs field is an error.
12241225
|""".stripMargin
12251226
}
1227+
1228+
case class AmbiguousOverload(tree: tpd.Tree, alts: List[SingleDenotation], pt: Type)(
1229+
err: typer.ErrorReporting.Errors)(
1230+
implicit ctx: Context)
1231+
extends Message(AmbiguousOverloadID) {
1232+
1233+
private def all = if (alts.length == 2) "both" else "all"
1234+
1235+
override def msg: String =
1236+
s"""|Ambiguous overload. The ${err.overloadedAltsStr(alts)}
1237+
|$all match ${err.expectedTypeStr(pt)}""".stripMargin
1238+
1239+
override def kind: String = "Reference"
1240+
1241+
override def explanation: String =
1242+
s"""|There are ${alts.length} methods that could be referenced as the compiler knows too little about the expected type.
1243+
|You may specify the expected type e.g. by
1244+
|- assigning it to a value with a specified type, or
1245+
|- adding a type ascription as in (MyClass.myMethod: String => Int)
1246+
|""".stripMargin
1247+
}
1248+
12261249
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,15 +1799,14 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
17991799
typr.println(i"adapt overloaded $ref with alternatives ${altDenots map (_.info)}%, %")
18001800
val alts = altDenots map (alt =>
18011801
TermRef.withSigAndDenot(ref.prefix, ref.name, alt.info.signature, alt))
1802-
def expectedStr = err.expectedTypeStr(pt)
18031802
resolveOverloaded(alts, pt) match {
18041803
case alt :: Nil =>
18051804
adapt(tree.withType(alt), pt, original)
18061805
case Nil =>
18071806
def noMatches =
18081807
errorTree(tree,
18091808
em"""none of the ${err.overloadedAltsStr(altDenots)}
1810-
|match $expectedStr""")
1809+
|match ${err.expectedTypeStr(pt)}""")
18111810
def hasEmptyParams(denot: SingleDenotation) = denot.info.paramInfoss == ListOfNil
18121811
pt match {
18131812
case pt: FunProto =>
@@ -1820,10 +1819,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
18201819
}
18211820
case alts =>
18221821
val remainingDenots = alts map (_.denot.asInstanceOf[SingleDenotation])
1823-
def all = if (remainingDenots.length == 2) "both" else "all"
1824-
errorTree(tree,
1825-
em"""Ambiguous overload. The ${err.overloadedAltsStr(remainingDenots)}
1826-
|$all match $expectedStr""")
1822+
errorTree(tree, AmbiguousOverload(tree, remainingDenots, pt)(err))
18271823
}
18281824
}
18291825

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package reporting
44

55
import core.Contexts.Context
66
import diagnostic.messages._
7+
import dotty.tools.dotc.core.Types.WildcardType
78
import dotty.tools.dotc.parsing.Tokens
89
import org.junit.Assert._
910
import org.junit.{Ignore, Test}
@@ -329,4 +330,31 @@ class ErrorMessagesTests extends ErrorMessagesTest {
329330
assertEquals("B", qual.show)
330331
assertEquals("class C", cls.show)
331332
}
333+
334+
@Test def ambiugousOverloadWithWildcard =
335+
checkMessagesAfter("frontend") {
336+
"""object Context {
337+
| trait A {
338+
| def foo(s: String): String
339+
| def foo: String = foo("foo")
340+
| }
341+
| object B extends A {
342+
| def foo(s: String): String = s
343+
| }
344+
| B.foo
345+
|}
346+
""".stripMargin
347+
}
348+
.expect { (ictx, messages) =>
349+
implicit val ctx: Context = ictx
350+
val defn = ictx.definitions
351+
352+
assertMessageCount(1, messages)
353+
val AmbiguousOverload(tree, List(alt1, alt2), pt: WildcardType) :: Nil = messages
354+
assertEquals("method foo", alt1.show)
355+
assertEquals("(s: String)String", alt1.info.show)
356+
assertEquals("method foo", alt2.show)
357+
}
358+
359+
332360
}

0 commit comments

Comments
 (0)