Skip to content

Commit 16d68f6

Browse files
prolativKordyjan
authored andcommitted
* Preserve the more restrictive syntax for typed patterns in the language specification
* Make the parser's warning a migration warning
1 parent 9a1e7cb commit 16d68f6

File tree

8 files changed

+36
-6
lines changed

8 files changed

+36
-6
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2821,14 +2821,25 @@ object Parsers {
28212821
if (isIdent(nme.raw.BAR)) { in.nextToken(); pattern1(location) :: patternAlts(location) }
28222822
else Nil
28232823

2824-
/** Pattern1 ::= Pattern2 [Ascription]
2824+
/** Pattern1 ::= PatVar Ascription
2825+
* | [‘-’] integerLiteral Ascription
2826+
* | [‘-’] floatingPointLiteral Ascription
2827+
* | Pattern2
28252828
*/
28262829
def pattern1(location: Location = Location.InPattern): Tree =
28272830
val p = pattern2()
28282831
if in.isColon then
28292832
val isVariableOrNumber = isVarPattern(p) || p.isInstanceOf[Number]
28302833
if !isVariableOrNumber then
2831-
warning(em"Only variable and number literal patterns can have type ascriptions")
2834+
report.gradualErrorOrMigrationWarning(
2835+
em"""Type ascriptions after patterns other than:
2836+
| * variable pattern, e.g. `case x: String =>`
2837+
| * number literal pattern, e.g. `case 10.5: Double =>`
2838+
|are no longer supported. Remove the type ascription or move it to a separate variable pattern.""",
2839+
in.sourcePos(),
2840+
warnFrom = `3.3`,
2841+
errorFrom = future
2842+
)
28322843
in.nextToken()
28332844
ascription(p, location)
28342845
else p

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ class CompilationTests {
188188
compileFile("tests/neg-custom-args/i13026.scala", defaultOptions.and("-print-lines")),
189189
compileFile("tests/neg-custom-args/i13838.scala", defaultOptions.and("-Ximplicit-search-limit", "1000")),
190190
compileFile("tests/neg-custom-args/jdk-9-app.scala", defaultOptions.and("-release:8")),
191+
compileFile("tests/neg-custom-args/i10994.scala", defaultOptions.and("-source", "future")),
191192
).checkExpectedErrors()
192193
}
193194

docs/_docs/internals/syntax.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,10 @@ TypeCaseClauses ::= TypeCaseClause { TypeCaseClause }
319319
TypeCaseClause ::= ‘case’ (InfixType | ‘_’) ‘=>’ Type [semi]
320320
321321
Pattern ::= Pattern1 { ‘|’ Pattern1 } Alternative(pats)
322-
Pattern1 ::= Pattern2 [‘:’ RefinedType] Bind(name, Typed(Ident(wildcard), tpe))
322+
Pattern1 ::= PatVar ‘:’ RefinedType Bind(name, Typed(Ident(wildcard), tpe))
323+
| [‘-’] integerLiteral ‘:’ RefinedType Typed(pat, tpe)
324+
| [‘-’] floatingPointLiteral ‘:’ RefinedType Typed(pat, tpe)
325+
| Pattern2
323326
Pattern2 ::= [id ‘@’] InfixPattern [‘*’] Bind(name, pat)
324327
InfixPattern ::= SimplePattern { id [nl] SimplePattern } InfixOp(pat, op, pat)
325328
SimplePattern ::= PatVar Ident(wildcard)

docs/_docs/reference/syntax.md

Lines changed: 4 additions & 1 deletion
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

Lines changed: 7 additions & 0 deletions
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

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

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

Lines changed: 4 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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)