Skip to content

Commit 03e4ab3

Browse files
author
Jendrik Wenke
committed
Add error message about double definition (#1589)
1 parent 2fb757a commit 03e4ab3

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
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
@@ -127,6 +127,7 @@ public enum ErrorMessageID {
127127
PolymorphicMethodMissingTypeInParentID,
128128
ParamsNoInlineID,
129129
JavaSymbolIsNotAValueID,
130+
DoubleDeclarationID,
130131
;
131132

132133
public int errorNumber() {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2079,4 +2079,10 @@ object messages {
20792079
}
20802080
val explanation = ""
20812081
}
2082+
2083+
case class DoubleDeclaration(decl: Symbol, previousSymbol: Symbol)(implicit ctx: Context) extends Message(DoubleDeclarationID) {
2084+
val kind = "Duplicate Symbol"
2085+
val msg = hl"$decl is already defined as $previousSymbol${previousSymbol.showExtendedLocation}"
2086+
val explanation = ""
2087+
}
20822088
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ trait Checking {
627627
def explanation =
628628
if (!decl.isRealMethod) ""
629629
else "\n(the definitions have matching type signatures)"
630-
ctx.error(em"$decl is already defined as $other$ofType$explanation", decl.pos)
630+
ctx.error(DoubleDeclaration(decl, other), decl.pos)
631631
}
632632
if (decl is Synthetic) doubleDefError(other, decl)
633633
else doubleDefError(decl, other)

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ package dotty.tools
22
package dotc
33
package reporting
44

5-
import core.Contexts.Context
6-
import diagnostic.messages._
7-
import dotty.tools.dotc.core.Flags
8-
import dotty.tools.dotc.core.Flags.FlagSet
5+
import dotty.tools.dotc.core.Contexts.Context
96
import dotty.tools.dotc.core.Types.WildcardType
107
import dotty.tools.dotc.parsing.Tokens
8+
import dotty.tools.dotc.reporting.diagnostic.messages._
119
import org.junit.Assert._
1210
import org.junit.Test
1311

@@ -1293,4 +1291,19 @@ class ErrorMessagesTests extends ErrorMessagesTest {
12931291

12941292
assert(ctx.reporter.hasErrors)
12951293
}
1294+
1295+
@Test def typeDoubleDeclaration =
1296+
checkMessagesAfter("frontend") {
1297+
"""
1298+
|class Foo {
1299+
| val a = 1
1300+
| val a = 2
1301+
|}
1302+
""".stripMargin
1303+
}.expect { (ictx, messages) =>
1304+
implicit val ctx: Context = ictx
1305+
assertMessageCount(1, messages)
1306+
val DoubleDeclaration(symbol, previousSymbol) :: Nil = messages
1307+
assertEquals(symbol.name.mangledString, "a")
1308+
}
12961309
}

0 commit comments

Comments
 (0)