Skip to content

Commit

Permalink
Allow case in pattern bindings even without -Xsource:3
Browse files Browse the repository at this point in the history
In #9558 (which shipped with 2.13.6) we added support for `case` bindings
under -Xsource:3. Since this parser change does not break any existing
code and since IntelliJ and scalameta/metals now understand this syntax
in Scala 2 code, it should be safe to enable it by default to further
ease cross-compilation between Scala 2 and 3.
  • Loading branch information
smarter committed Aug 9, 2021
1 parent 164b9c5 commit 349d75a
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 48 deletions.
4 changes: 1 addition & 3 deletions src/compiler/scala/tools/nsc/ast/parser/Parsers.scala
Expand Up @@ -1999,10 +1999,8 @@ self =>
def generator(eqOK: Boolean, allowNestedIf: Boolean = true): List[Tree] = {
val start = in.offset
val hasCase = in.token == CASE
if (hasCase) {
if (!currentRun.isScala3) syntaxError(in.offset, s"`case` keyword in for comprehension requires the -Xsource:3 flag.")
if (hasCase)
in.skipCASE()
}

val hasVal = in.token == VAL
if (hasVal)
Expand Down
7 changes: 0 additions & 7 deletions test/files/neg/for-comprehension-case-future.check

This file was deleted.

24 changes: 0 additions & 24 deletions test/files/neg/for-comprehension-case-future.scala

This file was deleted.

14 changes: 4 additions & 10 deletions test/files/neg/for-comprehension-case.check
@@ -1,13 +1,7 @@
for-comprehension-case.scala:5: error: `case` keyword in for comprehension requires the -Xsource:3 flag.
case Some(x) <- List(Some(1), None)
^
for-comprehension-case.scala:12: error: `case` keyword in for comprehension requires the -Xsource:3 flag.
case y = x + 1
^
for-comprehension-case.scala:12: error: '<-' expected but '=' found.
for-comprehension-case.scala:20: error: '<-' expected but '=' found.
case y = x + 1
^
for-comprehension-case.scala:13: error: illegal start of simple expression
} yield x+y
for-comprehension-case.scala:21: error: illegal start of simple expression
} yield x + y
^
4 errors
2 errors
16 changes: 12 additions & 4 deletions test/files/neg/for-comprehension-case.scala
@@ -1,14 +1,22 @@
class A {
// fail
// ok
val a =
for {
case Some(x) <- List(Some(1), None)
} yield x
y = x + 1
} yield x + y

// fail
// ok
val b =
for {
Some(x) <- List(Some(1), None)
case y = x + 1
Some(y) <- List(None, Some(2))
} yield x+y

// fail
val c =
for {
case Some(x) <- List(Some(1), None)
case y = x + 1
} yield x + y
}

0 comments on commit 349d75a

Please sign in to comment.