Skip to content

Commit 001eb6d

Browse files
authored
Enforce -new-syntax under -language:future (#23443)
No more Java style conditionals and loops. Always requires then/do. Also: Enforce latest given syntax, instead of the intermediate one. The two were rolled into one PR because they require changing the same submodules.
2 parents 3b9a505 + ff889c4 commit 001eb6d

28 files changed

+168
-180
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ enum MigrationVersion(val warnFrom: SourceVersion, val errorFrom: SourceVersion)
3131
case ImportRename extends MigrationVersion(future, future)
3232
case ParameterEnclosedByParenthesis extends MigrationVersion(future, future)
3333
case XmlLiteral extends MigrationVersion(future, future)
34-
case GivenSyntax extends MigrationVersion(future, never)
34+
case GivenSyntax extends MigrationVersion(future, future)
3535
case ImplicitParamsWithoutUsing extends MigrationVersion(`3.7`, future)
3636
case Scala2Implicits extends MigrationVersion(future, future)
3737

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Feature.isPreviewEnabled
88
import util.Property
99

1010
enum SourceVersion:
11-
case `3.0-migration`, `3.0`
11+
case `3.0-migration`, `3.0`
1212
case `3.1-migration`, `3.1`
1313
case `3.2-migration`, `3.2`
1414
case `3.3-migration`, `3.3`
@@ -44,8 +44,10 @@ enum SourceVersion:
4444
def enablesNamedTuples = isAtLeast(`3.7`)
4545
def enablesBetterFors(using Context) = isAtLeast(`3.7`) && isPreviewEnabled
4646

47+
def requiresNewSyntax = isAtLeast(future)
48+
4749
object SourceVersion extends Property.Key[SourceVersion]:
48-
50+
4951
/* The default source version used by the built compiler */
5052
val defaultSourceVersion = `3.7`
5153

compiler/src/dotty/tools/dotc/core/CheckRealizable.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ class CheckRealizable(using Context) {
196196
}
197197
}
198198
if sourceVersion.isAtLeast(future) then
199-
// check fields only from version 3.x.
199+
// check fields only from version 3.future.
200200
// Reason: An embedded field could well be nullable, which means it
201201
// should not be part of a path and need not be checked; but we cannot recognize
202202
// this situation until we have a typesystem that tracks nullability.

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -393,10 +393,9 @@ object Parsers {
393393
syntaxError(em"""This construct is not allowed under $option.${rewriteNotice(`3.0-migration`, option)}""", span)
394394

395395
def rewriteToNewSyntax(span: Span = Span(in.offset)): Boolean = {
396-
if (in.newSyntax) {
397-
if (in.rewrite) return true
398-
syntaxVersionError("-new-syntax", span)
399-
}
396+
if in.newSyntax then
397+
if in.rewrite then return true
398+
syntaxVersionError("-new-syntax or -language:future", span)
400399
false
401400
}
402401

@@ -2559,7 +2558,7 @@ object Parsers {
25592558
}
25602559
}
25612560

2562-
/** `if' `(' Expr `)' {nl} Expr [[semi] else Expr]
2561+
/** `if' `(' Expr `)' {nl} Expr [[semi] else Expr] -- Scala 2 compat
25632562
* `if' Expr `then' Expr [[semi] else Expr]
25642563
*/
25652564
def ifExpr(start: Offset, mkIf: (Tree, Tree, Tree) => If): If =

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import scala.collection.mutable
1717
import scala.collection.immutable.SortedMap
1818
import rewrites.Rewrites.patch
1919
import config.Feature
20-
import config.Feature.migrateTo3
20+
import config.Feature.{migrateTo3, sourceVersion}
2121
import config.SourceVersion.{`3.0`, `3.0-migration`}
2222
import config.MigrationVersion
2323
import reporting.{NoProfile, Profile, Message}
@@ -184,7 +184,7 @@ object Scanners {
184184

185185
val rewrite = ctx.settings.rewrite.value.isDefined
186186
val oldSyntax = ctx.settings.oldSyntax.value
187-
val newSyntax = ctx.settings.newSyntax.value
187+
val newSyntax = ctx.settings.newSyntax.value || sourceVersion.requiresNewSyntax
188188

189189
val rewriteToIndent = ctx.settings.indent.value && rewrite
190190
val rewriteNoIndent = ctx.settings.noindent.value && rewrite

tests/warn/abstract-givens-new.scala renamed to tests/neg/abstract-givens-new.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class C:
44

55
trait T:
66
given Int is C // ok
7-
given intC: Int is C // warn
7+
given intC: Int is C // error
88
given intC2: (Int is C)() // ok
99
given intC3: Int is C {} // also ok
1010

tests/neg/context-bounds-migration-future.check

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,3 @@
44
| method foo does not take more parameters
55
|
66
| longer explanation available when compiling with `-explain`
7-
-- Warning: tests/neg/context-bounds-migration-future.scala:6:6 --------------------------------------------------------
8-
6 |given [T]: C[T] = C[T]()
9-
| ^
10-
| This old given syntax is no longer supported; use `=>` instead of `:`

tests/neg/context-bounds-migration-future.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
class C[T]
44
def foo[X: C] = ()
55

6-
given [T]: C[T] = C[T]()
6+
given [T] => C[T] = C[T]()
77

88
def Test =
99
foo(C[Int]()) // error

0 commit comments

Comments
 (0)