Skip to content

Follow up to #6002 #6067

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

Merged
merged 1 commit into from
Mar 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,8 @@ object desugar {
case _ => false
}

def namePos = cdef.sourcePos.withSpan(cdef.nameSpan)

val isObject = mods.is(Module)
val isCaseClass = mods.is(Case) && !isObject
val isCaseObject = mods.is(Case) && isObject
Expand All @@ -361,10 +363,10 @@ object desugar {
val constrVparamss =
if (originalVparamss.isEmpty) { // ensure parameter list is non-empty
if (isCaseClass && originalTparams.isEmpty)
ctx.error(CaseClassMissingParamList(cdef), cdef.sourcePos.withSpan(cdef.nameSpan))
ctx.error(CaseClassMissingParamList(cdef), namePos)
ListOfNil
} else if (isCaseClass && originalVparamss.head.exists(_.mods.is(Implicit))) {
ctx.error("Case classes should have a non-implicit parameter list", cdef.sourcePos.withSpan(cdef.nameSpan))
ctx.error("Case classes should have a non-implicit parameter list", namePos)
ListOfNil
}
else originalVparamss.nestedMap(toDefParam)
Expand All @@ -391,6 +393,8 @@ object desugar {
val stats = impl.body.map(expandConstructor)
if (isEnum) {
val (enumCases, enumStats) = stats.partition(DesugarEnums.isEnumCase)
if (enumCases.isEmpty)
ctx.error("Enumerations must constain at least one case", namePos)
val enumCompanionRef = new TermRefTree()
val enumImport = Import(impliedOnly = false, enumCompanionRef, enumCases.flatMap(caseIds))
(enumImport :: enumStats, enumCases, enumCompanionRef)
Expand Down
31 changes: 14 additions & 17 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,9 @@ object Parsers {
}

private[this] var inEnum = false
private def withinEnum[T](isEnum: Boolean)(body: => T): T = {
private def withinEnum[T](body: => T): T = {
val saved = inEnum
inEnum = isEnum
inEnum = true
try body
finally inEnum = saved
}
Expand Down Expand Up @@ -1586,7 +1586,7 @@ object Parsers {
case parent :: Nil if in.token != LBRACE =>
reposition(if (parent.isType) ensureApplied(wrapNew(parent)) else parent)
case _ =>
New(reposition(templateBodyOpt(emptyConstructor, parents, Nil, isEnum = false)))
New(reposition(templateBodyOpt(emptyConstructor, parents, Nil)))
}
}

Expand Down Expand Up @@ -2555,12 +2555,7 @@ object Parsers {
val modName = ident()
val clsName = modName.toTypeName
val constr = classConstr()
val templ = templateOpt(constr, isEnum = true)
templ match {
case Template(_, _, _, List(EmptyTree)) =>
syntaxError("enum body should not be empty.", start)
case _ =>
}
val templ = template(constr, isEnum = true)
finalizeDef(TypeDef(clsName, templ), addMod(mods, enumMod), start)
}

Expand Down Expand Up @@ -2633,7 +2628,7 @@ object Parsers {
val tparams1 = tparams.map(tparam => tparam.withMods(tparam.mods | PrivateLocal))
val vparamss1 = vparamss.map(_.map(vparam =>
vparam.withMods(vparam.mods &~ Param | ParamAccessor | PrivateLocal)))
val templ = templateBodyOpt(makeConstructor(tparams1, vparamss1), parents, Nil, isEnum = false)
val templ = templateBodyOpt(makeConstructor(tparams1, vparamss1), parents, Nil)
if (tparams.isEmpty && vparamss.isEmpty) ModuleDef(name, templ)
else TypeDef(name.toTypeName, templ)
}
Expand Down Expand Up @@ -2696,26 +2691,28 @@ object Parsers {
def template(constr: DefDef, isEnum: Boolean = false): Template = {
val (parents, derived) = inheritClauses()
newLineOptWhenFollowedBy(LBRACE)
if (isEnum && in.token != LBRACE)
syntaxErrorOrIncomplete(ExpectedTokenButFound(LBRACE, in.token))
templateBodyOpt(constr, parents, derived, isEnum)
if (isEnum) {
val (self, stats) = withinEnum(templateBody())
Template(constr, parents, derived, self, stats)
}
else templateBodyOpt(constr, parents, derived)
}

/** TemplateOpt = [Template]
*/
def templateOpt(constr: DefDef, isEnum: Boolean = false): Template = {
def templateOpt(constr: DefDef): Template = {
newLineOptWhenFollowedBy(LBRACE)
if (in.token == EXTENDS || isIdent(nme.derives) || in.token == LBRACE)
template(constr, isEnum)
template(constr)
else
Template(constr, Nil, Nil, EmptyValDef, Nil)
}

/** TemplateBody ::= [nl] `{' TemplateStatSeq `}'
*/
def templateBodyOpt(constr: DefDef, parents: List[Tree], derived: List[Tree], isEnum: Boolean): Template = {
def templateBodyOpt(constr: DefDef, parents: List[Tree], derived: List[Tree]): Template = {
val (self, stats) =
if (in.token == LBRACE) withinEnum(isEnum)(templateBody()) else (EmptyValDef, Nil)
if (in.token == LBRACE) templateBody() else (EmptyValDef, Nil)
Template(constr, parents, derived, self, stats)
}

Expand Down
7 changes: 6 additions & 1 deletion tests/neg/i5015.scala
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
enum A extends AnyRef { } // error
enum A extends AnyRef { } // error: missing case

enum B { def foo = 1 } // error: missing case

enum C // error: missing case
// error: '{' expected, but eof found