Skip to content

Add explanation to "already defined" message #13090

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1861,11 +1861,15 @@ import transform.SymUtils._
i" in ${conflicting.associatedFile}"
else if conflicting.owner == owner then ""
else i" in ${conflicting.owner}"
private def note =
if owner.is(Method) || conflicting.is(Method) then
"\n\nNote that overloaded methods must all be defined in the same group of toplevel definitions"
else ""
def msg =
if conflicting.isTerm != name.isTermName then
em"$name clashes with $conflicting$where; the two must be defined together"
else
em"$name is already defined as $conflicting$where"
em"$name is already defined as $conflicting$where$note"
def explain = ""

class PackageNameAlreadyDefined(pkg: Symbol)(using Context) extends NamingMsg(PackageNameAlreadyDefinedID) {
Expand Down
12 changes: 12 additions & 0 deletions tests/neg/i13089.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- [E161] Naming Error: tests/neg/i13089.scala:6:8 ---------------------------------------------------------------------
6 | def fails : Unit = {} // error
| ^^^^^^^^^^^^^^^^^^^^^
| fails is already defined as method fails in tests/neg/i13089.scala
|
| Note that overloaded methods must all be defined in the same group of toplevel definitions
-- [E161] Naming Error: tests/neg/i13089.scala:8:6 ---------------------------------------------------------------------
8 | def baz(x: String): Boolean = true // error
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| baz is already defined as method baz in tests/neg/i13089.scala
|
| Note that overloaded methods must all be defined in the same group of toplevel definitions
21 changes: 21 additions & 0 deletions tests/neg/i13089.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package pkg:

trait Bar

extension (bar : Bar)
def fails : Unit = {} // error

def baz(x: String): Boolean = true // error

package pkg:

trait Foo
extension (foo : Foo)
def fails : Unit = {}
def works : Unit = {}

extension (bar : Bar)
def works : Unit = {}

def baz(x: Int): Boolean = true