Skip to content

Commit 60b5dd1

Browse files
authored
fix: better error messages when an enum derives from AnyVal (#22236)
Closes #21944
2 parents ae80285 + 54312c6 commit 60b5dd1

File tree

6 files changed

+21
-2
lines changed

6 files changed

+21
-2
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
219219
case DeprecatedAssignmentSyntaxID // errorNumber: 203
220220
case DeprecatedInfixNamedArgumentSyntaxID // errorNumber: 204
221221
case GivenSearchPriorityID // errorNumber: 205
222+
case EnumMayNotBeValueClassesID // errorNumber: 206
222223

223224
def errorNumber = ordinal - 1
224225

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

+6
Original file line numberDiff line numberDiff line change
@@ -3399,3 +3399,9 @@ class GivenSearchPriorityWarning(
33993399
|$migrationHints"""
34003400

34013401
def explain(using Context) = ""
3402+
3403+
final class EnumMayNotBeValueClasses(sym: Symbol)(using Context) extends SyntaxMsg(EnumMayNotBeValueClassesID):
3404+
def msg(using Context): String = i"$sym may not be a value class"
3405+
3406+
def explain(using Context) = ""
3407+
end EnumMayNotBeValueClasses

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -734,11 +734,16 @@ object Checking {
734734
case _ =>
735735
report.error(ValueClassesMayNotContainInitalization(clazz), stat.srcPos)
736736
}
737-
if (clazz.isDerivedValueClass) {
737+
// We don't check synthesised enum anonymous classes that are generated from
738+
// enum extending a value class type (AnyVal or an alias of it)
739+
// The error message 'EnumMayNotBeValueClassesID' will take care of generating the error message (See #22236)
740+
if (clazz.isDerivedValueClass && !clazz.isEnumAnonymClass) {
738741
if (clazz.is(Trait))
739742
report.error(CannotExtendAnyVal(clazz), clazz.srcPos)
740743
if clazz.is(Module) then
741744
report.error(CannotExtendAnyVal(clazz), clazz.srcPos)
745+
if (clazz.is(Enum))
746+
report.error(EnumMayNotBeValueClasses(clazz), clazz.srcPos)
742747
if (clazz.is(Abstract))
743748
report.error(ValueClassesMayNotBeAbstract(clazz), clazz.srcPos)
744749
if (!clazz.isStatic)

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1626,7 +1626,8 @@ class Namer { typer: Typer =>
16261626
}
16271627
else {
16281628
val pclazz = pt.typeSymbol
1629-
if pclazz.is(Final) then
1629+
// The second condition avoids generating a useless message (See #22236 for more details)
1630+
if pclazz.is(Final) && !(pclazz.is(Enum) && pclazz.isDerivedValueClass) then
16301631
report.error(ExtendFinalClass(cls, pclazz), cls.srcPos)
16311632
else if pclazz.isEffectivelySealed && pclazz.associatedFile != cls.associatedFile then
16321633
if pclazz.is(Sealed) && !pclazz.is(JavaDefined) then

tests/neg/i21944.check

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- [E206] Syntax Error: tests/neg/i21944.scala:1:5 ---------------------------------------------------------------------
2+
1 |enum Orientation extends AnyVal: // error
3+
| ^
4+
| class Orientation may not be a value class

tests/neg/i21944.scala

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
enum Orientation extends AnyVal: // error
2+
case North, South, East, West

0 commit comments

Comments
 (0)