Skip to content

Enforce -new-syntax under -language:future #23443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions compiler/src/dotty/tools/dotc/config/SourceVersion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Feature.isPreviewEnabled
import util.Property

enum SourceVersion:
case `3.0-migration`, `3.0`
case `3.0-migration`, `3.0`
case `3.1-migration`, `3.1`
case `3.2-migration`, `3.2`
case `3.3-migration`, `3.3`
Expand Down Expand Up @@ -44,8 +44,10 @@ enum SourceVersion:
def enablesNamedTuples = isAtLeast(`3.7`)
def enablesBetterFors(using Context) = isAtLeast(`3.7`) && isPreviewEnabled

def requiresNewSyntax = isAtLeast(future)

object SourceVersion extends Property.Key[SourceVersion]:

/* The default source version used by the built compiler */
val defaultSourceVersion = `3.7`

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/CheckRealizable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class CheckRealizable(using Context) {
}
}
if sourceVersion.isAtLeast(future) then
// check fields only from version 3.x.
// check fields only from version 3.future.
// Reason: An embedded field could well be nullable, which means it
// should not be part of a path and need not be checked; but we cannot recognize
// this situation until we have a typesystem that tracks nullability.
Expand Down
9 changes: 4 additions & 5 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,9 @@ object Parsers {
syntaxError(em"""This construct is not allowed under $option.${rewriteNotice(`3.0-migration`, option)}""", span)

def rewriteToNewSyntax(span: Span = Span(in.offset)): Boolean = {
if (in.newSyntax) {
if (in.rewrite) return true
syntaxVersionError("-new-syntax", span)
}
if in.newSyntax then
if in.rewrite then return true
syntaxVersionError("-new-syntax or -language:future", span)
false
}

Expand Down Expand Up @@ -2556,7 +2555,7 @@ object Parsers {
}
}

