Skip to content

Commit bc43b3a

Browse files
authored
Merge pull request #7156 from dotty-staging/change-lambda-syntax
Require (...) around parameters of a lambda
2 parents dc88e01 + 8df2abc commit bc43b3a

27 files changed

+66
-56
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,9 +423,19 @@ object Parsers {
423423
/** Convert tree to formal parameter list
424424
*/
425425
def convertToParams(tree: Tree): List[ValDef] = tree match {
426-
case Parens(t) => convertToParam(t) :: Nil
427-
case Tuple(ts) => ts map (convertToParam(_))
428-
case t => convertToParam(t) :: Nil
426+
case Parens(t) =>
427+
convertToParam(t) :: Nil
428+
case Tuple(ts) =>
429+
ts.map(convertToParam(_))
430+
case t: Typed =>
431+
in.errorOrMigrationWarning(
432+
em"parentheses are required around the parameter of a lambda${rewriteNotice("-language:Scala2")}",
433+
t.span)
434+
patch(source, t.span.startPos, "(")
435+
patch(source, t.span.endPos, ")")
436+
convertToParam(t) :: Nil
437+
case t =>
438+
convertToParam(t) :: Nil
429439
}
430440

431441
/** Convert tree to formal parameter
@@ -2915,7 +2925,7 @@ object Parsers {
29152925
else from
29162926
}
29172927

2918-
val handleImport: Tree => Tree = { tree: Tree =>
2928+
val handleImport: Tree => Tree = { (tree: Tree) =>
29192929
if (in.token == USCORE) mkTree(importGiven, tree, wildcardIdent() :: Nil)
29202930
else if (in.token == LBRACE) mkTree(importGiven, tree, inBraces(importSelectors()))
29212931
else tree
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
scala> try { 0 } catch { _: Throwable => 1 }
1+
scala> try { 0 } catch { (_: Throwable) => 1 }
22
val res0: Int = 0

tests/neg/i5592.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ object Test {
1111
}
1212

1313
// these are both fine
14-
val eqReflexive1: (x: Obj) => (EQ[x.type, x.type]) = { x: Obj => implicitly }
15-
val eqReflexive2: Forall[[x] =>> EQ[x, x]] = { x: Obj => implicitly }
14+
val eqReflexive1: (x: Obj) => (EQ[x.type, x.type]) = { (x: Obj) => implicitly }
15+
val eqReflexive2: Forall[[x] =>> EQ[x, x]] = { (x: Obj) => implicitly }
1616

1717
// this compiles
1818
val eqSymmetric1: (x: Obj) => (y: Obj) => EQ[x.type, y.type] => EQ[y.type, x.type] = {
19-
{ x: Obj => { y: Obj => { xEqy: EQ[x.type, y.type] => xEqy.commute } } }
19+
{ (x: Obj) => { (y: Obj) => { (xEqy: EQ[x.type, y.type]) => xEqy.commute } } }
2020
}
2121

2222
val eqSymmetric2: Forall[[x] =>> (y: Obj) => (EQ[x, y.type]) => (EQ[y.type, x])] = {
23-
{ x: Obj => { y: Obj => { xEqy: EQ[x.type, y.type] => xEqy.commute } } } // error
23+
{ (x: Obj) => { (y: Obj) => { (xEqy: EQ[x.type, y.type]) => xEqy.commute } } } // error
2424
}
2525

2626
val eqSymmetric3: Forall[[x] =>> Forall[[y] =>> EQ[x, y] => EQ[y, x]]] = {
27-
{ x: Obj => { y: Obj => { xEqy: EQ[x.type, y.type] => xEqy.commute } } } // error
27+
{ (x: Obj) => { (y: Obj) => { (xEqy: EQ[x.type, y.type]) => xEqy.commute } } } // error
2828
}
2929
}

tests/new/patterns.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ object test {
3333
trait John[A,B] {
3434
def filter(x:Any) = x match {
3535
case (x::xs, _) => "ga"
36-
case _ => {x:String => "foobar"}
36+
case _ => { (x:String) => "foobar"}
3737
}
3838
}

tests/pos/depmet_implicit_norm_ret.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ object Test{
2727
// bug: inferred return type = (Stream[A]) => java.lang.Object with Test.ZipWith[B]{type T = Stream[B]}#T
2828
// this seems incompatible with vvvvvvvvvvvvvvvvvvvvvv -- #3731
2929
def map1[A,B](f : A => B) = ZipWith(f)(SuccZipWith) // this typechecks but fails in -Ycheck:first
30-
val tst1: Stream[Int] = map1[String, Int]{x: String => x.length}.apply(Stream("a"))
30+
val tst1: Stream[Int] = map1[String, Int]{(x: String) => x.length}.apply(Stream("a"))
3131

3232
def map2[A,B](f : A => B) = ZipWith(f) // this finds ZeroZipWith where scalac finds SuccZipWith and fails typechecking in the next line.
33-
val tst2: Stream[Int] = map2{x: String => x.length}.apply(Stream("a"))
33+
val tst2: Stream[Int] = map2{(x: String) => x.length}.apply(Stream("a"))
3434
}

tests/pos/gadt-TypeSafeLambda.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ object TypeSafeLambda {
103103
val interped: (Env) => String =
104104
interp[Term, Exp, Prod, Arr, Env, String] (c, exp)
105105

106-
interped((((), 1), { i: Int => i.toString })) : String // "1"
106+
interped((((), 1), { (i: Int) => i.toString })) : String // "1"
107107
}
108108

109109
}

tests/pos/i94-nada.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ trait Test1 {
2525
case class Left[A,B](x: A) extends Either[A,B] with Monad[A]
2626
case class Right[A,B](x: B) extends Either[A,B] with Monad[B]
2727
def flatMap[X,Y,M[X]<:Monad[X]](m: M[X], f: X => M[Y]): M[Y] = f(m.x)
28-
println(flatMap(Right(1), {x: Int => Right(x)}))
28+
println(flatMap(Right(1), {(x: Int) => Right(x)}))
2929
}
3030
trait Test2 {
3131
trait Monad[X] {
@@ -35,9 +35,9 @@ trait Test2 {
3535
case class Left[A,B](x: A) extends Either[A,B] with Monad[A]
3636
case class Right[A,B](x: B) extends Either[A,B] with Monad[B]
3737
def flatMap[X,Y,M[X]](m: M[X], f: X => M[Y]): M[Y]
38-
println(flatMap(Left(1), {x: Int => Left(x)}))
38+
println(flatMap(Left(1), {(x: Int) => Left(x)}))
3939
}
4040
trait Test3 {
4141
def flatMap[X,Y,M[X]](m: M[X], f: X => M[Y]): M[Y]
42-
println(flatMap(Some(1), {x: Int => Some(x)}))
42+
println(flatMap(Some(1), {(x: Int) => Some(x)}))
4343
}

tests/pos/pat_gilles.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
abstract class Table2 {
22

33

4-
val x: Any => Unit = { zz:Any =>
4+
val x: Any => Unit = { (zz:Any) =>
55
zz match {
66
case Table2.CellUpdated(row, column) =>
77
val foo = Table2.CellUpdated(2,2)

0 commit comments

Comments
 (0)