Skip to content

Commit 331cfcb

Browse files
committed
error on custom definition of enum getters
1 parent d448fb6 commit 331cfcb

File tree

6 files changed

+40
-24
lines changed

6 files changed

+40
-24
lines changed

compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ enum ErrorMessageID extends java.lang.Enum[ErrorMessageID] {
167167
ModifierNotAllowedForDefinitionID,
168168
CannotExtendJavaEnumID,
169169
InvalidReferenceInImplicitNotFoundAnnotationID,
170-
TraitMayNotDefineNativeMethodID
170+
TraitMayNotDefineNativeMethodID,
171+
EnumGettersRedefinitionID
171172

172173
def errorNumber = ordinal - 2
173174
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2031,6 +2031,10 @@ import ast.tpd
20312031
def explain = ""
20322032
}
20332033

2034+
class EnumGettersRedefinition(decl: Symbol)(using Context) extends NamingMsg(EnumGettersRedefinitionID):
2035+
def msg = em"redefinition of $decl: ${decl.info} in an ${hl("enum")}"
2036+
def explain = em"users may not may not supply their own definition for $decl when inside an ${hl("enum")}"
2037+
20342038
class DoubleDefinition(decl: Symbol, previousDecl: Symbol, base: Symbol)(using Context) extends NamingMsg(DoubleDefinitionID) {
20352039
def msg = {
20362040
def nameAnd = if (decl.name != previousDecl.name) " name and" else ""

compiler/src/dotty/tools/dotc/typer/Checking.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,12 @@ trait Checking {
889889
if (decl.matches(other) && !javaFieldMethodPair) {
890890
def doubleDefError(decl: Symbol, other: Symbol): Unit =
891891
if (!decl.info.isErroneous && !other.info.isErroneous)
892-
report.error(DoubleDefinition(decl, other, cls), decl.srcPos)
892+
if decl.owner.is(Enum, butNot=JavaDefined|Case) && decl.span.isSynthetic && (
893+
decl.name == nme.ordinal || decl.name == nme.enumLabel)
894+
then
895+
report.error(EnumGettersRedefinition(decl), other.srcPos)
896+
else
897+
report.error(DoubleDefinition(decl, other, cls), decl.srcPos)
893898
if (decl is Synthetic) doubleDefError(other, decl)
894899
else doubleDefError(decl, other)
895900
}

tests/neg/enumsLabel-overrides.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
trait Mixin { def enumLabel: String = "mixin" }
2+
3+
enum Mixed extends Mixin {
4+
case B // error: overriding method enumLabel in trait Mixin of type => String;
5+
}
6+
7+
enum MixedAlso {
8+
case C extends MixedAlso with Mixin // error: overriding method enumLabel in trait Mixin of type => String;
9+
}
10+
11+
trait HasEnumLabel { def enumLabel: String }
12+
13+
enum MyEnum extends HasEnumLabel {
14+
case D // ok
15+
}

tests/neg/enumsLabel-singleimpl.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
enum Labelled {
2+
3+
case A
4+
5+
def enumLabel: String = "nolabel" // error: redefinition of method enumLabel: => String in an enum
6+
}
7+
8+
enum Ordinalled {
9+
10+
case A
11+
12+
def ordinal: Int = -1 // error: redefinition of method ordinal: => Int in an enum
13+
}

tests/neg/enumsLabelDef.scala

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)