From 284ac783b054ec70dac56789531a39d2da08a58d Mon Sep 17 00:00:00 2001 From: adampauls Date: Mon, 21 Mar 2022 20:41:39 -0700 Subject: [PATCH] Tests for trailing commas --- tests/neg/t11900.check | 18 ++++++++ tests/neg/t11900.scala | 79 +++++++++++++++++++++++++++++++++ tests/neg/trailingCommas.scala | 24 ++++++++++ tests/pos/comma-separated.scala | 19 ++++++++ 4 files changed, 140 insertions(+) create mode 100644 tests/neg/t11900.check create mode 100644 tests/neg/t11900.scala create mode 100644 tests/pos/comma-separated.scala diff --git a/tests/neg/t11900.check b/tests/neg/t11900.check new file mode 100644 index 000000000000..531a1b8417fd --- /dev/null +++ b/tests/neg/t11900.check @@ -0,0 +1,18 @@ +-- Error: tests/neg/t11900.scala:44:16 --------------------------------------------------------------------------------- +44 | a => a + 1, // error: weird comma + | ^ + | end of statement expected but ',' found +-- Error: tests/neg/t11900.scala:48:16 --------------------------------------------------------------------------------- +48 | println("a"), // error: weird comma + | ^ + | end of statement expected but ',' found +-- Error: tests/neg/t11900.scala:52:16 --------------------------------------------------------------------------------- +52 | println("b"), // error: weird comma + | ^ + | end of statement expected but ',' found +-- [E032] Syntax Error: tests/neg/t11900.scala:64:8 -------------------------------------------------------------------- +64 | _*, // error + | ^ + | pattern expected + | + | longer explanation available when compiling with `-explain` \ No newline at end of file diff --git a/tests/neg/t11900.scala b/tests/neg/t11900.scala new file mode 100644 index 000000000000..d45f06bf180b --- /dev/null +++ b/tests/neg/t11900.scala @@ -0,0 +1,79 @@ + +trait t11900 { + // cf pos/trailing-commas + // + import scala.collection.{ + immutable, + mutable, + } + + def h[A, + ]: List[A] = Nil + + def u( + x: Int, + y: Int, + )(using List[Int], + Set[Int], + )(using l: List[Int], + s : Set[Int], + ): Int = 1 + + def g = List( + 1, + 2, + 3, + ) + + def star = + List(1, 2, 3, 4, 5) match { + case List( + 1, + 2, + 3, + ) => false + case List( + 1, + 2, + _*, + ) => true + } + + def f = + List(1, 2, 3).map { + a => a + 1, // error: weird comma + } + + class A() { + println("a"), // error: weird comma + } + + def b() = { + println("b"), // error: weird comma + } + + def starcrossed = + List(1, 2, 3, 4, 5) match { + case List( + 1, + 2, + 3, + ) => false + case List( + 1, + _*, // error + 2, + ) => true + } + + def p(p: (Int, + String, + ) + ): Unit + + def q: (Int, + String, + ) + + val z = 42 +} \ No newline at end of file diff --git a/tests/neg/trailingCommas.scala b/tests/neg/trailingCommas.scala index 2a24fc83c79e..5ddde5475f51 100644 --- a/tests/neg/trailingCommas.scala +++ b/tests/neg/trailingCommas.scala @@ -56,3 +56,27 @@ object `package` { case class Foo(foo: Any) case class Bar(foo: Any) } + +// Unparenthesized lists +trait Deriv1[T] +object Deriv1 { + def derived[T]: Deriv1[T] = new Deriv1[T] {} +} + +trait Deriv2[T] +object Deriv2 { + def derived[T]: Deriv2[T] = new Deriv2[T] {} +} + +class Derives1 derives Deriv1, Deriv2, +object End // error: an identifier expected, but 'object' found + +class Derives2 derives Deriv1, + Deriv2, +object End2 // error: an identifier expected, but 'object' found + +val a, + b, + c, + = (1, 2, 3) // error +val x, y, z, = (1, 2, 3) // error \ No newline at end of file diff --git a/tests/pos/comma-separated.scala b/tests/pos/comma-separated.scala new file mode 100644 index 000000000000..d97b7dd9e2ee --- /dev/null +++ b/tests/pos/comma-separated.scala @@ -0,0 +1,19 @@ +trait Bar[T] +object Bar { + def derived[T]: Bar[T] = new Bar[T] {} +} + +trait Baz[T] +object Baz { + def derived[T]: Baz[T] = new Baz[T] {} +} + +class Foo derives Bar, Baz + +class Foo2 derives Bar, + Baz + +val x, y, z = (1, 2, 3) +val a, + b, + c = (1, 2, 3) \ No newline at end of file