diff --git a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala index ac8e0cd8cc64..fcdbae3e8dc6 100644 --- a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala +++ b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala @@ -1296,7 +1296,7 @@ object Parsers { def possibleTemplateStart(isNew: Boolean = false): Unit = in.observeColonEOL() - if in.token == COLONEOL || in.token == WITH then + if in.token == COLONEOL then if in.lookahead.isIdent(nme.end) then in.token = NEWLINE else in.nextToken() @@ -2333,7 +2333,7 @@ object Parsers { possibleTemplateStart() val parents = if in.isNestedStart then Nil - else constrApps(commaOK = false) + else constrApps(exclude = COMMA) colonAtEOLOpt() possibleTemplateStart(isNew = true) parents match { @@ -3536,7 +3536,7 @@ object Parsers { val parents = if (in.token == EXTENDS) { in.nextToken() - constrApps(commaOK = true) + constrApps() } else Nil Template(constr, parents, Nil, EmptyValDef, Nil) @@ -3670,16 +3670,16 @@ object Parsers { /** ConstrApps ::= ConstrApp ({‘,’ ConstrApp} | {‘with’ ConstrApp}) */ - def constrApps(commaOK: Boolean): List[Tree] = + def constrApps(exclude: Token = EMPTY): List[Tree] = val t = constrApp() val ts = - if in.token == WITH || commaOK && in.token == COMMA then + val tok = in.token + if (tok == WITH || tok == COMMA) && tok != exclude then in.nextToken() - constrApps(commaOK) + constrApps(exclude = if tok == WITH then COMMA else WITH) else Nil t :: ts - /** `{`with` ConstrApp} but no EOL allowed after `with`. */ def withConstrApps(): List[Tree] = @@ -3704,7 +3704,7 @@ object Parsers { in.sourcePos()) Nil } - else constrApps(commaOK = true) + else constrApps() } else Nil newLinesOptWhenFollowedBy(nme.derives) @@ -3758,8 +3758,7 @@ object Parsers { /** with Template, with EOL interpreted */ def withTemplate(constr: DefDef, parents: List[Tree]): Template = - if in.token != WITH then syntaxError(em"`with` expected") - possibleTemplateStart() // consumes a WITH token + accept(WITH) val (self, stats) = templateBody() Template(constr, parents, Nil, self, stats) .withSpan(Span(constr.span.orElse(parents.head.span).start, in.lastOffset)) diff --git a/tests/neg/language-import.scala b/tests/neg/language-import.scala index cb9fccc02061..8f498f091f75 100644 --- a/tests/neg/language-import.scala +++ b/tests/neg/language-import.scala @@ -1,4 +1,4 @@ -object a with +object a: val l = _root_.scala.language import l.noAutoTupling // error import l.experimental.genericNumberLiterals // error @@ -7,16 +7,16 @@ object a with val language = b import language.experimental.genericNumberLiterals // error -object b with +object b: val strictEquality = 22 - object experimental with + object experimental: val genericNumberLiterals = 22 -object c with +object c: val language = b import b.strictEquality -object d with +object d: import language.experimental.genericNumberLiterals // ok import scala.language.noAutoTupling // ok import _root_.scala.language.strictEquality // ok