Skip to content

Commit

Permalink
Merge pull request #6348 from eed3si9n/wip/deprecate-val-in-for
Browse files Browse the repository at this point in the history
Remove val in for comprehension under -Xsource:2.14
  • Loading branch information
adriaanm committed Apr 30, 2018
2 parents 5661290 + 56945d9 commit 82908ef
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 11 deletions.
11 changes: 8 additions & 3 deletions src/compiler/scala/tools/nsc/ast/parser/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1830,7 +1830,7 @@ self =>
* Enumerators ::= Generator {semi Enumerator}
* Enumerator ::= Generator
* | Guard
* | val Pattern1 `=' Expr
* | Pattern1 `=' Expr
* }}}
*/
def enumerators(): List[Tree] = {
Expand Down Expand Up @@ -1862,8 +1862,13 @@ self =>
val hasEq = in.token == EQUALS

if (hasVal) {
if (hasEq) deprecationWarning(in.offset, "val keyword in for comprehension is deprecated", "2.10.0")
else syntaxError(in.offset, "val in for comprehension must be followed by assignment")
def msg(what: String, instead: String): String = s"`val` keyword in for comprehension is $what: $instead"
if (hasEq) {
val without = "instead, bind the value without `val`"
if (settings.isScala214) syntaxError(in.offset, msg("unsupported", without))
else deprecationWarning(in.offset, msg("deprecated", without), "2.10.0")
}
else syntaxError(in.offset, msg("unsupported", "just remove `val`"))
}

if (hasEq && eqOK) in.nextToken()
Expand Down
16 changes: 8 additions & 8 deletions test/files/neg/for-comprehension-old.check
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
for-comprehension-old.scala:3: warning: val keyword in for comprehension is deprecated
for-comprehension-old.scala:3: warning: `val` keyword in for comprehension is deprecated: instead, bind the value without `val`
for (x <- 1 to 5 ; val y = x) yield x+y // fail
^
for-comprehension-old.scala:5: warning: val keyword in for comprehension is deprecated
for-comprehension-old.scala:5: warning: `val` keyword in for comprehension is deprecated: instead, bind the value without `val`
for (val x <- 1 to 5 ; val y = x) yield x+y // fail
^
for-comprehension-old.scala:8: warning: val keyword in for comprehension is deprecated
for-comprehension-old.scala:8: warning: `val` keyword in for comprehension is deprecated: instead, bind the value without `val`
for (z <- 1 to 2 ; x <- 1 to 5 ; val y = x) yield x+y // fail
^
for-comprehension-old.scala:10: warning: val keyword in for comprehension is deprecated
for-comprehension-old.scala:10: warning: `val` keyword in for comprehension is deprecated: instead, bind the value without `val`
for (z <- 1 to 2 ; val x <- 1 to 5 ; val y = x) yield x+y // fail
^
for-comprehension-old.scala:4: error: val in for comprehension must be followed by assignment
for-comprehension-old.scala:4: error: `val` keyword in for comprehension is unsupported: just remove `val`
for (val x <- 1 to 5 ; y = x) yield x+y // fail
^
for-comprehension-old.scala:5: error: val in for comprehension must be followed by assignment
for-comprehension-old.scala:5: error: `val` keyword in for comprehension is unsupported: just remove `val`
for (val x <- 1 to 5 ; val y = x) yield x+y // fail
^
for-comprehension-old.scala:9: error: val in for comprehension must be followed by assignment
for-comprehension-old.scala:9: error: `val` keyword in for comprehension is unsupported: just remove `val`
for (z <- 1 to 2 ; val x <- 1 to 5 ; y = x) yield x+y // fail
^
for-comprehension-old.scala:10: error: val in for comprehension must be followed by assignment
for-comprehension-old.scala:10: error: `val` keyword in for comprehension is unsupported: just remove `val`
for (z <- 1 to 2 ; val x <- 1 to 5 ; val y = x) yield x+y // fail
^
four warnings found
Expand Down
25 changes: 25 additions & 0 deletions test/files/neg/for-comprehension-val.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
for-comprehension-val.scala:3: error: `val` keyword in for comprehension is unsupported: instead, bind the value without `val`
for (x <- 1 to 5 ; val y = x) yield x+y // fail
^
for-comprehension-val.scala:4: error: `val` keyword in for comprehension is unsupported: just remove `val`
for (val x <- 1 to 5 ; y = x) yield x+y // fail
^
for-comprehension-val.scala:5: error: `val` keyword in for comprehension is unsupported: just remove `val`
for (val x <- 1 to 5 ; val y = x) yield x+y // fail
^
for-comprehension-val.scala:5: error: `val` keyword in for comprehension is unsupported: instead, bind the value without `val`
for (val x <- 1 to 5 ; val y = x) yield x+y // fail
^
for-comprehension-val.scala:8: error: `val` keyword in for comprehension is unsupported: instead, bind the value without `val`
for (z <- 1 to 2 ; x <- 1 to 5 ; val y = x) yield x+y // fail
^
for-comprehension-val.scala:9: error: `val` keyword in for comprehension is unsupported: just remove `val`
for (z <- 1 to 2 ; val x <- 1 to 5 ; y = x) yield x+y // fail
^
for-comprehension-val.scala:10: error: `val` keyword in for comprehension is unsupported: just remove `val`
for (z <- 1 to 2 ; val x <- 1 to 5 ; val y = x) yield x+y // fail
^
for-comprehension-val.scala:10: error: `val` keyword in for comprehension is unsupported: instead, bind the value without `val`
for (z <- 1 to 2 ; val x <- 1 to 5 ; val y = x) yield x+y // fail
^
8 errors found
1 change: 1 addition & 0 deletions test/files/neg/for-comprehension-val.flags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-deprecation -Xsource:2.14
11 changes: 11 additions & 0 deletions test/files/neg/for-comprehension-val.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class A {
for (x <- 1 to 5 ; y = x) yield x+y // ok
for (x <- 1 to 5 ; val y = x) yield x+y // fail
for (val x <- 1 to 5 ; y = x) yield x+y // fail
for (val x <- 1 to 5 ; val y = x) yield x+y // fail

for (z <- 1 to 2 ; x <- 1 to 5 ; y = x) yield x+y // ok
for (z <- 1 to 2 ; x <- 1 to 5 ; val y = x) yield x+y // fail
for (z <- 1 to 2 ; val x <- 1 to 5 ; y = x) yield x+y // fail
for (z <- 1 to 2 ; val x <- 1 to 5 ; val y = x) yield x+y // fail
}

0 comments on commit 82908ef

Please sign in to comment.