Skip to content

Commit 68dcdec

Browse files
author
Jendrik Wenke
committed
don't show error about signatures when a var/val and a def are clashing
1 parent aa803ed commit 68dcdec

File tree

2 files changed

+114
-7
lines changed

2 files changed

+114
-7
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2083,11 +2083,14 @@ object messages {
20832083
case class DoubleDeclaration(decl: Symbol, previousSymbol: Symbol)(implicit ctx: Context) extends Message(DoubleDeclarationID) {
20842084
val kind = "Duplicate Symbol"
20852085
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-
}
2086+
val details = if (decl.isRealMethod && previousSymbol.isRealMethod) {
2087+
// compare the signatures when both symbols represent methods
2088+
decl.asTerm.signature.matchDegree(previousSymbol.asTerm.signature) match {
2089+
case Signature.NoMatch => ""
2090+
case Signature.ParamMatch => "\nOverloads with equal parameter types but different return types are not allowed."
2091+
case Signature.FullMatch => "\nThe definitions have the same signature after erasure."
2092+
}
2093+
} else ""
20912094
hl"${decl.showLocated} is already defined as ${previousSymbol.showDcl} in line ${previousSymbol.pos.line + 1}." + details
20922095
}
20932096
val explanation = ""

tests/neg/doubleDefinition.scala

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,126 @@
11
trait A
22
trait B
33

4+
// test with classes
5+
46
class Test1 {
57
def foo(x: List[A]): Function1[A, A] = ???
68
def foo(x: List[B]): Function2[B, B, B] = ???
79
// ok, different jvm signature
810
}
911

10-
1112
class Test2 {
1213
def foo(x: List[A]): Function1[A, A] = ???
1314
def foo(x: List[B]): Function1[B, B] = ??? // error: same jvm signature
1415
// scalac calls this "have same type after erasure"
1516
}
1617

17-
1818
class Test3 {
1919
// overload with same argument type, but different return types
2020
def foo(x: List[A]): Function1[A, A] = ???
2121
def foo(x: List[A]): Function2[B, B, B] = ??? // error
2222
}
23+
24+
class Test4 {
25+
val foo = 1
26+
def foo = 2 // error
27+
}
28+
29+
class Test4b {
30+
def foo = 2
31+
val foo = 1 // error
32+
}
33+
34+
class Test4c {
35+
def foo = 2
36+
var foo = 1 // error
37+
}
38+
39+
class Test4d {
40+
var foo = 1
41+
def foo = 2 // error
42+
}
43+
44+
45+
// test with traits
46+
47+
trait Test5 {
48+
def foo(x: List[A]): Function1[A, A] = ???
49+
def foo(x: List[B]): Function2[B, B, B] = ???
50+
// ok, different jvm signature
51+
}
52+
53+
trait Test6 {
54+
def foo(x: List[A]): Function1[A, A] = ???
55+
def foo(x: List[B]): Function1[B, B] = ??? // error: same jvm signature
56+
// scalac calls this "have same type after erasure"
57+
}
58+
59+
trait Test7 {
60+
// overload with same argument type, but different return types
61+
def foo(x: List[A]): Function1[A, A] = ???
62+
def foo(x: List[A]): Function2[B, B, B] = ??? // error
63+
}
64+
65+
class Test8 {
66+
val foo = 1
67+
def foo = 2 // error
68+
}
69+
70+
class Test8b {
71+
def foo = 2
72+
val foo = 1 // error
73+
}
74+
75+
class Test8c {
76+
def foo = 2
77+
var foo = 1 // error
78+
}
79+
80+
class Test8d {
81+
var foo = 1
82+
def foo = 2 // error
83+
}
84+
85+
// test method and contructor argument clashing
86+
87+
class Test9(val foo: Int) {
88+
def foo: String // error
89+
}
90+
91+
class Test10(val foo: Int) {
92+
def foo: Int // error
93+
}
94+
95+
abstract class Test11(val foo: Int) {
96+
def foo: String // error
97+
}
98+
99+
abstract class Test12(val foo: Int) {
100+
def foo: Int // error
101+
}
102+
103+
class Test13(var foo: Int) {
104+
def foo: String // error
105+
}
106+
107+
class Test14(var foo: Int) {
108+
def foo: Int // error
109+
}
110+
111+
abstract class Test15(var foo: Int) {
112+
def foo: String // error
113+
}
114+
115+
abstract class Test16(var foo: Int) {
116+
def foo: Int // error
117+
}
118+
119+
// don't error when shadowing
120+
121+
class Test17 {
122+
val foo = 1
123+
def bar() = {
124+
val foo = ""
125+
}
126+
}

0 commit comments

Comments
 (0)