Skip to content

Commit c8894a0

Browse files
authored
Merge pull request #13090 from dotty-staging/fix-13089
Add explanation to "already defined" message
2 parents 344bd19 + d6453ec commit c8894a0

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1861,11 +1861,15 @@ import transform.SymUtils._
18611861
i" in ${conflicting.associatedFile}"
18621862
else if conflicting.owner == owner then ""
18631863
else i" in ${conflicting.owner}"
1864+
private def note =
1865+
if owner.is(Method) || conflicting.is(Method) then
1866+
"\n\nNote that overloaded methods must all be defined in the same group of toplevel definitions"
1867+
else ""
18641868
def msg =
18651869
if conflicting.isTerm != name.isTermName then
18661870
em"$name clashes with $conflicting$where; the two must be defined together"
18671871
else
1868-
em"$name is already defined as $conflicting$where"
1872+
em"$name is already defined as $conflicting$where$note"
18691873
def explain = ""
18701874

18711875
class PackageNameAlreadyDefined(pkg: Symbol)(using Context) extends NamingMsg(PackageNameAlreadyDefinedID) {

tests/neg/i13089.check

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- [E161] Naming Error: tests/neg/i13089.scala:6:8 ---------------------------------------------------------------------
2+
6 | def fails : Unit = {} // error
3+
| ^^^^^^^^^^^^^^^^^^^^^
4+
| fails is already defined as method fails in tests/neg/i13089.scala
5+
|
6+
| Note that overloaded methods must all be defined in the same group of toplevel definitions
7+
-- [E161] Naming Error: tests/neg/i13089.scala:8:6 ---------------------------------------------------------------------
8+
8 | def baz(x: String): Boolean = true // error
9+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10+
| baz is already defined as method baz in tests/neg/i13089.scala
11+
|
12+
| Note that overloaded methods must all be defined in the same group of toplevel definitions

tests/neg/i13089.scala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package pkg:
2+
3+
trait Bar
4+
5+
extension (bar : Bar)
6+
def fails : Unit = {} // error
7+
8+
def baz(x: String): Boolean = true // error
9+
10+
package pkg:
11+
12+
trait Foo
13+
extension (foo : Foo)
14+
def fails : Unit = {}
15+
def works : Unit = {}
16+
17+
extension (bar : Bar)
18+
def works : Unit = {}
19+
20+
def baz(x: Int): Boolean = true
21+

0 commit comments

Comments
 (0)