Skip to content

Commit 606323b

Browse files
committed
add and use error message and id
1 parent f1940ee commit 606323b

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public enum ErrorMessageID {
102102
UncheckedTypePatternID,
103103
ExtendFinalClassID,
104104
EnumCaseDefinitionInNonEnumOwnerID,
105+
TailrecNotApplicableNeitherPrivateNorFinalID,
105106
;
106107

107108
public int errorNumber() {

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,6 +1777,7 @@ object messages {
17771777
val explanation =
17781778
hl"""A class marked with the ${"final"} keyword cannot be extended"""
17791779
}
1780+
17801781
case class EnumCaseDefinitionInNonEnumOwner(owner: Symbol)(implicit ctx: Context)
17811782
extends Message(EnumCaseDefinitionInNonEnumOwnerID) {
17821783
val kind = "Syntax"
@@ -1786,4 +1787,15 @@ object messages {
17861787
|If you want to create an ${"enum"} case, make sure the corresponding ${"enum class"} exists
17871788
|and has the ${"enum"} keyword."""
17881789
}
1790+
1791+
1792+
case class TailrecNotApplicableNeitherPrivateNorFinal(owner: Symbol)(implicit ctx: Context)
1793+
extends Message(TailrecNotApplicableNeitherPrivateNorFinalID) {
1794+
val kind = "Syntax"
1795+
val msg = hl"TailRec optimisation not applicable, ${owner} is neither `private` nor `final` so can be overridden"
1796+
val explanation =
1797+
hl"""
1798+
|The `@tailrec` annotation can only be used on methods that are either `private` or `final`.
1799+
"""
1800+
}
17891801
}

compiler/src/dotty/tools/dotc/transform/TailRec.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import Symbols._
1212
import Types._
1313
import NameKinds.TailLabelName
1414
import TreeTransforms.{MiniPhaseTransform, TransformerInfo}
15+
import reporting.diagnostic.messages.TailrecNotApplicableNeitherPrivateNorFinal
1516

1617
/**
1718
* A Tail Rec Transformer
@@ -162,7 +163,7 @@ class TailRec extends MiniPhaseTransform with DenotTransformer with FullParamete
162163
})
163164
}
164165
case d: DefDef if d.symbol.hasAnnotation(defn.TailrecAnnot) || methodsWithInnerAnnots.contains(d.symbol) =>
165-
ctx.error("TailRec optimisation not applicable, method is neither private nor final so can be overridden", sym.pos)
166+
ctx.error(TailrecNotApplicableNeitherPrivateNorFinal(sym), sym.pos)
166167
d
167168
case d if d.symbol.hasAnnotation(defn.TailrecAnnot) || methodsWithInnerAnnots.contains(d.symbol) =>
168169
ctx.error("TailRec optimisation not applicable, not a method", sym.pos)

0 commit comments

Comments
 (0)