Skip to content

New format for trait redefined final method from AnyRef error message… #3447

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public enum ErrorMessageID {
UndefinedNamedTypeParameterID,
IllegalStartOfStatementID,
TraitIsExpectedID,
TraitRedefinedFinalMethodFromAnyRefID,
;

public int errorNumber() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1916,4 +1916,10 @@ object messages {
|"""
}
}

case class TraitRedefinedFinalMethodFromAnyRef(method: Symbol)(implicit ctx: Context) extends Message(TraitRedefinedFinalMethodFromAnyRefID) {
val kind = "Syntax"
val msg = hl"Traits cannot redefine final $method from ${"class AnyRef"}."
val explanation = ""
}
}
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/RefChecks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ object RefChecks {
if (!cls.owner.is(ModuleClass)) {
def clashes(sym: Symbol) =
sym.isClass &&
sym.name.stripModuleClassSuffix == cls.name.stripModuleClassSuffix
sym.name.stripModuleClassSuffix == cls.name.stripModuleClassSuffix

val others = cls.owner.linkedClass.info.decls.filter(clashes)
others.foreach { other =>
Expand Down Expand Up @@ -606,7 +606,7 @@ object RefChecks {
// override a concrete method in Object. The jvm, however, does not.
val overridden = decl.matchingDecl(defn.ObjectClass, defn.ObjectType)
if (overridden.is(Final))
ctx.error("trait cannot redefine final method from class AnyRef", decl.pos)
ctx.error(TraitRedefinedFinalMethodFromAnyRef(overridden), decl.pos)
}
}

Expand Down
17 changes: 17 additions & 0 deletions compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1172,4 +1172,21 @@ class ErrorMessagesTests extends ErrorMessagesTest {
val TraitIsExpected(symbol) :: Nil = messages
assertEquals("class B", symbol.show)
}

@Test def traitRedefinedFinalMethodFromAnyRef =
checkMessagesAfter("refchecks") {
"""
|trait C {
| def wait (): Unit
|}
""".stripMargin
}
.expect { (ictx, messages) =>
implicit val ctx: Context = ictx

assertMessageCount(1, messages)
val TraitRedefinedFinalMethodFromAnyRef(method) = messages.head
assertEquals("method wait", method.show)
}

}