Skip to content

Commit 2d18af6

Browse files
authored
Merge pull request #1698 from maseev/iss1589-sequence-wildcard-pattern-message
Add an error message for incorrect sequence wildcard position Parsers.scala:1425 and Parsers.scala:1079
2 parents 816fbd0 + 759dcf3 commit 2d18af6

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,7 +1076,7 @@ object Parsers {
10761076
val uscoreStart = in.skipToken()
10771077
if (isIdent(nme.raw.STAR)) {
10781078
in.nextToken()
1079-
if (in.token != RPAREN) syntaxError("`_*' can be used only for last argument", uscoreStart)
1079+
if (in.token != RPAREN) syntaxError(SeqWildcardPatternPos(), uscoreStart)
10801080
Typed(t, atPos(uscoreStart) { Ident(tpnme.WILDCARD_STAR) })
10811081
} else {
10821082
syntaxErrorOrIncomplete(IncorrectRepeatedParameterSyntax())
@@ -1424,7 +1424,7 @@ object Parsers {
14241424
// `x: _*' is parsed in `ascription'
14251425
if (isIdent(nme.raw.STAR)) {
14261426
in.nextToken()
1427-
if (in.token != RPAREN) syntaxError("`_*' can be used only for last argument", wildIndent.pos)
1427+
if (in.token != RPAREN) syntaxError(SeqWildcardPatternPos(), wildIndent.pos)
14281428
atPos(wildIndent.pos) { Ident(tpnme.WILDCARD_STAR) }
14291429
} else wildIndent
14301430
case LPAREN =>

src/dotty/tools/dotc/reporting/diagnostic/messages.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,4 +789,27 @@ object messages {
789789
val msg = "unreachable code"
790790
val explanation = ""
791791
}
792+
793+
case class SeqWildcardPatternPos()(implicit ctx: Context) extends Message(31) {
794+
val kind = "Syntax"
795+
val msg = "`_*' can be used only for last argument"
796+
val explanation = {
797+
val code =
798+
"""def sumOfTheFirstTwo(list: List[Int]): Int = list match {
799+
| case List(first, second, x:_*) => first + second
800+
| case _ => 0
801+
|}"""
802+
hl"""|Sequence wildcard pattern is expected at the end of an argument list.
803+
|This pattern matches any remaining elements in a sequence.
804+
|Consider the following example:
805+
|
806+
|$code
807+
|
808+
|Calling:
809+
|
810+
|${"sumOfTheFirstTwo(List(1, 2, 10))"}
811+
|
812+
|would give 3 as a result"""
813+
}
814+
}
792815
}

0 commit comments

Comments
 (0)