Skip to content

Commit 2983865

Browse files
committed
Avoid misleading error message
The error message falsely referred to missing support for early definitions in the example, but none were present. Fixes #14326
1 parent 7ce60e1 commit 2983865

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
@@ -3752,11 +3752,11 @@ object Parsers {
37523752
}
37533753
else Nil
37543754
possibleTemplateStart()
3755-
if (isEnum) {
3756-
val (self, stats) = withinEnum(templateBody())
3755+
if isEnum then
3756+
val (self, stats) = withinEnum(templateBody(parents))
37573757
Template(constr, parents, derived, self, stats)
3758-
}
3759-
else templateBodyOpt(constr, parents, derived)
3758+
else
3759+
templateBodyOpt(constr, parents, derived)
37603760
}
37613761

37623762
/** TemplateOpt = [Template]
@@ -3779,15 +3779,15 @@ object Parsers {
37793779
def templateBodyOpt(constr: DefDef, parents: List[Tree], derived: List[Tree]): Template =
37803780
val (self, stats) =
37813781
if in.isNestedStart then
3782-
templateBody()
3782+
templateBody(parents)
37833783
else
37843784
checkNextNotIndented()
37853785
(EmptyValDef, Nil)
37863786
Template(constr, parents, derived, self, stats)
37873787

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

@@ -4039,7 +4039,7 @@ object Parsers {
40394039
EmptyTree
40404040
}
40414041

4042-
override def templateBody(rewriteWithColon: Boolean): (ValDef, List[Thicket]) = {
4042+
override def templateBody(parents: List[Tree], rewriteWithColon: Boolean): (ValDef, List[Thicket]) = {
40434043
skipBraces()
40444044
(EmptyValDef, List(EmptyTree))
40454045
}

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)