Skip to content

Commit dfe0c54

Browse files
committed
Vararg patterns must be variable patterns or wildcards
Fixes #11457
1 parent 0273336 commit dfe0c54

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2628,13 +2628,19 @@ object Parsers {
26282628
ascription(p, location)
26292629
else p
26302630

2631-
/** Pattern3 ::= InfixPattern [‘*’]
2631+
/** Pattern3 ::= InfixPattern
2632+
* | PatVar ‘*’
26322633
*/
26332634
def pattern3(): Tree =
26342635
val p = infixPattern()
26352636
if followingIsVararg() then
26362637
atSpan(in.skipToken()) {
2637-
Typed(p, Ident(tpnme.WILDCARD_STAR))
2638+
p match
2639+
case p @ Ident(name) if name.isVarPattern =>
2640+
Typed(p, Ident(tpnme.WILDCARD_STAR))
2641+
case _ =>
2642+
syntaxError(em"`*` must follow pattern variable")
2643+
p
26382644
}
26392645
else p
26402646

@@ -2726,7 +2732,7 @@ object Parsers {
27262732
if (in.token == RPAREN) Nil else patterns(location)
27272733

27282734
/** ArgumentPatterns ::= ‘(’ [Patterns] ‘)’
2729-
* | ‘(’ [Patterns ‘,’] Pattern2 ‘*’ ‘)’
2735+
* | ‘(’ [Patterns ‘,’] PatVar ‘*’ ‘)’
27302736
*/
27312737
def argumentPatterns(): List[Tree] =
27322738
inParens(patternsOpt(Location.InPatternArgs))

docs/docs/internals/syntax.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ PatVar ::= varid
296296
| ‘_’
297297
Patterns ::= Pattern {‘,’ Pattern}
298298
ArgumentPatterns ::= ‘(’ [Patterns] ‘)’ Apply(fn, pats)
299-
| ‘(’ [Patterns ‘,’] Pattern2 ‘*’ ‘)’
299+
| ‘(’ [Patterns ‘,’] PatVar ‘*’ ‘)’
300300
```
301301

302302
### Type and Value Parameters

docs/docs/reference/syntax.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ PatVar ::= varid
290290
| ‘_’
291291
Patterns ::= Pattern {‘,’ Pattern}
292292
ArgumentPatterns ::= ‘(’ [Patterns] ‘)’
293-
| ‘(’ [Patterns ‘,’] Pattern2 ‘*’ ‘)’
293+
| ‘(’ [Patterns ‘,’] PatVar ‘*’ ‘)’
294294
```
295295

296296
### Type and Value Parameters

tests/neg/i11457.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
val x = Seq(1, 2) match
3+
case Seq(x, y*) => println(y) // prints List(2) which looks correct
4+
5+
val y = Seq(1, 2) match
6+
case Seq(x, (y)*) => println(y) // error

0 commit comments

Comments
 (0)