Skip to content

Commit

Permalink
Fix scala#10994: align typed pattern syntax to Scala 2
Browse files Browse the repository at this point in the history
In Scala 2, a typed pattern `p: T` restricts that `p` can only be a
pattern variable.

In Dotty, scala#6919 allows `p` to be any pattern, in order to support
 pattern matching on generic number literals.

This PR aligns the syntax with Scala 2 by stipulating that in a typed
pattern `p: T`, either

- `p` is a pattern variable, or
- `p` is a number literal
  • Loading branch information
liufengyun authored and dwijnand committed Oct 7, 2022
1 parent bfa32a4 commit 260183f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
6 changes: 4 additions & 2 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2809,11 +2809,13 @@ object Parsers {
if (isIdent(nme.raw.BAR)) { in.nextToken(); pattern1(location) :: patternAlts(location) }
else Nil

/** Pattern1 ::= Pattern2 [Ascription]
/** Pattern1 ::= PatVar Ascription
* | SimpleLiteral Ascription
* | Pattern2
*/
def pattern1(location: Location = Location.InPattern): Tree =
val p = pattern2()
if in.isColon then
if (isVarPattern(p) || p.isInstanceOf[Number]) && in.isColon then
in.nextToken()
ascription(p, location)
else p
Expand Down
4 changes: 3 additions & 1 deletion docs/_docs/internals/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,9 @@ TypeCaseClauses ::= TypeCaseClause { TypeCaseClause }
TypeCaseClause ::= ‘case’ (InfixType | ‘_’) ‘=>’ Type [semi]
Pattern ::= Pattern1 { ‘|’ Pattern1 } Alternative(pats)
Pattern1 ::= Pattern2 [‘:’ RefinedType] Bind(name, Typed(Ident(wildcard), tpe))
Pattern1 ::= PatVar ‘:’ RefinedType Bind(name, Typed(Ident(wildcard), tpe))
| SimpleLiteral ‘:’ RefinedType Bind(name, Typed(Ident(wildcard), tpe))
| Pattern2
Pattern2 ::= [id ‘@’] InfixPattern [‘*’] Bind(name, pat)
InfixPattern ::= SimplePattern { id [nl] SimplePattern } InfixOp(pat, op, pat)
SimplePattern ::= PatVar Ident(wildcard)
Expand Down
2 changes: 2 additions & 0 deletions tests/neg/i10994.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def foo = true match
case (b: Boolean): Boolean => () // error

0 comments on commit 260183f

Please # to comment.