/** `if' `(' Expr `)' {nl} Expr [[semi] else Expr]
/** `if' `(' Expr `)' {nl} Expr [[semi] else Expr] -- Scala 2 compat
* `if' Expr `then' Expr [[semi] else Expr]
*/
def ifExpr(start: Offset, mkIf: (Tree, Tree, Tree) => If): If =
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/parsing/Scanners.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import scala.collection.mutable
import scala.collection.immutable.SortedMap
import rewrites.Rewrites.patch
import config.Feature
import config.Feature.migrateTo3
import config.Feature.{migrateTo3, sourceVersion}
import config.SourceVersion.{`3.0`, `3.0-migration`}
import config.MigrationVersion
import reporting.{NoProfile, Profile, Message}
Expand Down Expand Up @@ -184,7 +184,7 @@ object Scanners {

val rewrite = ctx.settings.rewrite.value.isDefined
val oldSyntax = ctx.settings.oldSyntax.value
val newSyntax = ctx.settings.newSyntax.value
val newSyntax = ctx.settings.newSyntax.value || sourceVersion.requiresNewSyntax

val rewriteToIndent = ctx.settings.indent.value && rewrite
val rewriteNoIndent = ctx.settings.noindent.value && rewrite
Expand Down
55 changes: 24 additions & 31 deletions tests/pos/hylolib-cb/BitArray.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,86 +22,79 @@ final class BitArray private (

/** Reserves enough storage to store `n` elements in `this`. */
def reserveCapacity(n: Int, assumeUniqueness: Boolean = false): BitArray =
if (n == 0) {
if n == 0 then
this
} else {
else
val k = 1 + ((n - 1) >> 5)
if (assumeUniqueness) {
if assumeUniqueness then
_bits = _bits.reserveCapacity(k, assumeUniqueness)
this
} else {
else
new BitArray(_bits.reserveCapacity(k), _count)
}
}

/** Adds a new element at the end of the array. */
def append(bit: Boolean, assumeUniqueness: Boolean = false): BitArray =
val result = if assumeUniqueness && (count < capacity) then this else copy(count + 1)
val p = BitArray.Position(count)
if (p.bucket >= _bits.count) {
if p.bucket >= _bits.count then
result._bits = _bits.append(if bit then 1 else 0)
} else {
else
result.setValue(bit, p)
}
result._count += 1
result

/** Removes and returns the last element, or returns `None` if the array is empty. */
def popLast(assumeUniqueness: Boolean = false): (BitArray, Option[Boolean]) =
if (isEmpty) {
if isEmpty then
(this, None)
} else {
else
val result = if assumeUniqueness then this else copy()
val bit = result.at(BitArray.Position(count))
result._count -= 1
(result, Some(bit))
}

/** Removes all elements in the array, keeping allocated storage iff `keepStorage` is true. */
def removeAll(
keepStorage: Boolean = false,
assumeUniqueness: Boolean = false
): BitArray =
if (isEmpty) {
if isEmpty then
this
} else if (keepStorage) {
else if keepStorage then
val result = if assumeUniqueness then this else copy()
result._bits.removeAll(keepStorage, assumeUniqueness = true)
result._count = 0
result
} else {
else
BitArray()
}

/** Returns `true` iff all elements in `this` are `false`. */
def allFalse: Boolean =
if (isEmpty) {
if isEmpty then
true
} else {
else
val k = (count - 1) >> 5
def loop(i: Int): Boolean =
if (i == k) {
if i == k then
val m = (1 << (count & 31)) - 1
(_bits.at(k) & m) == 0
} else if (_bits.at(i) != 0) {
else if _bits.at(i) != 0 then
false
} else {
else
loop(i + 1)
}
loop(0)
}

/** Returns `true` iff all elements in `this` are `true`. */
def allTrue: Boolean =
if (isEmpty) {
if isEmpty then {
true
} else {
val k = (count - 1) >> 5
def loop(i: Int): Boolean =
if (i == k) {
if i == k then {
val m = (1 << (count & 31)) - 1
(_bits.at(k) & m) == m
} else if (_bits.at(i) != ~0) {
} else if _bits.at(i) != ~0 then {
false
} else {
loop(i + 1)
Expand Down Expand Up @@ -136,14 +129,14 @@ final class BitArray private (
assumeUniqueness: Boolean = false
): BitArray =
require(this.count == other.count)
if (isEmpty) {
if isEmpty then {
this
} else {
val result = if assumeUniqueness then this else copy()
var u = assumeUniqueness
val k = (count - 1) >> 5

for (i <- 0 until k) {
for i <- 0 until k do {
result._bits = result._bits.modifyAt(
i, (n) => operation(n, other._bits.at(n)),
assumeUniqueness = u
Expand Down Expand Up @@ -184,7 +177,7 @@ final class BitArray private (
* O(1).
*/
def positionAfter(p: BitArray.Position): BitArray.Position =
if (p.offsetInBucket == 63) {
if p.offsetInBucket == 63 then {
BitArray.Position(p.bucket + 1, 0)
} else {
BitArray.Position(p.bucket, p.offsetInBucket + 1)
Expand Down Expand Up @@ -244,7 +237,7 @@ final class BitArray private (

/** Returns an independent copy of `this`. */
def copy(minimumCapacity: Int = 0): BitArray =
if (minimumCapacity > capacity) {
if minimumCapacity > capacity then {
// If the requested capacity on the copy is greater than what we have, `reserveCapacity` will
// create an independent value.
reserveCapacity(minimumCapacity)
Expand Down Expand Up @@ -313,7 +306,7 @@ object BitArray {
/** Creates an array with the given `bits`. */
def apply[T](bits: Boolean*): BitArray =
var result = new BitArray(HyArray[Int](), 0)
for (b <- bits) result = result.append(b, assumeUniqueness = true)
for b <- bits do result = result.append(b, assumeUniqueness = true)
result

}
Expand Down
30 changes: 15 additions & 15 deletions tests/pos/hylolib-cb/Collection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ trait Collection[Self] {
*/
def isBefore(i: Position, j: Position): Boolean =
val e = self.endPosition
if (i.eq(e)) {
if i.eq(e) then {
false
} else if (j.eq(e)) {
} else if j.eq(e) then {
true
} else {
def _isBefore(n: Position): Boolean =
if (n.eq(j)) {
if n.eq(j) then {
true
} else if (n.eq(e)) {
} else if n.eq(e) then {
false
} else {
_isBefore(self.positionAfter(n))
Expand All @@ -98,7 +98,7 @@ extension [Self: Collection as s](self: Self) {
* O(1)
*/
def headAndTail: Option[(s.Element, Slice[Self])] =
if (self.isEmpty) {
if self.isEmpty then {
None
} else {
val p = self.startPosition
Expand All @@ -115,7 +115,7 @@ extension [Self: Collection as s](self: Self) {
def reduce[T](partialResult: T, combine: (T, s.Element) => T): T =
val e = self.endPosition
def loop(p: s.Position, r: T): T =
if (p.eq(e)) {
if p.eq(e) then {
r
} else {
loop(self.positionAfter(p), combine(r, self.at(p)))
Expand All @@ -134,9 +134,9 @@ extension [Self: Collection as s](self: Self) {
def forEach(action: (s.Element) => Boolean): Boolean =
val e = self.endPosition
def loop(p: s.Position): Boolean =
if (p.eq(e)) {
if p.eq(e) then {
true
} else if (!action(self.at(p))) {
} else if !action(self.at(p)) then {
false
} else {
loop(self.positionAfter(p))
Expand Down Expand Up @@ -190,9 +190,9 @@ extension [Self: Collection as s](self: Self) {
def firstPositionWhere(predicate: (s.Element) => Boolean): Option[s.Position] =
val e = self.endPosition
def loop(p: s.Position): Option[s.Position] =
if (p.eq(e)) {
if p.eq(e) then {
None
} else if (predicate(self.at(p))) {
} else if predicate(self.at(p)) then {
Some(p)
} else {
loop(self.positionAfter(p))
Expand Down Expand Up @@ -238,12 +238,12 @@ extension [Self: Collection as s](self: Self) {
* O(n) where n is the number of elements in `self`.
*/
def leastElement(isOrderedBefore: (s.Element, s.Element) => Boolean): Option[s.Element] =
if (self.isEmpty) {
if self.isEmpty then {
None
} else {
val e = self.endPosition
def _least(p: s.Position, least: s.Element): s.Element =
if (p.eq(e)) {
if p.eq(e) then {
least
} else {
val x = self.at(p)
Expand All @@ -264,11 +264,11 @@ extension [Self: Collection as s](self: Self)(using
/** Returns `true` if `self` contains the same elements as `other`, in the same order. */
def elementsEqual[T](using o: Collection[T] { type Element = s.Element })(other: T): Boolean =
def loop(i: s.Position, j: o.Position): Boolean =
if (i `eq` self.endPosition) {
if i `eq` self.endPosition then {
j `eq` other.endPosition
} else if (j `eq` other.endPosition) {
} else if j `eq` other.endPosition then {
false
} else if (self.at(i) `neq` other.at(j)) {
} else if self.at(i) `neq` other.at(j) then {
false
} else {
loop(self.positionAfter(i), other.positionAfter(j))
Expand Down
24 changes: 12 additions & 12 deletions tests/pos/hylolib-cb/HyArray.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@ final class HyArray[Element: Value as elementIsCValue](

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

val newStorage = new scala.Array[AnyRef | Null](newCapacity)
val s = _storage.asInstanceOf[scala.Array[AnyRef | Null]]
var i = 0
while (i < count) {
while i < count do {
newStorage(i) = _storage(i).asInstanceOf[Element].copy().asInstanceOf[AnyRef]
i += 1
}

if (assumeUniqueness) {
if assumeUniqueness then {
_storage = newStorage
this
} else {
Expand Down Expand Up @@ -69,7 +69,7 @@ final class HyArray[Element: Value as elementIsCValue](

/** Removes and returns the last element, or returns `None` if the array is empty. */
def popLast(assumeUniqueness: Boolean = false): (HyArray[Element], Option[Element]) =
if (isEmpty) {
if isEmpty then {
(this, None)
} else {
val result = if assumeUniqueness then this else copy()
Expand All @@ -82,9 +82,9 @@ final class HyArray[Element: Value as elementIsCValue](
keepStorage: Boolean = false,
assumeUniqueness: Boolean = false
): HyArray[Element] =
if (isEmpty) {
if isEmpty then {
this
} else if (keepStorage) {
} else if keepStorage then {
val result = if assumeUniqueness then this else copy()
Arrays.fill(result._storage, null)
result._count = 0
Expand Down Expand Up @@ -123,8 +123,8 @@ final class HyArray[Element: Value as elementIsCValue](
override def toString: String =
var s = "["
var i = 0
while (i < count) {
if (i > 0) { s += ", " }
while i < count do {
if i > 0 then { s += ", " }
s += s"${at(i)}"
i += 1
}
Expand All @@ -134,14 +134,14 @@ final class HyArray[Element: Value as elementIsCValue](
* allocating new storage.
*/
def copy(minimumCapacity: Int = 0): HyArray[Element] =
if (minimumCapacity > capacity) {
if minimumCapacity > capacity then {
// If the requested capacity on the copy is greater than what we have, `reserveCapacity` will
// create an independent value.
reserveCapacity(minimumCapacity)
} else {
val clone = HyArray[Element]().reserveCapacity(max(minimumCapacity, count))
var i = 0
while (i < count) {
while i < count do {
clone._storage(i) = _storage(i).asInstanceOf[Element].copy().asInstanceOf[AnyRef]
i += 1
}
Expand All @@ -156,7 +156,7 @@ object HyArray {
/** Creates an array with the given `elements`. */
def apply[T: Value](elements: T*): HyArray[T] =
var a = new HyArray[T](null, 0)
for (e <- elements) a = a.append(e, assumeUniqueness = true)
for e <- elements do a = a.append(e, assumeUniqueness = true)
a

}
Expand Down
Loading
Loading