diff options
author | Som Snytt <som.snytt@gmail.com> | 2016-03-15 13:24:55 -0700 |
---|---|---|
committer | Som Snytt <som.snytt@gmail.com> | 2016-05-20 16:38:04 -0700 |
commit | 1e565d879360709758950332c19a77fffee073d1 (patch) | |
tree | 8378eabb74d56ff5148a01141b8269785bd64848 | |
parent | 2eb1cc2e3df1627cde35afa1237cb10f508fe2f2 (diff) | |
download | scala-1e565d879360709758950332c19a77fffee073d1.tar.gz scala-1e565d879360709758950332c19a77fffee073d1.tar.bz2 scala-1e565d879360709758950332c19a77fffee073d1.zip |
SI-8044 Allow any id in explicit pattern binding
Allows arbitrary identifier in `X @ pat`, including
non-varids. This goes to regularity.
Users of this syntax are not likely to be confused
by the "backquoted var id is stable" rule.
Also for sequence pattern, `X @ _*`.
-rw-r--r-- | spec/08-pattern-matching.md | 4 | ||||
-rw-r--r-- | src/compiler/scala/tools/nsc/ast/parser/Parsers.scala | 7 | ||||
-rw-r--r-- | test/files/pos/t8044.scala | 8 |
3 files changed, 13 insertions, 6 deletions
diff --git a/spec/08-pattern-matching.md b/spec/08-pattern-matching.md index 6753fa3ec7..35eb97b948 100644 --- a/spec/08-pattern-matching.md +++ b/spec/08-pattern-matching.md @@ -13,7 +13,7 @@ chapter: 8 Pattern1 ::= boundvarid ‘:’ TypePat | ‘_’ ‘:’ TypePat | Pattern2 - Pattern2 ::= boundvarid [‘@’ Pattern3] + Pattern2 ::= id [‘@’ Pattern3] | Pattern3 Pattern3 ::= SimplePattern | SimplePattern {id [nl] SimplePattern} @@ -22,7 +22,7 @@ chapter: 8 | Literal | StableId | StableId ‘(’ [Patterns] ‘)’ - | StableId ‘(’ [Patterns ‘,’] [varid ‘@’] ‘_’ ‘*’ ‘)’ + | StableId ‘(’ [Patterns ‘,’] [id ‘@’] ‘_’ ‘*’ ‘)’ | ‘(’ [Patterns] ‘)’ | XmlPattern Patterns ::= Pattern {‘,’ Patterns} diff --git a/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala b/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala index 1e239f91a6..abfb6ae679 100644 --- a/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala +++ b/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala @@ -1922,10 +1922,9 @@ self => } /** {{{ - * Pattern2 ::= boundvarid [ @ Pattern3 ] + * Pattern2 ::= id @ Pattern3 + * | `_' @ Pattern3 * | Pattern3 - * SeqPattern2 ::= boundvarid [ @ SeqPattern3 ] - * | SeqPattern3 * }}} */ def pattern2(): Tree = { @@ -1936,7 +1935,7 @@ self => case Ident(nme.WILDCARD) => in.nextToken() pattern3() - case Ident(name) if nme.isVariableName(name) => + case Ident(name) => in.nextToken() atPos(p.pos.start) { Bind(name, pattern3()) } case _ => p diff --git a/test/files/pos/t8044.scala b/test/files/pos/t8044.scala index 8259f06a8a..2519a8306b 100644 --- a/test/files/pos/t8044.scala +++ b/test/files/pos/t8044.scala @@ -4,4 +4,12 @@ trait T { def g = 42 match { case `type` @ _ => `type` } def h = 42 match { case `type` : Int => `type` } def i = (null: Any) match { case _: Int | _: String => 17 } + + // arbitrary idents allowed in @ syntax + def j = "Fred" match { case Name @ (_: String) => Name } + def k = "Fred" match { case * @ (_: String) => * } + + // also in sequence pattern + def m = List(1,2,3,4,5) match { case List(1, `Rest of them` @ _*) => `Rest of them` } + } |