Skip to content

Commit

Permalink
Avoid misleading error message
Browse files Browse the repository at this point in the history
The error message falsely referred to missing support for early definitions
in the example, but none were present.

Fixes #14326
  • Loading branch information
odersky committed Jan 23, 2022
1 parent 7ce60e1 commit 2983865
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
18 changes: 9 additions & 9 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3752,11 +3752,11 @@ object Parsers {
}
else Nil
possibleTemplateStart()
if (isEnum) {
val (self, stats) = withinEnum(templateBody())
if isEnum then
val (self, stats) = withinEnum(templateBody(parents))
Template(constr, parents, derived, self, stats)
}
else templateBodyOpt(constr, parents, derived)
else
templateBodyOpt(constr, parents, derived)
}

/** TemplateOpt = [Template]
Expand All @@ -3779,15 +3779,15 @@ object Parsers {
def templateBodyOpt(constr: DefDef, parents: List[Tree], derived: List[Tree]): Template =
val (self, stats) =
if in.isNestedStart then
templateBody()
templateBody(parents)
else
checkNextNotIndented()
(EmptyValDef, Nil)
Template(constr, parents, derived, self, stats)

def templateBody(rewriteWithColon: Boolean = true): (ValDef, List[Tree]) =
def templateBody(parents: List[Tree], rewriteWithColon: Boolean = true): (ValDef, List[Tree]) =
val r = inDefScopeBraces(templateStatSeq(), rewriteWithColon)
if in.token == WITH then
if in.token == WITH && parents.isEmpty then
syntaxError(EarlyDefinitionsNotSupported())
in.nextToken()
template(emptyConstructor)
Expand All @@ -3796,7 +3796,7 @@ object Parsers {
/** with Template, with EOL <indent> interpreted */
def withTemplate(constr: DefDef, parents: List[Tree]): Template =
accept(WITH)
val (self, stats) = templateBody(rewriteWithColon = false)
val (self, stats) = templateBody(parents, rewriteWithColon = false)
Template(constr, parents, Nil, self, stats)
.withSpan(Span(constr.span.orElse(parents.head.span).start, in.lastOffset))

Expand Down Expand Up @@ -4039,7 +4039,7 @@ object Parsers {
EmptyTree
}

override def templateBody(rewriteWithColon: Boolean): (ValDef, List[Thicket]) = {
override def templateBody(parents: List[Tree], rewriteWithColon: Boolean): (ValDef, List[Thicket]) = {
skipBraces()
(EmptyValDef, List(EmptyTree))
}
Expand Down
4 changes: 4 additions & 0 deletions tests/neg/parent-refinement.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Error: tests/neg/parent-refinement.scala:5:2 ------------------------------------------------------------------------
5 | with Ordered[Year] { // error
| ^^^^
| end of toplevel definition expected but 'with' found
7 changes: 7 additions & 0 deletions tests/neg/parent-refinement.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

trait Id { type Value }
case class Year(value: Int) extends AnyVal
with Id { type Value = Int }
with Ordered[Year] { // error

}

0 comments on commit 2983865

Please sign in to comment.