Skip to content

Commit

Permalink
* Preserve the more restrictive syntax for typed patterns in the lang…
Browse files Browse the repository at this point in the history
…uage specification

* Make the parser's warning a migration warning
  • Loading branch information
prolativ committed May 11, 2023
1 parent b65ef59 commit 07c395b
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 6 deletions.
15 changes: 13 additions & 2 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Expand Up @@ -2871,14 +2871,25 @@ object Parsers {
if (isIdent(nme.raw.BAR)) { in.nextToken(); pattern1(location) :: patternAlts(location) }
else Nil

/** Pattern1 ::= Pattern2 [Ascription]
/** Pattern1 ::= PatVar Ascription
* | [‘-’] integerLiteral Ascription
* | [‘-’] floatingPointLiteral Ascription
* | Pattern2
*/
def pattern1(location: Location = Location.InPattern): Tree =
val p = pattern2()
if in.isColon then
val isVariableOrNumber = isVarPattern(p) || p.isInstanceOf[Number]
if !isVariableOrNumber then
warning(em"Only variable and number literal patterns can have type ascriptions")
report.gradualErrorOrMigrationWarning(
em"""Type ascriptions after patterns other than:
| * variable pattern, e.g. `case x: String =>`
| * number literal pattern, e.g. `case 10.5: Double =>`
|are no longer supported. Remove the type ascription or move it to a separate variable pattern.""",
in.sourcePos(),
warnFrom = `3.3`,
errorFrom = future
)
in.nextToken()
ascription(p, location)
else p
Expand Down
1 change: 1 addition & 0 deletions compiler/test/dotty/tools/dotc/CompilationTests.scala
Expand Up @@ -185,6 +185,7 @@ class CompilationTests {
compileFile("tests/neg-custom-args/i13026.scala", defaultOptions.and("-print-lines")),
compileFile("tests/neg-custom-args/i13838.scala", defaultOptions.and("-Ximplicit-search-limit", "1000")),
compileFile("tests/neg-custom-args/jdk-9-app.scala", defaultOptions.and("-release:8")),
compileFile("tests/neg-custom-args/i10994.scala", defaultOptions.and("-source", "future")),
).checkExpectedErrors()
}

Expand Down
5 changes: 4 additions & 1 deletion docs/_docs/internals/syntax.md
Expand Up @@ -314,7 +314,10 @@ 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))
| [‘-’] integerLiteral ‘:’ RefinedType Typed(pat, tpe)
| [‘-’] floatingPointLiteral ‘:’ RefinedType Typed(pat, tpe)
| Pattern2
Pattern2 ::= [id ‘@’] InfixPattern [‘*’] Bind(name, pat)
InfixPattern ::= SimplePattern { id [nl] SimplePattern } InfixOp(pat, op, pat)
SimplePattern ::= PatVar Ident(wildcard)
Expand Down
5 changes: 4 additions & 1 deletion docs/_docs/reference/syntax.md
Expand Up @@ -312,7 +312,10 @@ TypeCaseClauses ::= TypeCaseClause { TypeCaseClause }
TypeCaseClause ::= ‘case’ (InfixType | ‘_’) ‘=>’ Type [semi]
Pattern ::= Pattern1 { ‘|’ Pattern1 }
Pattern1 ::= Pattern2 [‘:’ RefinedType]
Pattern1 ::= PatVar ‘:’ RefinedType
| [‘-’] integerLiteral ‘:’ RefinedType
| [‘-’] floatingPointLiteral ‘:’ RefinedType
| Pattern2
Pattern2 ::= [id ‘@’] InfixPattern [‘*’]
InfixPattern ::= SimplePattern { id [nl] SimplePattern }
SimplePattern ::= PatVar
Expand Down
7 changes: 7 additions & 0 deletions tests/neg-custom-args/i10994.check
@@ -0,0 +1,7 @@
-- Error: tests/neg-custom-args/i10994.scala:2:19 ----------------------------------------------------------------------
2 | case (b: Boolean): Boolean => () // error
| ^
| Type ascriptions after patterns other than:
| * variable pattern, e.g. `case x: String =>`
| * number literal pattern, e.g. `case 10.5: Double =>`
| are no longer supported. Remove the type ascription or move it to a separate variable pattern.
2 changes: 2 additions & 0 deletions tests/neg-custom-args/i10994.scala
@@ -0,0 +1,2 @@
def foo = true match
case (b: Boolean): Boolean => () // error
5 changes: 4 additions & 1 deletion tests/neg/t5702-neg-bad-and-wild.check
Expand Up @@ -59,7 +59,10 @@
-- Warning: tests/neg/t5702-neg-bad-and-wild.scala:13:22 ---------------------------------------------------------------
13 | case List(1, _*3:) => // error // error
| ^
| Only variable and number literal patterns can have type ascriptions
| Type ascriptions after patterns other than:
| * variable pattern, e.g. `case x: String =>`
| * number literal pattern, e.g. `case 10.5: Double =>`
| are no longer supported. Remove the type ascription or move it to a separate variable pattern.
-- Warning: tests/neg/t5702-neg-bad-and-wild.scala:22:20 ---------------------------------------------------------------
22 | val K(x @ _*) = k
| ^
Expand Down
2 changes: 1 addition & 1 deletion tests/pos/i10994.scala
@@ -1,2 +1,2 @@
def foo = true match
case (b: Boolean): Boolean => ()
case (b: Boolean): Boolean => () // warning

0 comments on commit 07c395b

Please sign in to comment.