From d6453ec28ad2fa3bc332acb80ddb657137f2759d Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Fri, 16 Jul 2021 09:59:53 +0200 Subject: [PATCH] Add explanation to "already defined" message Fixes #13089 --- .../dotty/tools/dotc/reporting/messages.scala | 6 +++++- tests/neg/i13089.check | 12 +++++++++++ tests/neg/i13089.scala | 21 +++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 tests/neg/i13089.check create mode 100644 tests/neg/i13089.scala diff --git a/compiler/src/dotty/tools/dotc/reporting/messages.scala b/compiler/src/dotty/tools/dotc/reporting/messages.scala index fb7c6d41e885..0e47c4a1ff57 100644 --- a/compiler/src/dotty/tools/dotc/reporting/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/messages.scala @@ -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) { diff --git a/tests/neg/i13089.check b/tests/neg/i13089.check new file mode 100644 index 000000000000..ed0f9d5effd9 --- /dev/null +++ b/tests/neg/i13089.check @@ -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 diff --git a/tests/neg/i13089.scala b/tests/neg/i13089.scala new file mode 100644 index 000000000000..5b91996de837 --- /dev/null +++ b/tests/neg/i13089.scala @@ -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 +