Skip to content

Commit 8005ac3

Browse files
committed
Improve error message for disallowed modifiers on enums
1 parent 7d369e2 commit 8005ac3

File tree

4 files changed

+92
-92
lines changed

4 files changed

+92
-92
lines changed

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4263,21 +4263,21 @@ object Parsers {
42634263
finalizeDef(ModuleDef(name, templ), mods, start)
42644264
}
42654265

4266-
private def checkAccessOnly(mods: Modifiers, where: String): Modifiers =
4267-
// We allow `infix to mark the `enum`s type as infix.
4268-
// Syntax rules disallow the soft infix modifier on `case`s.
4269-
4270-
val flags = mods.flags.toTypeFlags
4271-
val flags1 = flags & (AccessFlags | Enum | Infix | Into).toTypeFlags
4272-
if flags1 != flags then
4273-
syntaxError(em"Only access modifiers are allowed on enum $where")
4274-
mods.withFlags(flags1)
4275-
else mods
4266+
private def checkAccessOnly(mods: Modifiers, caseStr: String): Modifiers =
4267+
// We allow `infix` and `into` on `enum` definitions.
4268+
// Syntax rules disallow these soft infix modifiers on `case`s.
4269+
val flags = mods.flags
4270+
var flags1 = flags
4271+
for mod <- mods.mods do
4272+
if !mod.flags.isOneOf(AccessFlags | Enum | Infix | Into) then
4273+
syntaxError(em"This modifier is not allowed on an enum$caseStr", mod.span)
4274+
flags1 = flags1 &~ mod.flags
4275+
if flags1 != flags then mods.withFlags(flags1) else mods
42764276

