Skip to content

Add new style error message for @tailrec #3312

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 3 commits into from
Oct 24, 2017
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 @@ -104,6 +104,7 @@ public enum ErrorMessageID {
EnumCaseDefinitionInNonEnumOwnerID,
ExpectedTypeBoundOrEqualsID,
ClassAndCompanionNameClashID,
TailrecNotApplicableID,
;

public int errorNumber() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1777,6 +1777,7 @@ object messages {
val explanation =
hl"""A class marked with the ${"final"} keyword cannot be extended"""
}

case class EnumCaseDefinitionInNonEnumOwner(owner: Symbol)(implicit ctx: Context)
extends Message(EnumCaseDefinitionInNonEnumOwnerID) {
val kind = "Syntax"
Expand Down Expand Up @@ -1817,4 +1818,12 @@ object messages {
| - ${other.owner} defines ${other}"""
}
}

case class TailrecNotApplicable(method: Symbol)(implicit ctx: Context)
extends Message(TailrecNotApplicableID) {
val kind = "Syntax"
val msg = hl"TailRec optimisation not applicable, $method is neither ${"private"} nor ${"final"}."
val explanation =
hl"A method annotated ${"@tailrec"} must be declared ${"private"} or ${"final"} so it can't be overridden."
}
}
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/TailRec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Symbols._
import Types._
import NameKinds.TailLabelName
import TreeTransforms.{MiniPhaseTransform, TransformerInfo}
import reporting.diagnostic.messages.TailrecNotApplicable

/**
* A Tail Rec Transformer
Expand Down Expand Up @@ -161,7 +162,7 @@ class TailRec extends MiniPhaseTransform with DenotTransformer with FullParamete
})
}
case d: DefDef if d.symbol.hasAnnotation(defn.TailrecAnnot) || methodsWithInnerAnnots.contains(d.symbol) =>
ctx.error("TailRec optimisation not applicable, method is neither private nor final so can be overridden", sym.pos)
ctx.error(TailrecNotApplicable(sym), sym.pos)
d
case d if d.symbol.hasAnnotation(defn.TailrecAnnot) || methodsWithInnerAnnots.contains(d.symbol) =>
ctx.error("TailRec optimisation not applicable, not a method", sym.pos)
Expand Down
15 changes: 15 additions & 0 deletions compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,21 @@ class ErrorMessagesTests extends ErrorMessagesTest {
assertEquals("object Qux", owner.show)
}

@Test def tailrecNotApplicableNeitherPrivateNorFinal =
checkMessagesAfter("tailrec") {
"""
|class Foo {
| @scala.annotation.tailrec
| def foo: Unit = foo
|}
""".stripMargin
}.expect { (ictx, messages) =>
implicit val ctx: Context = ictx
assertMessageCount(1, messages)
val TailrecNotApplicable(method) :: Nil = messages
assertEquals(method.show, "method foo")
}

@Test def expectedTypeBoundOrEquals =
checkMessagesAfter("frontend") {
"""object typedef {
Expand Down