Skip to content

Commit b6c58f1

Browse files
committed
Fix #5495: Reject invalid modifiers on enums
Fixes #5034
1 parent 11a9a78 commit b6c58f1

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

compiler/src/dotty/tools/dotc/ast/DesugarEnums.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ object DesugarEnums {
6969

7070
/** Add implied flags to an enum class or an enum case */
7171
def addEnumFlags(cdef: TypeDef)(implicit ctx: Context): TypeDef =
72-
if (cdef.mods.isEnumClass) cdef.withMods(cdef.mods.withFlags(cdef.mods.flags | Abstract | Sealed))
73-
else if (isEnumCase(cdef)) cdef.withMods(cdef.mods.withFlags(cdef.mods.flags | Final))
72+
if (cdef.mods.isEnumClass) cdef.withMods(cdef.mods.toTypeFlags | Abstract | Sealed)
73+
else if (isEnumCase(cdef)) cdef.withMods(cdef.mods | Final)
7474
else cdef
7575

7676
private def valuesDot(name: String) = Select(Ident(nme.DOLLAR_VALUES), name.toTermName)

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2388,17 +2388,25 @@ object Parsers {
23882388
/** EnumDef ::= id ClassConstr [`extends' [ConstrApps]] EnumBody
23892389
*/
23902390
def enumDef(start: Offset, mods: Modifiers, enumMod: Mod): TypeDef = atPos(start, nameStart) {
2391+
val InvalidEnumClassModifiers = ModifierFlags &~ (Private | Protected)
2392+
if (mods.is(InvalidEnumClassModifiers))
2393+
syntaxError("Only access modifiers are allowed on enum definitions", mods.pos)
2394+
2395+
val mods1 = addMod(mods, enumMod) &~ InvalidEnumClassModifiers
23912396
val modName = ident()
23922397
val clsName = modName.toTypeName
23932398
val constr = classConstr(clsName)
23942399
val impl = templateOpt(constr, isEnum = true)
2395-
TypeDef(clsName, impl).withMods(addMod(mods, enumMod)).setComment(in.getDocComment(start))
2400+
TypeDef(clsName, impl).withMods(mods1).setComment(in.getDocComment(start))
23962401
}
23972402

23982403
/** EnumCase = `case' (id ClassConstr [`extends' ConstrApps] | ids)
23992404
*/
24002405
def enumCase(start: Offset, mods: Modifiers): DefTree = {
2401-
val mods1 = addMod(mods, atPos(in.offset)(Mod.Enum())) | Case
2406+
if (mods.is(ModifierFlags))
2407+
syntaxError("No modifier are allowed on enum cases", mods.pos)
2408+
2409+
val mods1 = addMod(mods, atPos(in.offset)(Mod.Enum())) &~ ModifierFlags | Case
24022410
accept(CASE)
24032411

24042412
in.adjustSepRegions(ARROW)

tests/neg/i5495.scala

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
abstract enum Foo1 {} // error: only access modifiers allowed
2+
final enum Foo2 {} // error: only access modifiers allowed
3+
sealed enum Foo3 {} // error: only access modifiers allowed
4+
implicit enum Foo4 {} // error: only access modifiers allowed
5+
lazy enum Foo5 {} // error: only access modifiers allowed
6+
erased enum Foo6 {} // error: only access modifiers allowed
7+
override enum Foo7 {} // error: only access modifiers allowed
8+
9+
enum Foo7 {
10+
abstract case C1() // error: no modifier allowed
11+
final case C2() // error: no modifier allowed
12+
sealed case C3() // error: no modifier allowed
13+
implicit case C4() // error: no modifier allowed
14+
lazy case C5() // error: no modifier allowed
15+
erased case C6() // error: no modifier allowed
16+
override case C7() // error: no modifier allowed
17+
private case C8() // error: no modifier allowed
18+
protected case C9() // error: no modifier allowed
19+
}
20+
21+
enum Foo8 {
22+
abstract case C1 // error: no modifier allowed
23+
final case C2 // error: no modifier allowed
24+
sealed case C3 // error: no modifier allowed
25+
implicit case C4 // error: no modifier allowed
26+
lazy case C5 // error: no modifier allowed
27+
erased case C6 // error: no modifier allowed
28+
override case C7 // error: no modifier allowed
29+
private case C8 // error: no modifier allowed
30+
protected case C9 // error: no modifier allowed
31+
}

0 commit comments

Comments
 (0)