Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix syntax and parsing of vararg patterns #18055

Merged
merged 3 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1666,7 +1666,7 @@ object Parsers {
if in.token == LPAREN then funParamClause() :: funParamClauses() else Nil

/** InfixType ::= RefinedType {id [nl] RefinedType}
* | RefinedType `^`
* | RefinedType `^` // under capture checking
*/
def infixType(): Tree = infixTypeRest(refinedType())

Expand Down Expand Up @@ -2881,13 +2881,13 @@ object Parsers {
if (isIdent(nme.raw.BAR)) { in.nextToken(); pattern1(location) :: patternAlts(location) }
else Nil

/** Pattern1 ::= PatVar Ascription
* | [‘-’] integerLiteral Ascription
* | [‘-’] floatingPointLiteral Ascription
/** Pattern1 ::= PatVar `:` RefinedType
* | [‘-’] integerLiteral `:` RefinedType
* | [‘-’] floatingPointLiteral `:` RefinedType
* | Pattern2
*/
def pattern1(location: Location = Location.InPattern): Tree =
val p = pattern2()
val p = pattern2(location)
if in.isColon then
val isVariableOrNumber = isVarPattern(p) || p.isInstanceOf[Number]
if !isVariableOrNumber then
Expand All @@ -2905,26 +2905,29 @@ object Parsers {
else p

/** Pattern3 ::= InfixPattern
* | PatVar ‘*’
*/
def pattern3(): Tree =
def pattern3(location: Location): Tree =
val p = infixPattern()
if followingIsVararg() then
val start = in.skipToken()
p match
case p @ Ident(name) if name.isVarPattern =>
Typed(p, atSpan(start) { Ident(tpnme.WILDCARD_STAR) })
case _ =>
syntaxError(em"`*` must follow pattern variable", start)
p
if location.inArgs then
p match
case p @ Ident(name) if name.isVarPattern =>
Typed(p, atSpan(start) { Ident(tpnme.WILDCARD_STAR) })
case _ =>
syntaxError(em"`*` must follow pattern variable", start)
p
else
syntaxError(em"bad use of `*` - sequence pattern not allowed here", start)
p
else p

/** Pattern2 ::= [id `@'] Pattern3
*/
val pattern2: () => Tree = () => pattern3() match
val pattern2: Location => Tree = location => pattern3(location) match
case p @ Ident(name) if in.token == AT =>
val offset = in.skipToken()
pattern3() match {
pattern3(location) match {
case pt @ Bind(nme.WILDCARD, pt1: Typed) if pt.mods.is(Given) =>
atSpan(startOffset(p), 0) { Bind(name, pt1).withMods(pt.mods) }
case Typed(Ident(nme.WILDCARD), pt @ Ident(tpnme.WILDCARD_STAR)) =>
Expand Down Expand Up @@ -2954,6 +2957,7 @@ object Parsers {
* | XmlPattern
* | `(' [Patterns] `)'
* | SimplePattern1 [TypeArgs] [ArgumentPatterns]
* | ‘given’ RefinedType
* SimplePattern1 ::= SimpleRef
* | SimplePattern1 `.' id
* PatVar ::= id
Expand Down Expand Up @@ -3597,7 +3601,7 @@ object Parsers {
* VarDcl ::= id {`,' id} `:' Type
*/
def patDefOrDcl(start: Offset, mods: Modifiers): Tree = atSpan(start, nameStart) {
val first = pattern2()
val first = pattern2(Location.InPattern)
var lhs = first match {
case id: Ident if in.token == COMMA =>
in.nextToken()
Expand Down
2 changes: 1 addition & 1 deletion docs/_docs/internals/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ Pattern1 ::= PatVar ‘:’ RefinedType
| [‘-’] integerLiteral ‘:’ RefinedType Typed(pat, tpe)
| [‘-’] floatingPointLiteral ‘:’ RefinedType Typed(pat, tpe)
| Pattern2
Pattern2 ::= [id ‘@’] InfixPattern [‘*’] Bind(name, pat)
Pattern2 ::= [id ‘@’] InfixPattern Bind(name, pat)
InfixPattern ::= SimplePattern { id [nl] SimplePattern } InfixOp(pat, op, pat)
SimplePattern ::= PatVar Ident(wildcard)
| Literal Bind(name, Ident(wildcard))
Expand Down
2 changes: 1 addition & 1 deletion docs/_docs/reference/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ Pattern1 ::= PatVar ‘:’ RefinedType
| [‘-’] integerLiteral ‘:’ RefinedType
| [‘-’] floatingPointLiteral ‘:’ RefinedType
| Pattern2
Pattern2 ::= [id ‘@’] InfixPattern [‘*’]
Pattern2 ::= [id ‘@’] InfixPattern
InfixPattern ::= SimplePattern { id [nl] SimplePattern }
SimplePattern ::= PatVar
| Literal
Expand Down
2 changes: 2 additions & 0 deletions tests/neg/i17443.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def run() =
val x = List(1) match { case (xs*) => xs } // error
4 changes: 4 additions & 0 deletions tests/neg/i8715.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Error: tests/neg/i8715.scala:2:46 -----------------------------------------------------------------------------------
2 |def Test = List(42) match { case List(xs @ (ys*)) => xs } // error
| ^
| bad use of `*` - sequence pattern not allowed here
2 changes: 2 additions & 0 deletions tests/neg/i8715.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@main
def Test = List(42) match { case List(xs @ (ys*)) => xs } // error
2 changes: 0 additions & 2 deletions tests/pos/i8715.scala

This file was deleted.