Skip to content

Backport "Fix incorrect warning on type ascription for backquoted identifiers" to 3.3 LTS #408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2960,7 +2960,11 @@ object Parsers {
def pattern1(location: Location = Location.InPattern): Tree =
val p = pattern2()
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.gradualErrorOrMigrationWarning(
em"""Type ascriptions after patterns other than:
Expand Down
14 changes: 14 additions & 0 deletions tests/neg/i15784.check
Original file line number Diff line number Diff line change
Expand Up @@ -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.
8 changes: 7 additions & 1 deletion tests/neg/i15784.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
def i15784 = List(42) match
case List(_, Rest @ `a`) => Rest // error
case List(_, Rest @ A) => Rest // error
case _ => ???
case _ => ???

def case2 = 42 match
case X: Int => X // warn

def case3 = 42 match
case `Int`: Int => `Int` // warn
15 changes: 13 additions & 2 deletions tests/pos/i15784.scala
Original file line number Diff line number Diff line change
@@ -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