Skip to content

Commit 69c9ec6

Browse files
authored
Merge pull request #408 from scala/backport-lts-3.3-23088
Backport "Fix incorrect warning on type ascription for backquoted identifiers" to 3.3 LTS
2 parents f899aca + f6dae08 commit 69c9ec6

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -2960,7 +2960,11 @@ object Parsers {
29602960
def pattern1(location: Location = Location.InPattern): Tree =
29612961
val p = pattern2()
29622962
if in.isColon then
2963-
val isVariableOrNumber = isVarPattern(p) || p.isInstanceOf[Number]
2963+
val isVariable = unsplice(p) match {
2964+
case x: Ident => x.name.isVarPattern
2965+
case _ => false
2966+
}
2967+
val isVariableOrNumber = isVariable || p.isInstanceOf[Number]
29642968
if !isVariableOrNumber then
29652969
report.gradualErrorOrMigrationWarning(
29662970
em"""Type ascriptions after patterns other than:

tests/neg/i15784.check

+14
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,17 @@
1010
| Not found: A
1111
|
1212
| longer explanation available when compiling with `-explain`
13+
-- Warning: tests/neg/i15784.scala:7:8 ---------------------------------------------------------------------------------
14+
7 | case X: Int => X // warn
15+
| ^
16+
| Type ascriptions after patterns other than:
17+
| * variable pattern, e.g. `case x: String =>`
18+
| * number literal pattern, e.g. `case 10.5: Double =>`
19+
| are no longer supported. Remove the type ascription or move it to a separate variable pattern.
20+
-- Warning: tests/neg/i15784.scala:10:12 -------------------------------------------------------------------------------
21+
10 | case `Int`: Int => `Int` // warn
22+
| ^
23+
| Type ascriptions after patterns other than:
24+
| * variable pattern, e.g. `case x: String =>`
25+
| * number literal pattern, e.g. `case 10.5: Double =>`
26+
| are no longer supported. Remove the type ascription or move it to a separate variable pattern.

tests/neg/i15784.scala

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
def i15784 = List(42) match
22
case List(_, Rest @ `a`) => Rest // error
33
case List(_, Rest @ A) => Rest // error
4-
case _ => ???
4+
case _ => ???
5+
6+
def case2 = 42 match
7+
case X: Int => X // warn
8+
9+
def case3 = 42 match
10+
case `Int`: Int => `Int` // warn

tests/pos/i15784.scala

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1-
def i15784 = List(42) match
1+
//> using options -Werror
2+
3+
def case1 = List(42) match
24
case List(_, rest @ _*) => rest
5+
case _ => ???
6+
7+
def case2 = List(42) match
38
case List(_, Rest @ _*) => Rest
9+
case _ => ???
10+
11+
def case3 = List(42) match
412
case List(_, `Rest` @ _*) => Rest
513
case _ => ???
614

7-
def i15784_auxiliary = 42 match
15+
def case4 = 42 match
816
case `type` : Int => `type`
17+
18+
def case5 = 42 match
19+
case X @ (_: Int) => 32

0 commit comments

Comments
 (0)