Skip to content

Commit 0e97414

Browse files
authored
Merge pull request #14742 from adampauls/no_weird_trailing_commas_simple
Add tests for trailing comma parsing
2 parents 54dc0c1 + 284ac78 commit 0e97414

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

tests/neg/t11900.check

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- Error: tests/neg/t11900.scala:44:16 ---------------------------------------------------------------------------------
2+
44 | a => a + 1, // error: weird comma
3+
| ^
4+
| end of statement expected but ',' found
5+
-- Error: tests/neg/t11900.scala:48:16 ---------------------------------------------------------------------------------
6+
48 | println("a"), // error: weird comma
7+
| ^
8+
| end of statement expected but ',' found
9+
-- Error: tests/neg/t11900.scala:52:16 ---------------------------------------------------------------------------------
10+
52 | println("b"), // error: weird comma
11+
| ^
12+
| end of statement expected but ',' found
13+
-- [E032] Syntax Error: tests/neg/t11900.scala:64:8 --------------------------------------------------------------------
14+
64 | _*, // error
15+
| ^
16+
| pattern expected
17+
|
18+
| longer explanation available when compiling with `-explain`

tests/neg/t11900.scala

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2+
trait t11900 {
3+
// cf pos/trailing-commas
4+
//
5+
import scala.collection.{
6+
immutable,
7+
mutable,
8+
}
9+
10+
def h[A,
11+
]: List[A] = Nil
12+
13+
def u(
14+
x: Int,
15+
y: Int,
16+
)(using List[Int],
17+
Set[Int],
18+
)(using l: List[Int],
19+
s : Set[Int],
20+
): Int = 1
21+
22+
def g = List(
23+
1,
24+
2,
25+
3,
26+
)
27+
28+
def star =
29+
List(1, 2, 3, 4, 5) match {
30+
case List(
31+
1,
32+
2,
33+
3,
34+
) => false
35+
case List(
36+
1,
37+
2,
38+
_*,
39+
) => true
40+
}
41+
42+
def f =
43+
List(1, 2, 3).map {
44+
a => a + 1, // error: weird comma
45+
}
46+
47+
class A() {
48+
println("a"), // error: weird comma
49+
}
50+
51+
def b() = {
52+
println("b"), // error: weird comma
53+
}
54+
55+
def starcrossed =
56+
List(1, 2, 3, 4, 5) match {
57+
case List(
58+
1,
59+
2,
60+
3,
61+
) => false
62+
case List(
63+
1,
64+
_*, // error
65+
2,
66+
) => true
67+
}
68+
69+
def p(p: (Int,
70+
String,
71+
)
72+
): Unit
73+
74+
def q: (Int,
75+
String,
76+
)
77+
78+
val z = 42
79+
}

tests/neg/trailingCommas.scala

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,27 @@ object `package` {
5656
case class Foo(foo: Any)
5757
case class Bar(foo: Any)
5858
}
59+
60+
// Unparenthesized lists
61+
trait Deriv1[T]
62+
object Deriv1 {
63+
def derived[T]: Deriv1[T] = new Deriv1[T] {}
64+
}
65+
66+
trait Deriv2[T]
67+
object Deriv2 {
68+
def derived[T]: Deriv2[T] = new Deriv2[T] {}
69+
}
70+
71+
class Derives1 derives Deriv1, Deriv2,
72+
object End // error: an identifier expected, but 'object' found
73+
74+
class Derives2 derives Deriv1,
75+
Deriv2,
76+
object End2 // error: an identifier expected, but 'object' found
77+
78+
val a,
79+
b,
80+
c,
81+
= (1, 2, 3) // error
82+
val x, y, z, = (1, 2, 3) // error

tests/pos/comma-separated.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
trait Bar[T]
2+
object Bar {
3+
def derived[T]: Bar[T] = new Bar[T] {}
4+
}
5+
6+
trait Baz[T]
7+
object Baz {
8+
def derived[T]: Baz[T] = new Baz[T] {}
9+
}
10+
11+
class Foo derives Bar, Baz
12+
13+
class Foo2 derives Bar,
14+
Baz
15+
16+
val x, y, z = (1, 2, 3)
17+
val a,
18+
b,
19+
c = (1, 2, 3)

0 commit comments

Comments
 (0)