Skip to content

Commit 5292858

Browse files
committed
Syntax change: multiple patterns in definition
Allow `val a, b, c = ...` only if a, b, c are identifiers. It looks weird if they are general patterns, and it seems this gives unnecessary power.
1 parent 853f300 commit 5292858

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2355,13 +2355,21 @@ object Parsers {
23552355
tmplDef(start, mods)
23562356
}
23572357

2358-
/** PatDef ::= Pattern2 {`,' Pattern2} [`:' Type] `=' Expr
2359-
* VarDef ::= PatDef | id {`,' id} `:' Type `=' `_'
2360-
* ValDcl ::= id {`,' id} `:' Type
2361-
* VarDcl ::= id {`,' id} `:' Type
2358+
/** PatDef ::= ids [‘:’ Type] ‘=’ Expr
2359+
* | Pattern2 [‘:’ Type] ‘=’ Expr
2360+
* VarDef ::= PatDef | id {`,' id} `:' Type `=' `_'
2361+
* ValDcl ::= id {`,' id} `:' Type
2362+
* VarDcl ::= id {`,' id} `:' Type
23622363
*/
23632364
def patDefOrDcl(start: Offset, mods: Modifiers): Tree = atSpan(start, nameStart) {
2364-
val lhs = commaSeparated(pattern2)
2365+
val first = pattern2()
2366+
val lhs = first match {
2367+
case id: Ident if in.token == COMMA =>
2368+
in.nextToken()
2369+
id :: commaSeparated(() => termIdent())
2370+
case _ =>
2371+
first :: Nil
2372+
}
23652373
val tpt = typedOpt()
23662374
val rhs =
23672375
if (tpt.isEmpty || in.token == EQUALS) {

docs/docs/internals/syntax.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,8 @@ Def ::= ‘val’ PatDef
359359
| ‘type’ {nl} TypeDcl
360360
| TmplDef
361361
| INT
362-
PatDef ::= Pattern2 {‘,’ Pattern2} [‘:’ Type] ‘=’ Expr PatDef(_, pats, tpe?, expr)
362+
PatDef ::= ids [‘:’ Type] ‘=’ Expr
363+
| Pattern2 [‘:’ Type] ‘=’ Expr PatDef(_, pats, tpe?, expr)
363364
VarDef ::= PatDef
364365
| ids ‘:’ Type ‘=’ ‘_’
365366
DefDef ::= DefSig [(‘:’ | ‘<:’) Type] ‘=’ Expr DefDef(_, name, tparams, vparamss, tpe, expr)

tests/neg/multi-patterns.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
object Test {
2+
val (a :: as), bs = List(1, 2, 3) // error
3+
val B @ List(), C: List[Int] = List() // error
4+
}

tests/pos/i3412.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
class Test {
22
val A @ List() = List()
3-
val B @ List(), C: List[Int] = List()
4-
val D @ List(), E @ List() = List()
53
}

0 commit comments

Comments
 (0)