Skip to content

Commit 36d94f3

Browse files
oderskyolsdavis
authored andcommitted
Avoid misleading error message
The error message falsely referred to missing support for early definitions in the example, but none were present. Fixes scala#14326
1 parent a938020 commit 36d94f3

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3758,11 +3758,11 @@ object Parsers {
37583758
}
37593759
else Nil
37603760
possibleTemplateStart()
3761-
if (isEnum) {
3762-
val (self, stats) = withinEnum(templateBody())
3761+
if isEnum then
3762+
val (self, stats) = withinEnum(templateBody(parents))
37633763
Template(constr, parents, derived, self, stats)
3764-
}
3765-
else templateBodyOpt(constr, parents, derived)
3764+
else
3765+
templateBodyOpt(constr, parents, derived)
37663766
}
37673767

37683768
/** TemplateOpt = [Template]
@@ -3785,15 +3785,15 @@ object Parsers {
37853785
def templateBodyOpt(constr: DefDef, parents: List[Tree], derived: List[Tree]): Template =
37863786
val (self, stats) =
37873787
if in.isNestedStart then
3788-
templateBody()
3788+
templateBody(parents)
37893789
else
37903790
checkNextNotIndented()
37913791
(EmptyValDef, Nil)
37923792
Template(constr, parents, derived, self, stats)
37933793

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

@@ -4045,7 +4045,7 @@ object Parsers {
40454045
EmptyTree
40464046
}
40474047

4048-
override def templateBody(rewriteWithColon: Boolean): (ValDef, List[Thicket]) = {
4048+
override def templateBody(parents: List[Tree], rewriteWithColon: Boolean): (ValDef, List[Thicket]) = {
40494049
skipBraces()
40504050
(EmptyValDef, List(EmptyTree))
40514051
}

tests/neg/parent-refinement.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- Error: tests/neg/parent-refinement.scala:5:2 ------------------------------------------------------------------------
2+
5 | with Ordered[Year] { // error
3+
| ^^^^
4+
| end of toplevel definition expected but 'with' found

tests/neg/parent-refinement.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
trait Id { type Value }
3+
case class Year(value: Int) extends AnyVal
4+
with Id { type Value = Int }
5+
with Ordered[Year] { // error
6+
7+
}

0 commit comments

Comments
 (0)