Skip to content

Commit cdbcf68

Browse files
committed
Drop with after class
Disallow `with` after class. It was still allowed before by accident since the relevant code slipped through a revert commit.
1 parent 0273336 commit cdbcf68

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

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

+9-10
Original file line numberDiff line numberDiff line change
@@ -1296,7 +1296,7 @@ object Parsers {
12961296

12971297
def possibleTemplateStart(isNew: Boolean = false): Unit =
12981298
in.observeColonEOL()
1299-
if in.token == COLONEOL || in.token == WITH then
1299+
if in.token == COLONEOL then
13001300
if in.lookahead.isIdent(nme.end) then in.token = NEWLINE
13011301
else
13021302
in.nextToken()
@@ -2333,7 +2333,7 @@ object Parsers {
23332333
possibleTemplateStart()
23342334
val parents =
23352335
if in.isNestedStart then Nil
2336-
else constrApps(commaOK = false)
2336+
else constrApps(exclude = COMMA)
23372337
colonAtEOLOpt()
23382338
possibleTemplateStart(isNew = true)
23392339
parents match {
@@ -3536,7 +3536,7 @@ object Parsers {
35363536
val parents =
35373537
if (in.token == EXTENDS) {
35383538
in.nextToken()
3539-
constrApps(commaOK = true)
3539+
constrApps()
35403540
}
35413541
else Nil
35423542
Template(constr, parents, Nil, EmptyValDef, Nil)
@@ -3670,16 +3670,16 @@ object Parsers {
36703670

36713671
/** ConstrApps ::= ConstrApp ({‘,’ ConstrApp} | {‘with’ ConstrApp})
36723672
*/
3673-
def constrApps(commaOK: Boolean): List[Tree] =
3673+
def constrApps(exclude: Token = EMPTY): List[Tree] =
36743674
val t = constrApp()
36753675
val ts =
3676-
if in.token == WITH || commaOK && in.token == COMMA then
3676+
val tok = in.token
3677+
if (tok == WITH || tok == COMMA) && tok != exclude then
36773678
in.nextToken()
3678-
constrApps(commaOK)
3679+
constrApps(exclude = if tok == WITH then COMMA else WITH)
36793680
else Nil
36803681
t :: ts
36813682

3682-
36833683
/** `{`with` ConstrApp} but no EOL allowed after `with`.
36843684
*/
36853685
def withConstrApps(): List[Tree] =
@@ -3704,7 +3704,7 @@ object Parsers {
37043704
in.sourcePos())
37053705
Nil
37063706
}
3707-
else constrApps(commaOK = true)
3707+
else constrApps()
37083708
}
37093709
else Nil
37103710
newLinesOptWhenFollowedBy(nme.derives)
@@ -3758,8 +3758,7 @@ object Parsers {
37583758

37593759
/** with Template, with EOL <indent> interpreted */
37603760
def withTemplate(constr: DefDef, parents: List[Tree]): Template =
3761-
if in.token != WITH then syntaxError(em"`with` expected")
3762-
possibleTemplateStart() // consumes a WITH token
3761+
accept(WITH)
37633762
val (self, stats) = templateBody()
37643763
Template(constr, parents, Nil, self, stats)
37653764
.withSpan(Span(constr.span.orElse(parents.head.span).start, in.lastOffset))

tests/neg/language-import.scala

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
object a with
1+
object a:
22
val l = _root_.scala.language
33
import l.noAutoTupling // error
44
import l.experimental.genericNumberLiterals // error
@@ -7,16 +7,16 @@ object a with
77
val language = b
88
import language.experimental.genericNumberLiterals // error
99

10-
object b with
10+
object b:
1111
val strictEquality = 22
12-
object experimental with
12+
object experimental:
1313
val genericNumberLiterals = 22
1414

15-
object c with
15+
object c:
1616
val language = b
1717
import b.strictEquality
1818

19-
object d with
19+
object d:
2020
import language.experimental.genericNumberLiterals // ok
2121
import scala.language.noAutoTupling // ok
2222
import _root_.scala.language.strictEquality // ok

0 commit comments

Comments
 (0)