Skip to content

Commit 45c6227

Browse files
committed
Fix #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, #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 8effbc4 commit 45c6227

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
@@ -2601,11 +2601,13 @@ object Parsers {
26012601
if (isIdent(nme.raw.BAR)) { in.nextToken(); pattern1(location) :: patternAlts(location) }
26022602
else Nil
26032603

2604-
/** Pattern1 ::= Pattern2 [Ascription]
2604+
/** Pattern1 ::= PatVar Ascription
2605+
* | SimpleLiteral Ascription
2606+
* | Pattern2
26052607
*/
26062608
def pattern1(location: Location = Location.InPattern): Tree =
26072609
val p = pattern2()
2608-
if in.token == COLON then
2610+
if (isVarPattern(p) || p.isInstanceOf[Number]) && in.token == COLON then
26092611
in.nextToken()
26102612
ascription(p, location)
26112613
else p

docs/docs/internals/syntax.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,9 @@ TypeCaseClauses ::= TypeCaseClause { TypeCaseClause }
263263
TypeCaseClause ::= ‘case’ InfixType ‘=>’ Type [nl]
264264
265265
Pattern ::= Pattern1 { ‘|’ Pattern1 } Alternative(pats)
266-
Pattern1 ::= Pattern2 [‘:’ RefinedType] Bind(name, Typed(Ident(wildcard), tpe))
266+
Pattern1 ::= PatVar ‘:’ RefinedType Bind(name, Typed(Ident(wildcard), tpe))
267+
| SimpleLiteral ‘:’ RefinedType Bind(name, Typed(Ident(wildcard), tpe))
268+
| Pattern2
267269
Pattern2 ::= [id ‘@’] InfixPattern Bind(name, pat)
268270
InfixPattern ::= SimplePattern { id [nl] SimplePattern } InfixOp(pat, op, pat)
269271
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)