42774277
/** EnumDef ::= id ClassConstr InheritClauses EnumBody
42784278
*/
42794279
def enumDef(start: Offset, mods: Modifiers): TypeDef = atSpan(start, nameStart) {
4280-
val mods1 = checkAccessOnly(mods, "definitions")
4280+
val mods1 = checkAccessOnly(mods, "")
42814281
val modulName = ident()
42824282
val clsName = modulName.toTypeName
42834283
val constr = classConstr(ParamOwner.Class)
@@ -4288,7 +4288,7 @@ object Parsers {
42884288
/** EnumCase = `case' (id ClassConstr [`extends' ConstrApps] | ids)
42894289
*/
42904290
def enumCase(start: Offset, mods: Modifiers): DefTree = {
4291-
val mods1 = checkAccessOnly(mods, "cases") | EnumCase
4291+
val mods1 = checkAccessOnly(mods, " case") | EnumCase
42924292
accept(CASE)
42934293

42944294
atSpan(start, nameStart) {

tests/neg/i5525.check

Lines changed: 71 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,91 @@
1-
-- Error: tests/neg/i5525.scala:1:14 -----------------------------------------------------------------------------------
2-
1 |abstract enum Foo1 {} // error: only access modifiers allowed
3-
| ^^^^
4-
| Only access modifiers are allowed on enum definitions
5-
-- Error: tests/neg/i5525.scala:2:14 -----------------------------------------------------------------------------------
6-
2 |final enum Foo2 {} // error: only access modifiers allowed
7-
| ^^^^
8-
| Only access modifiers are allowed on enum definitions
9-
-- Error: tests/neg/i5525.scala:3:14 -----------------------------------------------------------------------------------
10-
3 |sealed enum Foo3 {} // error: only access modifiers allowed
11-
| ^^^^
12-
| Only access modifiers are allowed on enum definitions
13-
-- Error: tests/neg/i5525.scala:4:14 -----------------------------------------------------------------------------------
14-
4 |implicit enum Foo4 {} // error: only access modifiers allowed
15-
| ^^^^
16-
| Only access modifiers are allowed on enum definitions
17-
-- Error: tests/neg/i5525.scala:5:14 -----------------------------------------------------------------------------------
18-
5 |lazy enum Foo5 {} // error: only access modifiers allowed
19-
| ^^^^
20-
| Only access modifiers are allowed on enum definitions
21-
-- Error: tests/neg/i5525.scala:6:14 -----------------------------------------------------------------------------------
22-
6 |override enum Foo7 {} // error: only access modifiers allowed
23-
| ^^^^
24-
| Only access modifiers are allowed on enum definitions
25-
-- Error: tests/neg/i5525.scala:7:14 -----------------------------------------------------------------------------------
26-
7 |inline enum Foo8 {} // error: only access modifiers allowed
27-
| ^^^^
28-
| Only access modifiers are allowed on enum definitions
29-
-- Error: tests/neg/i5525.scala:8:14 -----------------------------------------------------------------------------------
30-
8 |opaque enum Foo9 {} // error: only access modifiers allowed
31-
| ^^^^
32-
| Only access modifiers are allowed on enum definitions
33-
-- Error: tests/neg/i5525.scala:11:12 ----------------------------------------------------------------------------------
1+
-- Error: tests/neg/i5525.scala:1:0 ------------------------------------------------------------------------------------
2+
1 |abstract enum Foo1 { case C } // error: only access modifiers allowed
3+
|^^^^^^^^
4+
|This modifier is not allowed on an enum
5+
-- Error: tests/neg/i5525.scala:2:0 ------------------------------------------------------------------------------------
6+
2 |final enum Foo2 { case C } // error: only access modifiers allowed
7+
|^^^^^
8+
|This modifier is not allowed on an enum
9+
-- Error: tests/neg/i5525.scala:3:0 ------------------------------------------------------------------------------------
10+
3 |sealed enum Foo3 { case C } // error: only access modifiers allowed
11+
|^^^^^^
12+
|This modifier is not allowed on an enum
13+
-- Error: tests/neg/i5525.scala:4:0 ------------------------------------------------------------------------------------
14+
4 |implicit enum Foo4 { case C } // error: only access modifiers allowed
15+
|^^^^^^^^
16+
|This modifier is not allowed on an enum
17+
-- Error: tests/neg/i5525.scala:5:0 ------------------------------------------------------------------------------------
18+
5 |lazy enum Foo5 { case C } // error: only access modifiers allowed
19+
|^^^^
20+
|This modifier is not allowed on an enum
21+
-- Error: tests/neg/i5525.scala:6:0 ------------------------------------------------------------------------------------
22+
6 |override enum Foo7 { case C } // error: only access modifiers allowed
23+
|^^^^^^^^
24+
|This modifier is not allowed on an enum
25+
-- Error: tests/neg/i5525.scala:7:0 ------------------------------------------------------------------------------------
26+
7 |inline enum Foo8 { case C } // error: only access modifiers allowed
27+
|^^^^^^
28+
|This modifier is not allowed on an enum
29+
-- Error: tests/neg/i5525.scala:8:0 ------------------------------------------------------------------------------------
30+
8 |opaque enum Foo9 { case C } // error: only access modifiers allowed
31+
|^^^^^^
32+
|This modifier is not allowed on an enum
33+
-- Error: tests/neg/i5525.scala:11:2 -----------------------------------------------------------------------------------
3434
11 | abstract case C1() // error: only access modifiers allowed
35-
| ^^^^
36-
| Only access modifiers are allowed on enum cases
37-
-- Error: tests/neg/i5525.scala:12:12 ----------------------------------------------------------------------------------
35+
| ^^^^^^^^
36+
| This modifier is not allowed on an enum case
37+
-- Error: tests/neg/i5525.scala:12:2 -----------------------------------------------------------------------------------
3838
12 | final case C2() // error: only access modifiers allowed
39-
| ^^^^
40-
| Only access modifiers are allowed on enum cases
41-
-- Error: tests/neg/i5525.scala:13:12 ----------------------------------------------------------------------------------
39+
| ^^^^^
40+
| This modifier is not allowed on an enum case
41+
-- Error: tests/neg/i5525.scala:13:2 -----------------------------------------------------------------------------------
4242
13 | sealed case C3() // error: only access modifiers allowed
43-
| ^^^^
44-
| Only access modifiers are allowed on enum cases
45-
-- Error: tests/neg/i5525.scala:14:12 ----------------------------------------------------------------------------------
43+
| ^^^^^^
44+
| This modifier is not allowed on an enum case
45+
-- Error: tests/neg/i5525.scala:14:2 -----------------------------------------------------------------------------------
4646
14 | implicit case C4() // error: only access modifiers allowed
47-
| ^^^^
48-
| Only access modifiers are allowed on enum cases
49-
-- Error: tests/neg/i5525.scala:15:12 ----------------------------------------------------------------------------------
47+
| ^^^^^^^^
48+
| This modifier is not allowed on an enum case
49+
-- Error: tests/neg/i5525.scala:15:2 -----------------------------------------------------------------------------------
5050
15 | lazy case C5() // error: only access modifiers allowed
51-
| ^^^^
52-
| Only access modifiers are allowed on enum cases
53-
-- Error: tests/neg/i5525.scala:16:12 ----------------------------------------------------------------------------------
51+
| ^^^^
52+
| This modifier is not allowed on an enum case
53+
-- Error: tests/neg/i5525.scala:16:2 -----------------------------------------------------------------------------------
5454
16 | override case C7() // error: only access modifiers allowed
55-
| ^^^^
56-
| Only access modifiers are allowed on enum cases
57-
-- Error: tests/neg/i5525.scala:22:12 ----------------------------------------------------------------------------------
55+
| ^^^^^^^^
56+
| This modifier is not allowed on an enum case
57+
-- Error: tests/neg/i5525.scala:22:2 -----------------------------------------------------------------------------------
5858
22 | abstract case C1 // error: only access modifiers allowed
59-
| ^^^^
60-
| Only access modifiers are allowed on enum cases
61-
-- Error: tests/neg/i5525.scala:23:12 ----------------------------------------------------------------------------------
59+
| ^^^^^^^^
60+
| This modifier is not allowed on an enum case
61+
-- Error: tests/neg/i5525.scala:23:2 -----------------------------------------------------------------------------------
6262
23 | final case C2 // error: only access modifiers allowed
63-
| ^^^^
64-
| Only access modifiers are allowed on enum cases
65-
-- Error: tests/neg/i5525.scala:24:12 ----------------------------------------------------------------------------------
63+
| ^^^^^
64+
| This modifier is not allowed on an enum case
65+
-- Error: tests/neg/i5525.scala:24:2 -----------------------------------------------------------------------------------
6666
24 | sealed case C3 // error: only access modifiers allowed
67-
| ^^^^
68-
| Only access modifiers are allowed on enum cases
69-
-- Error: tests/neg/i5525.scala:25:12 ----------------------------------------------------------------------------------
67+
| ^^^^^^
68+
| This modifier is not allowed on an enum case
69+
-- Error: tests/neg/i5525.scala:25:2 -----------------------------------------------------------------------------------
7070
25 | implicit case C4 // error: only access modifiers allowed
71-
| ^^^^
72-
| Only access modifiers are allowed on enum cases
73-
-- Error: tests/neg/i5525.scala:26:12 ----------------------------------------------------------------------------------
71+
| ^^^^^^^^
72+
| This modifier is not allowed on an enum case
73+
-- Error: tests/neg/i5525.scala:26:2 -----------------------------------------------------------------------------------
7474
26 | lazy case C5 // error: only access modifiers allowed
75-
| ^^^^
76-
| Only access modifiers are allowed on enum cases
77-
-- Error: tests/neg/i5525.scala:27:12 ----------------------------------------------------------------------------------
75+
| ^^^^
76+
| This modifier is not allowed on an enum case
77+
-- Error: tests/neg/i5525.scala:27:2 -----------------------------------------------------------------------------------
7878
27 | override case C7 // error: only access modifiers allowed
79-
| ^^^^
80-
| Only access modifiers are allowed on enum cases
79+
| ^^^^^^^^
80+
| This modifier is not allowed on an enum case
8181
-- Error: tests/neg/i5525.scala:33:12 ----------------------------------------------------------------------------------
8282
33 | inline case C10() // error: only access modifiers allowed
8383
| ^^^^
8484
| end of statement expected but 'case' found
85-
-- Error: tests/neg/i5525.scala:36:11 ----------------------------------------------------------------------------------
85+
-- Error: tests/neg/i5525.scala:36:0 -----------------------------------------------------------------------------------
8686
36 |final enum Foo13 { // error: only access modifiers and `into` allowed
87-
| ^^^^^
88-
| Only access modifiers are allowed on enum definitions
87+
|^^^^^
88+
|This modifier is not allowed on an enum
8989
-- Error: tests/neg/i5525.scala:42:8 -----------------------------------------------------------------------------------
9090
42 | infix case C2 extends Foo14[Int, Int] // error // error
9191
| ^^^^

tests/neg/i5525.scala

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
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-
override enum Foo7 {} // error: only access modifiers allowed
7-
inline enum Foo8 {} // error: only access modifiers allowed
8-
opaque enum Foo9 {} // error: only access modifiers allowed
1+
abstract enum Foo1 { case C } // error: only access modifiers allowed
2+
final enum Foo2 { case C } // error: only access modifiers allowed
3+
sealed enum Foo3 { case C } // error: only access modifiers allowed
4+
implicit enum Foo4 { case C } // error: only access modifiers allowed
5+
lazy enum Foo5 { case C } // error: only access modifiers allowed
6+
override enum Foo7 { case C } // error: only access modifiers allowed
7+
inline enum Foo8 { case C } // error: only access modifiers allowed
8+
opaque enum Foo9 { case C } // error: only access modifiers allowed
99

1010
enum Foo10 {
1111
abstract case C1() // error: only access modifiers allowed

tests/neg/i5525b.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//> using options -language:experimental.erasedDefinitions
22

3-
erased enum Foo6 {} // error: only access modifiers allowed
3+
erased enum Foo6 { case C } // error: only access modifiers allowed
44

55
enum Foo10 { // error: Enumerations must contain at least one case
66
erased case C6() // error // error

0 commit comments

Comments
 (0)