Skip to content

Commit aa9ce63

Browse files
author
Jendrik Wenke
committed
WIP try to improve error messages
1 parent d4455fd commit aa9ce63

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2082,7 +2082,14 @@ object messages {
20822082

20832083
case class DoubleDeclaration(decl: Symbol, previousSymbol: Symbol)(implicit ctx: Context) extends Message(DoubleDeclarationID) {
20842084
val kind = "Duplicate Symbol"
2085-
val msg = hl"$decl is already defined as $previousSymbol${previousSymbol.showExtendedLocation}"
2085+
val msg = {
2086+
val details = decl.asTerm.signature.matchDegree(previousSymbol.asTerm.signature) match {
2087+
case Signature.NoMatch => "" // matchDegree also returns NoMatch if one of the terms is not a method
2088+
case Signature.ParamMatch => "\nOverloads with equal parameter types but different return types are not allowed."
2089+
case Signature.FullMatch => "\nThe definitions have the same signature after erasure."
2090+
}
2091+
hl"${decl.showLocated} is already defined as ${previousSymbol.showDcl} in line ${previousSymbol.pos.line}." + details
2092+
}
20862093
val explanation = ""
20872094
}
20882095
}

tests/neg/doubleDefinition.scala

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
trait A
2+
trait B
3+
4+
class Test1 {
5+
def foo(x: List[A]): Function1[A, A] = ???
6+
def foo(x: List[B]): Function2[B, B, B] = ???
7+
// ok, different jvm signature
8+
}
9+
10+
11+
class Test2 {
12+
13+
14+
15+
def foo(
16+
x: List[A]
17+
): Function1[A,
18+
A] =
19+
???
20+
def foo(x: List[B]): Function1[B, B] = ??? // error: same jvm signature
21+
// scalac calls this "have same type after erasure"
22+
}
23+
24+
25+
class Test3 {
26+
// overload with same argument type, but different return types
27+
def foo(x: List[A]): Function1[A, A] = ???
28+
def foo(x: List[A]): Function2[B, B, B] = ??? // error
29+
}

0 commit comments

Comments
 (0)