Skip to content

Empty enum bodies are accepted #5015

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Blaisorblade opened this issue Aug 25, 2018 · 3 comments
Closed

Empty enum bodies are accepted #5015

Blaisorblade opened this issue Aug 25, 2018 · 3 comments

Comments

@Blaisorblade
Copy link
Contributor

Blaisorblade commented Aug 25, 2018

After enum A extends AnyRef the REPL thinks the line is not over and requires braces. The REPL should instead do the same it does for classes:

scala> class A extends AnyRef
// defined class A

scala> enum A extends AnyRef
     | {}
// defined class A

(noticed while testing #5008).

EDIT: enum A extends AnyRef {} is forbidden by the grammar, see below.

@allanrenucci
Copy link
Contributor

This is a parser issue:

enum Foo extends AnyRef
-- [E040] Syntax Error: tests/allan/Test.scala:99:23 ---------------------------
99 |enum Foo extends AnyRef
   |                       ^
   |                       '{' expected, but eof found

@allanrenucci
Copy link
Contributor

Shouldn't we require enum to be non empty anyway?

@Blaisorblade
Copy link
Contributor Author

Blaisorblade commented Aug 25, 2018

Oh right, that's actually required by the grammar (so this is a parser issue):

EnumDef           ::=  id ClassConstr [‘extends’ [ConstrApps]] EnumBody
EnumBody          ::=  [nl] ‘{’ [SelfType] EnumStat {semi EnumStat} ‘}’
EnumStat          ::=  TemplateStat
                    |  {Annotation [nl]} {Modifier} EnumCase
EnumCase          ::=  ‘case’ (id ClassConstr [‘extends’ ConstrApps]] | ids)

We should additionally require at least one EnumCase (in either Parser or Desugar), lest people write

enum Root {
  case class Foo()
}

Defining case classes inside enums is legal but is most likely a typo. It seems hard to forbid (for consistency reasons) since enums can have arbitrary members, tho maybe we should anyway — this looks very much likely a future wart.

@Blaisorblade Blaisorblade changed the title REPL multi-row input does not handle correctly enum Empty enum bodies are accepted Aug 25, 2018
liufengyun added a commit to dotty-staging/dotty that referenced this issue Mar 1, 2019
liufengyun added a commit that referenced this issue Mar 6, 2019
Fix #5015: disallow empty enum definition
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants