Skip to content

Commit 07c395b

Browse files
committed
* Preserve the more restrictive syntax for typed patterns in the language specification
* Make the parser's warning a migration warning
1 parent b65ef59 commit 07c395b

File tree

8 files changed

+36
-6
lines changed

8 files changed

+36
-6
lines changed

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

+13-2
Original file line numberDiff line numberDiff line change
@@ -2871,14 +2871,25 @@ object Parsers {
28712871
if (isIdent(nme.raw.BAR)) { in.nextToken(); pattern1(location) :: patternAlts(location) }
28722872
else Nil
28732873

2874-
/** Pattern1 ::= Pattern2 [Ascription]
2874+
/** Pattern1 ::= PatVar Ascription
2875+
* | [‘-’] integerLiteral Ascription
2876+
* | [‘-’] floatingPointLiteral Ascription
2877+
* | Pattern2
28752878
*/
28762879
def pattern1(location: Location = Location.InPattern): Tree =
28772880
val p = pattern2()
28782881
if in.isColon then
28792882
val isVariableOrNumber = isVarPattern(p) || p.isInstanceOf[Number]
28802883
if !isVariableOrNumber then
2881-
warning(em"Only variable and number literal patterns can have type ascriptions")
2884+
report.gradualErrorOrMigrationWarning(
2885+
em"""Type ascriptions after patterns other than:
2886+
| * variable pattern, e.g. `case x: String =>`
2887+
| * number literal pattern, e.g. `case 10.5: Double =>`
2888+
|are no longer supported. Remove the type ascription or move it to a separate variable pattern.""",
2889+
in.sourcePos(),
2890+
warnFrom = `3.3`,
2891+
errorFrom = future
2892+
)
28822893
in.nextToken()
28832894
ascription(p, location)
28842895
else p

compiler/test/dotty/tools/dotc/CompilationTests.scala

+1
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ class CompilationTests {
185185
compileFile("tests/neg-custom-args/i13026.scala", defaultOptions.and("-print-lines")),
186186
compileFile("tests/neg-custom-args/i13838.scala", defaultOptions.and("-Ximplicit-search-limit", "1000")),
187187
compileFile("tests/neg-custom-args/jdk-9-app.scala", defaultOptions.and("-release:8")),
188+
compileFile("tests/neg-custom-args/i10994.scala", defaultOptions.and("-source", "future")),
188189
).checkExpectedErrors()
189190
}
190191

docs/_docs/internals/syntax.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,10 @@ TypeCaseClauses ::= TypeCaseClause { TypeCaseClause }
314314
TypeCaseClause ::= ‘case’ (InfixType | ‘_’) ‘=>’ Type [semi]
315315
316316
Pattern ::= Pattern1 { ‘|’ Pattern1 } Alternative(pats)
317-
Pattern1 ::= Pattern2 [‘:’ RefinedType] Bind(name, Typed(Ident(wildcard), tpe))
317+
Pattern1 ::= PatVar ‘:’ RefinedType Bind(name, Typed(Ident(wildcard), tpe))
318+
| [‘-’] integerLiteral ‘:’ RefinedType Typed(pat, tpe)
319+
| [‘-’] floatingPointLiteral ‘:’ RefinedType Typed(pat, tpe)
320+
| Pattern2
318321
Pattern2 ::= [id ‘@’] InfixPattern [‘*’] Bind(name, pat)
319322
InfixPattern ::= SimplePattern { id [nl] SimplePattern } InfixOp(pat, op, pat)
320323
SimplePattern ::= PatVar Ident(wildcard)

docs/_docs/reference/syntax.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,10 @@ TypeCaseClauses ::= TypeCaseClause { TypeCaseClause }
312312
TypeCaseClause ::= ‘case’ (InfixType | ‘_’) ‘=>’ Type [semi]
313313
314314
Pattern ::= Pattern1 { ‘|’ Pattern1 }
315-
Pattern1 ::= Pattern2 [‘:’ RefinedType]
315+
Pattern1 ::= PatVar ‘:’ RefinedType
316+
| [‘-’] integerLiteral ‘:’ RefinedType
317+
| [‘-’] floatingPointLiteral ‘:’ RefinedType
318+
| Pattern2
316319
Pattern2 ::= [id ‘@’] InfixPattern [‘*’]
317320
InfixPattern ::= SimplePattern { id [nl] SimplePattern }
318321
SimplePattern ::= PatVar

tests/neg-custom-args/i10994.check

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- Error: tests/neg-custom-args/i10994.scala:2:19 ----------------------------------------------------------------------
2+
2 | case (b: Boolean): Boolean => () // error
3+
| ^
4+
| Type ascriptions after patterns other than:
5+
| * variable pattern, e.g. `case x: String =>`
6+
| * number literal pattern, e.g. `case 10.5: Double =>`
7+
| are no longer supported. Remove the type ascription or move it to a separate variable pattern.

tests/neg-custom-args/i10994.scala

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

tests/neg/t5702-neg-bad-and-wild.check

+4-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@
5959
-- Warning: tests/neg/t5702-neg-bad-and-wild.scala:13:22 ---------------------------------------------------------------
6060
13 | case List(1, _*3:) => // error // error
6161
| ^
62-
| Only variable and number literal patterns can have type ascriptions
62+
| Type ascriptions after patterns other than:
63+
| * variable pattern, e.g. `case x: String =>`
64+
| * number literal pattern, e.g. `case 10.5: Double =>`
65+
| are no longer supported. Remove the type ascription or move it to a separate variable pattern.
6366
-- Warning: tests/neg/t5702-neg-bad-and-wild.scala:22:20 ---------------------------------------------------------------
6467
22 | val K(x @ _*) = k
6568
| ^

tests/pos/i10994.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
def foo = true match
2-
case (b: Boolean): Boolean => ()
2+
case (b: Boolean): Boolean => () // warning

0 commit comments

Comments
 (0)