From 7ababc99e25fa9df415e442ee6584e63a4a64714 Mon Sep 17 00:00:00 2001 From: Yoonjae Jeon Date: Fri, 2 May 2025 22:40:14 +0900 Subject: [PATCH] Fix incorrect warning on type ascription for backquoted identifiers in patterns address reviews minor fix --- .../src/dotty/tools/dotc/parsing/Parsers.scala | 6 +++++- tests/neg/i15784.check | 14 ++++++++++++++ tests/neg/i15784.scala | 8 +++++++- tests/pos/i15784.scala | 15 +++++++++++++-- 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala index e396839c972e..f8a9b71de119 100644 --- a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala +++ b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala @@ -3177,7 +3177,11 @@ object Parsers { def pattern1(location: Location = Location.InPattern): Tree = val p = pattern2(location) if in.isColon then - val isVariableOrNumber = isVarPattern(p) || p.isInstanceOf[Number] + val isVariable = unsplice(p) match { + case x: Ident => x.name.isVarPattern + case _ => false + } + val isVariableOrNumber = isVariable || p.isInstanceOf[Number] if !isVariableOrNumber then report.errorOrMigrationWarning( em"""Type ascriptions after patterns other than: diff --git a/tests/neg/i15784.check b/tests/neg/i15784.check index c1540b33d956..7d015689950d 100644 --- a/tests/neg/i15784.check +++ b/tests/neg/i15784.check @@ -10,3 +10,17 @@ | Not found: A | | longer explanation available when compiling with `-explain` +-- Warning: tests/neg/i15784.scala:7:8 --------------------------------------------------------------------------------- +7 | case X: Int => X // warn + | ^ + | 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/i15784.scala:10:12 ------------------------------------------------------------------------------- +10 | case `Int`: Int => `Int` // warn + | ^ + | 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. diff --git a/tests/neg/i15784.scala b/tests/neg/i15784.scala index a58a27fb3cfa..4be909198eaa 100644 --- a/tests/neg/i15784.scala +++ b/tests/neg/i15784.scala @@ -1,4 +1,10 @@ def i15784 = List(42) match case List(_, Rest @ `a`) => Rest // error case List(_, Rest @ A) => Rest // error - case _ => ??? \ No newline at end of file + case _ => ??? + +def case2 = 42 match + case X: Int => X // warn + +def case3 = 42 match + case `Int`: Int => `Int` // warn diff --git a/tests/pos/i15784.scala b/tests/pos/i15784.scala index ab4b82638a42..dff40f4a4ddc 100644 --- a/tests/pos/i15784.scala +++ b/tests/pos/i15784.scala @@ -1,8 +1,19 @@ -def i15784 = List(42) match +//> using options -Werror + +def case1 = List(42) match case List(_, rest @ _*) => rest + case _ => ??? + +def case2 = List(42) match case List(_, Rest @ _*) => Rest + case _ => ??? + +def case3 = List(42) match case List(_, `Rest` @ _*) => Rest case _ => ??? -def i15784_auxiliary = 42 match +def case4 = 42 match case `type` : Int => `type` + +def case5 = 42 match + case X @ (_: Int) => 32