Skip to content

Commit 55e82f6

Browse files
committed
Enforce -new-syntax under -language:future
No more Java style conditionals and loops. Always requires then/do.
1 parent 8dd0770 commit 55e82f6

File tree

15 files changed

+140
-146
lines changed

15 files changed

+140
-146
lines changed

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

@@ -2556,7 +2555,7 @@ object Parsers {
25562555
}
25572556
}
25582557

2559-
/** `if' `(' Expr `)' {nl} Expr [[semi] else Expr]
2558+
/** `if' `(' Expr `)' {nl} Expr [[semi] else Expr] -- Scala 2 compat
25602559
* `if' Expr `then' Expr [[semi] else Expr]
25612560
*/
25622561
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/pos/hylolib-cb/BitArray.scala

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,86 +22,79 @@ final class BitArray private (
2222

2323
/** Reserves enough storage to store `n` elements in `this`. */
2424
def reserveCapacity(n: Int, assumeUniqueness: Boolean = false): BitArray =
25-
if (n == 0) {
25+
if n == 0 then
2626
this
27-
} else {
27+
else
2828
val k = 1 + ((n - 1) >> 5)
29-
if (assumeUniqueness) {
29+
if assumeUniqueness then
3030
_bits = _bits.reserveCapacity(k, assumeUniqueness)
3131
this
32-
} else {
32+
else
3333
new BitArray(_bits.reserveCapacity(k), _count)
34-
}
35-
}
3634

3735
/** Adds a new element at the end of the array. */
3836
def append(bit: Boolean, assumeUniqueness: Boolean = false): BitArray =
3937
val result = if assumeUniqueness && (count < capacity) then this else copy(count + 1)
4038
val p = BitArray.Position(count)
41-
if (p.bucket >= _bits.count) {
39+
if p.bucket >= _bits.count then
4240
result._bits = _bits.append(if bit then 1 else 0)
43-
} else {
41+
else
4442
result.setValue(bit, p)
45-
}
4643
result._count += 1
4744
result
4845

4946
/** Removes and returns the last element, or returns `None` if the array is empty. */
5047
def popLast(assumeUniqueness: Boolean = false): (BitArray, Option[Boolean]) =
51-
if (isEmpty) {
48+
if isEmpty then
5249
(this, None)
53-
} else {
50+
else
5451
val result = if assumeUniqueness then this else copy()
5552
val bit = result.at(BitArray.Position(count))
5653
result._count -= 1
5754
(result, Some(bit))
58-
}
5955

6056
/** Removes all elements in the array, keeping allocated storage iff `keepStorage` is true. */
6157
def removeAll(
6258
keepStorage: Boolean = false,
6359
assumeUniqueness: Boolean = false
6460
): BitArray =
65-
if (isEmpty) {
61+
if isEmpty then
6662
this
67-
} else if (keepStorage) {
63+
else if keepStorage then
6864
val result = if assumeUniqueness then this else copy()
6965
result._bits.removeAll(keepStorage, assumeUniqueness = true)
7066
result._count = 0
7167
result
72-
} else {
68+
else
7369
BitArray()
74-
}
7570

7671
/** Returns `true` iff all elements in `this` are `false`. */
7772
def allFalse: Boolean =
78-
if (isEmpty) {
73+
if isEmpty then
7974
true
80-
} else {
75+
else
8176
val k = (count - 1) >> 5
8277
def loop(i: Int): Boolean =
83-
if (i == k) {
78+
if i == k then
8479
val m = (1 << (count & 31)) - 1
8580
(_bits.at(k) & m) == 0
86-
} else if (_bits.at(i) != 0) {
81+
else if _bits.at(i) != 0 then
8782
false
88-
} else {
83+
else
8984
loop(i + 1)
90-
}
9185
loop(0)
92-
}
9386

9487
/** Returns `true` iff all elements in `this` are `true`. */
9588
def allTrue: Boolean =
96-
if (isEmpty) {
89+
if isEmpty then {
9790
true
9891
} else {
9992
val k = (count - 1) >> 5
10093
def loop(i: Int): Boolean =
101-
if (i == k) {
94+
if i == k then {
10295
val m = (1 << (count & 31)) - 1
10396
(_bits.at(k) & m) == m
104-
} else if (_bits.at(i) != ~0) {
97+
} else if _bits.at(i) != ~0 then {
10598
false
10699
} else {
107100
loop(i + 1)
@@ -136,14 +129,14 @@ final class BitArray private (
136129
assumeUniqueness: Boolean = false
137130
): BitArray =
138131
require(this.count == other.count)
139-
if (isEmpty) {
132+
if isEmpty then {
140133
this
141134
} else {
142135
val result = if assumeUniqueness then this else copy()
143136
var u = assumeUniqueness
144137
val k = (count - 1) >> 5
145138

146-
for (i <- 0 until k) {
139+
for i <- 0 until k do {
147140
result._bits = result._bits.modifyAt(
148141
i, (n) => operation(n, other._bits.at(n)),
149142
assumeUniqueness = u
@@ -184,7 +177,7 @@ final class BitArray private (
184177
* O(1).
185178
*/
186179
def positionAfter(p: BitArray.Position): BitArray.Position =
187-
if (p.offsetInBucket == 63) {
180+
if p.offsetInBucket == 63 then {
188181
BitArray.Position(p.bucket + 1, 0)
189182
} else {
190183
BitArray.Position(p.bucket, p.offsetInBucket + 1)
@@ -244,7 +237,7 @@ final class BitArray private (
244237

245238
/** Returns an independent copy of `this`. */
246239
def copy(minimumCapacity: Int = 0): BitArray =
247-
if (minimumCapacity > capacity) {
240+
if minimumCapacity > capacity then {
248241
// If the requested capacity on the copy is greater than what we have, `reserveCapacity` will
249242
// create an independent value.
250243
reserveCapacity(minimumCapacity)
@@ -313,7 +306,7 @@ object BitArray {
313306
/** Creates an array with the given `bits`. */
314307
def apply[T](bits: Boolean*): BitArray =
315308
var result = new BitArray(HyArray[Int](), 0)
316-
for (b <- bits) result = result.append(b, assumeUniqueness = true)
309+
for b <- bits do result = result.append(b, assumeUniqueness = true)
317310
result
318311

319312
}

tests/pos/hylolib-cb/Collection.scala

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ trait Collection[Self] {
6969
*/
7070
def isBefore(i: Position, j: Position): Boolean =
7171
val e = self.endPosition
72-
if (i.eq(e)) {
72+
if i.eq(e) then {
7373
false
74-
} else if (j.eq(e)) {
74+
} else if j.eq(e) then {
7575
true
7676
} else {
7777
def _isBefore(n: Position): Boolean =
78-
if (n.eq(j)) {
78+
if n.eq(j) then {
7979
true
80-
} else if (n.eq(e)) {
80+
} else if n.eq(e) then {
8181
false
8282
} else {
8383
_isBefore(self.positionAfter(n))
@@ -98,7 +98,7 @@ extension [Self: Collection as s](self: Self) {
9898
* O(1)
9999
*/
100100
def headAndTail: Option[(s.Element, Slice[Self])] =
101-
if (self.isEmpty) {
101+
if self.isEmpty then {
102102
None
103103
} else {
104104
val p = self.startPosition
@@ -115,7 +115,7 @@ extension [Self: Collection as s](self: Self) {
115115
def reduce[T](partialResult: T, combine: (T, s.Element) => T): T =
116116
val e = self.endPosition
117117
def loop(p: s.Position, r: T): T =
118-
if (p.eq(e)) {
118+
if p.eq(e) then {
119119
r
120120
} else {
121121
loop(self.positionAfter(p), combine(r, self.at(p)))
@@ -134,9 +134,9 @@ extension [Self: Collection as s](self: Self) {
134134
def forEach(action: (s.Element) => Boolean): Boolean =
135135
val e = self.endPosition
136136
def loop(p: s.Position): Boolean =
137-
if (p.eq(e)) {
137+
if p.eq(e) then {
138138
true
139-
} else if (!action(self.at(p))) {
139+
} else if !action(self.at(p)) then {
140140
false
141141
} else {
142142
loop(self.positionAfter(p))
@@ -190,9 +190,9 @@ extension [Self: Collection as s](self: Self) {
190190
def firstPositionWhere(predicate: (s.Element) => Boolean): Option[s.Position] =
191191
val e = self.endPosition
192192
def loop(p: s.Position): Option[s.Position] =
193-
if (p.eq(e)) {
193+
if p.eq(e) then {
194194
None
195-
} else if (predicate(self.at(p))) {
195+
} else if predicate(self.at(p)) then {
196196
Some(p)
197197
} else {
198198
loop(self.positionAfter(p))
@@ -238,12 +238,12 @@ extension [Self: Collection as s](self: Self) {
238238
* O(n) where n is the number of elements in `self`.
239239
*/
240240
def leastElement(isOrderedBefore: (s.Element, s.Element) => Boolean): Option[s.Element] =
241-
if (self.isEmpty) {
241+
if self.isEmpty then {
242242
None
243243
} else {
244244
val e = self.endPosition
245245
def _least(p: s.Position, least: s.Element): s.Element =
246-
if (p.eq(e)) {
246+
if p.eq(e) then {
247247
least
248248
} else {
249249
val x = self.at(p)
@@ -264,11 +264,11 @@ extension [Self: Collection as s](self: Self)(using
264264
/** Returns `true` if `self` contains the same elements as `other`, in the same order. */
265265
def elementsEqual[T](using o: Collection[T] { type Element = s.Element })(other: T): Boolean =
266266
def loop(i: s.Position, j: o.Position): Boolean =
267-
if (i `eq` self.endPosition) {
267+
if i `eq` self.endPosition then {
268268
j `eq` other.endPosition
269-
} else if (j `eq` other.endPosition) {
269+
} else if j `eq` other.endPosition then {
270270
false
271-
} else if (self.at(i) `neq` other.at(j)) {
271+
} else if self.at(i) `neq` other.at(j) then {
272272
false
273273
} else {
274274
loop(self.positionAfter(i), other.positionAfter(j))

tests/pos/hylolib-cb/HyArray.scala

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,21 @@ final class HyArray[Element: Value as elementIsCValue](
2727

2828
/** Reserves enough storage to store `n` elements in `this`. */
2929
def reserveCapacity(n: Int, assumeUniqueness: Boolean = false): HyArray[Element] =
30-
if (n <= capacity) {
30+
if n <= capacity then {
3131
this
3232
} else {
3333
var newCapacity = max(1, capacity)
34-
while (newCapacity < n) { newCapacity = newCapacity << 1 }
34+
while newCapacity < n do { newCapacity = newCapacity << 1 }
3535

3636
val newStorage = new scala.Array[AnyRef | Null](newCapacity)
3737
val s = _storage.asInstanceOf[scala.Array[AnyRef | Null]]
3838
var i = 0
39-
while (i < count) {
39+
while i < count do {
4040
newStorage(i) = _storage(i).asInstanceOf[Element].copy().asInstanceOf[AnyRef]
4141
i += 1
4242
}
4343

44-
if (assumeUniqueness) {
44+
if assumeUniqueness then {
4545
_storage = newStorage
4646
this
4747
} else {
@@ -69,7 +69,7 @@ final class HyArray[Element: Value as elementIsCValue](
6969

7070
/** Removes and returns the last element, or returns `None` if the array is empty. */
7171
def popLast(assumeUniqueness: Boolean = false): (HyArray[Element], Option[Element]) =
72-
if (isEmpty) {
72+
if isEmpty then {
7373
(this, None)
7474
} else {
7575
val result = if assumeUniqueness then this else copy()
@@ -82,9 +82,9 @@ final class HyArray[Element: Value as elementIsCValue](
8282
keepStorage: Boolean = false,
8383
assumeUniqueness: Boolean = false
8484
): HyArray[Element] =
85-
if (isEmpty) {
85+
if isEmpty then {
8686
this
87-
} else if (keepStorage) {
87+
} else if keepStorage then {
8888
val result = if assumeUniqueness then this else copy()
8989
Arrays.fill(result._storage, null)
9090
result._count = 0
@@ -123,8 +123,8 @@ final class HyArray[Element: Value as elementIsCValue](
123123
override def toString: String =
124124
var s = "["
125125
var i = 0
126-
while (i < count) {
127-
if (i > 0) { s += ", " }
126+
while i < count do {
127+
if i > 0 then { s += ", " }
128128
s += s"${at(i)}"
129129
i += 1
130130
}
@@ -134,14 +134,14 @@ final class HyArray[Element: Value as elementIsCValue](
134134
* allocating new storage.
135135
*/
136136
def copy(minimumCapacity: Int = 0): HyArray[Element] =
137-
if (minimumCapacity > capacity) {
137+
if minimumCapacity > capacity then {
138138
// If the requested capacity on the copy is greater than what we have, `reserveCapacity` will
139139
// create an independent value.
140140
reserveCapacity(minimumCapacity)
141141
} else {
142142
val clone = HyArray[Element]().reserveCapacity(max(minimumCapacity, count))
143143
var i = 0
144-
while (i < count) {
144+
while i < count do {
145145
clone._storage(i) = _storage(i).asInstanceOf[Element].copy().asInstanceOf[AnyRef]
146146
i += 1
147147
}
@@ -156,7 +156,7 @@ object HyArray {
156156
/** Creates an array with the given `elements`. */
157157
def apply[T: Value](elements: T*): HyArray[T] =
158158
var a = new HyArray[T](null, 0)
159-
for (e <- elements) a = a.append(e, assumeUniqueness = true)
159+
for e <- elements do a = a.append(e, assumeUniqueness = true)
160160
a
161161

162162
}

0 commit comments

Comments
 (0)