Skip to content

Commit 0657db9

Browse files
committed
Have -new-syntax and -old-syntax options enforce syntax version
1 parent 6d6180d commit 0657db9

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

compiler/src/dotty/tools/dotc/config/ScalaSettings.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ class ScalaSettings extends Settings.SettingGroup {
4848
val silentWarnings: Setting[Boolean] = BooleanSetting("-nowarn", "Silence all warnings.") withAbbreviation "--no-warnings"
4949
val fromTasty: Setting[Boolean] = BooleanSetting("-from-tasty", "Compile classes from tasty in classpath. The arguments are used as class names.") withAbbreviation "--from-tasty"
5050

51-
val newSyntax: Setting[Boolean] = BooleanSetting("-new-syntax", "When used with -rewrite, produces control expressions without parentheses")
52-
val oldSyntax: Setting[Boolean] = BooleanSetting("-old-syntax", "when used with -rewrite, produces control expressions with parentheses")
51+
val newSyntax: Setting[Boolean] = BooleanSetting("-new-syntax", "Require `then` and `do` in control expressions")
52+
val oldSyntax: Setting[Boolean] = BooleanSetting("-old-syntax", "Require `(...)` around conditions")
5353

5454
/** Decompiler settings */
5555
val printTasty: Setting[Boolean] = BooleanSetting("-print-tasty", "Prints the raw tasty.") withAbbreviation "--print-tasty"

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

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,31 @@ object Parsers {
351351
accept(SEMI)
352352
}
353353

354+
def rewriteNotice(additionalOption: String = "") = {
355+
val optionStr = if (additionalOption.isEmpty) "" else " " ++ additionalOption
356+
i"\nThis construct can be rewritten automatically under$optionStr -rewrite."
357+
}
358+
359+
def syntaxVersionError(option: String, span: Span) = {
360+
syntaxError(em"""This construct is not allowed under $option.${rewriteNotice(option)}""", span)
361+
}
362+
363+
def rewriteToNewSyntax(span: Span = Span(in.offset)): Boolean = {
364+
if (in.newSyntax) {
365+
if (in.rewrite) return true
366+
syntaxVersionError("-new-syntax", span)
367+
}
368+
false
369+
}
370+
371+
def rewriteToOldSyntax(span: Span = Span(in.offset)): Boolean = {
372+
if (in.oldSyntax) {
373+
if (in.rewrite) return true
374+
syntaxVersionError("-old-syntax", span)
375+
}
376+
false
377+
}
378+
354379
def errorTermTree: Literal = atSpan(in.offset) { Literal(Constant(null)) }
355380

356381
private[this] var inFunReturnType = false
@@ -1367,15 +1392,15 @@ object Parsers {
13671392
expr1Rest(postfixExprRest(simpleExprRest(t)), Location.ElseWhere)
13681393
}
13691394
if (in.token == altToken) {
1370-
if (in.rewriteOldSyntax) revertToParens(t)
1395+
if (rewriteToOldSyntax()) revertToParens(t)
13711396
in.nextToken()
13721397
}
1373-
else if (in.rewriteNewSyntax)
1398+
else if (rewriteToNewSyntax(t.span))
13741399
dropParensOrBraces(t.span.start, s"${tokenString(altToken)}")
13751400
t
13761401
} else {
13771402
val t = inSepRegion(LPAREN, RPAREN)(expr())
1378-
if (in.rewriteOldSyntax) revertToParens(t)
1403+
if (rewriteToOldSyntax(t.span.startPos)) revertToParens(t)
13791404
accept(altToken)
13801405
t
13811406
}
@@ -1469,7 +1494,7 @@ object Parsers {
14691494
in.errorOrMigrationWarning(
14701495
i"""`do <body> while <cond>' is no longer supported,
14711496
|use `while ({<body> ; <cond>}) ()' instead.
1472-
|The statement can be rewritten automatically under -language:Scala2 -rewrite.
1497+
|${rewriteNotice("-language:Scala2")}
14731498
""")
14741499
val start = in.skipToken()
14751500
atSpan(start) {
@@ -2009,7 +2034,8 @@ object Parsers {
20092034
val closingOnNewLine = in.isAfterLineEnd()
20102035
accept(leading + 1)
20112036
openParens.change(leading, -1)
2012-
if (in.rewriteNewSyntax && enumsOnNewLine == (leading == LBRACE)) { // Don't rewrite if that would change meaning of newlines
2037+
if (rewriteToNewSyntax(Span(start)) && enumsOnNewLine == (leading == LBRACE)) {
2038+
// Don't rewrite if that would change meaning of newlines
20132039
newLinesOpt()
20142040
dropParensOrBraces(start, if (in.token == YIELD || in.token == DO) "" else "do")
20152041
}
@@ -2024,7 +2050,7 @@ object Parsers {
20242050
val ts = // If we use indent syntax, the distinction below should no longer be necessary
20252051
if (impliedBraces) enumerators()
20262052
else inSepRegion(LPAREN, RPAREN)(enumerators())
2027-
if (in.rewriteOldSyntax && ts.nonEmpty) {
2053+
if (rewriteToOldSyntax(Span(start)) && ts.nonEmpty) {
20282054
if (impliedBraces) {
20292055
patch(source, Span(forEnd), " {")
20302056
patch(source, Span(in.offset), "} ")
@@ -2042,7 +2068,7 @@ object Parsers {
20422068
ForYield(enums, expr())
20432069
}
20442070
else if (in.token == DO) {
2045-
if (in.rewriteOldSyntax) dropTerminator()
2071+
if (rewriteToOldSyntax()) dropTerminator()
20462072
in.nextToken()
20472073
ForDo(enums, expr())
20482074
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,8 @@ object Scanners {
220220
val keepComments: Boolean = !ctx.settings.YdropComments.value
221221

222222
val rewrite = ctx.settings.rewrite.value.isDefined
223-
val rewriteNewSyntax = rewrite && ctx.settings.newSyntax.value
224-
val rewriteOldSyntax = rewrite && ctx.settings.oldSyntax.value
223+
val oldSyntax = ctx.settings.oldSyntax.value
224+
val newSyntax = ctx.settings.newSyntax.value
225225

226226
/** All doc comments kept by their end position in a `Map` */
227227
private[this] var docstringMap: SortedMap[Int, Comment] = SortedMap.empty
@@ -237,7 +237,7 @@ object Scanners {
237237
def nextPos: Int = (lookahead.getc(): @switch) match {
238238
case ' ' | '\t' => nextPos
239239
case CR | LF | FF =>
240-
// if we encounter line delimitng whitespace we don't count it, since
240+
// if we encounter line delimiting whitespace we don't count it, since
241241
// it seems not to affect positions in source
242242
nextPos - 1
243243
case _ => lookahead.charOffset - 1

0 commit comments

Comments
 (0)