Skip to content

Fix incorrect warning on type ascription for backquoted identifiers #23088

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 7, 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 @@ -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:
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the test rig checks the // warn in a neg test, but of course it's verified by the check file. (I'm wondering if the test rig should do that, as an enhancement. I know a difference is that it's OK to have zero warns in a warn test but not OK to have zero errors in a neg test.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, would it be better if I make a separate tests/warn/i15784.scala?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I think it's fine. Maybe with -Werror you can mark them error. But I was just musing aloud, in case I read this again later.

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