Skip to content

Commit 260183f

Browse files
liufengyundwijnand
authored andcommitted
Fix scala#10994: align typed pattern syntax to Scala 2
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
1 parent bfa32a4 commit 260183f

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2809,11 +2809,13 @@ object Parsers {
28092809
if (isIdent(nme.raw.BAR)) { in.nextToken(); pattern1(location) :: patternAlts(location) }
28102810
else Nil
28112811

2812-
/** Pattern1 ::= Pattern2 [Ascription]
2812+
/** Pattern1 ::= PatVar Ascription
2813+
* | SimpleLiteral Ascription
2814+
* | Pattern2
28132815
*/
28142816
def pattern1(location: Location = Location.InPattern): Tree =
28152817
val p = pattern2()
2816-
if in.isColon then
2818+
if (isVarPattern(p) || p.isInstanceOf[Number]) && in.isColon then
28172819
in.nextToken()
28182820
ascription(p, location)
28192821
else p

docs/_docs/internals/syntax.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,9 @@ TypeCaseClauses ::= TypeCaseClause { TypeCaseClause }
318318
TypeCaseClause ::= ‘case’ (InfixType | ‘_’) ‘=>’ Type [semi]
319319
320320
Pattern ::= Pattern1 { ‘|’ Pattern1 } Alternative(pats)
321-
Pattern1 ::= Pattern2 [‘:’ RefinedType] Bind(name, Typed(Ident(wildcard), tpe))
321+
Pattern1 ::= PatVar ‘:’ RefinedType Bind(name, Typed(Ident(wildcard), tpe))
322+
| SimpleLiteral ‘:’ RefinedType Bind(name, Typed(Ident(wildcard), tpe))
323+
| Pattern2
322324
Pattern2 ::= [id ‘@’] InfixPattern [‘*’] Bind(name, pat)
323325
InfixPattern ::= SimplePattern { id [nl] SimplePattern } InfixOp(pat, op, pat)
324326
SimplePattern ::= PatVar Ident(wildcard)

tests/neg/i10994.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def foo = true match
2+
case (b: Boolean): Boolean => () // error

0 commit comments

Comments
 (0)