Skip to content

Commit b078274

Browse files
Jannyboy11mbovel
andcommitted
Improve error message for conflicting definitions
Now includes the signature in the message also when emitted from the typer. Co-authored-by: Matthieu Bovel <[email protected]>
1 parent 5f50490 commit b078274

File tree

7 files changed

+49
-3
lines changed

7 files changed

+49
-3
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import dotty.tools.dotc.util.SourceFile
3838
import dotty.tools.dotc.config.SourceVersion
3939
import DidYouMean.*
4040
import Message.Disambiguation
41+
import dotty.tools.dotc.transform.Erasure
4142

4243
/** Messages
4344
* ========
@@ -2333,7 +2334,7 @@ class DoubleDefinition(decl: Symbol, previousDecl: Symbol, base: Symbol)(using C
23332334
extends NamingMsg(DoubleDefinitionID) {
23342335
def msg(using Context) = {
23352336
def nameAnd = if (decl.name != previousDecl.name) " name and" else ""
2336-
def erasedType = if ctx.erasedTypes then i" ${decl.info}" else ""
2337+
def erasedType = if ctx.erasedTypes then i"${decl.info}" else s"${atPhase(Phases.erasurePhase) {(decl.info.show)}}"
23372338
def details(using Context): String =
23382339
if (decl.isRealMethod && previousDecl.isRealMethod) {
23392340
import Signature.MatchDegree.*
@@ -2361,7 +2362,7 @@ extends NamingMsg(DoubleDefinitionID) {
23612362
|Consider adding a @targetName annotation to one of the conflicting definitions
23622363
|for disambiguation."""
23632364
else ""
2364-
i"have the same$nameAnd type$erasedType after erasure.$hint"
2365+
i"have the same$nameAnd type $erasedType after erasure.$hint"
23652366
}
23662367
}
23672368
else ""
@@ -2374,7 +2375,7 @@ extends NamingMsg(DoubleDefinitionID) {
23742375
}
23752376
val clashDescription =
23762377
if (decl.owner eq previousDecl.owner)
2377-
"Double definition"
2378+
"Conflicting definitions"
23782379
else if ((decl.owner eq base) || (previousDecl eq base))
23792380
"Name clash between defined and inherited member"
23802381
else

tests/neg/i23402.check

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- [E120] Naming Error: tests\neg\i23402.scala:4:5 ---------------------------------------------------------------------
2+
4 | def apply(p1: String)(p2: Int): A = A(p1, p2) // error
3+
| ^
4+
| Conflicting definitions:
5+
| def apply(p1: String, p2: Int): A in object A at line 3 and
6+
| def apply(p1: String)(p2: Int): A in object A at line 4
7+
| have the same type (p1: String)(p2: Int): A after erasure.
8+
|
9+
| Consider adding a @targetName annotation to one of the conflicting definitions
10+
| for disambiguation.

tests/neg/i23402.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class A(p1: String, p2: Int)
2+
object A {
3+
def apply(p1: String, p2: Int): A = A(p1, p2)
4+
def apply(p1: String)(p2: Int): A = A(p1, p2) // error
5+
}

tests/neg/i23402b.check

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- [E120] Naming Error: tests\neg\i23402b.scala:4:5 --------------------------------------------------------------------
2+
4 | def apply[T](p1: String)(p2: Int): A = A(p1, p2) // error
3+
| ^
4+
| Conflicting definitions:
5+
| def apply[T](p1: String, p2: Int): A in object A at line 3 and
6+
| def apply[T](p1: String)(p2: Int): A in object A at line 4
7+
| have the same type [T](p1: String)(p2: Int): A after erasure.
8+
|
9+
| Consider adding a @targetName annotation to one of the conflicting definitions
10+
| for disambiguation.

tests/neg/i23402b.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class A(p1: String, p2: Int)
2+
object A {
3+
def apply[T](p1: String, p2: Int): A = A(p1, p2)
4+
def apply[T](p1: String)(p2: Int): A = A(p1, p2) // error
5+
}

tests/neg/i23402c.check

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- [E120] Naming Error: tests\neg\i23402c.scala:4:5 --------------------------------------------------------------------
2+
4 | def apply[T](p1: String)(p2: Int): A = A(p1, p2) // error
3+
| ^
4+
| Conflicting definitions:
5+
| def apply(p1: String, p2: Int): A in object A at line 3 and
6+
| def apply[T](p1: String)(p2: Int): A in object A at line 4
7+
| have the same type (p1: String, p2: Int): A after erasure.
8+
|
9+
| Consider adding a @targetName annotation to one of the conflicting definitions
10+
| for disambiguation.

tests/neg/i23402c.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class A(p1: String, p2: Int)
2+
object A {
3+
def apply(p1: String, p2: Int): A = A(p1, p2)
4+
def apply[T](p1: String)(p2: Int): A = A(p1, p2) // error
5+
}

0 commit comments

Comments
 (0)